MATLAB Answers

# filtfilt vs self coded filtfilt giving different results.

1 view (last 30 days)
Sriharsha Ramaraju on 22 Nov 2018
Answered: Mathieu NOE on 26 Apr 2021
Hello, I have a large iEEG dataset. I wanted to bandpass the iEEG signal between 0.5-125Hz. So, I used 2nd order butterworth filter. I used "filtfilt" to eleminate phase lage. Alternatively, I also applied the conventional "filter" in both directions to eliminate phase-lag. However, using "filtfilt" is changing my data: negative values to positive values and vice-versa, also phase-lagged. But, my conventional "filter" applied in both directions working perfectly fine. Can someone help me with this. I have attached the filtered signals and raw signal in plot and also the code. Happy to provide the data for the plot.
%filtfilt EEG is channles x datapoints
cut_off=[0.5 125];
sample_rate=512;
ny=sample_rate/2;
cut_off=cut_off/ny;
order=2;
[b,a] = butter(order, cut_off,'bandpass');
eeg_filtfilt = filtfilt(b,a,EEG);
%conventional filter applied in both directions (own code)
sample_rate=eeg_data.Fs;
ny=sample_rate/2;
cut_off=cut_off/ny;
order=2;
[b,a] = butter(order, cut_off,'bandpass');
EEG_f = filter(b,a,EEG);
EEG_f_reverse = EEG_f(end:-1:1, :);
EEG_f_reverse_f = filter(b,a,EEG_f_reverse);
eeg_own = EEG_f_reverse_f(end:-1:1, :);
##### 1 CommentShowHide None
Micke Malmström on 25 Apr 2021
I think that when you use filter forwards AND backwards the filter order is doubled. If you specify order 4 for filtfilt maybe they will be the same?

Sign in to comment.

### Answers (1)

Mathieu NOE on 26 Apr 2021
hello
IMHO the most obvious error is that your second simulation runs on a completely different filter
see the second time you're doing :
cut_off=cut_off/ny;
your second filter has ny times lower cut off frequencies vs the first filter , so no surprise the home made filter (x2) method seems to fail;
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!