Matlab Code PDF
Matlab Code PDF
% Load data
EMG1 = load('EMG_movement.txt');
fs = 1000;
A = detrend(EMG1); %Remove the mean of the signal
figure (1)
subplot(2,1,1) %First figure in subplot
plot(EMG1)
title('Raw EMG Data')
a = EMG1 - mean(EMG1);
%% Fast Fourier Transform(FTT) of Raw EMG Data
B = length(a);
B1 = 2^nextpow2(B);
f = fft(a,B1);
B2 = f(1:B1/2);
B3 = fs*(0:B1/2-1)/B1;
figure (1)
subplot(2,1,2) %Second figure in subplot
plot(B3,abs(B2))
title('FFT Raw EMG Data')
%% FIR band Pass filter for Band pass
cutoff1=20/(fs/2);
cutoff2=400/(fs/2);
% FIR Filter
h=fir1(500,[cutoff1 cutoff2],'bandpass');
k=filter(h,1,a);
figure(2)
subplot(2,1,1)
plot(k)
title('FIR Filtered EMG data')
figure(3)
freqz(h)
%% IIR band Pass filter
[h1,d]=butter(5,[cutoff1 cutoff2],'bandpass');
k1=filter(h1,d,a);
figure(2)
subplot(2,1,2)
plot(k1)
title('IIR Filtered EMG data')
figure(4)
freqz(h1,d)
%% New Filter
cutoff3=30/(fs/2);
cutoff4=410/(fs/2);
% FIR Filter Bandstop filter
h2=fir1(500,[cutoff3 cutoff2],'stop');
k2=filter(h2,2,k1);
figure(5)
subplot(2,1,1)
plot(k2)
%% FFT of FIR Filter
nfft1= length(k);
nfft21=2^nextpow2(nfft1);
f1=fft(k,nfft21);
ff1=f1(1:nfft21/2);
xfft1=fs*(0:nfft21/2-1)/nfft21;
figure(6)
subplot(2,1,1)
plot(xfft1,abs(ff1));
title('FFT from FIR Filter')
Task 2
% Load data
ECG1 = load('ECG_60.txt');
fs = 1000;
A = detrend(ECG1); %Remove the mean of the signal
figure (1)
subplot(2,1,1) %First figure in subplot
plot(ECG1)
title('Raw ECG Data')
a = ECG1 - mean(ECG1);
%% Find Peak
A1 = findpeaks(ECG1);
%% FFT of Raw ECG Data
nfft= length(a);
nfft2=2^nextpow2(nfft);
f=fft(a,nfft2);
ff=f(1:nfft2/2);
xfft=fs*(0:nfft2/2-1)/nfft2;
figure(1)
subplot(2,1,2)
plot(xfft,abs(ff));
title('FFT Raw ECG Data')
%% Plot a sine wave
figure (2)
plot(ECG1)
hold on
for amplitude = 20:50
freq = 50;
t = linspace(0,3.2*10^4);
y = 500+amplitude.*sin(2*pi*freq.*t);
plot(t,y,'k')
end
b = y-mean(y);
%% FFT of noisy ECG signal
nfft3 = length(b);
nfft4 = 2^nextpow2(nfft);
f2=fft(a,nfft4);
ff3=f2(1:nfft4/2);
xfft2=fs*(0:nfft4/2-1)/nfft4;
figure(3)
plot(xfft2,abs(ff3));
title('FFT Raw ECG Data')
%% FIR Filter Bandstop filter
cutoff1 = 50/(fs/2);
h2=fir1(500,cutoff1,'stop');
k2=filter(h2,2,a);
figure(3)
plot(k2)
%% FFT of filtered ECG signal
nfft5 = length(k2);
nfft6 = 2^nextpow2(nfft);
f2=fft(a,nfft5);
ff4=f3(1:nfft5/2);
xfft3=fs*(0:nfft5/2-1)/nfft5;
figure(4)
plot(xfft3,abs(ff4));
title('FFT Filtered ECG Data')
%% Power spectral density (PSD)
figure(5)
periodogram(k2,rectwin(length(k2)),length(k2),fs)
Task 3
%% Load data (blinking)
EEG1 = load('EEG.txt');
fs = 1000;
A1 = detrend(EEG1); %Remove the mean of the signal
figure (1)
subplot(4,1,1) %First figure in subplot
plot(EEG1)
title('Raw Blinking Data')
a1 = EEG1 - mean(EEG1);
%% Load data (Eye)
EEG2 = load('Eye.txt');
A2 = detrend(EEG2); %Remove the mean of the signal
figure (1)
subplot(4,1,2) %First figure in subplot
plot(EEG2)
title('Raw Eye Data')
a2 = EEG2 - mean(EEG2);
%% Load data (Relax)
EEG3 = load('Relax.txt');
A4 = detrend(EEG3); %Remove the mean of the signal
figure (1)
subplot(4,1,3) %First figure in subplot
plot(EEG3)
title('Raw Relax Data')
a3 = EEG3 - mean(EEG3);
%% Load data(moving arms)
EEG4 = load('Moving arm.txt');
A5 = detrend(EEG4); %Remove the mean of the signal
figure (1)
subplot(4,1,4) %First figure in subplot
plot(EEG4)
title('Raw Moving arm Data')
a4 = EEG4 - mean(EEG4);
%% FFT of Raw EEG blinking Data
c= length(a1);
c2=2^nextpow2(c);
f=fft(a1,c2);
ff=f(1:c2/2);
xfft=fs*(0:c2/2-1)/c2;
figure(2)
subplot(4,1,1)
plot(xfft,abs(ff));
title('FFT of Raw EEG blinking Data')
%% FFT of Raw EEG Eye Data
d= length(a2);
d2=2^nextpow2(d);
f=fft(a2,d2);
ff=f(1:d2/2);
xfft=fs*(0:d2/2-1)/d2;
figure(2)
subplot(4,1,2)
plot(xfft,abs(ff));
title('FFT of Raw EEG Eye Data')
%% FFT of Raw EEG Relax Data
e= length(a3);
e2=2^nextpow2(e);
f=fft(a3,e2);
ff=f(1:e2/2);
xfft=fs*(0:e2/2-1)/e2;
figure(2)
subplot(4,1,3)
plot(xfft,abs(ff));
title('FFT of Raw EEG Relax Data')
%% FFT of Raw EEG Moving arm Data
g= length(a4);
g2=2^nextpow2(g);
f=fft(a4,g2);
ff=f(1:g2/2);
xfft=fs*(0:g2/2-1)/g2;
figure(2)
subplot(4,1,4)
plot(xfft,abs(ff));
title('FFT of Raw EEG Moving arm Data')
%% FIR band Pass filter for alpha Band pass (Blinking)
cutoff1 = 8/(fs/2);
cutoff2 = 16/(fs/2);
% FIR Filter
h=fir1(50,[cutoff1 cutoff2],'bandpass');
k=filter(h,1,a1);
figure(3)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(3)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for beta Band pass(Blinking)
cutoff3 = 16/(fs/2);
cutoff4 = 18/(fs/2);
% FIR Filter
h=fir1(50,[cutoff3 cutoff4],'bandpass');
k=filter(h,1,a1);
figure(4)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(4)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for delta Band pass(Blinking)
cutoff5 = 0.0001/(fs/2);
cutoff6 = 4/(fs/2);
% FIR Filter
h=fir1(50,[cutoff5 cutoff6],'bandpass');
k=filter(h,1,a1);
figure(5)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(5)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for theta Band pass(Blinking)
cutoff7 = 4/(fs/2);
cutoff8 = 7/(fs/2);
% FIR Filter
h=fir1(50,[cutoff7 cutoff8],'bandpass');
k=filter(h,1,a1);
figure(6)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(6)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for alpha Band pass(Eye)
cutoff1 = 8/(fs/2);
cutoff2 = 16/(fs/2);
% FIR Filter
h=fir1(50,[cutoff1 cutoff2],'bandpass');
k=filter(h,1,a2);
figure(7)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(7)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for beta Band pass(Eye)
cutoff3 = 16/(fs/2);
cutoff4 = 18/(fs/2);
% FIR Filter
h=fir1(50,[cutoff3 cutoff4],'bandpass');
k=filter(h,1,a2);
figure(8)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(8)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for delta Band pass(Eye)
cutoff5 = 0.0001/(fs/2);
cutoff6 = 4/(fs/2);
% FIR Filter
h=fir1(50,[cutoff5 cutoff6],'bandpass');
k=filter(h,1,a2);
figure(9)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(9)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for theta Band pass(Eye)
cutoff7 = 4/(fs/2);
cutoff8 = 7/(fs/2);
% FIR Filter
h=fir1(50,[cutoff7 cutoff8],'bandpass');
k=filter(h,1,a2);
figure(10)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(10)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for alpha Band pass(Relax)
cutoff1 = 8/(fs/2);
cutoff2 = 16/(fs/2);
% FIR Filter
h=fir1(50,[cutoff1 cutoff2],'bandpass');
k=filter(h,1,a3);
figure(11)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(11)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for beta Band pass(Relax)
cutoff3 = 16/(fs/2);
cutoff4 = 18/(fs/2);
% FIR Filter
h=fir1(50,[cutoff3 cutoff4],'bandpass');
k=filter(h,1,a3);
figure(12)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(12)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for delta Band pass(Relax)
cutoff5 = 0.0001/(fs/2);
cutoff6 = 4/(fs/2);
% FIR Filter
h=fir1(50,[cutoff5 cutoff6],'bandpass');
k=filter(h,1,a3);
figure(13)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(13)
subplot(2,1,2)
freqz(h)
%% FIR band Pass filter for theta Band pass(Relax)
cutoff7 = 4/(fs/2);
cutoff8 = 7/(fs/2);
% FIR Filter
h=fir1(50,[cutoff7 cutoff8],'bandpass');
k=filter(h,1,a3);
figure(14)
subplot(2,1,1)
plot(k)
title('FIR Filtered EEG data')
figure(14)
subplot(2,1,2)
freqz(h)