PSD welch - Value Error

Hi,

I’m getting a value error when I run my epochs through psd_welch().

  • MNE version: e.g. 0.24.0
  • operating system: Windows 10
ValueError                                Traceback (most recent call last)
<ipython-input-104-243081137e4d> in <module>
      1 kwargs = dict(fmin=2, fmax=40, n_jobs=None)
----> 2 psds_welch_mean, freqs_mean = psd_welch(epochs, average='mean', **kwargs)
      3 psds_welch_median, freqs_median = psd_welch(epochs, average='median', **kwargs)
      4 
      5 # Convert power to dB scale.

<decorator-gen-256> in psd_welch(inst, fmin, fmax, tmin, tmax, n_fft, n_overlap, n_per_seg, picks, proj, n_jobs, reject_by_annotation, average, window, verbose)

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\mne\time_frequency\psd.py in psd_welch(inst, fmin, fmax, tmin, tmax, n_fft, n_overlap, n_per_seg, picks, proj, n_jobs, reject_by_annotation, average, window, verbose)
    253                            n_overlap=n_overlap, n_per_seg=n_per_seg,
    254                            average=average, n_jobs=n_jobs, window=window,
--> 255                            verbose=verbose)
    256 
    257 

<decorator-gen-255> in psd_array_welch(x, sfreq, fmin, fmax, n_fft, n_overlap, n_per_seg, n_jobs, average, window, verbose)

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\mne\time_frequency\psd.py in psd_array_welch(x, sfreq, fmin, fmax, n_fft, n_overlap, n_per_seg, n_jobs, average, window, verbose)
    143     # Prep the PSD
    144     n_fft, n_per_seg, n_overlap = _check_nfft(n_times, n_fft, n_per_seg,
--> 145                                               n_overlap)
    146     win_size = n_fft / float(sfreq)
    147     logger.info("Effective window size : %0.3f (s)" % win_size)

c:\users\user\appdata\local\programs\python\python37\lib\site-packages\mne\time_frequency\psd.py in _check_nfft(n, n_fft, n_per_seg, n_overlap)
     45                           'n_times. If you want zero-padding, you have to set '
     46                           'n_per_seg to relevant length. Got n_fft of %d while'
---> 47                           ' signal length is %d.') % (n_fft, n))
     48     n_per_seg = n_fft if n_per_seg is None or n_per_seg > n_fft else n_per_seg
     49     n_per_seg = n if n_per_seg > n else n_per_seg

ValueError: If n_per_seg is None n_fft is not allowed to be > n_times. If you want zero-padding, you have to set n_per_seg to relevant length. Got n_fft of 256 while signal length is 91.

Can someone help me resolve this error?

Thanks

Hello Arjun,

your Signal is too short for the frequency resolution that is automatically set by PSD_Welch (256). You have basically 3 ways to prevent this error, which way you choose depends on your research question (e.g. do you want a very fine resolution or do you average over broadband later anyways?):

  • increase the signal length if possible (n_times)
  • decrease n_fft to 125 or even shorter (n_fft)
  • artificially increase your signal by zero padding (n_per_seg)

Hope that helps.

Best,

Carina

2 Likes

Hi @CarinaFo ,

Thanks. I was also planning to reduce the n_fft but my signal length is 91. That means my n_fft should be even smaller.
Can the n_fft be lesser that 90? Can the length of FFT used be this lesser?

Thanks

HI Arjun,
This is a pretty clear and concise explanation of n_fft and n_per_seg for the scipy.signal.spectrogram() function, which uses those variables the same way that we do in MNE-Python:

Both the answer and the comments following it might help you understand better.

2 Likes