PSD Scaling (matplotlib version 1.4.X versus 1.5.X)

Hello,

I noticed an inconsistency with the pyplot.psd function when upgrading
from matplotlib version 1.4.0 to 1.5.3

I have written some code (below) that will show what I'm talking about
when run with the different matplotlib versions. In 1.4.0, the line "PX2
/= fft_size" was needed, but gives the wrong answer (P_ave is not equal
to 1, and Pxw.max() is -36dB instead of 0dB) if it is included when
using 1.5.3

I am mainly concerned with knowing if this change was intentional; will
it behave like this from now on (no longer needing division by fft_size)?

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np

fft_size = 4096
fs = 2500.00

gram_lines = 100;
t = np.arange(1.0,gram_lines*fft_size,1.0);
t = t / fs;
del_f = fs/fft_size;
base_freq = 10.0 # in Hz
bin_err = 0.0
f = (int(base_freq / del_f) + bin_err) * del_f # ensure f is in center
of Fourier band
A = np.sqrt(2.0); # if the amplitude is sqrt(2) then the total power =
1.0 linear and 0.0 in dB
dat = A * np.sin(2 * np.pi * f * t);

(Px2, F) = plt.psd(dat, NFFT = fft_size, Fs=fs, Fc=0,
detrend=mlab.detrend_none, window=mlab.window_none, noverlap=0, pad_to =
None, sides='onesided', scale_by_freq = False);
Px2 /= fft_size; #comment this line out if using 1.5.1??
P_ave = sum(Px2);
Px2 = 10.0*np.log10(Px2);

#print results to compare actual with expected
print "Sum of psd Px2 = %f, This should equal 1.0." %P_ave
print "Max psd Px2 = %f dB. This should equal 0.0 dB if freq is a
multiple of freq resolution.\n" %(Px2.max())

···

--
View this message in context: http://matplotlib.1069221.n5.nabble.com/PSD-Scaling-matplotlib-version-1-4-X-versus-1-5-X-tp47064.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.

I suspect that this is related to
Incorrect and Inconsistent output of function PSD when scale_by_freq=False · Issue #4328 · matplotlib/matplotlib · GitHub which was fixed by
FIX: Correct output of mlab._spectral_helper when scale_by_freq=False by e-q · Pull Request #4593 · matplotlib/matplotlib · GitHub and was an intentional
change.

Tom

···

On Thu, May 12, 2016 at 3:13 PM plotalot <dr369 at nau.edu> wrote:

Hello,

I noticed an inconsistency with the pyplot.psd function when upgrading
from matplotlib version 1.4.0 to 1.5.3

I have written some code (below) that will show what I'm talking about
when run with the different matplotlib versions. In 1.4.0, the line "PX2
/= fft_size" was needed, but gives the wrong answer (P_ave is not equal
to 1, and Pxw.max() is -36dB instead of 0dB) if it is included when
using 1.5.3

I am mainly concerned with knowing if this change was intentional; will
it behave like this from now on (no longer needing division by fft_size)?

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import numpy as np

fft_size = 4096
fs = 2500.00

gram_lines = 100;
t = np.arange(1.0,gram_lines*fft_size,1.0);
t = t / fs;
del_f = fs/fft_size;
base_freq = 10.0 # in Hz
bin_err = 0.0
f = (int(base_freq / del_f) + bin_err) * del_f # ensure f is in center
of Fourier band
A = np.sqrt(2.0); # if the amplitude is sqrt(2) then the total power =
1.0 linear and 0.0 in dB
dat = A * np.sin(2 * np.pi * f * t);

(Px2, F) = plt.psd(dat, NFFT = fft_size, Fs=fs, Fc=0,
detrend=mlab.detrend_none, window=mlab.window_none, noverlap=0, pad_to =
None, sides='onesided', scale_by_freq = False);
Px2 /= fft_size; #comment this line out if using 1.5.1??
P_ave = sum(Px2);
Px2 = 10.0*np.log10(Px2);

#print results to compare actual with expected
print "Sum of psd Px2 = %f, This should equal 1.0." %P_ave
print "Max psd Px2 = %f dB. This should equal 0.0 dB if freq is a
multiple of freq resolution.\n" %(Px2.max())

--
View this message in context:
http://matplotlib.1069221.n5.nabble.com/PSD-Scaling-matplotlib-version-1-4-X-versus-1-5-X-tp47064.html
Sent from the matplotlib - devel mailing list archive at Nabble.com.
_______________________________________________
Matplotlib-devel mailing list
Matplotlib-devel at python.org
Matplotlib-devel Info Page

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/matplotlib-devel/attachments/20160514/a36e71c9/attachment.html&gt;