我非常喜欢编码。对于我的课程,我需要完成一项练习,其中必须对睡眠阶段进行分类。代码如下:
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
但是当代码执行时,python给了我一个错误:
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
我正在努力寻找此错误的来源。请帮我!非常感谢你提前!
答案 0 :(得分:0)
我自己设法找到了错误:psd函数应该放在for循环外面,因为只能为一个值做FFT。