7
$\begingroup$

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: enter image description here

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

$\endgroup$
1
  • 1
    $\begingroup$ filtfilt does a forward-backward filtering. Have you tried filtering, reversing the output, filtering again and finally reversing again in Mathematica? $\endgroup$
    – rm -rf
    Apr 21, 2014 at 5:24

1 Answer 1

6
$\begingroup$

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[5], 1], data];
backwards = RecurrenceFilter[ToDiscreteTimeModel[ButterworthFilterModel[5], 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.

$\endgroup$
3
  • $\begingroup$ Good. Great Thanks. $\endgroup$ Apr 21, 2014 at 22:07
  • $\begingroup$ 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 :) $\endgroup$ Oct 15, 2014 at 19:09
  • $\begingroup$ 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. $\endgroup$
    – user41395
    Aug 4, 2016 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.