Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ddd46fd

Browse files
committed
Move code for wrapping negative frequencies from specgram() into _spectral_helper(), so that psd(), csd(), and cohere() can benefit from this functionality as well. While this changes API a little, this is much more sensible behavior.
svn path=/trunk/matplotlib/; revision=7121
1 parent c5f3734 commit ddd46fd

4 files changed

Lines changed: 53 additions & 6 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
2009-05-18 Fix the linespacing bug of multiline text (#1239682). See
1+
2009-05-18 Make psd(), csd(), and cohere() wrap properly for complex/two-sided
2+
versions, like specgram() (SF #2791686) - RMM
3+
4+
2009-05-18 Fix the linespacing bug of multiline text (#1239682). See
25
examples/pylab_examples/multiline.py -JJL
36

47
2009-05-18 Add *annotation_clip* attr. for text.Annotation class.

doc/api/api_changes.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ list may help describe what changes may be necessary in your code.
1919

2020
Changes for 0.98.x
2121
==================
22+
* psd(), csd(), and cohere() will now automatically wrap negative
23+
frequency components to the beginning of the returned arrays.
24+
This is much more sensible behavior and makes them consistent
25+
with specgram(). The previous behavior was more of an oversight
26+
than a design decision.
27+
2228
* Added new keyword parameters *nonposx*, *nonposy* to
2329
:class:`matplotlib.axes.Axes` methods that set log scale
2430
parameters. The default is still to mask out non-positive
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#This is a ported version of a Matlab example from the signal processing
2+
#toolbox that showed some difference at one time between Matplotlib's and
3+
#MatLab's scaling of the PSD. This differs from psd_demo3.py in that
4+
#this uses a complex signal, so we can see that complex PSD's work properly
5+
import numpy as np
6+
import matplotlib.pyplot as plt
7+
import matplotlib.mlab as mlab
8+
9+
fs = 1000
10+
t = np.linspace(0, 0.3, 301)
11+
A = np.array([2, 8]).reshape(-1, 1)
12+
f = np.array([150, 140]).reshape(-1, 1)
13+
xn = (A * np.exp(2j * np.pi * f * t)).sum(axis=0) + 5 * np.random.randn(*t.shape)
14+
15+
yticks = np.arange(-50, 30, 10)
16+
xticks = np.arange(-500,550,100)
17+
plt.subplots_adjust(hspace=0.45, wspace=0.3)
18+
ax = plt.subplot(1, 2, 1)
19+
20+
plt.psd(xn, NFFT=301, Fs=fs, window=mlab.window_none, pad_to=1024,
21+
scale_by_freq=True)
22+
plt.title('Periodogram')
23+
plt.yticks(yticks)
24+
plt.xticks(xticks)
25+
plt.grid(True)
26+
plt.xlim(-500, 500)
27+
28+
plt.subplot(1, 2, 2, sharex=ax, sharey=ax)
29+
plt.psd(xn, NFFT=150, Fs=fs, window=mlab.window_none, noverlap=75, pad_to=512,
30+
scale_by_freq=True)
31+
plt.title('Welch')
32+
plt.xticks(xticks)
33+
plt.yticks(yticks)
34+
plt.ylabel('')
35+
plt.grid(True)
36+
plt.xlim(-500, 500)
37+
38+
plt.show()

lib/matplotlib/mlab.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ def _spectral_helper(x, y, NFFT=256, Fs=2, detrend=detrend_none,
327327
t = 1./Fs * (ind + NFFT / 2.)
328328
freqs = float(Fs) / pad_to * np.arange(numFreqs)
329329

330+
if (np.iscomplexobj(x) and sides == 'default') or sides == 'twosided':
331+
# center the frequency range at zero
332+
freqs = np.concatenate((freqs[numFreqs//2:] - Fs, freqs[:numFreqs//2]))
333+
Pxy = np.concatenate((Pxy[numFreqs//2:, :], Pxy[:numFreqs//2, :]), 0)
334+
330335
return Pxy, freqs, t
331336

332337
#Split out these keyword docs so that they can be used elsewhere
@@ -485,11 +490,6 @@ def specgram(x, NFFT=256, Fs=2, detrend=detrend_none, window=window_hanning,
485490
noverlap, pad_to, sides, scale_by_freq)
486491
Pxx = Pxx.real #Needed since helper implements generically
487492

488-
if (np.iscomplexobj(x) and sides == 'default') or sides == 'twosided':
489-
# center the frequency range at zero
490-
freqs = np.concatenate((freqs[NFFT/2:]-Fs,freqs[:NFFT/2]))
491-
Pxx = np.concatenate((Pxx[NFFT/2:,:],Pxx[:NFFT/2,:]),0)
492-
493493
return Pxx, freqs, t
494494

495495
specgram.__doc__ = specgram.__doc__ % kwdocd

0 commit comments

Comments
 (0)