Exemple #1
0
def eRatio(dirName):
    for root, dirs, files in os.walk(str(dirName)):
        for file in files:
            if file.endswith('.wav'):
                wavobj = wavio.read(root + '\\' + file)
                sampleRate = wavobj.rate
                data = wavobj.data
                if data is not 'float':
                    data = data.astype('float')  # data / 32768.0
                if np.shape(np.shape(data))[0] > 1:
                    data = data[:, 0]
                post = SupportClasses.postProcess(data, sampleRate, [])
                print(file, post.eRatioConfd(seg=None))
Exemple #2
0
def deleteClick2(dirName):
    """
    Given the directory of sounds this deletes the annotation segments with rain corrupted.
    Check to make sure the segment to delete has no sign of kiwi - use fundamental frq rather than eRatio in 'deleteClick'
    """
    for root, dirs, files in os.walk(str(dirName)):
        for file in files:
            if file.endswith('.data') and file[:-5] in files:
                # go through each segment
                file = root + '/' + file
                with open(file) as f:
                    segments = json.load(f)
                    newSegments = copy.deepcopy(segments)
                    wavobj = wavio.read(file[:-5])
                    audioData = wavobj.data
                    if audioData is not 'float':
                        audioData = audioData / 32768.0
                    audioData = audioData[:, 0].squeeze()
                    sampleRate = wavobj.rate
                    if sampleRate != 16000:
                        audioData = librosa.core.audio.resample(audioData, sampleRate, 16000)
                        sampleRate = 16000
                    # Find T_ERatio based on first 5 secs as it varies accorss the recordings
                    post = SupportClasses.postProcess(audioData, sampleRate, [])
                    # T_ERatio = post.eRatioConfd([1, 6, "", ""])
                    print(file)
                    if len(segments)>2:
                        ff = Features.Features(audioData, sampleRate)
                        mfcc = ff.get_mfcc()
                        mean = np.mean(mfcc[1, :])
                        std = np.std(mfcc[1, :])
                        thr = mean - 2 * std  # mfcc1 thr for the file
                    else:
                        thr = 0

                    chg = False
                    for seg in segments:
                        if seg[0] == -1:
                            continue
                        else:
                            # read the sound segment and check for wind
                            secs = seg[1] - seg[0]
                            wavobj = wavio.read(file[:-5], nseconds=secs, offset=seg[0])
                            data = wavobj.data
                            sampleRate =wavobj.rate
                            if data is not 'float':
                                data = data / 32768.0
                            data = data[:, 0].squeeze()

                            # check for clicks
                            ff = Features.Features(data, sampleRate)
                            mfcc = ff.get_mfcc()
                            mfcc1 = mfcc[1, :]  # mfcc1 of the segment

                            if thr == 0:
                                ff = Features.Features(audioData, sampleRate)
                                mfcc = ff.get_mfcc()
                                mean = np.mean(mfcc[1,:])
                                std = np.std(mfcc[1,:])
                                thr = mean - 2 * std    # mfcc1 thr for the file

                            if np.min(mfcc1) < thr:
                                # # now check eRatio
                                # eRatio = post.eRatioConfd(seg)  # eRatio(file[:-5], seg, thr=T_ERatio)
                                # eRatioBefore = post.eRatioConfd([seg[0] - 10, seg[0], "", ""])
                                # eRatioAfter = post.eRatioConfd([seg[1], seg[1] + 10, "", ""])
                                # if eRatio > eRatioBefore*1.05 or eRatio > eRatioAfter*1.05:
                                #     continue

                                # now check f. frq.
                                # down sample to avoid higher frq noise
                                if sampleRate != 16000:
                                    data = librosa.core.audio.resample(data, sampleRate, 16000)
                                    sampleRate = 16000
                                # denoise prior to f frq detection
                                waveletDenoiser = WaveletFunctions.WaveletFunctions(data=data, wavelet=None, maxLevel=12)
                                data = waveletDenoiser.waveletDenoise(data, thresholdType='soft', wavelet='dmey2', maxLevel=12)
                                sp = SignalProc.SignalProc([], 0, 512, 256)
                                sgRaw = sp.spectrogram(data, 512,256, mean_normalise=True, onesided=True, multitaper=False)
                                segment = Segment.Segment(data, sgRaw, sp, sampleRate,512, 256)
                                pitch, y, minfreq, W = segment.yin()
                                ind = np.squeeze(np.where(pitch > minfreq))
                                pitch = pitch[ind]
                                ff = np.mean(pitch)
                                if ff>500 and ff<5000:
                                    continue
                                else:
                                    print(seg)
                                    newSegments.remove(seg)
                                    chg = True
                    if chg:
                        file = open(file, 'w')
                        json.dump(newSegments, file)
