I am trying to compute the Fourier transform of a list, say an array of the form {{t1, y[t1]},.....{tn, y[tn]}}; apply some filters in the spectral components, and then back transform in time domain.
I have written my own routine to do Discete Fourier Transform of lists, as follow
FourierAmpl[data_] :=
Module[{ydata, xdata, \[CapitalDelta]freqdata, xfreqdata, datatime,
datafourier},
ydata =
Join[Transpose[data][[2]],
Table[Transpose[data][[2]][[-1]], {ii, 1, zerotabbing}]];
xdata = Join[Transpose[data][[1]],
Table[Transpose[data][[1]][[-1]] + \[CapitalDelta]x ii, {ii, 1,
zerotabbing}]]; \[CapitalDelta]freqdata = 1/(
xdata[[Length[xdata]]] - xdata[[1]]);
xfreqdata =
Table[\[CapitalDelta]freqdata ii, {ii, 0, Length[xdata] - 1}];
datafourier =
Drop[Transpose[{xfreqdata, Abs[Fourier[ydata - Mean[ydata]]]/
1}], -Round[0.5 Length[xfreqdata]]];
datafourier
]
which directly computes the Fourier Transform of a 2D list, provided that these quantities are defined zerotabbing
and \[CapitalDelta]x
, which respectively indicate a number of extra zeros that can be added after the signal, to improve the spectral resolution; while \[CapitalDelta]x
the spacing in the time signal (dt).
I have also written a function to compute also the argument of the FFT:
renormalize[args_] :=
Module[{pairs, diffs, j, len = Length[args], corr = 0},
pairs = Partition[args, 2, 1];
diffs = Map[#[[1]] - #[[2]] &, pairs];
PrependTo[diffs, 0];
diffs = 2*Pi*Sign[Chop[diffs, Pi]];
Table[corr += diffs[[j]];
corr + args[[j]], {j, 1, len}]]
FourierPhase[data_] :=
Module[{ydata, xdata, \[CapitalDelta]freqdata, xfreqdata, argdata,
phasedata},
ydata =
Join[Transpose[data][[2]],
Table[Transpose[data][[2]][[-1]], {ii, 1, zerotabbing}]];
xdata = Join[Transpose[data][[1]],
Table[Transpose[data][[1]][[-1]] + \[CapitalDelta]x ii, {ii, 1,
zerotabbing}]]; \[CapitalDelta]freqdata = 1/(
xdata[[Length[xdata]]] - xdata[[1]]);
xfreqdata =
Table[\[CapitalDelta]freqdata ii, {ii, 0, Length[xdata] - 1}];
argdata = renormalize[Arg[Fourier[ydata - Mean[ydata]]]];
phasedata =
Drop[Transpose[{xfreqdata, argdata}], -Round[
0.5 Length[xfreqdata]]];
phasedata
]
The code seems to work in properly computing the FFT. For example, for a very simple function such as
Test = Table [{tt, Sin[2 \[Pi] tt]}, {tt, 0, 6, 0.05}];
\[CapitalDelta]t =
Test\[Transpose][[1]][[2]] -
Test\[Transpose][[1]][[1]]; zerotabbing = 0;
The function
ListLinePlot[FourierAmpl[Test], PlotRange -> All]
returns
But now if I would like to recompute back the original signal applying twice FourierAmpl
the resulting function is far from the original function:
ListLinePlot[FourierAmpl[FourierAmpl[Test]], PlotRange -> All]
Where is my mistake? I fail in finding it. Thanks for your help!