To design and analyze digital FIR and IIR filters for noise reduction using MATLAB

To design and analyze digital FIR and IIR filters for noise reduction using MATLAB, follow these steps:

1. Generate a Noisy Test Signal

Create a clean signal with desired frequencies and add noise:

matlab
Copy
Fs = 10000;                 % Sampling frequency (Hz)
t = 0:1/Fs:1;               % Time vector (1 second)
y_clean = sin(2*pi*1000*t) + 0.5*sin(2*pi*2000*t); % Clean signal
noise = 0.5*randn(size(t)); % Gaussian white noise
y_noisy = y_clean + noise;  % Noisy signal

2. Design FIR and IIR Filters

Use designfilt to create lowpass filters with specified parameters:

matlab
Copy
% FIR Filter (Equiripple)
fir_lp = designfilt('lowpassfir', ...
    'PassbandFrequency', 2500, ...
    'StopbandFrequency', 3500, ...
    'PassbandRipple', 0.5, ...
    'StopbandAttenuation', 40, ...
    'SampleRate', Fs);

% IIR Filter (Elliptic)
iir_lp = designfilt('lowpassiir', ...
    'PassbandFrequency', 2500, ...
    'StopbandFrequency', 3500, ...
    'PassbandRipple', 0.5, ...
    'StopbandAttenuation', 40, ...
    'SampleRate', Fs, ...
    'DesignMethod', 'ellip');

3. Apply Filters Using Zero-Phase Filtering

Use filtfilt to eliminate phase delay:

matlab
Copy
y_fir = filtfilt(fir_lp, y_noisy); % FIR filtered signal
y_iir = filtfilt(iir_lp, y_noisy); % IIR filtered signal

4. Analyze Results

Time-Domain Plots

matlab
Copy
subplot(3,1,1);
plot(t, y_clean);
title('Clean Signal');
subplot(3,1,2);
plot(t, y_noisy);
title('Noisy Signal');
subplot(3,1,3);
plot(t, y_fir, 'b', t, y_iir, 'r');
legend('FIR Filtered', 'IIR Filtered');
title('Filtered Signals');

Frequency-Domain Analysis

Compute and plot FFTs:

matlab
Copy
NFFT = 2^nextpow2(length(y_clean));
f = Fs/2*linspace(0,1,NFFT/2+1);

Y_clean = fft(y_clean, NFFT);
Y_noisy = fft(y_noisy, NFFT);
Y_fir = fft(y_fir, NFFT);
Y_iir = fft(y_iir, NFFT);

figure;
subplot(2,1,1);
plot(f, 20*log10(abs(Y_clean(1:NFFT/2+1))), 'k');
hold on;
plot(f, 20*log10(abs(Y_noisy(1:NFFT/2+1))), 'r');
title('Frequency Response: Clean vs. Noisy');
legend('Clean', 'Noisy');

subplot(2,1,2);
plot(f, 20*log10(abs(Y_fir(1:NFFT/2+1))), 'b');
hold on;
plot(f, 20*log10(abs(Y_iir(1:NFFT/2+1))), 'r');
title('Frequency Response: FIR vs. IIR');
legend('FIR', 'IIR');

Performance Metrics

Calculate SNR and MSE:

matlab
Copy
% Signal-to-Noise Ratio (SNR)
SNR_noisy = 10*log10(var(y_clean)/var(y_noisy - y_clean));
SNR_fir = 10*log10(var(y_clean)/var(y_fir - y_clean));
SNR_iir = 10*log10(var(y_clean)/var(y_iir - y_clean));

% Mean Squared Error (MSE)
MSE_fir = mean((y_clean - y_fir).^2);
MSE_iir = mean((y_clean - y_iir).^2);

disp(['SNR Noisy: ', num2str(SNR_noisy), ' dB']);
disp(['SNR FIR: ', num2str(SNR_fir), ' dB']);
disp(['SNR IIR: ', num2str(SNR_iir), ' dB']);
disp(['MSE FIR: ', num2str(MSE_fir)]);
disp(['MSE IIR: ', num2str(MSE_iir)]);

5. Filter Visualization

Use fvtool to inspect filter characteristics:

matlab
Copy
fvtool(fir_lp, iir_lp);
legend('FIR', 'IIR');

Key Observations

  • FIR Filters:
    • Linear phase (no phase distortion).
    • Higher order (e.g., fir_lp.FilterOrder) for similar specs.
    • Stable by design.
  • IIR Filters:
    • Lower order for the same specs.
    • Non-linear phase (unless using filtfilt).
    • Check stability with isstable(iir_lp).

Conclusion

  • Use FIR when phase linearity is critical (e.g., audio processing).
  • Use IIR for computational efficiency in high-order applications.
  • filtfilt ensures zero-phase filtering but doubles the effective order.

This approach effectively reduces noise while preserving signal integrity. Adjust filter parameters based on specific application requirements.