calculating PSD with EpochsEEGLAB

  • MNE version: latest
  • operating system Windows 11

i have a eeg data from .set file which has shape 501293000 that is 50 trials , 129 channels ,3000 samples. i want to calculate mean psd , and then mean alpha, beta , gamma psd over all the trials for a particular one or two channels .
I’m trying to use psd_welch but facing error , can someone please correct my code?

eeg_data_path = 'C/abc.set'
epochs = mne.io.read_epochs_eeglab(eeg_data_path)

# Define the EEG channel name you're interested in
channel_name = 'E70'  # Change to the desired channel name

# Select the specific channel
alpha_band = (8, 13)
gamma_band = (30, 40)

# Select the specific channel
eeg_channel = raw.copy().pick_channels([channel_name])


# Calculate PSD for alpha and gamma frequency bands using Welch method
alpha_psd, alpha_freqs = mne.time_frequency.psd_array_welch(eeg_channel, fmin=alpha_band[0], fmax=alpha_band[1], sfreq=1000)
gamma_psd, gamma_freqs = mne.time_frequency.psd_array_welch(eeg_channel, fmin=gamma_band[0], fmax=gamma_band[1], sfreq=1000)


# Calculate the mean PSD values for alpha and gamma bands
mean_alpha_psd = np.mean(alpha_psd, axis=0)
mean_gamma_psd = np.mean(gamma_psd, axis=0)

Hello @Sidlotia and welcome to the forum.

I believe you meant eeg_channel = epochs.copy()... here.

It’s safer to pass sfreq=epochs.info["sfreq"].

It may also be worth looking at epochs.compute_psd() for a higher-level interface:
https://mne.tools/stable/generated/mne.Epochs.html#mne.Epochs.compute_psd

Best wishes,
Richard

i tried using it earlier also but same eror comes alpha_psd, alpha_freqs =

mne.time_frequency.psd_array_welch(eeg_channel, fmin=alpha_band[0], fmax=alpha_band[1],  sfreq=epochs.info["sfreq"])

TypeError: string indices must be integers, not 'tuple'

As @richard mentioned, please use this interface to compute PSDs. The function you are using requires a NumPy array as its first argument (but you are passing a Raw or an Epochs object).

1 Like

hey thanks for the reply.

I am not able to understand it shows some error if used this func , can you please modify my that line according to this func requirement

I am working with EEG and MNE for first time , so please expect some silly ques or doubts, sorry in advance.

It should be psd = epochs.compute_psd(). This will give you an EpochsSpectrum object, which you can use for further calculations such as averaging over epochs and averaging specific frequencies.

2 Likes