I am using the compu_psd function but I got different value once getting the data and then plot the bar chart
with the code:
Epochs[3].compute_psd().plot()
I will get
but with the following code:
A=Epochs[0].compute_psd()
freqs = A.freqs
psd_data = A.data
# Define frequency bands
bands = {
'Delta': (0.5, 4),
'Theta': (4, 8),
'Alpha': (8, 13),
'Beta': (13, 30)
}
# Function to calculate mean power for each band
def bandpower(psd, freqs, band):
band_freqs = np.logical_and(freqs >= band[0], freqs <= band[1])
band_power = np.mean(psd[:, :, band_freqs], axis=-1)
return np.mean(band_power, axis=0)
# Calculate band power for each band
band_powers = {band: bandpower(psd_data, freqs, bands[band]) for band in bands}
# Plotting
fig, ax = plt.subplots()
bands_names = list(band_powers.keys())
mean_powers = [np.mean(band_powers[band]) for band in bands_names]
ax.bar(bands_names, mean_powers)
ax.set_xlabel('Frequency Bands')
ax.set_ylabel('Mean Power (dB μV²/Hz)')
ax.set_title('Mean Power in Different Frequency Bands')
plt.show()
the output is
from the range of the data it seem the units and range are not correct
also the value of PSD is like:
array([[[1.52438752e-08, 3.02824429e-08, 2.79907505e-08, ...,
5.28638010e-10, 5.97301896e-10, 3.06707155e-10],
[8.78913704e-09, 1.74219543e-08, 1.64144076e-08, ...,
3.62823502e-10, 4.07407201e-10, 2.07554074e-10],
[5.59985449e-09, 1.12223745e-08, 1.04044687e-08, ...,
3.19684554e-10, 3.59507033e-10, 1.76134342e-10],
...,
[1.93100238e-08, 3.79889025e-08, 3.58651468e-08, ...,
4.68529638e-10, 5.13241100e-10, 2.54467878e-10],
[8.75686133e-10, 1.80090731e-09, 1.67881735e-09, ...,
1.37151917e-11, 1.47513447e-11, 5.01511143e-12],
[2.02489226e-08, 3.96566553e-08, 3.75657206e-08, ...,
3.70278273e-10, 3.83677705e-10, 1.94774120e-10]],
[[7.36418110e-09, 1.47644338e-08, 1.37755441e-08, ...,
2.96773610e-11, 2.29391550e-11, 1.14274344e-11],
[6.45813300e-09, 1.27413796e-08, 1.22074800e-08, ...,
2.19821739e-11, 1.87610058e-11, 8.58657088e-12],
[1.03793212e-08, 2.01215105e-08, 1.97360644e-08, ...,
1.36124355e-11, 1.53256654e-11, 9.10302910e-12],
...,
[9.14992207e-09, 1.78669762e-08, 1.69899880e-08, ...,
2.80164403e-11, 3.02477285e-11, 1.10422672e-11],
[7.30567495e-11, 1.39693116e-10, 9.40173733e-11, ...,
3.32571274e-12, 1.68263134e-12, 8.40415550e-13],
...
7.25500835e-10, 8.33091477e-10, 3.95014426e-10],
[1.50080844e-09, 2.97818117e-09, 2.80849585e-09, ...,
3.57650936e-11, 3.79948234e-11, 1.64251653e-11],
[2.07465722e-08, 4.11018029e-08, 3.87241280e-08, ...,
6.62117343e-10, 7.08359830e-10, 3.41941640e-10]]])
and if I use:
10*np.log10(psd_data)
the output is:
array([[[ -78.16904615, -75.18809093, -75.52985457, ...,
-92.76841614, -92.23806107, -95.13276093],
[ -80.56053764, -77.58903129, -77.84774786, ...,
-94.40304589, -93.89971299, -96.82868737],
[ -82.51823258, -79.49915242, -79.82780091, ...,
-94.95278347, -94.44292609, -97.54155957],
...,
[ -77.14217191, -74.20343253, -74.45327387, ...,
-93.29262932, -92.89678573, -95.94367032],
[ -90.57651528, -87.44508639, -87.7499655 , ...,
-108.62798119, -108.31168388, -112.99719413],
[ -76.93598079, -74.01683918, -74.25208276, ...,
-94.3147177 , -94.16033437, -97.1046875 ]],
[[ -81.3287554 , -78.30783204, -78.60891237, ...,
-105.2757472 , -106.39422583, -109.42051264],
[ -81.89893015, -78.94783546, -79.13373977, ...,
-106.57929361, -107.26743881, -110.66180241],
[ -79.83831046, -76.96339419, -77.04739447, ...,
-108.66064165, -108.1458066 , -110.40814069],
...,
[ -80.38582605, -77.47948941, -77.69806929, ...,
-105.52587046, -105.19307234, -109.56941749],
[-101.36339654, -98.54824994, -100.26791887, ...,
-114.78115266, -117.74011027, -120.7550592 ],
...
-91.39362083, -90.79307309, -94.03387043],
[ -88.23674736, -85.26048886, -85.51526214, ...,
-104.46540634, -104.2027557 , -107.84490251],
[ -76.83053649, -73.86139127, -74.12018353, ...,
-91.79065036, -91.49746075, -94.66048009]]])
It is negative and it seem sth is wrong due to units