Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 CONTENT PART A: BIOMEDICAL SIGNAL PROCESSING USING MATLAB 1 Operations on Signals a. Generation of a step function b. Amplitude Scaling c. Time Scaling d. Shifting e. Circular Shifting f. Linear Convolution g. Circular Convolution. 2 To verify properties of DFT: a. Linearity property b. Circular convolution c. Multiplication in time domain d. Parseval’s theorem e. Circular folding/Correlation f. Time shifting g. Frequency shifting. 3 Verification of Sampling Theorem 4 Design of digital IIR Butterworth filters a. Bilinear transformation b. impulse invariant methods 5 To design FIR filters using windowing Technique 6 Design and Implementation of Moving Average filters 7 Design and Implementation of Derivative Based Filters 8 Design and Implementation of Notch Filters and Comb Filters 9 To perform QRS detection using PAN - TOMPKINS algorithm 10 To perform derivative based QRS detection 11 Detection of EEG rhythms 12 To perform Spectral Analysis of Biomedical Signals PART B : BIOMEDICAL SIGNAL PROCESSING USING PYTHON 1 Program to find signal to noise ratio 2 Verification of sampling theorem 2 Program to design 3 - point and 8 - point Moving average filter 4 Program to remove high frequency noise from an ECG signal using moving average filter 5 Program to design high pass derivative based filters to remove lower frequency noise 6 Program to remove lower frequency noise and hence baseline wandering using der ivative based filters 7 Program to detect the presence of Rhythms’ in EEG signal Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 PART A: BIOMEDICAL SIGNAL PROCESSING USING MATLAB Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 1. Operations on Signals: h. Generation of a step function i. Amplitude Scaling j. Time Scaling k. Shifting l. Circular Shifting m. Linear Convolution n. Circular Convolution. 1a Step Function clc; clear all; close all; x=[1 1 1 1]; N=length(x); n=0:N - 1; figure(1) subplot(2,2,1) stem(n,x) title('Step function') 1b %amplitude scaling f=2; a=f.*x; subplot(2,2,2) stem(n,a) title('Amplitude scaling') 1c Time Scaling %Time upscaling b=f.*n; subplot(2,2,3) stem(b,x) title('Time upscaling') %Time downscaling c=downsample(x,f); c1=length(c); d=0:c1 - 1; subplot(2,2,4) stem(d,c) title('Time downscaling') Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 Output: Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 1d Linear Shift Write a MATLAB program to perform the following linearly shift combinations for the causal signal x(n)=[14356] using precedence rule. i. x(n - k) ii. x(n+k) iii. x( - n+k) iv. x( - n - k) k=2 clc; close all; clear all; x=[1 4 3 5 6]; n=length(x); m=0:n - 1; k=2; subplot(2,3,1) stem(m,x); title('x(n)'); xlabel('Time'); ylabel('Amplitude'); a=k:n+k - 1; subplot(2,3,2) stem(a,x); title('x(n - 2)'); xlabel('Time'); ylabel('Amplitude'); b= - k:n - k - 1; subplot(2,3,3) stem(b,x); title('x(n+2)'); xlabel('Time'); ylabel('Amplitude'); y=flip(x); c= - (n+1): - k; subplot(2,3,4) stem(c,y); title('x( - n+2)'); xlabel('Time'); ylabel('Amplitude'); d= - k:k; subplot(2,3,5) stem(d,y); title('x( - n - 2)'); xlabel('Time'); ylabel('Amplitude'); Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 Output: 1e Circular Shifting clc; clear all; close all; x=[1 5 3 2]; m=3; N=length(x); y=circshift(x',m); stem(y) OUTPUT: Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 1 f Linear Convolution Write a MATLAB program to compute linear convolution of two sequences. X1=[123456] X2=[1512179] clc; close all; clear all; x1=[1 2 3 4 5 6]; x2=[1 5 1 2 1 7 9]; y=conv(x1,x2) disp(y); OUTPUT: y = 1 7 14 23 33 50 69 59 72 77 87 54 1 7 14 23 33 50 69 59 72 77 87 54 1 g Circular Convolution Write a MATLAB program to perform circular convolution of two sequences by (i) Inbuilt function (ii) By using Stockham’s method %Inbuilt Function clc; close all; clear all; x1=[1 2 3 4]; x2=[1 5 3 2]; M=length(x1); N=length(x2); if M>N x2=[x2,zeros(1,M - N)]; a=cconv(x1,x2,M) Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 if N>M x1=[x1,zeros(1,N - M)]; b=cconv(x1,x2,N) end if M==N a=cconv(x1,x2,M) end Output: a = 3 4 2 5 2 4 27 2. To verify properties of DFT: a. linearity property b. Circular convolution c. Multiplication in time domain d. Parseval’s theorem e. Circular folding /Correlation f. Time shifting g. frequency shifting. 2a linearity property Write a MATLAB program to compute the DFT of a sequence which is the resul t of addition of two signals x1 and x2 scaled by factors a1 and a2 and hence prove linearity property of DFT. clc; clear all; close all; x1=[1 2 3 4]; x2=[1 5 3 2]; a=2; b=3; lhs=fft((a.*x1)+(b.*x2)) X1=fft(x1); X2=fft(x2) rhs=((a.*X1)+(b.*X2)) OUTPUT: lhs = 53.0000 + 0.0000i - 10.0000 - 5.0000i - 13.0000 + 0.0000i - 10.0000 + 5.0000i rhs = 53.0000 + 0.0000i - 10.0000 - 5.0000i - 13.0000 + 0.0000i - 10.0000 + 5.0000i 2b Circular convolution %Stockholm's method clc; clear all; close all; x1=[1 2 3 4]; Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 x2=[1 5 3 2]; X1=fft(x1); X2=fft(x2); Y=X1.*X2; y=ifft(Y); disp(y); OUTPUT X1= X2= Y= Y= 2c: Multiplication in time domain Write a MATLAB program to compute the DFT of a sequence which is the result of multiplication of two signals x1 and x2 in its time domain. clc; close all; clear all; x1=[1 2 3 4]; x2=[1 5 3 2]; N=length(x1); lhs=fft(x1.*x2) X1=fft(x1); X2=fft(x2); Y=cconv(X1,X2,N); rhs=Y/N OUTPUT: lhs = 28.0000 + 0.0000i - 8.0000 - 2.0000i - 8.0000 + 0.0000i - 8.0000 + 2.0000i rhs = 28.0000 + 0.0000i - 8.0000 - 2.0000i - 8.0000 + 0.0000i - 8.0000 + 2.0000i 2d Parseval’s Theorem Find the energy of the signal below and hence prove the Parsevals theorem x = [1234] clc; close all; clear all; x=[1 2 3 4]; N=length(x); lhs=sum(conj(x).*x) X=fft(x); rhs=(sum(conj(X).*X))/N OUTPUT: lhs = 30 rhs = 30 Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 2e Correlation Illustrate the correlation property of DFT for the sequences x1(n)=[1532] and x2(n)=[2456] and hence find the correlation of two sequences. clc; clear all; close all; x=[1 5 3 2]; y=[2 4 5 6]; X=fft(x); Y=conj(fft(y)); R=X.*Y; r=ifft(R); disp(r); Output: 49 38 49 51 2f Time Shifting Illustrate the time shifting property of DFT for the sequence x(n)=[1532] and shift value 3. clc; clear all; close all; x=[1 5 3 2]; m=3; N=length(x); y=circshift(x',m); lhs=fft(y) X=fft(x); for k=0:N - 1 w(k+1)=exp(( - j*2*pi*k*m)/N); end rhs=X.*w OUTPUT: lhs = 11.0000 + 0.0000i 3.0000 - 2.0000i 3.0000 + 0.0000i 3.0000 + 2.0000i rhs = 11.0000 + 0.0000i 3.0000 - 2.0000i 3.0000 + 0.0000i 3.0000 + 2.0000i Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 2g Frequency Shifting Illustrate the frequency shifting property of DFT for the sequence x(n)=[1532] and shift value3 clear all; close all; x=[1 5 3 2]; l=3; N=length(x); for n=0:N - 1 w(n+1)=exp((j*2*pi*l*n)/N); end lhs=fft(x.*w) X=fft(x); rhs=circshift(X,l) OUTPUT: lhs = - 2.0000 - 3.0000i - 3.0000 + 0.0000i - 2.0000 + 3.0000i 11.0000 - 0.0000i rhs = - 2.0000 - 3.0000i - 3.0000 + 0.0000i - 2.0000 + 3.0000i 11.0000 + 0.0000i 3. Verification of Sampling Theorem Consider a cos wave of 80 Hz and Verify Sampling Theorem by considering fs1=100Hz,fs2=300Hz,fs3=1000Hz clc; clear all ; close all ; t=0:0.001:0.1; x=cos(2.*pi.*80.*t); N=length(x); subplot(4,1,1); plot(t,x); xlabel( 'time' ); ylabel( 'amplitude' ); title( 'input signal' ); fs=input( 'Enter the value of sampling frequency' ); n=0:1/fs:0.1; x1=sin(2.*pi.*80.*n); N1=length(x1); subplot(4,1,2); stem(n,x1); xlabel( 'time' ); ylabel( 'amplitude' ); title( 'sampled signal' ); X=fft(x); k=0:N - 1; f1=k.*80./N; subplot(4,1,3); Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 stem(f1,abs(X)); xlabel( 'frequency' ); ylabel( 'amplitude' ); title( 'FT of input signal' ); X1=fft(x1); k1=0:N1 - 1; f2=k1.*fs./N1; subplot(4,1,4); stem(f2,abs(X1)); xlabel( 'frequency' ); ylabel( 'amplitude' ); title( 'FT of s ampled signal' ); OUTPUT: FS=100 Hz fs=100 Hz Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 Fs=300 Hz Fs= 1000 Hz 4. Design of digital IIR Butterworth filters a. Bilinear transformation b. impulse invariant methods 4a Bilinear Transformation Design a high pass Butterworth filter using Bilinear transformation for satisfying the following constraints: Cut off 50Hz N=2 ,Sampling frequency 150Hz clc; clear all; close all; Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 wc=input('enter wc'); fs=input('enter fs'); N=input('enter order'); wc=2*pi*wc/fs; wc1=2*fs*tan(wc/2); [b,a]=butter(N,wc,'high','s'); [bz,az]=bilinear(b,a,fs); disp(bz); disp(az); figure(1); H=freqs(b,a); subplot(2,1,1); plot(abs(H)); title('Analog frequency response |H|'); xlabel('Frequency') ylabel('Magnitude') su bplot(2,1,2); plot(20*log10(abs(H))); title('Analog frequency response in dB'); xlabel('Frequency') ylabel('Magnitude') figure(2); [H,f]=freqz(bz,az,512,fs); subplot(2,1,1); plot(f,abs(H)); title('Digital frequency response |H|'); xlabel('Frequency') ylabel('Magnitude') subplot(2,1,2); plot(f,20*log10(abs(H))); Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 title('digital frequency response |H|'); xlabel('Frequency') ylabel('Magnitude') Output: enter wc 50 enter fs 150 enter order 2 0.9902 - 1.9804 0.9902 1.0000 - 1.9803 0.9804 4b Impulse Invariance Methods Design a low pass Chebyshev filter using Impulse Invariant method for satisfying the following constraints: Pass Band 0 – 400Hz Stop band 2.1 - 4kHz Pass band ripple 2dB Stop band attenuation 20dB Sampling frequency 10kHz clc; Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 cle ar all; close all; % Chebyshev low pass filter design using Impulse invariance Technique kp=input('enter the pass band ripple'); ks=input('enter stopband ripple'); wp=input('pass band edge frequency'); ws=input('enter stop band edge frequency'); fs=input('enter sampling frequency'); %digital filter specifications wp=2*pi*wp/fs; ws=2*pi*ws/fs; %pre warping wp1= wp*fs; ws1=ws*fs; %order of analog filter [N W]=cheb1ord(wp1,ws1,kp,ks,'s'); %system function of analog filter [b a]=cheby1(N,kp,W,'low','s'); %digital filter using bilinear transformation [bz,az]=impinvar(b,a,fs); disp(bz) disp(az) %to plot analog filter response figure(1); H=freqs(b,a); subplot(2,1,1); plot(abs(H)); title('Analog frequency response |H|'); xlabel ('Frequency') ylabel('Magnitude') subplot(2,1,2); plot(20*log10(abs(H))); title('Analog frequency response in dB'); xlabel('Frequency') ylabel('Magnitude') Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 figure(2); [H,f]=freqz(bz,az,512,fs); subplot(2,1,1); plot(f,abs(H)); title('Digital frequency r esponse |H|'); xlabel('Frequency') ylabel('Magnitude') subplot(2,1,2); plot(f,20*log10(abs(H))); title('Digital frequency response |H|'); xlabel('Frequency') ylabel('Magnitude') Output: enter the pass band ripple 2 enter stopband ripple 20 pass band edge frequency 400 enter stop band edge frequency 2100 enter sampling frequency 10000 0 0.0371 0 1.0000 - 1.7702 0.8171 Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 5. To design FIR filters using windowing Technique Design a lowpass filter for the following desired frequency response using Rectangular window for N=7 and wc=1.0471 rad clear all; close all; clc; wc=input('Enter digital cutoff frequency in radians'); N=input('Enter the order'); wd=rectwin(N); %change as rectwin,hamming,hann,bartlett,blackman according to requ ired window function fprintf('wd \ n'); disp(wd'); alpha=(N - 1)/2; eps=0.001; n=0:N - 1; Hd=sin(wc*(n - alpha+eps))./(pi*(n - alpha+eps)); %%for low pass %Hd=((sin(pi*(n - alpha+eps))) - (sin(wc*(n - alpha+eps))))./(pi*(n - alpha+eps));%%for high pass fprintf('Hd \ n'); disp(Hd); Hn=Hd.*wd'; fprintf('Hn \ n'); disp(Hn); % to plot the responses figure(1) w=0:0.1:pi; N1=length(w); wd1=rectwin(N1); n1=0:N1 - 1; figure(1) subplot(2,1,1) %to plot the window function in time domain plot(n1,wd1); xlabel( 'Time'); ylabel('Window Magnitude'); title('Window function'); %to plot the window function in frequency domain subplot(2,1,2) [h1,f1]=freqz(wd1,N1); plot(f1,(20*log10(abs(h1)))); xlabel('Frequency'); ylabel('Window Magnitude in dB'); title('Window function'); Biomedical Signal Processing Lab Manual – MLL58 2021 - 2022 %to draw the frequency response - Hd(w) figure(2) [h2,f2]=freqz(Hd); subplot(2,2,1); plot(f2,abs(h2)); xlabel('Frequency'); ylabel('Hd Magnitude'); title('Hd function'); %to draw the frequency response - H(w) [H f]=freqz(H n,512,10000); subplot(2,2,2); plot(f,abs(H)); xlabel('Frequency'); ylabel('Magnitude'); title('Magnitude response |H|'); subplot(2,2,3); plot(f,20*log10(abs(H))); xlabel('Frequency'); ylabel('Magnitude'); title('Magnitude response in db'); Output: Enter digital cut off frequency in radians 1.0471 Enter the order 7 wd 1 1 1 1 1 1 1 Hd 0.0001 0.1380 0.2758 0.3333 0.2755 0.1377 - 0.0001 Hn 0.0001 0.1380 0.2758 0.3333 0.2755 0.1377 - 0.0001