Message Boards Message Boards

1
|
690 Views
|
8 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Wrong frequencies in Periodogram?

POSTED BY: Vladimir Ivanov
8 Replies

Hi Vladimir, yes that is a bug. It is due to an incorrect setting of the DataRange option. To correct please see the attached notebook.

Attachments:

Mariusz, thank you.

POSTED BY: Vladimir Ivanov

Well - ok! I do admit that I was simply not willig to even consider a bug in a function which belongs to the prominent Fourier-family ...

At least PeriodogramArray[] seems to give a correct result:

n = 100;
data = Cos[2 \[Pi] Range[0, n - 1]*0.25] // N;
pdg = Periodogram[data, PlotRange -> All, ScalingFunctions -> {"Linear", "Linear"}, PlotStyle -> Dotted];
pdata = MapIndexed[{(First[#2] - 1)/n, #1} &, PeriodogramArray[data]];
Show[ListLinePlot[pdata[[;; 50]], PlotRange -> All, GridLines -> {{.25}, None}], pdg]

enter image description here

POSTED BY: Henrik Schachner

Yes, this is a bit weird. Now that I'm back at a computer I can test it a bit. First I will note that the experiment suggested by Henrik does in fact give the expected frequency. But that strikes me as weird because if one "periodizes" that data by repeating it then there's a jump where the end of one set and the beginning of the next are both zero.

First I tried Fourier. To refine the estimate I use 10 times as many data points.

n = 1000;
data = Cos[2 \[Pi] Range[0, n - 1]*0.25] // N;
ft = Fourier[data];
Position[Abs@ft, Max[Abs[ft]]]

(* Out[134]= {{251}, {751}} *)

This suggests 25 is correct, noting that the first element is the DC component and 251-1 is 250.

Now back to periodograms. I have no familiarity with Periodogram. I will claim a passing familiarity with the Wolfram Resource Function IrregularPeriodogram (having written it a few years back). So I will go back to the original data and show what we have.

n = 100;
data = Cos[2 \[Pi] Range[0, n - 1]*0.25] // N;

I'll show the periodogram to the extent that we are sure to recover the frequency (pretending we already know the right ballpark). Note that I am subtracting off the mean value. This is unneccessary in this case because it is zero (or rather, numerical fuzz). But I'll need it for the next case.

Plot[ResourceFunction["IrregularPeriodogram"][t, Range[Length[data]], 
  data - Mean[data]], {t, 0, 5}, PlotRange -> All, PlotPoints -> 50]

enter image description here

Now find the max frequency, that is, locate a peak value.

NMaximize[ ResourceFunction["IrregularPeriodogram"][t, Range[Length[data]], 
  data - Mean[data]], {t, 1.4, 1.8}]

(* Out[161]= {25., {t -> 1.5708}} *)

Very clearly this is 25. Now I'll redo this, but taking data to n instead of n-1. The periodogram plot is similar to the one above so I will omit it and just get that frequency estimate.

datan = Cos[2 \[Pi] Range[0, n]*0.25] // N;
NMaximize[
 ResourceFunction["IrregularPeriodogram"][t, Range[Length[datan]], 
  datan - Mean[datan]], {t, 1.4, 1.8}]

(* Out[183]= {25.7425, {t -> 1.57079}} *)

This is further evidence that what you did in the original post was correct; if we go to n-1 we get the expected frequency and going to n we do not.

What to make of this? I don't know. You might want to send a bug report to our Tech Support. I'll also point someone more familiar with our signal processing to this question.

POSTED BY: Daniel Lichtblau

I guess this is because your data are not perfectly periodic - in the sense that the first value is not identical to the last one. To assure this you just have to include one more point, e.g.:

n = 100;
data = Cos[2 \[Pi] Range[0, n]*0.25] // N;

The length of the data list is now 101, and the periodogram is probably like you are expecting it.

POSTED BY: Henrik Schachner

Henrik, the discrete Fourier transform works for any signal, not only for ones with the first and last elements equal.

We can check it formally, making sure that the explicit inverse Fourier transform

Table[Chop[1/Sqrt[n] (Fourier[data] . Table[Exp[-2 \[Pi] I t s / n ], {s, 0, n-1}])], {t, 0, n-1}]

brings us back to the initial data. And this expression, in the case of my signal, includes Exp[-2 [Pi] I t * 0.25 ] rather than Exp[-2 [Pi] I t * 0.255102 ], so the true frequency must be 0.25.

POSTED BY: Vladimir Ivanov

Try it using Most[] on that range; it may be that you included one point too many.

POSTED BY: Daniel Lichtblau

Why? Length[Range[0,100-1]] is 100.

POSTED BY: Vladimir Ivanov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract