将内核应用于频谱图时返回索引错误
Index error returned while applying kernel to spectrogram
我在网上找到了这段代码,并为它提供了一个名称为 wavs 的文本文件,但无法让它工作。它是一个鲸鱼检测器,并将内核应用于频谱图。我记得以前尝试过并且没有问题,但现在我无法让它工作。我已经检查了我的频谱图是否已正确计算,所以我猜错误与我的数据无关,而是与代码本身有关。
files = open('file.txt', 'r').readlines()
sig_ref, samp_ref = sf.read(files[0][:-1])
f_ref, t_ref = sg.spectrogram(sig_ref, samp_ref, 'hann', 16384, 10000)[:2]
f0_p = 10000 #average start frequency
f1_p = 30000 #average end frequency
bdwdth_p = 11.3 # average bandwidth
dur_p = 0.5 #average duration
[tvec_p, fvec_p, BlueKernel_p] = buildkernel(f0_p, f1_p, bdwdth_p, dur_p, f_ref, t_ref, samp_ref) #build kernel from input parameters
f0_m = 250 #average start frequency
f1_m = 900 #average end frequency
bdwdth_m = 400 # average bandwidth
dur_m = 1 #average duration
[tvec_m, fvec_m, BlueKernel_m] = buildkernel(f0_m, f1_m, bdwdth_m, dur_m, f_ref, t_ref, samp_ref) #build kernel from input parameters
time = []
conf = []
species = []
for file in files:
sig, samp = sf.read(file[:-1])
f, t, Sxx = sg.spectrogram(sig, samp, 'hann', 16384, 10000)
CorrValP = cc(f, t, Sxx, tvec_p, fvec_p, BlueKernel_p)
CorrValM = cc(f, t, Sxx, tvec_m, fvec_m, BlueKernel_m)
np.save('test_dolphin', CorrValP)
detectP = sg.find_peaks(CorrValP/max(CorrValP), height = 0.5)
detectM = sg.find_peaks(CorrValM/max(CorrValM), height = 0.5)
species.extend(['dolphin' for i in range(len(detectP))])
time.extend(t[detectP])
conf.extend(CorrValP[detectP]/np.amax(CorrValP))
species.extend(['whale' for i in range(len(detectM))])
time.extend(t[detectM])
conf.extend(CorrValM[detectM]/np.amax(CorrValM))
dic = {'file': np.tile(file, len(time)), 'species' : species, 'T' : time, 'confidence' : conf}
我收到以下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
谢谢!
错误来自 scipy 的查找峰。
它不仅 returns 峰值而且还有其他信息。替换这些行:
detectP = sg.find_peaks(CorrValP/max(CorrValP), height = 0.5)[0]
detectM = sg.find_peaks(CorrValM/max(CorrValM), height = 0.5)[0]
我在网上找到了这段代码,并为它提供了一个名称为 wavs 的文本文件,但无法让它工作。它是一个鲸鱼检测器,并将内核应用于频谱图。我记得以前尝试过并且没有问题,但现在我无法让它工作。我已经检查了我的频谱图是否已正确计算,所以我猜错误与我的数据无关,而是与代码本身有关。
files = open('file.txt', 'r').readlines()
sig_ref, samp_ref = sf.read(files[0][:-1])
f_ref, t_ref = sg.spectrogram(sig_ref, samp_ref, 'hann', 16384, 10000)[:2]
f0_p = 10000 #average start frequency
f1_p = 30000 #average end frequency
bdwdth_p = 11.3 # average bandwidth
dur_p = 0.5 #average duration
[tvec_p, fvec_p, BlueKernel_p] = buildkernel(f0_p, f1_p, bdwdth_p, dur_p, f_ref, t_ref, samp_ref) #build kernel from input parameters
f0_m = 250 #average start frequency
f1_m = 900 #average end frequency
bdwdth_m = 400 # average bandwidth
dur_m = 1 #average duration
[tvec_m, fvec_m, BlueKernel_m] = buildkernel(f0_m, f1_m, bdwdth_m, dur_m, f_ref, t_ref, samp_ref) #build kernel from input parameters
time = []
conf = []
species = []
for file in files:
sig, samp = sf.read(file[:-1])
f, t, Sxx = sg.spectrogram(sig, samp, 'hann', 16384, 10000)
CorrValP = cc(f, t, Sxx, tvec_p, fvec_p, BlueKernel_p)
CorrValM = cc(f, t, Sxx, tvec_m, fvec_m, BlueKernel_m)
np.save('test_dolphin', CorrValP)
detectP = sg.find_peaks(CorrValP/max(CorrValP), height = 0.5)
detectM = sg.find_peaks(CorrValM/max(CorrValM), height = 0.5)
species.extend(['dolphin' for i in range(len(detectP))])
time.extend(t[detectP])
conf.extend(CorrValP[detectP]/np.amax(CorrValP))
species.extend(['whale' for i in range(len(detectM))])
time.extend(t[detectM])
conf.extend(CorrValM[detectM]/np.amax(CorrValM))
dic = {'file': np.tile(file, len(time)), 'species' : species, 'T' : time, 'confidence' : conf}
我收到以下错误:
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
谢谢!
错误来自 scipy 的查找峰。 它不仅 returns 峰值而且还有其他信息。替换这些行:
detectP = sg.find_peaks(CorrValP/max(CorrValP), height = 0.5)[0]
detectM = sg.find_peaks(CorrValM/max(CorrValM), height = 0.5)[0]