<jupyter_start><jupyter_text>CT1 = pd.read_csv(r".\Capstone Files\Catopuma temminckii\Catopuma temminckii sounds.csv")CT1_sound, CT1_rate = librosa.load(r".\Capstone Files\Catopuma temminckii\Catopuma_temminckii_V1736_02.wav")CT1T = (CT1, CT1_sound)<jupyter_code>audio_list = [AJ2, AJ3, AJ5, AJ13, D1T, D2T, D3T, D4T, D5T, C1T, C2T, C3T, C4T, C5T, C6T, L1T, L2T, L3T, L4T, L5T, L6T,
            L7T, L8T, L9T, L10T, L11T, L12T, LR1T, LR2T, LP1T, LP2T, LP3T, LP4T]
def frequency_range(audiofile):
    N= len(audiofile)
    y = np.abs(rfft(audiofile))
    x = rfftfreq(N, 1/ 22050)
    tup = (x, y)
    lst = []
    for x in range(len(tup[0])):
        if tup[1][x] > 2.5:
            lst.append(tup[0][x])
    freq_range = max(lst) - min(lst)
    return freq_range
def top_freq(file):
    f, t, mag = librosa.reassigned_spectrogram(file)
    mag_db = librosa.power_to_db(mag)
    freqs = [0]*len(f[0])
    for y in range(len(f[0])):
        for x in range(len(f)):
            if mag_db[x][y] > -10:
                freqs[y] = f[x][y]
    return freqs
def pulses(file):
    #Find max
    peaks = find_peaks(file, height = 0, prominence = 0.01)
    max_height = peaks[1]['peak_heights'] #array of the heights of  peaks
    pulse = len(peaks[0])/(len(file)/22050)
    return pulse
def partials(file):
    count = 0
    mags, freqs, line = plt.magnitude_spectrum(file, 22050)
    for i in range(len(mags)):
        if i > 100 and i <= (len(mags)-102):
            previous = [mags[i-x] for x in range(1,100)]
            post = [mags[i+x] for x in range(1,100)]
            if mags[i] > max(previous) and mags[i] > max(post) and mags[i] > 0.0005:
                count += 1
    return count
#Pass in a list of tuples, containing two items, the first being a dataframe of the times and the second being the audio file
def audio_feat_extractor(lst_tup):
    data = []
    for tup in lst_tup:
        csv = tup[0]
        audio = tup[1]
        for x, y in csv.iterrows():
            start = math.floor(y[3])
            end = math.ceil(y[4])
            temp = audio[start * 22050 : end *22050]
            zeros = sum(librosa.zero_crossings(temp))
            duration = end - start
            positive = np.absolute(temp)
            amplitude_range = max(positive)-min(positive)
            average_amp = np.mean(positive)
            range_freq = frequency_range(temp)
            pulse = pulses(temp)
            partial = partials(temp)
            mfcc = np.mean(librosa.feature.mfcc(y=temp, sr=22050, n_mfcc=13))
            S, phase = librosa.magphase(librosa.stft(temp))
            rolloff = np.mean(librosa.feature.spectral_rolloff(S=S, sr=22050))
            
            data.append([zeros, duration, amplitude_range, average_amp, range_freq, pulse, partial, mfcc, rolloff, y[1], y[2],
                         y[0], y[5]])
    
    output = pd.DataFrame(data, columns = ['Zero_Crossings', 'Duration', 'Amp_range','Avg_amp', 'Freq_range','Pulses_per_Sec',
                                           'Partials', 'MFCC', 'Spectral Rolloff', 'Sex', 'Age', 'Species', 'Call'])
    
    return output
features_dataframe = audio_feat_extractor(audio_list)
features_dataframe.to_csv('features.csv', index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13717632552404438, 'fraction_numerical': 0.040690505548705305, 'mean_word_length': 2.5896017699115044, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '631186', 'license': 'MIT', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1225, 'n_words': 273, 'path': 'Feature Extraction.ipynb', 'repo_name': 'kexantus/felidetect', 'size': '3244'}