I am very much newbie in coding. For my class I need to finish an exercise, where stages of sleep has to be classified. The code is below:
def classify_epoch(epoch,rate):
"""
This function returns a sleep stage classification (integers: 1 for NREM
stage 1, 2 for NREM stage 2, and 3 for NREM stage 3/4) given an epoch of
EEG and a sampling rate.
"""
stage = []
for i in epoch:
Pxx, freqs = m.psd(i, NFFT = 512, Fs = srate)
Pxxs=np.cumsum(Pxx)
Pxxn=Pxx/Pxxs
if Pxxn+1 <= Pxxn:
stage.append(1)
elif (Pxxn+1 > 1.15*Pxxn) and (Pxxn+1 < 1.4*Pxxn):
stage.append(2)
else:
stage.append(3)
return stage
But when the code is executed python gives me an error:
Traceback (most recent call last):
File "<ipython-input-123-3b84c0546ebf>", line 1, in <module>
runfile('C:/Users/Olga/Documents/Python Scripts/problem_set4/problem_set4.py', wdir='C:/Users/Olga/Documents/Python Scripts/problem_set4')
File "C:\Users\Olga\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 580, in runfile
execfile(filename, namespace)
File "C:/Users/Olga/Documents/Python Scripts/problem_set4/problem_set4.py", line 218, in <module>
test_examples(examples, srate)
File "C:/Users/Olga/Documents/Python Scripts/problem_set4/problem_set4.py", line 176, in test_examples
c[j,i/bin_size] = classify_epoch(examples[j,range(i,i+bin_size)],srate)
File "C:/Users/Olga/Documents/Python Scripts/problem_set4/problem_set4.py", line 104, in classify_epoch
Pxx, freqs = m.psd(i, NFFT = 512, Fs = srate)
File "C:\Users\Olga\Anaconda\lib\site-packages\matplotlib\mlab.py", line 959, in psd
sides=sides, scale_by_freq=scale_by_freq)
File "C:\Users\Olga\Anaconda\lib\site-packages\matplotlib\mlab.py", line 1022, in csd
mode='psd')
File "C:\Users\Olga\Anaconda\lib\site-packages\matplotlib\mlab.py", line 700, in _spectral_helper
if len(x) < NFFT:
TypeError: len() of unsized object
I am struggling to find the source of this error. Please, help me! Thank you very much in advance!
I managed to find the mistake myself: psd function should have been placed outside for loop, because it is impossible to do FFT for just one value.