I have troubles doing a simple high pass filtering...
Downloading the test data https://pastebin.com/74J8t2YV as "test.dat", you can see that overimposed on the signal there are some low frequencies and an almost constant slope, that I'd like to get rid of:
ListPlot[Import[NotebookDirectory[] <> "test.dat", Joined -> True]]
The problem is that using the following code, to have the signal oscillate around a more-or-less constant line I need to use hf > 2 pi / fsamp, but at frequencies so high the important details of the signal are lost.
Module[{dat, a, times, lf, hf = 1, fl, filt, avgy, fsamp},
dat = Import[NotebookDirectory[] <> "test.dat"];
times = dat\[Transpose][[1]];
a = dat\[Transpose][[2]];
avgy = Mean[a];
a = a /. y_ -> y - avgy; (* not interested in signal offset *)
fsamp = 1/Mean[Differences[times]]; (* sampling slightly irregular *)
hf = 2 \[Pi] 0.9/fsamp;
filt = HighpassFilter[a, hf];
ListPlot[{{times, a}\[Transpose], {times, filt}\[Transpose]},
Joined -> True]]
I'm not an expert at all in frequency analysis, but I did these kind of stuff blindly using Microcal Origin - and there the highpass filtering does not suffer of this drawback... what can be done?
Here is the results in Origin (the signal is the same, I just forgot to subtract the average)
unfortunately the help page doesnt enter into details about the algorithm, where you only choose the cutoff frequency
Answer
The default kernel length of HighpassFilter
seems to be too small, after modifying it to Round[Length@a/10]
, the result is almost the same as that of Origin:
times = dat\[Transpose][[1]];
a = dat\[Transpose][[2]];
avgy = Mean[a];
a = a - avgy;
(* Alternative method for obtaining a: *)
(*
a = Standardize[dat\[Transpose][[2]], Mean, 1 &];
*)
fsamp = 1/Mean@Differences@times;(*sampling slightly irregular*)
hf = 0.3;(*Found by trial and error *)
filt = HighpassFilter[#, hf, Round[Length@#/10], SampleRate -> Round@fsamp] &@a;
ListLinePlot[{{times, a}\[Transpose], {times, filt}\[Transpose]}, PlotRange -> All]
Comments
Post a Comment