To remove 50Hz powerline interference from ECG signals in MATLAB, a notch filter is typically used

To remove 50Hz powerline interference from ECG signals in MATLAB, a notch filter is typically used. Below is a step-by-step implementation with code and explanations.


Solution Code

matlab
Copy
% Load or generate ECG signal (example with synthetic signal)
Fs = 1000;                     % Sampling frequency (replace with your actual Fs)
t = 0:1/Fs:5;                  % Time vector (5 seconds)
ecg_clean = 2.5*ecg(Fs).';     % Synthetic clean ECG (scaled)
ecg_clean = repmat(ecg_clean, 1, ceil(length(t)/length(ecg_clean)));
ecg_clean = ecg_clean(1:length(t));

% Add 50Hz noise
f_noise = 50;                  % Noise frequency (Hz)
noise = 0.5*sin(2*pi*f_noise*t); % Simulate 50Hz interference
ecg_noisy = ecg_clean + noise; 

% Design a 50Hz notch filter
Q = 30;                        % Quality factor (adjust for bandwidth)
wo = f_noise/(Fs/2);           % Normalized notch frequency
[b, a] = iirnotch(wo, wo/Q);   % IIR Notch filter design

% Apply the filter (zero-phase filtering)
ecg_filtered = filtfilt(b, a, ecg_noisy);

% Plot results
figure;
subplot(3,1,1);
plot(t, ecg_noisy);
title('Noisy ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(3,1,2);
plot(t, ecg_filtered);
title('Filtered ECG Signal');
xlabel('Time (s)');
ylabel('Amplitude');

% Frequency domain analysis
N = length(ecg_noisy);
f = Fs*(0:(N/2))/N;

Y_noisy = fft(ecg_noisy);
P_noisy = abs(Y_noisy/N).^2;
P_noisy = P_noisy(1:N/2+1);

Y_filtered = fft(ecg_filtered);
P_filtered = abs(Y_filtered/N).^2;
P_filtered = P_filtered(1:N/2+1);

subplot(3,1,3);
semilogy(f, P_noisy, 'b', f, P_filtered, 'r');
legend('Noisy ECG', 'Filtered ECG');
title('Frequency Spectrum');
xlabel('Frequency (Hz)');
ylabel('Power');
xlim([0 100]);

Key Steps and Explanation

  1. Simulate Noisy ECG Signal:
    • A synthetic ECG signal is generated using MATLAB’s ecg function and contaminated with 50Hz sinusoidal noise.
  2. Notch Filter Design:
    • iirnotch is used to design an IIR notch filter centered at 50Hz.
    • wo: Normalized notch frequency (50Hz divided by Nyquist frequency Fs/2).
    • Q: Controls the bandwidth (higher Q = narrower notch).
  3. Zero-Phase Filtering:
    • filtfilt is used instead of filter to eliminate phase distortion, critical for preserving ECG waveform morphology.
  4. Frequency Domain Validation:
    • The power spectrum of the noisy and filtered signals is compared to confirm 50Hz removal.

Parameter Adjustments

  • Sampling Frequency (Fs): Replace with your ECG signal’s actual sampling rate.
  • Notch Bandwidth: Adjust Q (e.g., Q = 25 for a wider notch).
  • Noise Amplitude: Modify the noise scaling factor (e.g., 0.5*sin(...)) to match your data.

Alternative Methods

  1. Adaptive Filtering:
    • Use adaptfilt.lms for dynamic noise cancellation (requires a reference noise signal).
  2. Bandstop Filter:
    • For broader interference (e.g., 48–52Hz), use designfilt with bandstopiir.
    matlab
    Copy
    d = designfilt('bandstopiir', 'FilterOrder', 4, ...
                   'HalfPowerFrequency1', 48, 'HalfPowerFrequency2', 52, ...
                   'SampleRate', Fs);

ecg_filtered = filtfilt(d, ecg_noisy);

Copy
---

### Expected Output
- The filtered ECG signal will retain its clinical features (P-waves, QRS complexes) while suppressing 50Hz noise (visible in both time and frequency domains).
“Batch processing of TIFF files with