Data filtering using Butterworth filter

I want to filter data using Butterworth filter. I am currently using Matlab and I want to know how to do it in Mathematica.

I have the following data:

data1 = Table[
PDF[NormalDistribution[3.5, .8], i] +
PDF[NormalDistribution[6, 1], i], {i, -5, 15, .01}];
noise = RandomReal /@ RandomReal[{-0.2, .2}, Length[data1]];
data2 = data1 + noise;

I filtered this data using Matlab as follows:

[b,a] = butter(8,0.04);

filtfilt(b,a,data2 );

In the previous question's answer, bill s suggested to do it as follows in Mathematica:

RecurrenceFilter[
ToDiscreteTimeModel[ButterworthFilterModel[{2, 0.04}], 1], data2
]

The result is as follows: The problem I am facing is whatever I change the filter order and frequency, I am not able to produce the same filtered data as I am getting from Matlab.

Can someone suggest for me what to do and what values to use to get the same filtered data as I am getting from Matlab?

Thanks

• filtfilt does a forward-backward filtering. Have you tried filtering, reversing the output, filtering again and finally reversing again in Mathematica?
– rm -rf
Apr 21 '14 at 5:24

filtfilt does a forwards and then a backwards filtering, as rm -rf suggests. To filter data in this way, you might try

forwards = RecurrenceFilter[ToDiscreteTimeModel[ButterworthFilterModel, 1], data];
backwards = RecurrenceFilter[ToDiscreteTimeModel[ButterworthFilterModel, 1], Reverse[data]];
(forwards + Reverse[backwards])/2

The nice thing about filtfilt (or the forwards/backwards filter above) is that the filtering has no phase shift/delay when compared with the original data.

• Good. Great Thanks. Apr 21 '14 at 22:07
• it is nice I upvote this answer and you edited it. this was my second question in this forum and that time I didn't know there is upvote. sorry for waiting all this time to upvote your answer :) Oct 15 '14 at 19:09
• filtfilt is a zero-phase filter. Filters may be made to have zero phase and so prevent the filtered waveform being delayed compared to the waveform it filters, by using the procedure offered by bill s. Just need to halve the result to get the correct amplitude.
– user41395
Aug 4 '16 at 1:39