2
$\begingroup$

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.

$\endgroup$
9
  • $\begingroup$ No, it has more than 3 frequencies due to the present of the factor E^(-0.018 t)` and the square roots. $\endgroup$ Commented Sep 18, 2019 at 17:52
  • $\begingroup$ Thanks,@HenrikSchumacher. Could this be shown using FFT? $\endgroup$
    – Rob
    Commented Sep 18, 2019 at 17:53
  • $\begingroup$ Yes. You just have to sample myfun on an evenly spaced grid from -Pi to Pi` and apply Fourier. $\endgroup$ Commented Sep 18, 2019 at 17:55
  • 1
    $\begingroup$ Try this one or this one or... $\endgroup$
    – march
    Commented Sep 18, 2019 at 18:35
  • 1
    $\begingroup$ @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. $\endgroup$
    – march
    Commented Sep 18, 2019 at 18:57

2 Answers 2

6
$\begingroup$

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

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

Mathematica graphics

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}]

Mathematica graphics

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]]]]

Mathematica graphics

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]

Mathematica graphics

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.

$\endgroup$
4
$\begingroup$

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"}]

enter image description here

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

enter image description here

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.