Skip to content

Possible scaling error for 0 and Nyquist frequencies in multitaper spectra #14426

Open

Description

@tsbinns

_psd_from_mt expects the coefficients for a one-sided spectra, and as such doubles the values of frequency bins. However, I would expect the 0 and Nyquist frequencies to be excluded from this, e.g., as in SciPy's spectrogram. Without the frequency bin info, _psd_from_mt just scales everything.

def _psd_from_mt(x_mt, weights):
"""Compute PSD from tapered spectra.
Parameters
----------
x_mt : array, shape=(..., n_tapers, n_freqs)
Tapered spectra
weights : array, shape=(n_tapers,)
Weights used to combine the tapered spectra
Returns
-------
psd : array, shape=(..., n_freqs)
The computed PSD
"""
weights_sq = (weights * weights.conj()).real
psd = (weights_sq * (x_mt.real**2 + x_mt.imag**2)).sum(axis=-2)
psd *= 2 / weights_sq.sum(axis=-2)
return psd

I tried to look for somewhere else where this might be accounted for, but nothing popped out. Apologies if I missed some other scaling in the MT spectra construction or something.

Activity

  1. badrisatyam1-ctrl commented on Oct 17, 2026

    @badrisatyam1-ctrl
    Contributor

    Hi @tsbinns,
    I can reproduce this / I’d like to work on a fix, is that okay?

  2. tsbinns commented on Oct 18, 2026

    @tsbinns
    MemberAuthor

    @badrisatyam1-ctrl Thanks for your interest. It's probably worth just confirming with @larsoner or @drammock first whether this is expected behaviour.

  3. larsoner commented on Oct 22, 2026

    @larsoner
    Member

    _mt_spectra has a normalization that might cancel (it divides DC bin by sqrt(2), and same for nyquist for even-length FFTs) so I think we might be okay?

  4. tsbinns commented on Nov 4, 2026

    @tsbinns
    MemberAuthor

    @larsoner Okay yeah, that handles it. However, does this not create some inconsistency for output="complex"?

    With output="power", the route is _mt_spectra (pre-downscales DC and Nyquist) → _psd_from_mt (one-sided scaling; cancels out DC and Nyquist downscaling) → output. With output="complex", the route is _mt_spectra → output, so DC and Nyquist have been downscaled alone.

    This is also different to the Welch method (SciPy's spectrogram), where output="complex" returns DC, Nyquist, and one-sided freqs unscaled.

    Is it worth undoing the DC and Nyquist scaling for output="complex" in the multitaper method for consistency with the established approach in SciPy's spectrogram?

  5. larsoner commented on Nov 4, 2026

    @larsoner
    Member

    Hmmm I think the way we do it makes more sense for our use case actually. With what we have now, you can scale all bins (including DC and nyquist) by a factor of 2 (during complex-to-power conversion when plotting/aggregating complex Spectrum data) and be correct without knowing what the frequencies actually are. So we "un-special" DC and Nyquist essentially. I'd lean toward keeping it and just documenting the convention in the output="complex" docstring.

    I think the Welch "Scaling may be slightly off" comment in spectrum.py relates to this, and we should probably multiply the non-DC/non-Nyquist bins by 2 for consistency

  6. tsbinns commented on Nov 4, 2026

    @tsbinns
    MemberAuthor

    I think the Welch "Scaling may be slightly off" comment in spectrum.py relates to this, and we should probably multiply the non-DC/non-Nyquist bins by 2 for consistency

    Why scale the interior bins up and not downscale the DC and Nyquist bins? Doesn't that still leave an inconsistency between how users should handle the multitaper and Welch coefficients? I'm also thinking about MNE-Connectivity where we pass Welch coefficients to _psd_from_mt, just with dummy weights of ones. That relies on the interior bins not being scaled yet.

  7. tsbinns commented on Nov 18, 2026

    @tsbinns
    MemberAuthor

    Discussed in call w/ @larsoner.

    For consistency, would have everything use the current multitaper approach of pre-scaling DC and Nyquist for easier manipulation later. Would therefore need to make Welch behave the same. Should also add a note of this behaviour to the docs.

    TFR should already take this pre-scaling approach for Nyquist (depending on odd/even bin count; no DC for TFR), but should check this to be sure.

  8. ishaaaan17 commented on Nov 20, 2026

    @ishaaaan17
    Contributor

    hi @tsbinns @larsoner,
    following up on the discussion and your consensus:
    harmonizing welch's output="complex" with the multitaper convention (pre-scaling the dc and nyquist bins by $1/\sqrt{2}$) makes a lot of sense to me. it keeps downstream operations like complex-to-power conversion and cross-spectral density calculation clean and frequency-agnostic, with a uniform $\times 2$ factor. this also directly resolves the # scaling may be slightly off comment in spectrum.py (_prepare_data_for_plot).
    i would be happy to take this on and prepare a pr that:

    1. pre-scales the dc ($0\text{ Hz}$) and nyquist ($f_s / 2$) bins by $1/\sqrt{2}$ in psd_array_welch when output="complex".
    2. updates the complex-to-power conversion in spectrum.py to include the $\times 2$ factor, so welch and multitaper align.
    3. documents this convention clearly in the output="complex" docstrings across Spectrum and psd_array_*.
    4. adds regression tests that check energy conservation / parseval consistency between output="complex" and output="power" for both welch and multitaper.

    please let me know if it is okay for me to open a pr for this!

  9. tsbinns commented on Nov 20, 2026

    @tsbinns
    MemberAuthor

    Hi @ishaaaan17, thanks for the interest, but a PR has already been opened in #14574 addressing this.

  10. ishaaaan17 commented on Nov 20, 2026

    @ishaaaan17
    Contributor

    Thanks for letting me know @tsbinns! Glad to see it's already being addressed in #14574. I'll take a look at the PR and help test/review there if useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions