Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
5 views7 pages

Digital Signal Processing Lab Submission .

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Digital Signal Processing Lab Submission .

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

MATLAB Code:

Butterworth:
clear; % Clear all variables from the workspace
close all; % Close all figure windows
clc; % Clear the command window

%% 1. Load Data and Setup


% -------------------------------------------------------------------------
fprintf('Loading ECG data...\n');
try
% Load the data file using the provided path
ecg = load("C:\Users\student\Desktop\ecg_hfn.data.txt");
catch ME
% Error handling if the file is not found
error('Failed to load data file. Ensure the path is correct.\n%s',
ME.message);
end

fs = 1000;
slen = length(ecg);
t = (0:slen-1) / fs;

figure('Name', 'ECG Signal Filtering Results', 'NumberTitle', 'off',


'WindowState', 'maximized');

% Plot the original noisy ECG signal


subplot(5, 1, 1);
plot(t, ecg);
title('Original Noisy ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;
grid on;

%% 2. Butterworth Low-pass Filtering [cite: 237]


% -------------------------------------------------------------------------
fprintf('Applying Butterworth filters...\n');

% ---- Filter (a): Order 2, Cutoff 10 Hz ----


N2 = 2; fc2 = 10;
Wn2 = fc2 / (fs/2); % normalized cutoff frequency
[b2, a2] = butter(N2, Wn2, 'low');
ecg_b2 = filter(b2, a2, ecg);

subplot(5, 1, 2);
plot(t, ecg_b2);
title('Butterworth Filter: Order 2, Cutoff 10 Hz');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;
grid on;

% ---- Filter (b): Order 4, Cutoff 20 Hz ----


N4 = 4; fc4 = 20;
Wn4 = fc4 / (fs/2);
[b4, a4] = butter(N4, Wn4, 'low');
ecg_b4 = filter(b4, a4, ecg);
subplot(5, 1, 3);
plot(t, ecg_b4);
title('Butterworth Filter: Order 4, Cutoff 20 Hz');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;
grid on;

% ---- Filter (c): Order 6, Cutoff 40 Hz ----


N6 = 6; fc6 = 40;
Wn6 = fc6 / (fs/2);
[b6, a6] = butter(N6, Wn6, 'low');
ecg_b6 = filter(b6, a6, ecg);

subplot(5, 1, 4);
plot(t, ecg_b6);
title('Butterworth Filter: Order 6, Cutoff 40 Hz');
xlabel('Time (s)');
ylabel('Amplitude');
axis tight;
grid on;

% ---- Filter (d): Order 8, Cutoff 70 Hz ----


N8 = 8; fc8 = 70;
Wn8 = fc8 / (fs/2);
[b8, a8] = butter(N8, Wn8, 'low');
ecg_b8 = filter(b8, a8, ecg);

subplot(5, 1, 5);
plot(t, ecg_b8);
title('Butterworth Filter: Order 8, Cutoff 70 Hz');
xlabel('Time (s)');
_ylabel('Amplitude');
axis tight;
grid on;

Chebyshev:

% MATLAB SCRIPT: Expanded Chebyshev Filter Analysis for ECG Signal


% This script designs and evaluates 5 orders each for Chebyshev Type I and
% Type II filters to observe the effect of filter order on performance.

clear; % Clear all variables


close all; % Close all figures
clc; % Clear command window

%% 1. Load Data and Analyze Original Signal


% -------------------------------------------------------------------------
fprintf('Loading ECG data...\n');
try
% Load the data file from the specified path
ecg = load("C:\Users\student\Desktop\ecg_hfn.data.txt");
catch ME
% Error message if the file is not found
error('Failed to load data file. Ensure path is correct.\n%s', ME.message);
end
fs = 1000; % Sampling frequency: 1000 Hz
slen = length(ecg); % Signal length
t = (0:slen-1) / fs; % Time vector

% Create a figure for the expanded Chebyshev analysis


figure('Name', 'Expanded Chebyshev Filter Type I', 'NumberTitle', 'off',
'WindowState', 'maximized');

% Plot original signal in the time domain


subplot(6, 1, 1);
plot(t, ecg);
title('1: Original Noisy ECG (Time Domain)');
axis tight; grid on;

figure('Name', 'Expanded Chebyshev Filter Type II', 'NumberTitle', 'off',


'WindowState', 'maximized');

% Plot original signal in the time domain


subplot(6, 1, 1);
plot(t, ecg);
title('1: Original Noisy ECG (Time Domain)');
axis tight; grid on;

%% 2. Chebyshev Type I Filtering (5 Different Orders)


% For a consistent comparison, Fc is 50 Hz and passband ripple (Rp) is 0.5
dB
% -------------------------------------------------------------------------
fprintf('Applying Chebyshev Type I filters...\n');
fc_c1 = 50; % Cutoff frequency in Hz
Rp_c1 = 0.5; % Passband ripple in dB
Wn_c1 = fc_c1 / (fs/2);

% ---- Order 3 ----


[b, a] = cheby1(3, Rp_c1, Wn_c1, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 1, 2); plot(t, ecg_filt); title('3: Cheby-I (Order 3, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 5 ----


[b, a] = cheby1(5, Rp_c1, Wn_c1, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 1, 3); plot(t, ecg_filt); title('4: Cheby-I (Order 5, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 7 ----


[b, a] = cheby1(7, Rp_c1, Wn_c1, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 1, 4); plot(t, ecg_filt); title('5: Cheby-I (Order 7, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 9 ----


[b, a] = cheby1(9, Rp_c1, Wn_c1, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 1, 5); plot(t, ecg_filt); title('6: Cheby-I (Order 9, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 11 ----


[b, a] = cheby1(11, Rp_c1, Wn_c1, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 1, 6); plot(t, ecg_filt); title('7: Cheby-I (Order 11, Fc 50
Hz)'); axis tight; grid on;

%% 3. Chebyshev Type II Filtering (5 Different Orders)


% For a consistent comparison, Fc is 50 Hz and stopband attenuation (Rs) is
40 dB
% -------------------------------------------------------------------------
fprintf('Applying Chebyshev Type II filters...\n');
fc_c2 = 50; % Cutoff frequency in Hz
Rs_c2 = 40; % Stopband attenuation in dB
Wn_c2 = fc_c2 / (fs/2);

% ---- Order 3 ----


[b, a] = cheby2(3, Rs_c2, Wn_c2, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 2, 8); plot(t, ecg_filt); title('8: Cheby-II (Order 3, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 9 ----


[b, a] = cheby2(5, Rs_c2, Wn_c2, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 2, 9); plot(t, ecg_filt); title('9: Cheby-II (Order 5, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 7 ----


[b, a] = cheby2(7, Rs_c2, Wn_c2, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 2, 10); plot(t, ecg_filt); title('10: Cheby-II (Order 7, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 9 ----


[b, a] = cheby2(9, Rs_c2, Wn_c2, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 2, 11); plot(t, ecg_filt); title('11: Cheby-II (Order 9, Fc 50
Hz)'); axis tight; grid on;

% ---- Order 11 ----


[b, a] = cheby2(11, Rs_c2, Wn_c2, 'low');
ecg_filt = filter(b, a, ecg);
subplot(6, 2, 12); plot(t, ecg_filt); title('12: Cheby-II (Order 11, Fc 50
Hz)'); axis tight; grid on;

fprintf('Expanded Chebyshev analysis complete.\n');

Output:
The primary goal of filtering the ECG signal is to remove high-frequency
noise while perfectly preserving the critical diagnostic features of the P,
QRS, and T waves. The choice of filter type and its parameters—
specifically cutoff frequency and filter order—has a significant impact
on the outcome.

1. Butterworth Filter
The Butterworth filter is known as a "maximally flat" filter, which means it
guarantees a smooth, ripple-free passband. This is its biggest advantage.
 Effect of Changing Cutoff Frequency (fc)
o Too Low (e.g., 10 Hz): A low cutoff is very effective at
removing noise, but it's too aggressive. It cuts into the
essential high-frequency components of the sharp QRS
complex, causing it to appear rounded, flattened, and
distorted. 깎
o Too High (e.g., 70 Hz): A high cutoff preserves the QRS
shape perfectly but is too permissive. It allows a significant
amount of noise to pass through, resulting in a signal that is
not sufficiently clean. 🧐
o Conclusion: The cutoff frequency must be carefully chosen to
create a balance—a value like 40 Hz was found to be a good
compromise.
 Effect of Changing Filter Order
o The order determines the steepness of the filter's transition
from passband to stopband (the "rolloff").
o A low order (e.g., 2) has a very gradual rolloff, making it poor
at separating noise that is close to the cutoff frequency.
o A high order (e.g., 8) creates a much sharper rolloff,
providing better separation between the signal and noise. For
Butterworth filters, a higher order generally improves
performance, assuming the cutoff frequency is correct.
Overall: A reliable and safe choice, but its gentle rolloff means it may not
be as effective at noise removal as other types.
2. Chebyshev Type I Filter
This filter provides a much steeper rolloff than a Butterworth filter, but this
comes at the cost of introducing ripple in the passband.
 Effect of Changing Cutoff Frequency (fc)
o The principle is the same as for the Butterworth filter: the
frequency must be chosen to separate the signal from the
noise without distorting the signal.
 Effect of Changing Filter Order
o Increasing the order of a Type I filter makes its cutoff
dramatically sharper. This leads to exceptional noise
rejection.
o However, this creates a major trade-off. Higher orders worsen
the filter's phase response, which can cause significant
ringing and overshoot artifacts around the sharp edges of
the QRS complex. This distortion can corrupt the very features
you are trying to analyze.
o The passband ripple also becomes more pronounced with
higher orders.
Overall: While excellent at noise removal due to its sharpness, the risk of
signal distortion from both passband ripple and ringing makes it a less
desirable choice for high-fidelity applications like ECG analysis. ⚠️

3. Chebyshev Type II Filter


This filter combines the best attributes of the other two: a flat, ripple-
free passband (like Butterworth) and a steep rolloff (like Type I). The
ripple is moved to the stopband, where it only affects the noise being
removed.
 Effect of Changing Cutoff Frequency (fc)
o Again, the principle of selecting a balanced cutoff frequency
(e.g., 50 Hz) remains the same.
 Effect of Changing Filter Order
o Increasing the order makes the cutoff significantly sharper,
leading to superior noise rejection.
o Crucially, because the passband is flat and the phase
response is better than Type I, increasing the order does not
introduce significant ringing or amplitude distortion.
o This means you can use a high-order Type II filter to get
excellent noise removal while simultaneously preserving the
true morphology of the ECG waveform.
Overall: The Chebyshev Type II filter is the clear winner for this task. It
provides aggressive noise filtering without compromising the integrity of
the underlying signal, making it the most robust and effective choice. 🏆

You might also like