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

Skip to content

Commit 3afb042

Browse files
committed
Merge bug-fixes into master
2 parents 5d06a35 + 063e3f1 commit 3afb042

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

lib/matplotlib/mlab.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,15 @@ def _spectral_helper(x, y=None, NFFT=None, Fs=None, detrend_func=None,
772772
# Also include scaling factors for one-sided densities and dividing by
773773
# the sampling frequency, if desired. Scale everything, except the DC
774774
# component and the NFFT/2 component:
775-
result[1:-1] *= scaling_factor
775+
776+
# if we have a even number of frequencies, don't scale NFFT/2
777+
if not NFFT % 2:
778+
slc = slice(1, -1, None)
779+
# if we have an odd number, just don't scale DC
780+
else:
781+
slc = slice(1, None, None)
782+
783+
result[slc] *= scaling_factor
776784

777785
# MATLAB divides by the sampling frequency so that density function
778786
# has units of dB/Hz and can be integrated by the plotted frequency

lib/matplotlib/tests/test_mlab.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2962,8 +2962,6 @@ def test_evaluate_equal_dim_and_num_lt(self):
29622962
np.testing.assert_array_almost_equal(y, y_expected, 7)
29632963

29642964

2965-
#*****************************************************************
2966-
29672965
def test_contiguous_regions():
29682966
a, b, c = 3, 4, 5
29692967
# Starts and ends with True
@@ -2987,7 +2985,17 @@ def test_contiguous_regions():
29872985
assert_equal(mlab.contiguous_regions([]), [])
29882986

29892987

2990-
#*****************************************************************
2988+
def test_psd_onesided_norm():
2989+
u = np.array([0, 1, 2, 3, 1, 2, 1])
2990+
dt = 1.0
2991+
Su = np.abs(np.fft.fft(u) * dt)**2 / float(dt * u.size)
2992+
P, f = mlab.psd(u, NFFT=u.size, Fs=1/dt, window=mlab.window_none,
2993+
detrend=mlab.detrend_none, noverlap=0, pad_to=None,
2994+
scale_by_freq=None,
2995+
sides='onesided')
2996+
Su_1side = np.append([Su[0]], Su[1:4] + Su[4:][::-1])
2997+
assert_allclose(P, Su_1side, atol=1e-06)
2998+
29912999

29923000
if __name__ == '__main__':
29933001
import nose

0 commit comments

Comments
 (0)