# Figuring out the frequency components using FFT [duplicate]

The function defined below has three frequencies 2, 4 and 10. How to use the Fourier transform (FFT) to show these frequencies?

   myfun[t_] =
1/(2 Sqrt[
2]) (\[Sqrt]Abs[
3 + Cos[4 t] -
4 Cos[2 t] (-1 +
E^(-0.018 t) (Cos[9.99999594999918 t] +
0.0009000003645002213 Sin[9.99999594999918 t])^2) +
4 E^(-0.018 t) (Cos[9.99999594999918 t] +
0.0009000003645002213 Sin[9.99999594999918 t])^2])


Edit: The suggested duplicate deals with a simple function. It really doesn't work for this complicated scenario. Would appreciate if someone gets it done.

• No, it has more than 3 frequencies due to the present of the factor E^(-0.018 t) and the square roots. – Henrik Schumacher Sep 18 '19 at 17:52
• Thanks,@HenrikSchumacher. Could this be shown using FFT? – Rob Sep 18 '19 at 17:53
• Yes. You just have to sample myfun on an evenly spaced grid from -Pi to Pi and apply Fourier. – Henrik Schumacher Sep 18 '19 at 17:55
• Try this one or this one or... – march Sep 18 '19 at 18:35
• @Patrick. Note that your frequencies will not be 2, 4, and 10 as you think. First of all, since the Sin[10 t] is squared, the relevant angular frequency is actually 20. Second of all, those are particular angular frequencies that show up. Instead, you should see dominant peaks at $2/2\pi$, $4/2\pi$, and $20/2\pi$, which you will see if you use one of the linked answers. The one using Periodogram' is easy to use, and the other one shows the best way to use Fourier (the FFT) to get a power spectrum. – march Sep 18 '19 at 18:57

First lets plot your function to see what it looks like.

Plot[myfun[t], {t, 0, 100}]


This is only between 0 to 100 seconds. What we see is some very high frequencies but also a constant frequency which we can estimate at about 0.3 Hz. If we plot at later times

Plot[myfun[t], {t, 500, 600}]


We can see that the starting high frequencies have died away and the frequency at about 0.3 Hz remains.

Now we can do a Fourier analysis. As you have some high frequencies we need to sample at a very fast sample rate. I have guessed at the sample rate but hope it is good enough.

sr = 100;  (* Sample rate *)
data = Table[myfun[t], {t, 0, 1000, 1/sr}];
nn = Length@data


Plotting again shows that I seem to have captured the high frequency data

ListLinePlot[data[[1 ;; Round[100 sr]]]]


You should probably check by looking at a shorter time interval.

Now for the Fourier analysis.

ft = Fourier[data, FourierParameters -> {-1, -1}];
freqs = Table[f, {f, 0, sr (nn - 1)/nn, sr/nn}];


The second line of code is to make the frequencies that correspond to the data in the Fourier transform. See here for details.

ListLogPlot[Transpose[{freqs, Abs[ft]}][[1 ;; 10000]],
PlotRange -> All, Joined -> True]


We can see a dominant peak at about 0.3 Hz and lots of other peaks that make up your high frequencies.

Hope that helps.

Periodogram and Spectrogram are also useful:

data = Table[myfun[t], {t, 0, 100000, .1}];
Periodogram[data, SampleRate -> 10, PlotRange -> All, Frame -> True,
FrameLabel -> {"Hz", "dB"}]


Spectrogram[data, SampleRate -> 10, PlotRange -> {0, .5},
FrameLabel -> {"Time", "Hz"}]
`