First I define a function, just a sum of a few sin waves at different angular frequencies:
ubdat = 50;
ws = 10*{2, 5, 10, 20, 40}
fn = Table[Sum[Sin[w*x], {w, ws}], {x, 0, ubdat, .001}];
pts = Length@fn
ListPlot[fn, Joined -> True, PlotRange -> {{0, 1000}, All}]
{20, 50, 100, 200, 400}
If I take the Fourier transform and scale it correctly, you can see the correct peaks:
fnft = Abs@Fourier@fn;
fnftnormed = Table[{2*Pi*i/ubdat, fnft[[i]]}, {i, Length@fnft}];
ListPlot[fnftnormed, Joined -> True, PlotRange -> {{0, 500}, All}]
Now, I want to do a low pass filter on it, for, say, $\omega_c=140$. This should get rid of the peaks at 200 and 400, ideally. Doing it this way returns the same plots as above:
fnfilt = LowpassFilter[fn, 140];
ListPlot[fnfilt, Joined -> True, PlotRange -> {{0, 1000}, All}]
fnfiltft = Abs@Fourier@fnfilt;
fnfiltftnormed =
Table[{2*Pi*i/ubdat, fnfiltft[[i]]}, {i, Length@fnfiltft}];
ListPlot[fnfiltftnormed, Joined -> True, PlotRange -> {{0, 500}, All}]
I assume the problem is something to do with defining SampleRate, but the documentation explaining how it's defined or how to use it on the LowpassFilter page is very sparse:
By default, SampleRate->1 is assumed for images as well as data. For a sampled sound object of sample rate of r, SampleRate->r is used. With SampleRate->r, the cutoff frequency should be between 0 and $r*\pi$.
It appears to have a broken link at the bottom, so maybe that had something helpful. The page for SampleRate itself has even less info.
My naive attempt at choosing a sample rate would be dividing the number of samples by the total range, so in this case, Floor[pts/ubdat]=1000
. Using this does affect the FT, but not a whole lot:
fnfilt = LowpassFilter[fn, 140, SampleRate -> 1000];
ListPlot[fnfilt, Joined -> True, PlotRange -> {{0, 1000}, All}]
fnfiltft = Abs@Fourier@fnfilt;
fnfiltftnormed =
Table[{2*Pi*i/ubdat, fnfiltft[[i]]}, {i, Length@fnfiltft}];
ListPlot[fnfiltftnormed, Joined -> True, PlotRange -> {{0, 500}, All}]
So what am I missing? I've tried googling for some sort of guide on using filters in Mathematica, but I can't find anything and it's very frustrating.
LowpassFilter
is a black box, the transfer function is not known. The well-known and usual filters such asBiquadraticFilter, ButterworthFilter, ChebyshevFilter ...
, the transfer function is known and you can build a filter based on the transfer function. $\endgroup$