You are on page 1of 7

Digital Signal Processing

Task - 4

Slot : L5+L6

Team :

22BEC0962 - Pugalarasu M

22BEC0722 - G B Hitesh Vaibhav

22BEC731 - Sri Saradaa K

22BEC0389 - Varun M

Code :

clc;
clear all;
close all;

fs = 1000; % Sampling Frequency


N=2^14; % Order of Time Filter
t = 0:1/fs:8.568-1/fs; % Time Vector
x=load("ecg1.dat");% loading the ECG signal
k = 0:N-1; f = (fs/1024)*k;
X = fft(x,N); % Fast Fourier Transform Of The ECG Signal
subplot(2,1,1);
plot(t,x); % Plotting the Time domain representation of ECG Signal
xlabel("Time t");ylabel("x(t)");title("Time domain representation");
subplot(2,1,2)
plot(f,abs(X)); % Plotting the Frequency domain representation of ECG Signal
xlabel("Frequency f");ylabel("|X(k)|");title("Magnitude response");

1
% Butterworth Band Stop Filter
Rs=50; % Stop Ripple Factor
Rp=0.01; % Pass Ripple Factor
fp4 = [30 100]; % Pass Band Frequencies
fs4 = [59 61]; % Stop band frequencies
wp4 = fp4 .* 2/fs;
ws4 = fs4 .* 2/fs;
[Nb_4,Wn_4] = buttord(wp4,ws4,Rp,Rs);% Get Order of the filter
[b4,a4] = butter(Nb_4,Wn_4,'stop');% Co-efficients
[h4,f_4] = freqz(b4,a4,N,'whole',fs);% get magnitude

% Butterworth Low pass Filter


fp1 = 300 ;% Pass band frequencies
fs1 = 400 ;% stop band frequencies
wp1 = 2*fp1/fs;ws1 = 2*fs1/fs;
[Nb_1, Wn_1] = buttord(wp1,ws1,Rp,Rs);% get order of filter
[b1,a1] = butter(Nb_1,Wn_1);% co-efficients
[h1,f_1] = freqz(b1,a1,N,'whole',fs);% get magnitude
filtered=h4.*h1; % adding low pass and band stop
figure;subplot(2,1,1);
plot(f_1,abs(filtered));% plotting filtered signal
axis([0 600 0 1]);
xlabel("Frequency Hz");ylabel("Magnitude response");title("Butterworth Filter");
out_butter = filter(b1,a1,x);
out_butter_fft = fft(out_butter,N);% fft of original signal

2
subplot(2,1,2);
plot(f,abs(out_butter_fft));
axis([0 14000 0 4000]);
xlabel("Frequency Hz");ylabel("Amplitude");title("Filtered signal using Butterworth
")

idft = ifft(out_butter_fft,N);% inverse fft of transformed signal


out=idft([1:length(x)]);
figure;subplot(211);
plot(t,x);%plotting time domain representation
xlabel("Time t");ylabel("x(t)");title("Time domain representation of Original
Signal");
subplot(212);
plot(t,out);% plotting magnitude spectrum
xlabel("Time t");ylabel("x(t)");title("Time domain representation of Filtered
Signal");

3
%chebyshev band stop filter

[Nc_1, Wn_1] = cheb1ord(wp1,ws1,Rp,Rs);% get order of filter


[b2,a2] = cheby1(Nc_1,Rp,Wn_1);% co-efficients
[h2,f_2] = freqz(b2,a2,1024,'whole',fs);% magnitude
Rs=50; % ripple factor
Rp=0.01; % ripple factor
fp4 = [30 100];% pass band frequencies
fs4 = [59 61];% stop band frequencies
wp5 = fp4 .* 2/fs;
ws5 = fs4 .* 2/fs;
%
[Nc_5,Wn_5] = cheb1ord(wp5,ws5,Rp,Rs);% get order of filter
[b5,a5] = cheby1(Nc_5,Rp,Wn_5,'stop');% co-efficients
[h5,f_5] = freqz(b4,a4,1024,'whole',fs);% magnitude
filtered2=h2.*h5; % adding filter
figure;subplot(2,1,1);
plot(f_5,abs(filtered2));% plotting magnitude response
axis([0 600 0 1]);
xlabel("Frequency Hz");ylabel("Magnitude response");title("Chebyshev Filter");
out_cheby = filter(b5,a5,x);
out_cheby_fft = fft(out_cheby,N); % fft of original signal
subplot(2,1,2)
plot(f,abs(out_cheby_fft));% plotting magnitude spectrum
axis([0 14000 0 4000]);

4
xlabel("Frequency Hz");ylabel("Amplitude");title("Filtered signal using Chebyshev
Filter");

idft2 = ifft(out_cheby_fft,N);% inverse fft of signal


out=idft2([1:length(x)]);%get length
figure;subplot(211);
plot(t,x);% plotting the original ECG signal
xlabel("Time t");ylabel("x(t)");title("Time domain representation of Original
Signal");
subplot(212)
plot(t,out);% plotting the flitered time domain representation
xlabel("Time t");ylabel("x(t)");title("Time domain representation of Filtered
Signal");

5
Inference

Butterworth :

• Learnt , How to Design a filter by both butterworth and chebyshev Method.


• [Nb_1, Wn_1] = buttord(wp1,ws1,Rp,Rs) , using this function we can get the order of the filter and length
of x-axis(butterworth).
• [b1,a1] = butter(Nb_1,Wn_1) , using this function we can get the coefficients of the funtion .
• [h1,f_1] = freqz(b1,a1,N,'whole',fs) , using this function we can get the magnitude response of filter and
frequency (x-axis) .

chebyshev :

• [Nc_5,Wn_5] = cheb1ord(wp5,ws5,Rp,Rs) , using this function we can get the order of the filter and
length of x-axis(chebyshev).
• [b5,a5] = cheby1(Nc_5,Rp,Wn_5,'stop') , using this function we can get the coefficients of the funtion .

6
• [h5,f_5] = freqz(b4,a4,1024,'whole',fs),using this function we can get the magnitude response of filter and
frequency (x-axis) .

You might also like