Beispiel #1
0
    def findGoodSong(self):
        """
        """
        
        for i in range(self.STARTFILE, len(self.wav_index)):

            FILE = (self.FILEDIR + self.FILES[i]) 
            p, freqs, t, im = sf.spec(FILE)
            p = p
            sf.tellme('Select Song')
            plt.waitforbuttonpress()
            sf.tellme('Select Song')
            happy = False
            Proceed = False
            while not happy:
                happy = plt.waitforbuttonpress()

                if not happy:
                    tim = np.asarray(plt.ginput(n=0, timeout=-1))
                    ind = np.logical_and(t > tim[0,0], t < tim[1,0])
                    SongTime = mlab.find(ind)
                    
                    # Generate syllable spectrogram
                    self.winFreq = freqs > self.MINFREQ
                    self.winFreq = mlab.find(self.winFreq)
                    P = p[:,SongTime]
                    P = P[self.winFreq,:]
                    plt.close(1)
                    self.FIND_SONG = P.mean(0)
                    self.THRESHOLD = min(self.FIND_SONG) * 10.0
                    Proceed = True
                    break
                    
                if happy:
                    plt.close()
            if Proceed:
                break
Beispiel #2
0
    def cutSyllables(self, SYLLABLES, DISPLAYSYLL=False, DISPLAYSONG=False):
        """
        """
        
        if len(self.FILES) < self.MAXFILE + 1:
            ENDFILE = len(self.FILES)
        elif len(self.FILES) >= self.MAXFILE + 1:
            ENDFILE = self.MAXFILE
        
        song = 0
        fil = self.STARTFILE
        while fil < ENDFILE:
            FILE = (self.FILEDIR + self.FILES[fil])
            print 'processing file: ', fil
            ## Spectrogram ##       
            p, freqs, t, im = sf.spec(FILE)
            p = p * 0.9
            SongP, SongTime = sf.FindSong(p, self.FIND_SONG, t, self.winFreq,
                                         self.THRESHOLD)
            if SongP is False:
                plt.close(1)
                if len(self.FILES) < self.MAXFILE + 1:
                    ENDFILE = len(self.FILES)
                elif len(self.FILES) >= self.MAXFILE + 1 and ENDFILE < len(
                                                                self.FILES):
                    ENDFILE = ENDFILE + 1
                elif ENDFILE >= len(self.FILES):
                    ENDFILE = ENDFILE
                print 'Skipped because there was no song'
                pass
            
            ############# Find Syllables################
            elif SongP.any():
                mean_freq = np.zeros((len(SongTime),1))
                for j in range(0,len(SongTime)):        
                    fr = (sum(freqs[self.winFreq] * np.power(SongP[:, j], 2)) / 
                            sum(np.power(SongP[:, j], 2)))
                    mean_freq[j] = fr

                syllFound = sf.runs(SongP.mean(0) > self.THRESHOLD)
                FOUND = syllFound[0].any()
                if FOUND:
 
                    for syll in range(0,len(syllFound[0])):

                        time = np.arange(syllFound[0][syll],
                                         syllFound[1][syll])
                        if len(time) > 200:
                            plt.close(1)
                            plt.close(2)
                            print 'Skipped syllable because it was too long'
                            pass
                        elif len(time) <= 200:
                            P = SongP[:,time]
                            ent, mean_freq, duration = sf.measure(P, 
                                                                  time, 
                                                                  freqs, 
                                                                  self.winFreq, 
                                                                  t)
                            
                            if syll <= SYLLABLES - 1:

                                self.syllables[syll]['entropy'][song] = ent
                                self.syllables[syll]['freq'][song] = mean_freq
                                self.syllables[syll]['duration'][song] = (
                                                                    duration)
                                # Generate syllable spectrogram
                                if DISPLAYSYLL == 1:
                                    Z = 10*np.log10(abs(P))
                                    plt.figure()
                                    plt.imshow(Z,origin='lower', aspect='auto')
                                    plt.hold(True)
                                    plt.show(bloc=False)
                                    plt.tight_layout()
                                    plt.close(3)
                                plt.close(2)
                        plt.close(1)
                    song += 1
                                   
                if not FOUND:

                    plt.close(1)
                    plt.close(2)
                    if len(self.FILES) < self.MAXFILE + 1:
                        ENDFILE = len(self.FILES)
                    elif (len(self.FILES) >= self.MAXFILE + 1 and 
                            ENDFILE < len(self.FILES)):
                                
                        ENDFILE = ENDFILE + 1
                        
                    elif ENDFILE >= len(self.FILES):
                        ENDFILE = ENDFILE
                        print 'Skipped because there were no syllables \
                                extracted'           
            fil +=1