SLIDE 14 la = Fp’*la + H’*inv(Re(n))*e(n); %---------------------------------------------------------------- % Save Info %---------------------------------------------------------------- X(:,n) = xh; % Store estimate at time n+1 yh(n) = H*xh; % Store predicted estimate at time n end; end; %==================================================================== % Post-processing %==================================================================== A = X; %==================================================================== % Plotting %==================================================================== if pf>=1, if pf~=2, figure; FigureSet(1); colormap(jet(256)); else figure(1); end; Pf = zeros(l,Nt); % Frequencies of the poles Ry = zeros(Nf,Nt); % Memory allocation for spectral estimate k = round(linspace(1,ny,Nt)); % Sample indices to evalute spectral estimate at for c1=1:Nt, Ry(:,c1) = abs(freqz(1,[1;-A(:,k(c1))],Nf,fs)); rts = roots([1;-A(:,k(c1))]); % Obtain the roots of the polynomial Pf(:,c1) = sort(angle(rts)*fs/(2*pi)); % Store the sorted frequencies end; t = (k-0.5)/fs; % Times of estimate
Portland State University ECE 539/639 Kalman Filter
55
% Variable Initialization %==================================================================== Q = q*eye(m,m); % Covariance matrix for state/process noise R = r*eye(p,p); % Covariance matrix for observation noise S = zeros(m,p); % Cross-covariance matrix between measurement and observation noise P0 = p0*eye(l,l); % Initial state (x(0)) covariance matrix H = zeros(p,l); % Initial value of H(i) at i=0 xh = zeros(l,1); % Initial value of estimated state at time i=0 P = P0; X = zeros(l,ny); % Estimates of the state at all times K = zeros(l,ny); % Kalman gains Px = zeros(l,l,ny); % State error covariances yh = zeros(ny,1); % Predicted outputs e = zeros(ny,1); % Innovations %==================================================================== % Forward Recursions %==================================================================== for n=1:ny, %---------------------------------------------------------------- % Calculations for time n %---------------------------------------------------------------- id = n - (1:l); % Indices of observed signal to stuff in H(n) iv = find(id>0); % Indices that are non-negative H = zeros(1,l); H(iv) = y(id(iv)); % Update output matrix yh(n)= H*xh; % Predicted value of the signal e(n) = y(n) - yh(n); % Calculate the innovation at time n Re(n) = R + H*P*H’; % Update innovation covariance Kp = (F*P*H’ + G*S)*inv(Re(n)); % Kalman filter (prediction) coefficients %----------------------------------------------------------------
Portland State University ECE 539/639 Kalman Filter
53
tl = ’Time (s)’; % Default label of time axis if t(end)>2000, t = t/60; tl = ’Time (min)’; end; f = (0:Nf-1)*fs/(2*Nf); % Frequencies of estimate %---------------------------------------------------------------- % Spectrogram %---------------------------------------------------------------- ha1 = axes(’Position’,[0.15 0.21 0.81 0.69]); s = reshape(abs(Ry),Nf*Nt,1); p = [0 prctile(s,97)]; imagesc(t,f,Ry,p); xlim([0 t(end)]); ylim([0 f(end)]); set(ha1,’YAxisLocation’,’Right’); set(ha1,’XAxisLocation’,’Top’); set(ha1,’YDir’,’normal’); AxisSet; title(sprintf(’$\\ell$=%d $q$=%5.2g $r$=%5.2g $\\rho_0$=%5.2g $f_d$=%5.4g’,l,q,r,p0,fd)); %---------------------------------------------------------------- % Colorbar %---------------------------------------------------------------- ha2 = axes(’Position’,[0.135 0.21 0.01 0.69]); colorbar(ha2); set(ha2,’Box’,’Off’) set(ha2,’YTick’,[]); %---------------------------------------------------------------- % Power Spectral Density %---------------------------------------------------------------- ha3 = axes(’Position’,[0.08 0.21 0.05 0.69]); psd = mean((Ry.^2).’);
Portland State University ECE 539/639 Kalman Filter
56
% Calculations for time n+1 %---------------------------------------------------------------- xh = F*xh + Kp*e(n); % Predicted state at time n+1|n+1 P = F*P*F’ + G*Q*G’ - Kp*Re(n)*Kp’; % Calculate state error covariance matrix if eig(P)<0, warning(’Error covariance matrix is negative definite.’); end; %---------------------------------------------------------------- % Save Info %---------------------------------------------------------------- X(:,n) = xh; % Store estimate at time n+1 K(:,n) = Kp; Px(:,:,n) = P; % Store the state error covariance end; %==================================================================== % Backward Recursions (Bryson-Frazier Formulas) %==================================================================== if sf==1, Ps = zeros(l,l); % Smoothed state error covariance matrix L = zeros(l,l); la = zeros(l,1); for n=ny:-1:1, %---------------------------------------------------------------- % Calculations for time i %---------------------------------------------------------------- id = (n-1) - (0:l-1); % Indices of the observed signal to stuff in H(n) iv = find(id>0); H = zeros(1,l); H(iv) = y(id(iv)); % Calculate the output matrix H Pp = Px(:,:,n); % Recall the predicted state error covariance Fp = F - K(:,n)*H; xh = X(:,n) + Pp*Fp’*la;
Portland State University ECE 539/639 Kalman Filter
54