Given a table of data, the Fourier command gives the discrete frequency content of the data, where the resolution in frequency space is equal to 1/N, where N is the number of data points.
Higher resolution can be obtained with the same set of data by writing a custom Fourier command which samples frequencies in between those sampled by the Mathematica command. However, a naive application of this is very slow. For example, the following, for sets of real-valued data parametrized by k and m,
{kmax, mmax} = {2, 2};
Do[data[k][m] = Table[RandomReal[], {i, 1, 200}], {k, 1, kmax}, {m, 1, mmax}];
lenn = Length@data[1][1];
frac = 1/2;
fou = Table[Exp[2*Pi*I*(r - 1)*(s - 1)/lenn], {r, 1, lenn}];
Do[myFourier[k][m]=Table[Abs[Total[data[k][m]*fou]], {s, 1, lenn, frac}], {k, 1, kmax}, {m, 1, mmax}]; //Timing
takes about 1.4s to run on my computer. This is ~1000 times slower than
Do[Fourier[data[k][m]], {k, 1, kmax}, {m, 1, mmax}]; // Timing
which takes 0.0013s.
My understanding is that the Mathematica Fourier command is already optimized, and so I have attempted to Compile this code in various ways, but have not been able to find a successful implementation.
The question is, why is this simple table multiplication so slow, and can Compile be used to increase its efficiency?
Sin[Range[50.]]
list, but only find some small spurious peak generated, the shape of main peak seems to remain unchanged. $\endgroup$Table[Sin[2*Pi*f*t], {t, 0, 1, inc}]
whereinc
is very small (say ~0.005). The table looks nearly continuous and the frequency can be determined by eye. However, forf=1.5
, for example, or any non-integerf
, Fourier does not fully capture the peak. $\endgroup$Fourier
gives low resolution plots, even though more information is contained in the data, becauseFourier
only samples integer multiples of the fundamental frequency1/T
, whereT
is the length of the data in time. $\endgroup$