[Mne_analysis] TIME FREQUENCY ANALYSIS: power spectral density using Welch's method

Alexandre Gramfort alexandre.gramfort at inria.fr
Thu Dec 13 00:52:52 EST 2018
Search archives:

        External Email - Use Caution        

hi Jasmina,

can you send us a script that uses some mne dataset eg sample?
it's much easier for us to test and identify quickly what may go wrong
when we can run the code.

thanks
Alex


On Wed, Dec 12, 2018 at 4:39 PM Jasmina Wallace
<jasmina.wallace23 at gmail.com> wrote:
>
>         External Email - Use Caution
>
> Hi everybody!
> I've just started using MNE and I would like to do a time frequency analysis (mne.time_frequency.psd_welch) for all frequency bands with EEG and MEG data: delta 1-4 Hz, theta 4-7 Hz, lower and upper alpha in 7-13 Hz range and lower and faster beta in 13-30Hz range.
> Using the script here below I can compute the psd for each frequency needed changing the fmin and the fmax, except for the theta. The script allows to analyse one recorded  Run on EEG and MEG channels with open eyes (YO) and closed eyes (YF). More precisely, this script runs well and I don't have any error message, but in the end the final file .mat is empty. I tried to change the n_fft, using 128 or 64, but nothing has changed. So I couldn't find out where I'm going wrong and I can't understand where is the difference between the theta band script and the other ones.
> Any suggestion or comments are really welcome!
> Thank you very much!
> Best,
> Jasmina
>
> This is the script that I used :
> ***************
> dirs_f=['brain_rf01_s01']
> suj_count = len(dirs_f)
> i=0
> while i<suj_count:
>     n=dirs_f[i]
>     path1, dirs1, files1 = next(os.walk(data_path_BRF+n))
>
>     for ch in dirs1:
>         if ch.startswith("171"): # faire attention a changer le date
>            path2, dirs2, files2 = next(os.walk(path1+'/'+ch))
>
>            for file in files2:
>                if file.endswith('corrected_mne.fif') and (file.startswith('run01')):
>                    fname=path2+'/'+file
>                    raw = mne.io.read_raw_fif(fname, preload=True)
>                    info = mne.io.read_info(fname)
>                    ev=mne.find_events(raw, output='step')
>
>                    tmin, tmax = 8, 180  # use the first 120s of data
>                    fmin, fmax = 4, 7  # look at frequencies band
>                    n_fft = 2048  # the FFT size (n_fft). Ideally a power of 2
>
>                    psd_picks = mne.pick_types(raw.info, meg=True, eeg=True, stim=False, eog=False, exclude='bads')
>                    tmin=(ev[0][0]+20-raw.first_samp)/info['sfreq']
>                    tmax=(ev[3][0]-20-raw.first_samp) /info['sfreq']
>                    psd, freqs= psd_welch(raw, fmin, fmax, tmin, tmax, n_fft=256, n_overlap=0, n_per_seg=None, picks=psd_picks, proj=False, n_jobs=1, reject_by_annotation=True, verbose=None)
>
>                    filename, file_extension = os.path.splitext(fname)
>                    filename_psd, file_extension_psd = os.path.splitext(file)
>
>                    psds = 20 * np.log10(psd)
>                    ch_names=info['ch_names']
>                    ch_names_meeg = [ch_names[ii] for ii in np.array(psd_picks)]
>                    chan_types = [mne.io.pick.channel_type(info, ch) for ch in psd_picks ]
>                    psd_name_04_07=path2+'/'+filename_psd+'_YO_meg_04hz_07hz.mat'
>                    scipy.io.savemat(psd_name_04_07,{'freq_04_07':psds[74:380]})
>                    psd_name_04_07=path2+'/'+filename_psd+'_YO_eeg_04hz_07hz.mat'
>                    scipy.io.savemat(psd_name_04_07,{'freq_04_07':psds[0:74]})
>
>
>                    tmin=(ev[3][0]+20-raw.first_samp)/info['sfreq']
>                    tmax=(raw.last_samp-raw.first_samp) /info['sfreq']
>
>                    psd, freqs= psd_welch(raw, fmin, fmax, tmin, tmax, n_fft=256, n_overlap=0, n_per_seg=None, picks=psd_picks, proj=False, n_jobs=1, reject_by_annotation=True, verbose=None)
>
>                    psds = 20 * np.log10(psd)
>                    ch_names=info['ch_names']
>                    ch_names_meeg = [ch_names[ii] for ii in np.array(psd_picks)]
>                    chan_types = [mne.io.pick.channel_type(info, ch) for ch in psd_picks ]
>                    psd_name_04_07=path2+'/'+filename_psd+'_YF_meg_04hz_07hz.mat'
>                    scipy.io.savemat(psd_name_04_07,{'freq_04_07':psds[74:380]})
>
>                    psd_name_04_07=path2+'/'+filename_psd+'_YF_eeg_04hz_07hz.mat'
>                    scipy.io.savemat(psd_name_04_07,{'freq_04_07':psds[0:74]}
>     i=i+1
> ************************
> _______________________________________________
> Mne_analysis mailing list
> Mne_analysis at nmr.mgh.harvard.edu
> https://mail.nmr.mgh.harvard.edu/mailman/listinfo/mne_analysis



More information about the Mne_analysis mailing list