[Matplotlib-users] PSD Scaling (matplotlib version 1.4.X versus 1.5.X)

Dan Roberts droberts at spawar.navy.mil
Thu Apr 21 17:00:56 EDT 2016


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())

More information about the Matplotlib-users mailing list