ENH: Clarify scipy.signal.spectrogram documentation (scaling description does not consider mode) #14903
Labels
Documentation
Issues related to the SciPy documentation. Also check https://github.com/scipy/scipy.org
enhancement
A new feature or improvement
scipy.signal
Is your feature request related to a problem? Please describe.
I experienced some confusion when I was trying to compare results from scipy's spectrogram function and a custom spectrogram function I had written. It took some experimentation for me to figure out that while the documentation states the following:
This is actually only true if
mode
is set to'psd'
. I had selected 'magnitude' because I naively thought since I was using'spectrum'
and I wanted a power magnitude,'psd'
was inappropriate.scipy/scipy/signal/spectral.py
Lines 752 to 762 in 47bb6fe
scipy/scipy/signal/spectral.py
Lines 1810 to 1811 in 47bb6fe
scipy/scipy/signal/spectral.py
Lines 1834 to 1842 in 47bb6fe
For all other modes,
_spectral_helper
receives the mode parameterstft
. This in turn means that when our spectrogrammode
is notpsd
, the output is in V/sqrt(Hz) or V. It also silently ignores the one_sided parameter for scaling, but still omits negative frequency components. There is no mention of units in themode
help:From the documentation, I had incorrectly assumed that the
Sxx
output ofscipy.signal.spectrogram
always had the units described in the section on scaling. The use ofpsd
as the mode which returns the correct units for thespectrum
scaling (one that is explicitly not a density) was also part of the problem.Describe the solution you'd like.
My preferred solution would be to extend the help for
scaling
to include reference to themode
.E.g.,
This also has the benefit of not requiring any changes to the underlying math or existing functions.
Describe alternatives you've considered.
Updating
mode
to use terminology less likely to cause confusion would be another solution. As far as I can tell,psd
is actually extracting the absolute square ofcomplex
mode, or the square of themagnitude
mode, and is relevant for both power spectral density (PSD) and power spectrum (PS) scalings. Thus you could describe it as 'AbsSq'. But without changing thescaling
help, some inexperienced users might expect this output to have units ofV**4
. It would also cause compatibility problems.Additional context (e.g. screenshots, GIFs)
It might also be useful to mention that
return_onesided
does not actually scale the results in any way whenmode
is notpsd
. The onesided scaling by 2 is correcting for omitted power, and other modes do not actually return power.If you reverse the math, you could argue that the positive frequencies of a double sided linear spectrum or spectral density could be multiplied by the square root of two to obtain the equivalent magnitude of a single sided linear spectrum or spectral density, but as far as I know this factor of square root of 2 does not have any obvious physical meaning in linear space, compared to the simple 'you lost half the power by omitting negative frequencies' explanation in power space.
The text was updated successfully, but these errors were encountered: