# Butterworthfilter highpass/bandpass

this should be a fairly straightforward question since Mathematica has included functionality to filter data. Nevertheless, I am not being successful to solve the problem. Following the proposed solution here did not really help, since the syntax code is not working.

If you have 3 frequency data, you may make a lowpass filter and nicely filter out the low frequency data.

Generate data:

data1 = Table[{t,
3.23 Sin[2 \[Pi] 1.83 t] + 3.23 Sin[2 \[Pi] 5.33 t] +
3.23 Sin[2 \[Pi] 10.5 t]}, {t, 0, 100, 0.01}];
ListPlot[data1[[;; 1000 ;; 1]], PlotRange -> Automatic,
Joined -> True]


FFT of the data:

testdf1 = data1;
nn = Length@testdf1;(*Number of points*)
sr = 10^2.;(*Sample rate*)
freqs = Table[(n - 1) sr/nn, {n, nn}];
ft = Fourier[testdf1[[All, 2]], FourierParameters -> {-1, 1}]^1;
ListLogPlot[Transpose[{freqs, Abs[ft]}], PlotRange -> {{0, 15}, All},
Joined -> True]


Lowpass filter the data and compare it with initial data:

fn = data1[[All, 2]];
lowpass = ButterworthFilterModel[{10, 4. \[Pi] 2}];
filt1 = ToDiscreteTimeModel[lowpass, 1/sr];
fnfilt1 = RecurrenceFilter[filt1, fn];
ListPlot[{data1, Transpose[{data1[[All, 1]], fnfilt1}]},
Joined -> True, PlotRange -> {All, All}]


FFT of the filter data:

fnfiltft1 = Fourier[fnfilt1, FourierParameters -> {-1, 1}]^1;
ListLogPlot[Transpose[{freqs, Abs[fnfiltft1]}],
PlotRange -> {{0, 15}, All}, Joined -> True]


How do I do with similar procedure a highpass/bandpass filter ?

Everything I tried so far is not working reliably such as:

 \[Omega]p = 10.; \[Omega]s = 20; ap = 20.; as = 2.;
tf = ButterworthFilterModel[{"Highpass", {\[Omega]p, \[Omega]s}, {ap,
as}}]


Thank you very much !

• Where's the definitoin of fn? – xzczd Jun 20 '18 at 7:24
• Well spotted xzczd ! Just added before the lowpass definition. – BBmath Jun 21 '18 at 1:07

## 1 Answer

I'm not experienced in filtering, but I think there're 2 issues in your trial:

1. The lowpass filter defined by the 2nd syntax in the document is not suitable for you data, we need to control the parameter of filter more specifically i.e. make use of the 3rd syntax in the document.

2. The parameter for the highpass filter is wrong, notice $\omega=2\pi f$.

The following is my attempt (proper values of fp and fs are found by trial and error):

fp = 2.5; fs = 3.5; ap = 1.; as = 20.;
lowpass = ButterworthFilterModel[{"Lowpass", {fp, fs} 2 Pi, {ap, as}}];
filtLow = ToDiscreteTimeModel[lowpass, 1/sr];
fnfiltLow = RecurrenceFilter[filtLow, fn];

fs = 7; fp = 8; as = 20.; ap = 1.;
highpass = ButterworthFilterModel[{"Highpass", {fs, fp} 2 Pi, {as, ap}}];
filtHigh = ToDiscreteTimeModel[highpass, 1/sr];
fnfiltHigh = RecurrenceFilter[filtHigh, fn];

fs1 = 3; fp1 = 4; fp2 = 6; fs2 = 7; as = 20.; ap = 1.;
bandpass = ButterworthFilterModel[{"Bandpass", {fs1, fp1, fp2, fs2} 2 Pi, {as, ap}}];
filtBand = ToDiscreteTimeModel[bandpass, 1/sr];
fnfiltBand = RecurrenceFilter[filtBand, fn];

listplot = ListLinePlot[#, DataRange -> {0, 100}, PlotRange -> {{1, 2}, Automatic}] &;

listplot[{fnfiltLow, fnfiltBand, fnfiltHigh}]


Periodogram[{fnfiltLow, fnfiltBand, fnfiltHigh}, PlotRange -> {{0, 15}, All},
SampleRate -> Floor@sr]


• That seems to work. Thanks a lot ! – BBmath Jun 21 '18 at 1:52