SLIDE 10 end; pf = 0; % Default - no plotting if nargout==0, % Plot if no output arguments pf = 1; end; if exist(’pfa’) & ~isempty(pfa), pf = pfa; end; %==================================================================== % Parameter Definitions %==================================================================== nx = 2; % Dimension of the state vector %==================================================================== % Preprocessing %==================================================================== my = mean(y); % Save the mean value of y y = y(:) - mean(y); % Make sure y is a column vector ny = length(y); % No. samples Ts = 1/fs; % Sampling interval (sec) wmean = fmean/(2*pi); % Mean frequency Q = Ts*diag([0;nv(1)]); % Process noise covariance matrix for discrete-time process model R = nv(2); % Measurement noise covariance matrix for discrete-time process model P0 = nv(3)*eye(nx,nx); % Initial predicted error covariance matrix %==================================================================== % Memory Allocation %==================================================================== fi = zeros(ny,1); fv = zeros(ny,1); yh = zeros(ny,1); Hs = cell(ny,1);
Portland State University ECE 539/639 Extended Kalman Filter
39
Example 1: MATLAB Code
function [fi,fv,yh] = KalmanFrequencyTracker(y,fsa,fmna,nva,aa,pfa); %KalmanFilterFrequencyTracker: Tracks instantaneous frequency % % [fi,yh] = KalmanFrequencyTracker(y,fs,fmn,nv,a,pf); % % y Input signal. % fs Sample rate (Hz). Default = 1 Hz. % fmn A priori mean frequency % nv Noise variances of [fi y x0]. Default = [10 0.1 0.1]. % a Amplitude of the frequency component. Default = see manuscript. % pf Plot flag: 0=none (default), 1=screen, 2=current figure. % % fi Instantaneous frequency (fi) estimates. % yh Smoothed estimate of the input signal. % % Uses the following model a harmonic estimate of the signal % with time-varying amplitude and instantaneous frequency. % % Example: Generate the parametric spectrogram of an intracranial % pressure signal using a Blackman-Harris window that is 45 s in % duration. % % load Tremor.mat; % N = length(x); % fss = 750; % New sample rate (Hz) % Ns = ceil(N*fss/fs); % Number of samples % sti = floor((fss/fs)*(si-1))+1; % Indices of spikes % ks = 1:Ns; % Sample index % ts = (ks-0.5)/fss; % Time index % xs = zeros(Ns,1); % Allocate memory for spike train % xs(sti) = 1; % Create spike train % KalmanFrequencyTracker(xs,fss); % %
- S. Kim, J. McNames, "Tracking tremor frequency in spike trains
- J. McNames
Portland State University ECE 539/639 Extended Kalman Filter
37
Fs = cell(ny,1); Res = cell(ny,1); es = cell(ny,1); xhps = cell(ny,1); Pps = cell(ny+1,1); %==================================================================== % Kalman Filter %==================================================================== %-------------------------------------------------------------------- % Variable Initialization %-------------------------------------------------------------------- xhp = zeros(nx,1); % Initial value of predicted state at time i=0 Pp = P0; % Predicted state error covariance Pps{1} = Pp; xhps{1} = xhp; %-------------------------------------------------------------------- % Kalman Filter Recursions %-------------------------------------------------------------------- for n=1:ny, H = [a*cos(wmean*n*Ts+xhp(1)),0]; Re = R + H*Pp*H’; % Innovation covariance matrix Kf = Pp*H’*inv(Re); % Filtered estimate Kalman gain yhp = a*sin(wmean*n*Ts+xhp(1)); % Predicted estimate of the output e = y(n) - yhp; % Innovation xhm = xhp + Kf*e; % Measurement update estimate of the state F = [1 Ts;0 1]; % Linearized state transition matrix xhp = [xhm(1)+Ts*xhm(2);xhm(2)]; % Predicted state estimates Pm = Pp - Kf*Re*Kf’; % Measurement state error covariance Pp = F*Pm*F’ + Q; % Predicted state error covariance %---------------------------------------------------------------- % Store Variables for Smoothing %----------------------------------------------------------------
Portland State University ECE 539/639 Extended Kalman Filter
40
% using the extended Kalman filter," Proceedings of the 27th Annual % International Conference of the Engineering in Medicine and % Biology Society, September 2005. % % Version 1.00 JM % % See also Lowpass, ParametricSpectrogram, and KernelFilter. %==================================================================== % Error Checking %==================================================================== if nargin<2, help KalmanFrequencyTracker; return; end; %==================================================================== % Process Function Arguments %==================================================================== fs = 1; if exist(’fsa’,’var’) & ~isempty(fsa), fs = fsa; end; fmean = 2; if exist(’fmna’,’var’) & ~isempty(fmna), fmean = fmna; end; a = 0.1*std(y); % Amplitude of the frequency component if exist(’aa’,’var’) & ~isempty(aa), a = aa; end; nv = [0.01*var(y) var(y) 0.1]; % Noise Variances if exist(’nva’,’var’) & ~isempty(nva), nv = nva;
Portland State University ECE 539/639 Extended Kalman Filter
38