Proper use of window parameter for Welch power spectral density (scipy)?

680 Views Asked by At

I am trying to calculate the Welch power spectral density over specific frequency bands for EEG signal processing ($\delta$ (0–4 Hz), $\theta$ (4–8 Hz), $\alpha$ (8–13 Hz), $\beta$ (13–30 Hz), $\gamma_1$ (30–60 Hz), and $\gamma_2$ (60–90 Hz)). I thought I could accomplish this by passing an array of integers with the desired frequency bins to the 'window' parameter, but this doesn't quite work as expected. Unfortunately this particular use case of the parameter is not well documented, so I'm having a hard time understanding how I can alter my code to at least get close to the abovementioned bins.

Currently, I am doing the following:

bands = [0,4,8,13,30,60,90]
frequency_bins, psd = welch(sample, fs=256, window=bands)

However, frequency_bins==[0, 36.57142857, 73.14285714, 109.71428571]. Can anyone explain what the window parameter is accomplishing in this case, and if it is possible to somehow make the frequency_bins output equal to bands?

1

There are 1 best solutions below

0
On

I think this code may help you.

# Frequency bands !
frequencies_bandes = {"delta" : [0,4],
                    "theta" : [4,8],
                    "alpha" : [8, 13],
                    "beta" : [13,35],
                    "gamma" : [30, 40]}
 

# Welch method to get spectrums estimation check parameters in the begining of this file 
freq , spectrum = signal.welch(your_signal,sf,nperseg=nperseg,nfft=nfft,scaling='density')

# Geting frequencies indexes 
frequencies_indexes = [np.logical_and(freq >= band[0], freq <= band[1]) for band in frequencies_bandes.values()]

# Getting mean power_energy per frequency band 
mean_power_per_band_per_channel += [np.mean(spectrum[idx]) for idx in frequencies_indexes]