可以借鉴下
% Filtering
for i = 1:N,
Pbar(:,:,i) = Pminus;
Xbar(:,i) = Xminus;
if(i<order+1)
H = [-data(i-1:-1:1)' zeros(1,order-i+1)];
else
H = -data(i-1:-1:i-order)';
end
Yminus = H * Xminus + Vmean;
inov = data(i)-Yminus;
K = Pminus * H'/(H * Pminus * H' + alpha*R);
Pplus = ( (eye(order) - K * H) * Pminus * (eye(order) - K * H)' + K * R * K' )/alpha;
Xplus = Xminus + K*(inov); % A posteriori state estimate
Xminus = A * Xplus + Wmean; % State update
Pminus = A * Pplus * A' + Q;
ARCoefs(:,i) = Xplus;
% P(i) = max(diag(Pplus));
Phat(:,:,i) = Pplus;
Xhat(:,i) = Xplus;
end
% Smoothing
PSmoothed = zeros(size(Phat));
X = zeros(size(Xhat));
PSmoothed(:,:,N) = Phat(:,:,N);
X(:,N) = Xhat(:,N);
for k = N-1:-1:1,
S = Phat(:,:,k) * A' /Pbar(:,:,k+1);
X(:,k) = Xhat(:,k) + S * (X(:,k+1) - Xbar(:,k+1));
PSmoothed(:,:,k) = Phat(:,:,k) - S * (Pbar(:,:,k+1) - PSmoothed(:,:,k+1)) * S';
end
ARCoefs2 = X;
noise0 = NoiseGenerator('white',SignalPower,SNR,N,1);
noise1 = NoiseGenerator('colored',SignalPower,SNR,N,fs,beta,10);
noise2 = NoiseGenerator('MA',SignalPower,SNR,N,fs,0);
noise3 = NoiseGenerator('EM',SignalPower,SNR,N,fs,100);
noise4 = NoiseGenerator('BW',SignalPower,SNR,N,fs,100);
noise5 = NoiseGenerator('mixture',SignalPower,SNR,N,fs,[w_bw,w_em,w_ma],1000);
x0 = data + noise0;
x1 = data + noise1;
x2 = data + noise2;
x3 = data + noise3;
x4 = data + noise4;
x5 = data + noise5;
% Plot results
figure;
hold on
plot(t,noise0,'g');
plot(t,x0,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('White Gaussian Noise');
figure;
hold on
plot(t,noise1,'g');
plot(t,x1,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('Colored Gaussian Noise');
figure;
hold on
plot(t,noise2,'g');
plot(t,x2,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('Real Muscle Artifact Noise');
figure;
hold on
plot(t,noise3,'g');
plot(t,x3,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('Real Electrode Movement Noise');
figure;
hold on
plot(t,noise4,'g');
plot(t,x4,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('Real Baseline Wander Noise');
figure;
hold on
plot(t,noise5,'g');
plot(t,x5,'r');
plot(t,data,'b');
grid;
xlabel('time (sec.)');
legend('Noise','Noisy ECG','Original ECG');
title('Mixture or BW, EM, and MA Noise');