Exemple #3
0
def deleteClick(dirName):
    """
    Given the directory of sounds this deletes the annotation segments with wind/rain corrupted files.
    Targeting moderate wind and above. Check to make sure the segment to delete has no sign of kiwi
    """
    for root, dirs, files in os.walk(str(dirName)):
        for file in files:
            if file.endswith('.data') and file[:-5] in files:
                # go through each segment
                file = root + '/' + file
                with open(file) as f:
                    segments = json.load(f)
                    newSegments = copy.deepcopy(segments)
                    wavobj = wavio.read(file[:-5])
                    audioData = wavobj.data
                    if audioData is not 'float':
                        audioData = audioData / 32768.0
                    audioData = audioData[:, 0].squeeze()
                    sampleRate = wavobj.rate
                    # Find T_ERatio based on first 5 secs as it varies accorss the recordings
                    post = SupportClasses.postProcess(audioData, sampleRate, [])
                    # T_ERatio = post.eRatioConfd([1, 6, "", ""])
                    print(file)
                    chg = False
                    for seg in segments:
                        if seg[0] == -1:
                            continue
                        else:
                            # read the sound segment and check for wind
                            secs = seg[1] - seg[0]
                            wavobj = wavio.read(file[:-5], nseconds=secs, offset=seg[0])
                            data = wavobj.data
                            if data is not 'float':
                                data = data / 32768.0
                            data = data[:, 0].squeeze()

                            # check for clicks
                            ff = Features.Features(data, sampleRate)
                            mfcc = ff.get_mfcc()
                            mfcc1 = mfcc[1, :]  # mfcc1 of the segment

                            ff = Features.Features(audioData, sampleRate)
                            mfcc = ff.get_mfcc()
                            mean = np.mean(mfcc[1,:])
                            std = np.std(mfcc[1,:])
                            thr = mean - 2 * std    # mfcc1 thr for the file

                            if np.min(mfcc1) < thr:
                                # # # now check eRatio
                                # eRatio = post.eRatioConfdV2(seg)
                                # if eRatio > 1.0:
                                #     continue

                                # just check duration>10 sec
                                if secs > 10:
                                    continue
                                else:
                                    print(seg)
                                    newSegments.remove(seg)
                                    chg = True
                    if chg:
                        file = open(file, 'w')
                        json.dump(newSegments, file)
