Skip to content

Align complex Welch coefficient scaling with multitaper - #14574

Open
Arham-Begani wants to merge 2 commits into
mne-tools:mainfrom
Arham-Begani:fix-14426-welch-scaling
Open

Arham-Begani wants to merge 2 commits into
mne-tools:mainfrom
Arham-Begani:fix-14426-welch-scaling

Conversation

@Arham-Begani

@Arham-Begani Arham-Begani commented Nov 20, 2026 •

Copy link
Copy Markdown

Fixes #14426

Welch and multitaper used different scaling conventions for complex coefficients, so converting those coefficients to power required special handling at the ends of the spectrum. For real input, Welch now divides the DC coefficient and, for even-length FFTs, the Nyquist coefficient by √2. This makes (coefs * np.conj(coefs)).real * 2 give the per-segment power at every returned frequency. Tests compare that result with direct Welch power for odd and even FFT lengths and cropped frequency ranges.

I used OpenAI Codex to implement the Welch scaling fix, update the tests and documentation, and revise the patch after review. I reviewed commit 3afea41 myself and tested it byconverting complex coefficients to power with (coefs * np.conj(coefs)).real * 2 matched the power Welch returned directly. The tests covered odd and even FFT lengths and cropped frequency ranges.

@tsbinns tsbinns left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR @Arham-Begani. A couple of comments and suggestions below.
I haven't dug into the tests yet, so this is not covered in the review.

Comment thread mne/time_frequency/psd.py Resolved
Comment thread mne/time_frequency/psd.py Resolved
Comment thread mne/time_frequency/psd.py Resolved
Comment thread mne/time_frequency/psd.py Resolved
Comment thread mne/time_frequency/spectrum.py Resolved
Comment thread mne/time_frequency/spectrum.py Outdated
# Handle legacy Welch spectra and other complex SpectrumArray data.
if np.iscomplexobj(data):
data = (data * data.conj()).real # Scaling may be slightly off
data = (data * data.conj()).real

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than treating legacy Welch separately, I would suggest to handling it in one place, e.g.:

# convert complex → power (not for multitaper)
if np.iscomplexobj(data) and "taper" not in self._dims:
    data = (data * data.conj()).real * 2

# handle unaggregated Welch
if "segment" in self._dims:
    logger.info("Aggregating Welch estimates (median) before plotting...")
    data = np.nanmedian(data, axis=self._dims.index("segment"))

I think it's worth handling the power conversion and segment aggregation seperately, as then it would be compatible with custom methods peopel have stored in SpectrumArray objects.

It does mean the legacy Welch scaling will be wrong, but it was already wrong.

Also worth noting that it's important segments are aggregated after conversion to power.

Comment thread mne/time_frequency/spectrum.py Resolved
Comment thread mne/time_frequency/psd.py Resolved
Comment thread mne/time_frequency/spectrum.py Resolved
@tsbinns

tsbinns commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

2 * abs(coef) ** 2 is also not the appropriate conversion to power. (coefs * conj(coefs)).real * 2 is what gives an output identical to computing power directly from psd_array_welch.

coefs, freqs = psd_array_welch(
data, sfreq, average=None, output="complex", **kwargs
)
assert coefs.shape[-1] > 1
scale = np.where((freqs == 0) | np.isclose(freqs, sfreq / 2), 1.0, 2.0)
segment_power = (coefs * np.conj(coefs)).real * scale[:, np.newaxis]
for average in ("mean", "median"):
power, freqs_power = psd_array_welch(
data, sfreq, average=average, output="power", **kwargs
)
assert_array_equal(freqs, freqs_power)
if average == "mean":
converted = segment_power.mean(axis=-1)
else:
converted = np.median(segment_power, axis=-1) / _median_bias(
segment_power.shape[-1]
)
assert_allclose(converted, power)

(here it is scaling DC and Nyquist differently from the one-sided bins, which this PR would no longer require)

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Possible scaling error for 0 and Nyquist frequencies in multitaper spectra

2 participants