Skip to content

Multitaper keyword "bandwidth" does not behave as expected #11324

@moritz-gerster

Description

@moritz-gerster

Proposed documentation enhancement

I try to calculate power spectral densities using multitaper at different frequency resolutions but I don't get it to work. The documentation states

bandwidth[float]
The bandwidth of the multi taper windowing function in Hz. The default value is a window half-bandwidth of 4.

I understand this doc such that
mne.time_frequency.psd_multitaper(raw, bandwidth=None)

should yield the same as
mne.time_frequency.psd_multitaper(raw, bandwidth=4)

but it does not. Looking at the code, I see how half_nbw is obtained from bandwidth:

half_nbw = float(bandwidth) * n_times / (2. * sfreq)

So I try
mne.time_frequency.psd_multitaper(raw, bandwidth=(4 * raw.last_samp) / (2 * raw.info["sfreq"]))
which again yields something different.

My question:
Which float do I need to pass to bandwidth to reproduce bandwith=None?

Here is my code:

import mne
import numpy as np
import matplotlib.pyplot as plt

info = mne.create_info(["eeg1"], sfreq=256, ch_types="eeg")
data = data = np.random.randn(1, 256*10)
raw = mne.io.RawArray(data, info)

bandwidths = [None, 
              4, 
              (4 * raw.last_samp) / (2 * raw.info["sfreq"])]

_, axes = plt.subplots(1, 3, figsize=(10, 5), sharey=True)
for ax, bandwidth in zip(axes, bandwidths):
    raw.compute_psd(method="multitaper", bandwidth=bandwidth).plot(axes=ax)
    ax.set_xlabel("Frequency (Hz)")
    ax.set_title(f"Bandwidth={bandwidth}")
# plt.savefig("bandwidths.png", dpi=150)
plt.show()

This is the result:
bandwidths

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions