I have a signal that I want to identify the frequencies in it, I used the Fourier function but I can't get the frequency correctly. Here is a simplified example:
dt = 1/100;
ls = Table[0.1 Cos[30 x] + 2 Sin[x]^2, {x, 0, 200 dt, dt}];
ListPlot[ls, Mesh -> All, MeshStyle -> Red]
and the Fourier transform
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}},
DataRange -> {0, 1/dt},
FrameLabel -> {"Frequency", "Intensity"},
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 π)}, None}]
Why do I get the peak not at the original frequency 30/(2Pi), but with a frequency shift? Did I make a terrible mistake? What's the correct way to recover the original frequency using Mathematica's signal processing features?
I tried to padding zeros but still have a frequency shift.
ListPlot[Abs[Fourier[PadRight[ls, 2000]]]^2,
PlotRange -> {{0, 10}, {0, .1}}, DataRange -> {0, 1/dt},
FrameLabel -> {"Frequency", "Intensity"},
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 π)}, None}]
Edit
I'm still not convinced that this problem is due to that there are too few periods contains in the signal, nor that number of periods contained in the signal is not an integer. Consider this signal
ls = Table[0.1 Cos[30 x], {x, 0, 200 dt, dt}];
it contains the same number of periods, and the number of periods is not an integer, it gives a peak that are not centered,
ListPlot[Abs[Fourier[ls]]^2, PlotRange -> {{0, 10}, {0, 1}},
DataRange -> {0, 1/dt},
FrameLabel -> {"Frequency", "Intensity"},
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 π)}, None}]
but padding zero helps
ListPlot[Abs[Fourier[PadRight[ls, 2000]]]^2,
PlotRange -> {{0, 10}, {0, .1}}, DataRange -> {0, 1/dt},
FrameLabel -> {"Frequency", "Intensity"},
Mesh -> All, MeshStyle -> Red, GridLines -> {{30/(2 π)}, None}]