0% found this document useful (0 votes)
3 views

exp8_code

The document outlines a MATLAB script for generating, smoothing, and filtering an ECG signal. It includes steps for plotting the original ECG, applying a Savitzky-Golay filter, adding noise, designing a lowpass FIR filter, and comparing the original, noisy, and filtered signals. The script provides visualizations at each step to illustrate the effects of filtering on the ECG signal.

Uploaded by

monibks.ic.22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

exp8_code

The document outlines a MATLAB script for generating, smoothing, and filtering an ECG signal. It includes steps for plotting the original ECG, applying a Savitzky-Golay filter, adding noise, designing a lowpass FIR filter, and comparing the original, noisy, and filtered signals. The script provides visualizations at each step to illustrate the effects of filtering on the ECG signal.

Uploaded by

monibks.ic.22
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

25/11/24 11:18 AM C:\Users\MONIB SINGHA\Docu...\exp8_3.

m 1 of 2

% Parameters
fs = 1000; % Sampling frequency (Hz)
t = linspace(0, 1, 500); % Time vector (1 second, 500 samples)
ecg_signal = 1.5 * sin(2 * pi * 1 * t) + 0.5 * sin(2 * pi * 2 * t); % Simulated ECG
signal

% Step 1: Plot the Generated ECG Signal


figure;
subplot(3, 1, 1); % Subplot 1
plot(t, ecg_signal, 'b', 'LineWidth', 1.5);
title('Generated ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Step 2: Apply Savitzky-Golay filter to smooth the ECG signal


smoothed_ecg = sgolayfilt(ecg_signal, 3, 51); % Polynomial order = 3, Frame size = 51

% Plot Original vs Smoothed ECG Signal in the same figure


subplot(3, 1, 2); % Subplot 2
plot(t, ecg_signal, 'b', 'LineWidth', 1.5); hold on;
plot(t, smoothed_ecg, 'r--', 'LineWidth', 1.5);
title('Smoothed ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Original ECG Signal', 'Smoothed ECG Signal');
grid on;

% Step 3: Add High-Frequency Noise to the Smoothed ECG Signal


noise = 0.2 * sin(2 * pi * 50 * t); % 50 Hz high-frequency noise
noisy_ecg = smoothed_ecg + noise;

% Plot Noisy ECG Signal in the same figure


subplot(3, 1, 3); % Subplot 3
plot(t, noisy_ecg, 'r', 'LineWidth', 1.5);
title('Noisy ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');
grid on;

% Step 4: Design a Lowpass FIR Filter


cutoff_freq = 20; % Cutoff frequency (Hz)
filter_order = 100; % Filter order
nyquist = fs / 2; % Nyquist frequency
lowpass_filter = fir1(filter_order, cutoff_freq / nyquist, 'low', hamming
(filter_order + 1));

% Step 5: Apply the Lowpass Filter to the Noisy ECG Signal


filtered_ecg = filter(lowpass_filter, 1, noisy_ecg);
25/11/24 11:18 AM C:\Users\MONIB SINGHA\Docu...\exp8_3.m 2 of 2

% Step 6: Combine Plots for Noisy vs Filtered ECG Signal


figure;
plot(t, noisy_ecg, 'r', 'LineWidth', 1.5); hold on;
plot(t, filtered_ecg, 'g', 'LineWidth', 1.5);
title('Noisy vs Filtered ECG Signal' );
xlabel('Time (s)');
ylabel('Amplitude');
legend('Noisy ECG Signal', 'Filtered ECG Signal');
grid on;

% Step 7: Compare Original, Noisy, and Filtered Signals


figure;
plot(t, ecg_signal, 'b--', 'LineWidth', 1.5); hold on;
plot(t, noisy_ecg, 'r', 'LineWidth', 1.5);
plot(t, filtered_ecg, 'g', 'LineWidth', 1.5);
title('Comparison of ECG Signals');
xlabel('Time (s)');
ylabel('Amplitude');
legend('Original ECG Signal', 'Noisy ECG Signal', 'Filtered ECG Signal');
grid on;

You might also like