Thanks Paul, your explanation makes perfectly sense.

Indeed, computing "manually" the PDS (using numpy.fft) in a test case gives
me equal power for sine wave component of same amplitude, even when a
component is in the highest frequency bin.

However, giving the same test case to matplotlib.mlab.specgram() results in
the power density of the highest frequency bin being higher than the other
components.

For the test see this notebook:
http://nbviewer.ipython.org/gist/tritemio/162925a69a6ec29cdfe1


Antonio


On Wed, Feb 11, 2015 at 8:50 PM, Paul Blelloch <paul.blell...@ata-e.com>
wrote:

> I’m just answering off the top of my head, but it would seem to make sense
> to me that neither the 0 frequency nor the fmax frequency should be scaled
> by 2.  If you take an FFT of a signal of length N, you’ll get a transform
> of length N.  N/2 points will correspond to positive frequencies, one point
> to zero frequency and N/2-1 points to negative frequency.  So it’s the
> N/2-1 points that are reflected to the positive frequency axis, and the
> fmax frequency is the only one that doesn’t have a corresponding point on
> the negative axis.
>
>
>
> On the practical side, I’d say that you’re asking for trouble if you’re
> looking at the last frequency bin in a power spectrum.  If there’s any
> significant energy in that bin, there’s almost certainly energy at higher
> frequencies that’s being aliased down.  You really need to make sure that
> you have negligible power content before you get to the Nyquist frequency,
> usually by applying an analog filter to the data, or otherwise knowing that
> it cannot have undesired high frequency content.
>
>
>
> *From:* Antonino Ingargiola [mailto:trite...@gmail.com]
> *Sent:* Wednesday, February 11, 2015 6:45 PM
> *To:* matplotlib-users@lists.sourceforge.net
> *Subject:* [Matplotlib-users] mlab: computing power spectra
>
>
>
> Hi to all,
>
>
>
> I'm computing some spectrograms using the convenient functions defined in
> matplotlib.mlab.
>
>
>
> I found something that I don't completely understand. In computing the PSD
> the onesided power spectrum is scaled by a factor of 2 except for the first
> and the last bin:
>
>
>
>
> https://github.com/matplotlib/matplotlib/blob/v1.4.2/lib/matplotlib/mlab.py#L776
>
>
>
> in principle this should account for the contributions that in a two-sided
> spectrum is spread over negative and positive frequencies. However the lack
> of scaling in the highest frequency seems to cause a systematic
> underestimation on that frequency bin.
>
>
>
> Is it possible that the power spectrum should  be scaled, in the case of
> even-length signal, up to the last bin (so that -1 in the slicing should be
> removed)?
>
>
>
> With correct scaling a signal comprised of two sinusoids of same power,
> one being at the Nyquist frequency, should have the same amplitude
> (excluding scallopping losses). I don't have time to test it at the moment,
> but maybe someone more expert can already tell me if I'm missing something
> or the matplotlib scaling is wrong.
>
>
>
> Thanks,
>
> Antonio
>
------------------------------------------------------------------------------
Dive into the World of Parallel Programming. The Go Parallel Website,
sponsored by Intel and developed in partnership with Slashdot Media, is your
hub for all things parallel software development, from weekly thought
leadership blogs to news, videos, case studies, tutorials and more. Take a
look and join the conversation now. http://goparallel.sourceforge.net/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users

Reply via email to