Your issue may be due to insufficient filter order or improper design of the FIR filter. To apply an effective low-pass filter to your EEG signal, here are a few key suggestions:
Improvements to Your Code
-
Increase Filter Order:
- The filter order should be sufficiently high to achieve the desired attenuation for frequencies above the cutoff. However, an overly high order can make the filter unstable or cause excessive computation time.
-
Check the Normalized Cutoff Frequency:
- In your code, the normalized cutoff frequency is calculated as cutoff×2/sample_rate\text{cutoff} \times 2 / \text{sample\_rate}. This is correct, but ensure the resulting value is ≤1\leq 1.
-
Use a Window Method or Different Design Approach:
- The
fir1function uses a window-based design method, but you might achieve better performance with an alternative design approach, like usingdesignfilt.
- The
-
Analyze the Filter's Frequency Response:
- Plot the frequency response of the filter to confirm that it meets your requirements.
Corrected Code with Suggestions
Here’s an updated and more robust way to design and apply the filter:
% Given parameters
sample_rate = 50000; % 50 kHz
cutoff = 10; % Cutoff frequency in Hz
filter_order = 500; % Increase filter order for sharper cutoff
% Normalize cutoff frequency
normalized_cutoff = cutoff / (sample_rate / 2);
% Design the low-pass FIR filter
lowpass = fir1(filter_order, normalized_cutoff, 'low');
% Analyze the frequency response of the filter
fvtool(lowpass, 1);
% Apply the filter to your EEG data
y_lowpass = filter(lowpass, 1, y);
% Plot the original and filtered signals
t = (0:length(y)-1) / sample_rate; % Time vector
figure;
subplot(2, 1, 1);
plot(t, y);
title('Original Signal');
xlabel('Time (s)');
ylabel('Amplitude');
subplot(2, 1, 2);
plot(t, y_lowpass);
title('Low-pass Filtered Signal');
xlabel('Time (s)');
ylabel('Amplitude');
Key Points:
-
Filter Order:
- A higher order (e.g., 500) provides a sharper transition band but increases computation time. You can tune this value based on your needs.
-
Frequency Normalization:
- Ensure that the cutoff frequency is normalized with respect to the Nyquist frequency (sample_rate/2\text{sample\_rate}/2).
-
Filter Verification:
- Use
fvtoolto visualize the frequency response of the filter and verify that it attenuates frequencies above 10 Hz effectively.
- Use
Alternative: Using designfilt
Instead of fir1, you can use designfilt for more control over the filter design:
% Design a low-pass FIR filter with designfilt
d = designfilt('lowpassfir', ...
'FilterOrder', filter_order, ...
'CutoffFrequency', cutoff, ...
'SampleRate', sample_rate);
% Analyze the frequency response
fvtool(d);
% Apply the filter
y_lowpass = filter(d, y);
Verify the Filtered Signal
To ensure that frequencies above 10 Hz are removed:
- Perform a Fourier Transform of the original and filtered signals to compare the frequency spectra.
- Use
fftandplotto visualize the power in each frequency band.
% FFT of the original signal
Y_orig = fft(y);
f = linspace(0, sample_rate / 2, floor(length(Y_orig) / 2) + 1);
% FFT of the filtered signal
Y_filtered = fft(y_lowpass);
% Plot the magnitude spectra
figure;
plot(f, abs(Y_orig(1:length(f))), 'b');
hold on;
plot(f, abs(Y_filtered(1:length(f))), 'r');
title('Frequency Spectrum');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
legend('Original', 'Filtered');
Troubleshooting
- If the filtered signal still contains power above 10 Hz, increase the filter order or consider using an IIR filter (e.g., Butterworth) for a sharper cutoff.
- Ensure that your signal is not being corrupted by noise or artifacts during recording or preprocessing.
Let me know if you need additional guidance!
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: