0
$\begingroup$

This question already has an answer here:

With this code I get a nice plot of the Fourier transform, but the frequencies are doubled. By this I mean that when using a sine wave I generated at 220 Hz, the plot gives me a single peak at 440 Hz. I've tried searching around, but I'm stuck.

 data = Import[
"ExampleFilePath", "Data"];

length = Length[data];

samplerate = 44100;

increment = samplerate/length;

frequency = 
Table[f, {f, 0, (samplerate - increment), increment}] // 
N;

power = Abs[Fourier[data, FourierParameters -> {-1, 1}]]^2;

powerdata = Transpose[{frequency, power}];

edited to fix link

$\endgroup$

marked as duplicate by MarcoB, yohbs, bbgodfrey, Michael E2, glS May 12 '17 at 10:48

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • $\begingroup$ frequency = Table[f, {f, 0, (samplerate - increment), increment}] // N; is not right. This mistake is not related to Mathematica. $\endgroup$ – Kattern Apr 26 '17 at 2:13
  • $\begingroup$ I think your frequency specification is correct. I am assuming you want the frequency in Hz. I am unclear where you are getting your data from. Is this part of Wolfram example data? $\endgroup$ – Hugh Apr 26 '17 at 13:42
  • $\begingroup$ Some solutions discussed here: mathematica.stackexchange.com/questions/1714/… $\endgroup$ – yohbs May 2 '17 at 2:39
3
$\begingroup$

You may wish to look here to find some general information on Fourier.

Starting again by making some data we have

samplerate = 44100;
n = 44100;
data = Table[
    Cos[2 π 220 t], {t, 0, (n - 1) 1/samplerate, 1/
     samplerate}] // N;
ListLinePlot[data[[1 ;; 500]]]

Mathematica graphics

This has about 200 samples in one cycle so the approximate frequency is

    samplerate 1/200.

(* 220.5 *)

which is about what you were after.

Now we run your code and get

length = Length[data];
increment = samplerate/length;
frequency = Table[f, {f, 0, (samplerate - increment), increment}] // N;
power = Abs[Fourier[data, FourierParameters -> {-1, 1}]]^2;
powerdata = Transpose[{frequency, power}];
ListLinePlot[powerdata, PlotRange -> All]

Mathematica graphics

Which shows you have a very low frequency, compared to the sample rate. The usual mirror image is at the other end of the spectrum.

If we just plot the first 500 points of your spectrum we have

ListLinePlot[powerdata[[1 ;; 500]], PlotRange -> All]

Mathematica graphics

Which is correct. Note that the PlotRange-> All is essential since the relevant data will otherwise be ignored.

Hope that helps.

$\endgroup$

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