# getting psds of wide range of frequency

**URL:** https://mne.discourse.group/t/getting-psds-of-wide-range-of-frequency/5731
**Category:** Support & Discussions
**Tags:** eeg
**Created:** [October 14, 2022, 12:21pm UTC](https://mne.discourse.group/t/getting-psds-of-wide-range-of-frequency/5731 "2022-10-14T12:21:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![oconnell\_E](https://avatars.discourse-cdn.com/v4/letter/o/9e8a1a/32.png) [@oconnell\_E](https://mne.discourse.group/u/oconnell_E)
#### Post date: [October 14, 2022, 12:21pm UTC](https://mne.discourse.group/t/getting-psds-of-wide-range-of-frequency/5731/1 "2022-10-14T12:21:02Z")

</div>

Hello!

- MNE version: 1.0.3
- operating system: macOS 11.1

I have continuous resting state EEG data of some subjects which were measured at one timepoint.

To see how different the power between 5 and 30 Hz is, I first compute FFT (with multitaper) ,

`psds, freqs = psd_multitaper(preprocessed_raw, fmin=5, fmax=30, tmin=0, tmax=120)`

then I want to concatenate ‘psds’ of all subjects and compute the average of psd between 5-30 Hz to visualize them, for example, with following plot at the end.

![Screenshot 2022-10-14 at 13.54.59](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/3/320fb6b970ca39845d2c2edbb93d52aaa2ba917f.png)

However, the output “psds” have different shape depending on the data because of the different number of frequencies. For instance, when computing FFT between 5 and 30 Hz, psds is sometimes (n\_channels, 3200) or (n\_channels, 3010).  
I’m looking for a solution to this problem so that psds of each subject can be concatenated without getting lag of frequency power across subjects…

I’d like to know whether there’s a good way to deal with that.  
Thank you in advance!

---

<div class="post-metadata">

### Author: ![mscheltienne](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/mscheltienne/32/827_2.png) [@mscheltienne](https://mne.discourse.group/u/mscheltienne)
#### Post date: [October 18, 2022, 9:55am UTC](https://mne.discourse.group/t/getting-psds-of-wide-range-of-frequency/5731/2 "2022-10-18T09:55:36Z")

</div>

Hello,

Correct me if I’m wrong, to get the band power, e.g. 5 to 30 Hz, you need to integrate the spectral estimate (PSD) between those frequencies.

For instance, using `simpson` rule: [scipy.integrate.simpson — SciPy v1.9.2 Manual](https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.simpson.html)

```python
from scipy.integrate import simpson

# assumes MNE 1.2, else use the `psd_multitaper` and select fq manually
spectrum = raw.compute_psd(method="multitaper")
freq_res = spectrum.freqs[1] - spectrum.freqs[0]
data = spectrum.get_data(fmin=5, fmax=30) # shape (n_channels, n_freqs)
bandpower = simpson(data, dx=freq_res, axis=-1) # shape (n_channels,)

```

The integration will get rid of the mismatch between the `n_freqs` dimension.

Mathieu

---

<div class="post-metadata">

### Author: ![drammock](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/drammock/32/4_2.png) [@drammock](https://mne.discourse.group/u/drammock)
#### Post date: [October 18, 2022, 1:49pm UTC](https://mne.discourse.group/t/getting-psds-of-wide-range-of-frequency/5731/3 "2022-10-18T13:49:32Z")

</div>

the reason you get a different number of frequency bins is that the multitaper method computes frequency bin values based on the length of the signal. To avoid this you could truncate all signals to the same length before estimating spectral power (if that is a reasonable thing to do, given your data / analysis).

As @mscheltienne says, for _band power_ you can aggregate across bins even if the bin spacing is not identical for all files / subjects. When you’re dealing with 3000+ bins, I wouldn’t expect it to make much difference.
