# Questions for calculate power

**URL:** <https://mne.discourse.group/t/questions-for-calculate-power/7767>\
**Category:** Support & Discussions\
**Created:** [October 31, 2023, 3:46am UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767 "2023-10-31T03:46:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![North](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@North](https://mne.discourse.group/u/North)\
**Post date:** [October 31, 2023, 3:46am UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/1 "2023-10-31T03:46:27Z")

</div>

Hi, everyone

I want to calculate power (not psd) of EEG signal. Should I calculate psd first, then multiply psd with freq? Or if there are other methods? Thanks.

---

<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:** [November 1, 2023, 9:28pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/2 "2023-11-01T21:28:13Z")

</div>

Instantaneous power: `raw.get_data() ** 2`  
Average power (per channel): `np.mean(raw.get_data() ** 2, axis=-1)`

Is one of those what you’re after?

---

<div class="post-metadata">

**Author:** ![North](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@North](https://mne.discourse.group/u/North)\
**Post date:** [November 1, 2023, 11:20pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/3 "2023-11-01T23:20:02Z")

</div>

Maybe I didn’t describe my question clearly. I want to calculate the power of the alpha band, not the power spectrum density (which is power/frequency). Can I just multiply the frequency to psd?

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [November 2, 2023, 2:11am UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/4 "2023-11-02T02:11:24Z")

</div>

You could use `raw.compute_psd(fmin=8, fmax=12`, or the equivalent with [epochs.compute\_psd](https://mne.tools/stable/generated/mne.Epochs.html#mne.Epochs.compute_psd), to restrict your estimation to the alpha band. Does that help?

---

<div class="post-metadata">

**Author:** ![North](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@North](https://mne.discourse.group/u/North)\
**Post date:** [November 2, 2023, 2:28am UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/5 "2023-11-02T02:28:49Z")

</div>

Thanks. But that doesn’t solve my problem. I can use compute\_psd to calculate the power spectrum density.

psds, freqs = spectrum.get\_data(return\_freqs=True)

But that psd is density, what I want to calculate the the power. So, should just multiply the freqs with psds to get the power?

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [November 2, 2023, 1:34pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/6 "2023-11-02T13:34:42Z")

</div>

> [@North](#):
>
> just multiply the freqs with psds to get the power?

Hmm… maybe I’m missing something but this doesn’t seem like it would work. Would it make sense to multiply 8 (Hz) x the psd (at 8Hz)?

What I would do is sum the psd values between 8-12Hz. But it’s not clear to me the difference between psd and power that you are referring to.

have you searched either online or in the literature for a conversion calculation to go from psd to power? If you can find one, it should be easy enough to do with the output of compute\_psd

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [November 2, 2023, 2:01pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/7 "2023-11-02T14:01:47Z")

</div>

Alternatively, you could calculate the power of the signal as @drammock demonstrates (amplitude squared), and then bandpass filter your data between 8-12Hz?

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 2, 2023, 6:55pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/8 "2023-11-02T18:55:58Z")

</div>

Hello, you can either follow this tutorial here:

> **[Bandpower of an EEG signal](https://raphaelvallat.com/bandpower.html)**
>
> Raphael Vallat, PhD

Or use the Yasa package, which does all the work for you:

- Raw input: [yasa.bandpower — yasa 0.6.3 documentation](https://raphaelvallat.com/yasa/build/html/generated/yasa.bandpower.html)
- PSD input: [yasa.bandpower\_from\_psd — yasa 0.6.3 documentation](https://raphaelvallat.com/yasa/build/html/generated/yasa.bandpower_from_psd.html)

Best wishes,  
Richard

---

<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:** [November 2, 2023, 6:56pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/9 "2023-11-02T18:56:32Z")

</div>

> [@North](#):
>
> But that psd is density, what I want to calculate the the power. So, should just multiply the freqs with psds to get the power?

I think typically when people report “alpha band power” it is ~~mean power in the alpha band. So expanding on @scott-huberty 's suggestion you could do something like `raw.compute_psd(fmin=8, fmax=12).get_data().mean(axis=-1)` to get the mean power between 8 and 12 Hz.~~ See @richard’s answer below, it should be integrated, not a mean.

> [@scott-huberty](#):
>
> Alternatively, you could calculate the power of the signal as @drammock demonstrates (amplitude squared), and then bandpass filter your data between 8-12Hz?

Do not do that. Computing instantaneous signal power is a non-linear operation and will change the frequency content of the signal, so band-passing afterward will not have the effect you expect/want.

```python
import numpy as np
import matplotlib.pyplot as plt
import mne
t = np.linspace(0, 3, 3001)
freqs = [10, 16, 21, 25, 28, 30]
y = np.sum([np.sin(2 * np.pi * f * t) for f in freqs], axis=0)
alpha_band = mne.filter.filter_data(y, sfreq=1000, lfreq=8, hfreq=12)
inst_power = y ** 2
not_alpha = mne.filter.filter_data(inst_power, sfreq=1000, lfreq=8, hfreq=12)

fig, ax = plt.subplots()
ax.plot(t, alpha_band) # only the 10Hz component, as expected
ax.plot(t, not_alpha)

```

![Figure_1](https://global.discourse-cdn.com/free1/uploads/mne/original/2X/c/c4f20adc282a36ba450e89f5c6a04071ed301ce3.png)

---

<div class="post-metadata">

**Author:** ![richard](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/richard/32/15_2.png) [@richard](https://mne.discourse.group/u/richard)\
**Post date:** [November 2, 2023, 7:05pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/10 "2023-11-02T19:05:30Z")

</div>

> [@drammock](#):
>
> it is mean power in the alpha band. So expanding on @scott-huberty 's suggestion you could do something like `raw.compute_psd(fmin=8, fmax=12).get_data().mean(axis=-1)` to get the mean power between 8 and 12 Hz.

I think you actually need to integrate, this is also what Yasa does:

> <https://github.com/raphaelvallat/yasa/blob/9608fa41d22f21bb8ddf77cd790a6bf8610ed44f/yasa/spectral.py#L342>

Given that this seems to be all but trivial, but a common use case, I think we should maybe make it easier for users to get the power of a given frequency band…

---

<div class="post-metadata">

**Author:** ![scott-huberty](https://yyz2.discourse-cdn.com/free1/user_avatar/mne.discourse.group/scott-huberty/32/2715_2.png) [@scott-huberty](https://mne.discourse.group/u/scott-huberty)\
**Post date:** [November 2, 2023, 7:16pm UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/11 "2023-11-02T19:16:56Z")

</div>

> [@drammock](#):
>
> Do not do that. Computing instantaneous signal power is a non-linear operation and will change the frequency content of the signal, so band-passing afterward will not have the effect you expect/want.

My mistake, thanks for clarifying!

---

<div class="post-metadata">

**Author:** ![North](https://avatars.discourse-cdn.com/v4/letter/n/7ea924/32.png) [@North](https://mne.discourse.group/u/North)\
**Post date:** [November 3, 2023, 12:57am UTC](https://mne.discourse.group/t/questions-for-calculate-power/7767/12 "2023-11-03T00:57:16Z")

</div>

That solved my problem! Thank you all!
