8000 Changed normalization in _spectral_helper() to obtain conistent scaling by DietBru · Pull Request #8582 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Changed normalization in _spectral_helper() to obtain conistent scaling #8582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 12, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Added note to api_changes
  • Loading branch information
DietBru committed Jun 11, 2017
commit ee1c40dd94b5dae8f95a1582bee7dc4d3aaf223e
31 changes: 31 additions & 0 deletions doc/api/api_changes/2017-06-11-DB_magnitude_spectrum.rst
7FAB
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Correct scaling of :func:`magnitude_spectrum()`
```````````````````````````````````````````````

The functions :func:`matplotlib.mlab.magnitude_spectrum()` and :func:`matplotlib.pyplot.magnitude_spectrum()` implicitly assumed the sum
of windowing function values to be one. In Matplotlib and Numpy the
standard windowing functions are scaled to have maximum value of one,
which usually results in a sum of the order of n/2 for a n-point
signal. Thus the amplitude scaling :func:`magnitude_spectrum()` was
off by that amount when using standard windowing functions (`Bug 8417
<https://github.com/matplotlib/matplotlib/issues/8417>`_ ). Now the
behavior is consistent with :func:`matplotlib.pyplot.psd()` and
:func:`scipy.signal.welch()`. The following example demonstrates the
new and old scaling::

import matplotlib.pyplot as plt
import numpy as np

tau, n = 10, 1024 # 10 second signal with 1024 points
T = tau/n # sampling interval
t = np.arange(n)*T

a = 4 # amplitude
x = a*np.sin(40*np.pi*t) # 20 Hz sine with amplitude a

# New correct behavior: Amplitude at 20 Hz is a/2
plt.magnitude_spectrum(x, Fs=1/T, sides='onesided', scale='linear')

# Original behavior: Amplitude at 20 Hz is (a/2)*(n/2) for a Hanning window
w = np.hanning(n) # default window is a Hanning window
plt.magnitude_spectrum(x*np.sum(w), Fs=1/T, sides='onesided', scale='linear')

0