# specgram bug

Hi,

not 100% sure this is a bug, but here goes:

In file matplotlib/lib/matplotlib/mlab.py, the functions psd (power spectral density) and specgram returns the real part of the fourier transform.
% grep -n Pxx.real mlab.py
390: return Pxx.real,freqs
470: Pxx = Pxx.real #Needed since helper implements generically
(git version 4f902fac1c5bf267e3fdeb4c2045926d7498e85a, cloned from github today)

This all means that the specgram plot routine yields the real part of the Fourier transform, rather than its absolute square (forgetting normalization for simplicity of discussion). The definition of the PSD is that it is the absolute square of the Fourier transform:
https://en.wikipedia.org/wiki/Power_spectral_density#Energy_spectral_density

Hence, I believe this is a bug which should be fixed.

Cheers
Paul

Hi,
If you trace back into the code further, you will see that the Pxx is
computed as X = fft(x), Pxx = X * conj(X) which is real, but the data
type will be complex with a ~0 imaginary part (up to floating point
precision). Thus the Pxx.real is just to ensure that the resulting
data type is real instead of complex to save memory.
Glenn

ยทยทยท

On Mon, Nov 12, 2012 at 11:42 AM, Paul Anton Letnes <paul.anton.letnes@...287...> wrote:

Hi,

not 100% sure this is a bug, but here goes:

In file matplotlib/lib/matplotlib/mlab.py, the functions psd (power spectral density) and specgram returns the real part of the fourier transform.
% grep -n Pxx.real mlab.py
390: return Pxx.real,freqs
470: Pxx = Pxx.real #Needed since helper implements generically
(git version 4f902fac1c5bf267e3fdeb4c2045926d7498e85a, cloned from github today)

This all means that the specgram plot routine yields the real part of the Fourier transform, rather than its absolute square (forgetting normalization for simplicity of discussion). The definition of the PSD is that it is the absolute square of the Fourier transform:
https://en.wikipedia.org/wiki/Power_spectral_density#Energy_spectral_density

Hence, I believe this is a bug which should be fixed.

Cheers
Paul
------------------------------------------------------------------------------
Monitor your physical, virtual and cloud infrastructure from a single
web console. Get in-depth insight into apps, servers, databases, vmware,