1
$\begingroup$

I have a problem for obtain the frequency of oscillations with Fourier,

For example if I have

tf = 60;
NN = (tf + 1)/0.001;
dt = tf/NN;
data = Table[Sin[t], {t, 0., tf, 0.001}];
fft = Fourier[data];
peak1 = First[Position[Abs[fft], Max[Abs[fft]]]];

The frequency is obtained would be $2\pi k/ N dt$, where k is the position of the first peak. If I evaluate this expression I do not obtain the frequency.

$\endgroup$
3
  • 1
    $\begingroup$ There's a factor of 2 Pi in formula for Fourier. Shouldn't the frequency be (peak1 -1)/tf? $\endgroup$
    – Michael E2
    Commented Jul 31, 2020 at 11:43
  • $\begingroup$ Why did you set up NN and dt if you never use them on line 4? Also your Sin[t] should be Sin[2 pi f] for frequency f. You also need to take a look at the FourierParameters option of Fourier and finally, you should account for your sampling rate too. $\endgroup$
    – flinty
    Commented Jul 31, 2020 at 12:02
  • $\begingroup$ See the "Applications" section of the docs of Fourier for an example of how to determine the frequency. The shortcoming of that example is that it uses a dt (or dx) of 1, and doesn't show how to modify the various steps. However, it's not hard to figure out. $\endgroup$
    – Michael E2
    Commented Jul 31, 2020 at 12:03

1 Answer 1

2
$\begingroup$

OP's apparent intention:

tf = 60;
NN = (tf)/0.001;
dt = tf/NN;
data = Table[Sin[t], {t, 0., tf, 0.001}];
fft = Fourier[data];
peak1 = First@First[Position[Abs[fft], Max[Abs[fft]]]]
freq = (peak1 - 1)/tf
(*
  11
  0.166667
*)

It's closest estimate possible, since the adjacent estimates have a larger error:

1/(2 Pi) - (peak1 - {0, 1, 2})/tf // N
(*  {-0.0241784, -0.00751172, 0.00915494}  *)

Here is the method from the docs for Fourier (under "Applications"), which uses a more sophisticated approach:

Min[TakeLargest[Abs@fft, 2]];
peaks = Position[Abs[fft], x_ /; x >= %];
pos = First@First[peaks]
(*  11  *)
n = Length@fft;
fr = Abs[Fourier[data*Exp[2 Pi I (pos - 2) N[Range[0, n - 1]]/n], 
    FourierParameters -> {0, 2/n}]];
frpos = Position[fr, Max[fr]][[1, 1]];

period = N[n/(pos - 2 + 2 (frpos - 1)/n)] dt
(*  6.29278  *)
frequency = 1/period
1./(2 Pi)
(*
  0.158912
  0.159155
*)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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