B 1
B 1
B 1
Instrumentation
Practical File
Submitted by:
Suraj Kaul
547/IC/11
ICE-3
INDEX
S. no.
Topic
Signature
Experiment 1:
To calculate heart rate from ECG signal
clear all;
clc;
load ecg.mat;
ecg=val(1,:); %extract one set of ECG
fs=360; %sampling frequency of ECG signal
cnt=0;
l=length(ecg);
base=min(ecg);
ecg=ecg-base; %moving base line to 0
thres=1150-base; %magnitude above which R peak is read
for i=2:l-1
if(ecg(1,i)>thres)
if(ecg(1,i)>ecg(1,i-1)&&ecg(1,i)>ecg(1,i+1))
cnt=cnt+1;
end
end
end
N=length(ecg);
duration_in_seconds=N/fs;
time=0:(duration_in_seconds-1)/length(ecg):duration_in_seconds; %defining time
axis
duration_in_minutes=duration_in_seconds/60;
rateval=cnt/duration_in_minutes; %Heart rate=number of R peaks in 1 minute
n=(60/rateval); %time taken in 1 heartbeat
figure('color',[1 1 1], 'Position', [200 200 500 400]);
plot(time(1:n*fs), ecg(1:n*fs)); %plotting 1 heartbeat
title(strcat('Heart Rate: ',num2str(rateval),' Beats Per Minute'));
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
Magnitude (mV)
300
200
100
0
0
0.1
0.2
0.3
0.4
Time (sec)
0.5
0.6
0.7
Experiment 2:
Calculation of heart rate using wavelet technique
clear all;
clc;
load ecg.mat;
cnt=0;
n=6;
fs=360; %sampling frequency of ECG signal
ecg=val(1,:);
base=min(ecg);
ecg=ecg-base; %moving base line to 0
[c l]=wavedec(ecg,n,'db6');
for i=1:n
a{i}=wrcoef('a',c,l,'db6',i);
d{i}=wrcoef('d',c,l,'db6',i);
end
e1=d{3}+d{4}+d{5};
e2=(d{4}.*(d{3}+d{5}))./2^n;
e3=abs(e1.*e2);
T=0.15*max(e3);
l=length(e3);
cnt=0;
for i=2:l
if(e3(i)>T)
if(e3(i)>e3(i-1)&&e3(i)>e3(i+1))
cnt=cnt+1;
end
end
end
N=length(ecg);
duration_in_seconds=N/fs;
time=0:(duration_in_seconds-1)/length(ecg):duration_in_seconds; %defining time
axis
duration_in_minutes=duration_in_seconds/60;
rateval=cnt/duration_in_minutes; %Heart rate=number of R peaks in 1 minute
n=(60/rateval); %time taken in 1 heartbeat
figure('color',[1 1 1], 'Position', [200 200 500 400]);
plot(time(1:n*fs), e3(1:n*fs)); %plotting 1 heartbeat
title(strcat('Heart Rate: ',num2str(rateval),' Beats Per Minute'));
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
4
Magnitude (mV)
x 10
0
0
0.1
0.2
0.3
0.4
Time (sec)
0.5
0.6
0.7
Experiment 3:
Design of HP,LP and BP for filtering EEG signal
clear all;
clc;
load eeg.mat;
eeg = val(1,1:1000);
fs=100; %sampling frequency of EEG signal
N=length(eeg);
duration_in_seconds=N/fs;
time=1/N:(duration_in_seconds)/N:duration_in_seconds; %defining time axis
duration_in_minutes=duration_in_seconds/60;
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, eeg);
title('EEG signal');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(eeg,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
% Low Pass Filter Out
[b a] = butter(2,30*2/fs);
h=filter(b,a,eeg);
figure('color',[1 1 1]);
subplot(2,1,1), plot(time, h);
title('Low Pass fc=30Hz');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
% High Pass Filter out
[b a]=butter(2,5*2/fs,'high');
h=filter(b,a,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('High Pass fc=5Hz');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
% Band Pass Filter Out
[b a]=butter(2,[5 30]*2/fs);
h=filter(b,a,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Band Pass f1=5Hz f2=30Hz');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Experiment 4:
To plot EEG bands and study their frequency spectrum
clear all;
clc;
load eeg.mat;
eeg = val(1,1:1000);
fs=100; %sampling frequency of EEG signal
N=length(eeg);
duration_in_seconds=N/fs;
time=1/N:(duration_in_seconds)/N:duration_in_seconds; %defining time axis
duration_in_minutes=duration_in_seconds/60;
%%
[a b] = butter(2,[0.5 4]*2/fs);
[c d] = butter(2,[4 8]*2/fs);
[e f] = butter(2,[8 12]*2/fs);
[g h] = butter(2,[12 22]*2/fs);
[i j] = butter(2,[22 49]*2/fs);
%%
h=filter(a,b,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Delta');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
h=filter(c,d,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Theta');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
h=filter(e,f,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Alpha');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
h=filter(g,h,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Beta');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
h=filter(i,j,eeg);
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, h);
title('Gamma');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(h,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Experiment 5:
To plot EEG bands using wavelet and study their frequency spectrum
clear;
load eeg.mat;
eeg=val(1,:);
N=length(eeg);
fs=100; %sampling frequency of EEG signal
duration_in_seconds=N/fs;
time=1/N:(duration_in_seconds)/N:duration_in_seconds; %defining time axis
n=5;
wave='db6';
[c l]=wavedec(eeg, n, wave);
%%
%gamma
gamma = wrcoef('d', c, l, wave, 1);
figure('color',[1 1 1], 'Position', [200 200 500 400]);
subplot(2,1,1),plot(time, gamma)
title('Gamma Wave');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(gamma,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
%beta
beta = wrcoef('d', c, l, wave, 2);
figure('color',[1 1 1], 'Position', [200 200 500 400]);
subplot(2,1,1),plot(time, beta)
title('Beta Wave');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(beta,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
%theta
theta = wrcoef('d', c, l, wave, 4);
figure('color',[1 1 1], 'Position', [200 200 500 400]);
subplot(2,1,1),plot(time, theta)
title('Theta Wave');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(theta,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
%delta
delta = wrcoef('d', c, l, wave, 5);
figure('color',[1 1 1], 'Position', [200 200 500 400]);
subplot(2,1,1),plot(time, delta)
title('Delta Wave');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(delta,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Experiment 6:
To plot EEG and ECG spectrums
clear all;
clc;
load eeg.mat;
eeg = val(1,1:1000);
fs=100; %sampling frequency of EEG signal
N=length(eeg);
duration_in_seconds=N/fs;
time=1/N:(duration_in_seconds)/N:duration_in_seconds; %defining time axis
duration_in_minutes=duration_in_seconds/60;
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, eeg);
title('EEG signal');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(eeg,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
clear all
load ecg.mat;
ecg = val(1,1:1000);
fs=100; %sampling frequency of EEG signal
N=length(ecg);
duration_in_seconds=N/fs;
time=1/N:(duration_in_seconds)/N:duration_in_seconds; %defining time axis
duration_in_minutes=duration_in_seconds/60;
figure('color',[1 1 1]);
subplot(2,1,1),plot(time, ecg);
title('ECG signal');
xlabel('Time (sec)');
ylabel('Magnitude (mV)');
NFFT = 2^nextpow2(N); % Next power of 2 from length of y
Y = fft(ecg,NFFT)/N;
f = fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
subplot(2,1,2), plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
Magnitude (mV)
ECG signal
1400
1200
1000
800
0
5
Time (sec)
10
30
35
40
45
50
10
35
40
45
50
2000
|Y(f)|
0
0
10
15
20
EEG signal
Magnitude (mV)
25
Frequency (Hz)
x 10
2
0
-2
0
5
Time (sec)
6000
|Y(f)|
10
15
20
25
Frequency (Hz)
30