Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Index error returned while applying kernel to spectrogram

I found this code online and feed it a text file with names of wavs but can’t get it to work. It is a whale detector and applies a kernel to a spectrogram. I remember trying it before and having no issue with it but now I just can’t get it to work. I’ve checked that my spectrograms were correctly calculated so I guess the error is not linked to my data but to the code itself.

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}

I get the following error :

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Thanks !

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The error comes from scipy’s find peaks.
It does not only returns the peaks but other information as well. Replace those lines:

detectP = sg.find_peaks(CorrValP/max(CorrValP), height = 0.5)[0]
detectM = sg.find_peaks(CorrValM/max(CorrValM), height = 0.5)[0]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading