You are on page 1of 11

clear;

clc;
 
Fs=128;
T =1/Fs;
L =1280;
% t =(0:L-1)*T;

MENGAMBIL DATA ECG

load('16265m');
val(1,:);
ECG=ans;

MEMBANGKITKAN SINYAL NOISE RANDOM

noisee = 20*randn(size(ECG));

SINYAL ECG + NOISE

ECGnoise = ECG+noisee;

FILTER WIENER DENGAN FUNGSI WIENERFILTER

% NewECG = wienerFilter(ECG,ECGnoise,1,1,Fs);

FILTER WIENER TANPA FUNGSI WIENERFILTER

R=2; % response function % ... no smearing


noise = ECGnoise-ECG;
% work out how long to make FFT
N=length(ECGnoise);
% Wiener filter
Sf2=real(fft(ECG,N*2-1)).^2; % Smeared ideal
Nf2=real(fft(noise,N*2-1)).^2; % noise
Cf=real(fft(ECGnoise,N*2-1)); % ~= sqrt(Sf2+Nf2); % Corrupted ideal
H=Sf2./(Sf2+Nf2); % Optimal filter
Yhat=H.*Cf/R; % 'uncorrupted' ideal estimate ...
yhat=real(ifft(Yhat)); % ..... in time domain
yhat=yhat(1:length(ECGnoise));
Np=4;
[Pm Fm] = pwelch(ECG,length(ECG),Fs,hamming(length(ECG)));
[Pn Fn] = pwelch(noise,length(noise),Fs,hamming(length(noise)));
[Psig Fsig] = pwelch(ECGnoise,length(ECGnoise),Fs,hamming(length(ECGnoise)));
[Pw Fw] = pwelch(yhat,length(yhat),Fs,hamming(length(yhat)));
 
figure(1);
t=[1/Fs:1/Fs:length(ECGnoise)/Fs];
mn=min(min([ECG noise ECGnoise yhat])); mn = mn - (0.1*mn);
mx=max(max([ECG noise ECGnoise yhat])); mx = mx + (0.1*mx);
plot(t,ECGnoise,'b',t,yhat,'r');
axis([0 max(t) mn mx]);
xlabel('Time (s)');
ylabel('Amplitude (mV)');
legend('ECG+Noise','Filtered') ;
title('Hasil Wiener Filter');
MENGUBAH SINYAL KE DOMAIN FREKUENSI

% FFT Sinyal ECG Normal


freqECGNormal = fft(ECG) ;
fECGNormal = abs(freqECGNormal/L) ;
frqECGNormal = fECGNormal(1:L/2+1);
frqECGNormal(2:end-1)= 2*frqECGNormal(2:end-1);
% FFT Sinyal ECG + Noise
freqECGNoise = fft(ECGnoise);
fECGNoise = abs(freqECGNoise/L);
frqECGNoise = fECGNoise(1:L/2+1);
frqECGNoise(2:end-1)= 2*frqECGNoise(2:end-1);
% FFT Sinyal Noise
freqNoise = fft(noisee);
fNoise = abs(freqNoise/L);
frqNoise = fNoise(1:L/2+1);
frqNoise(2:end-1) =2*frqNoise(2:end-1);
% FFT Hasil Filter
freqNewECG =fft(yhat);
fNewECG = abs(freqNewECG/L);
frqNewECG = fNewECG(1:L/2+1);
frqNewECG(2:end-1)=2*frqNewECG(2:end-1);

PLOTTING

f=Fs*(0:(L/2))/L;
% Plotting ECG Normal domain waktu
figure(2)
plot(t,ans)
axis([0 max(t) mn mx]);
xlabel('Time (s)');
ylabel('Amplitude (mV)');
title('ECG Normal');

% Plotting ECG Normal domain freq


figure(3)
plot(f,frqECGNormal)
xlabel('Freq (Hz)');
ylabel('|frqECGNormal(f)|');
title('Domain Freq ECG Normal');
% Plotting Sinyal Noise domain waktu
figure(4)
plot(t,noisee)
axis([0 max(t) mn mx]);
xlabel('Time (s)');
ylabel('Amplitude (mV)');
title('Noise');
% Plotting Sinyal Noise domain freq
figure(5)
plot(f,frqNoise)
xlabel('Freq(Hz)');
ylabel('|frqNoise(f)|');
title('Domain Freq Noise');
% Plotting Sinyal ECG + Noise domain waktu
figure(6)
plot(t,ECGnoise)
axis([0 max(t) mn mx]);
xlabel('Time (s)');
ylabel('Amplitude (mV)');
title('ECG + Noise');
% Plotting Sinyal ECG + Noise domain freq
figure(7)
plot(f,frqECGNoise)
xlabel('Freq (Hz)');
ylabel('|frqECGNoise(f)|');
title('Domain Freq ECG + Noise');
% Plotting Sinyal ECG Baru hasil filter domain waktu
figure(8)
plot(t,yhat)
axis([0 max(t) mn mx]);
xlabel('Time (s)');
ylabel('Amplitude (mV)');
title('ECG Hasil Pemfilteran');
% Plotting Sinyal ECG Baru hasil filter domain frequensi
figure(9)
plot(f,frqNewECG)
xlabel('Freq (Hz)');
ylabel('|frqNewECG(Hz)|');
title('Domain Freq ECG Pemfilteran');
 
figure(10)
plot(f,frqNewECG,'r',f,frqECGNormal,'b')
xlabel('Freq (Hz)');
ylabel('|(Hz)|');
legend('Filtered','ECG+Noise') ;
title('Domain Freq ECG+Pemfilteran');

You might also like