Exemple #4
0
def deleteWindRain(dirName, windTest=True, rainTest=False, Tmean_wind = 1e-8):
    """
    Given the directory of sounds this deletes the annotation segments with wind/rain corrupted files.
    Targeting moderate wind and above. Check to make sure the segment to delete has no sign of kiwi
    Automatic Identification of Rainfall in Acoustic Recordings by Carol Bedoya, Claudia Isaza, Juan M.Daza, and Jose D.Lopez
    """
    #Todo: find thrs
    Tmean_rain = 1e-8   # Mean threshold
    Tsnr_rain = 3.5     # SNR threshold

    # Tmean_wind = 1e-9   # Mean threshold
    # Tsnr_wind = 0.5     # SNR threshold

    cnt = 0
    for root, dirs, files in os.walk(str(dirName)):
        for file in files:
            if file.endswith('.data') and file[:-5] in files:
                # go through each segment
                file = root + '/' + file
                with open(file) as f:
                    segments = json.load(f)
                    newSegments=copy.deepcopy(segments)
                    wavobj = wavio.read(file[:-5])
                    audioData = wavobj.data
                    # # ***
                    # if audioData.dtype is not 'float':
                    #     audioData = audioData.astype('float')  # / 32768.0
                    # if np.shape(np.shape(audioData))[0] > 1:
                    #     audioData = np.squeeze(audioData[:, 0])
                    # import librosa
                    # if wavobj.rate != 16000:
                    #     audioData = librosa.core.audio.resample(audioData, wavobj.rate, 16000)
                    #     sampleRate = 16000
                    # # ****
                    if audioData is not 'float':
                        audioData = audioData / 32768.0
                    audioData = audioData[:, 0].squeeze()
                    sampleRate = wavobj.rate

                    # Find T_ERatio based on first 5 secs as it varies accorss the recordings
                    post = SupportClasses.postProcess(audioData, sampleRate, [])
                    # T_ERatio = post.eRatioConfd([1, 6, "", ""])

                    chg = False
                    for seg in segments:
                        if seg[0] == -1:
                            continue
                        else:
                            # read the sound segment and check for wind
                            secs = seg[1]-seg[0]
                            wavobj = wavio.read(file[:-5], nseconds=secs, offset=seg[0])
                            data = wavobj.data
                            # # ***
                            # if data.dtype is not 'float':
                            #     data = data.astype('float')  # / 32768.0
                            # if np.shape(np.shape(data))[0] > 1:
                            #     data = np.squeeze(data[:, 0])
                            # if wavobj.rate != 16000:
                            #     data = librosa.core.audio.resample(data, wavobj.rate, 16000)

                            if data is not 'float':
                                data = data / 32768.0
                            data = data[:,0].squeeze()

                            wind_lower = 2.0 * 100 / sampleRate
                            wind_upper = 2.0 * 250 / sampleRate
                            rain_lower = 2.0 * 600 / sampleRate
                            rain_upper = 2.0 * 1200 / sampleRate

                            f, p = signal.welch(data, fs=sampleRate, window='hamming', nperseg=512, detrend=False)

                            if windTest:
                                limite_inf = int(round(len(p) * wind_lower)) # minimum frequency of the rainfall frequency band 0.00625(in
                                                                     # normalized frequency); in Hz = 0.00625 * (44100 / 2) = 100 Hz
                                limite_sup = int(round(len(p) * wind_upper)) # maximum frequency of the rainfall frequency band 0.03125(in
                                                                     # normalized frequency); in Hz = 0.03125 * (44100 / 2) = 250 Hz
                                a_wind = p[limite_inf:limite_sup] # section of interest of the power spectral density.Step 2 in Algorithm 2.1

                                mean_a_wind = np.mean(a_wind) # mean of the PSD in the frequency band of interest.Upper part of the step 3 in Algorithm 2.1
                                std_a_wind = np.std(a_wind)  # standar deviation of the PSD in the frequency band of the interest. Lower part of the step 3 in Algorithm 2.1

                                # c_wind = mean_a_wind / std_a_wind  # signal to noise ratio of the analysed recording. step 3 in Algorithm 2.1

                                if mean_a_wind > Tmean_wind:
                                    # eRatio = post.eRatioConfd(seg) #eRatio(file[:-5], seg, thr=T_ERatio)
                                    # eRatioBefore = post.eRatioConfd([seg[0]-10, seg[0], "", ""])
                                    # if eRatio > eRatioBefore*1.05: # or eRatio > eRatioAfter:  #it was 10 secs Before eratio
                                    # #version2
                                    # eRatio = post.eRatioConfdV2(seg)
                                    # if eRatio > 1.0:
                                    #     potentialCall = True

                                    # # now check f. frq.
                                    # # down sample will helkp to avoid higher frq noise
                                    # if sampleRate != 16000:
                                    #     data = librosa.core.audio.resample(data, sampleRate, 16000)
                                    #     sampleRate = 16000
                                    # # denoise prior to f. frq. detection
                                    # waveletDenoiser = WaveletFunctions.WaveletFunctions(data=data, wavelet=None,
                                    #                                                     maxLevel=12)
                                    # data = waveletDenoiser.waveletDenoise(data, thresholdType='soft', wavelet='dmey2',
                                    #                                       maxLevel=12)
                                    # sp = SignalProc.SignalProc([], 0, 512, 256)
                                    # sgRaw = sp.spectrogram(data, 512, 256, mean_normalise=True, onesided=True,
                                    #                        multitaper=False)
                                    # segment = Segment.Segment(data, sgRaw, sp, sampleRate, 512, 256)
                                    # pitch, y, minfreq, W = segment.yin(minfreq=600)
                                    # ind = np.squeeze(np.where(pitch > minfreq))
                                    # pitch = pitch[ind]
                                    # ff = np.mean(pitch)
                                    # if ff > 500 and ff < 5000:
                                    #     potentialCall = True

                                    # else:
                                    #     potentialCall = False

                                    # just check duration>10 sec
                                    if secs>10:
                                        potentialCall = True
                                    else:
                                        potentialCall = False
                                    if not potentialCall:
                                        print(file, seg, "--> windy")
                                        newSegments.remove(seg)
                                        chg = True
                                else:
                                    print(file, seg, "--> not windy")
                            if rainTest:
                                limite_inf = int(round(len(p) * rain_lower)) # minimum frequency of the rainfall frequency band 0.0272 (in
                                                                             # normalized frequency); in Hz=0.0272*(44100/2)=599.8  Hz
                                limite_sup = int(round(len(p) * rain_upper)) # maximum frequency of the rainfall frequency band 0.0544 (in
                                                                             # normalized frequency); in Hz=0.0544*(44100/2)=1199.5 Hz
                                a_rain = p[limite_inf:limite_sup]   # section of interest of the power spectral density.Step 2 in Algorithm 2.1

                                mean_a_rain = np.mean(a_rain)   # mean of the PSD in the frequency band of interest.Upper part of the step 3 in Algorithm 2.1
                                std_a_rain = np.std(a_rain)     # standar deviation of the PSD in the frequency band of the interest. Lower part of the step 3 in Algorithm 2.1

                                c_rain = mean_a_rain / std_a_rain   # signal to noise ratio of the analysed recording. step 3 in Algorithm 2.1

                                if c_rain > Tsnr_rain:
                                    # check if it is not kiwi
                                    eRatio = post.eRatioConfd(seg)  # eRatio(file[:-5], seg, thr=T_ERatio)
                                    eRatioBefore = post.eRatioConfd([seg[0] - 10, seg[0], "", ""])
                                    # eRatioAfter = post.eRatioConfd([seg[1], seg[1] + 5, "", ""])
                                    # T_ERatio = (eRatioBefore + eRatioAfter) / 2
                                    if eRatio > eRatioBefore:  # or eRatio > eRatioAfter:  #it was 10 secs Before eratio
                                        potentialCall = True
                                    else:
                                        potentialCall = False
                                    if not potentialCall:
                                        print(file, seg, "--> windy")
                                        newSegments.remove(seg)
                                        chg = True
                                else:
                                    # rainy.append(0)
                                    print(file, "--> not rainy")

                    if chg:
                        file = open(file, 'w')
                        json.dump(newSegments, file)
                cnt += 1
                print(file, cnt)