ValueError: highpass frequency [-0.5 0.4975 ...] must be greater than zero

Dear Colleagues

I’m trying to plot a “normal” rest EEG edf file:

raw.filter(l_freq=0.5, h_freq=None)
raw.filter(0.5, 50)
notches = np.arange(60)
raw.notch_filter(notches, phase='zero-double', fir_design='firwin2')

The problem is the last line. With it I have the above error, without it, the plot is all right. Any hint? Thanks in advance.

np.arange(60) is not valid for notches. It returns the frequencies:

array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
       34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
       51, 52, 53, 54, 55, 56, 57, 58, 59])

And the first one, 0 is raising.

What you usually want to do is filter at 60 Hz (+ eventually 120, 180 Hz). For 60 only, you could use:

np.arange(60, 61, 60)
Out[68]: array([60])

For 60 and 120, you could use:

np.arange(60, 121, 60)

The syntax is np.arange(start, stop (excluded! which is why +1), step).

A useful idiom I often use is

notches = np.arange(1, 4) * 60

where 60 is your local AC line frequency (50 in parts of Europe I think?). The args (1, 4) will yield three notches, so here 60, 120, 180. Same result as @mscheltienne but might be easier to understand if you’re less familiar with arange's step parameter.

1 Like

Looks simpler!
Yes, 50 Hz powerline in Europe.

1 Like

Thank you. Perfect!!

https://mne.tools/0.21/auto_examples/time_frequency/plot_compute_raw_data_spectrum.html

Just for EEG raw:

# In [ ]:

# Pick MEG magnetometers in the Left-temporal region
# selection = read_selection('Left-temporal')
picks = mne.pick_types(raw.info, meg=False, eeg=True, eog=False,
                       stim=False, exclude='bads')  #, selection=selection)

# Let's just look at the first few channels for demonstration purposes
picks = picks[0:19]

plt.figure()
ax = plt.axes()
raw.plot_psd(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, n_fft=n_fft,
             n_jobs=1, proj=False, ax=ax, color=(0, 0, 1),  picks=picks,
             show=False, average=True)

raw.plot_psd(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, n_fft=n_fft,
             n_jobs=1, proj=True, ax=ax, color=(0, 1, 0), picks=picks,
             show=False, average=True)

# And now do the same with SSP + notch filtering
# Pick all channels for notch since the SSP projection mixes channels together
raw.notch_filter(np.arange(60, 61, 60), n_jobs=1, fir_design='firwin')
raw.plot_psd(tmin=tmin, tmax=tmax, fmin=fmin, fmax=fmax, n_fft=n_fft,
             n_jobs=1, proj=True, ax=ax, color=(1, 0, 0), picks=picks,
             show=False, average=True)

ax.set_title('Twenty Channels EEG Raw')
plt.legend(ax.lines[::3], ['Without SSP', 'With SSP', 'SSP + Notch'])

![image|617x500](upload://xFhFuy0u61d6SwN4jpqTdqvI4RW.png)