-
Notifications
You must be signed in to change notification settings - Fork 39
/
feature_extraction.py
89 lines (69 loc) · 3.67 KB
/
feature_extraction.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import os
import numpy as np
import pandas as pd
import scipy
import librosa
from tqdm import tqdm
WAV_DIR = 'wav_files/'
wav_files = os.listdir(WAV_DIR)
col_names = ['file_name', 'signal_mean', 'signal_std', 'signal_skew', 'signal_kurtosis',
'zcr_mean', 'zcr_std', 'rmse_mean', 'rmse_std', 'tempo',
'spectral_centroid_mean', 'spectral_centroid_std',
'spectral_bandwidth_2_mean', 'spectral_bandwidth_2_std',
'spectral_bandwidth_3_mean', 'spectral_bandwidth_3_std',
'spectral_bandwidth_4_mean', 'spectral_bandwidth_4_std'] + \
['spectral_contrast_' + str(i+1) + '_mean' for i in range(7)] + \
['spectral_contrast_' + str(i+1) + '_std' for i in range(7)] + \
['spectral_rolloff_mean', 'spectral_rolloff_std'] + \
['mfccs_' + str(i+1) + '_mean' for i in range(20)] + \
['mfccs_' + str(i+1) + '_std' for i in range(20)] + \
['chroma_stft_' + str(i+1) + '_mean' for i in range(12)] + \
['chroma_stft_' + str(i+1) + '_std' for i in range(12)]
df = pd.DataFrame(columns=col_names)
for f in tqdm(wav_files):
try:
# Read wav-file
y, sr = librosa.load(WAV_DIR+f, sr = 22050)
feature_list = [f]
feature_list.append(np.mean(abs(y)))
feature_list.append(np.std(y))
feature_list.append(scipy.stats.skew(abs(y)))
feature_list.append(scipy.stats.kurtosis(y))
zcr = librosa.feature.zero_crossing_rate(y + 0.0001, frame_length=2048, hop_length=512)[0]
feature_list.append(np.mean(zcr))
feature_list.append(np.std(zcr))
rmse = librosa.feature.rmse(y + 0.0001)[0]
feature_list.append(np.mean(rmse))
feature_list.append(np.std(rmse))
tempo = librosa.beat.tempo(y, sr=sr)
feature_list.extend(tempo)
spectral_centroids = librosa.feature.spectral_centroid(y+0.01, sr=sr)[0]
feature_list.append(np.mean(spectral_centroids))
feature_list.append(np.std(spectral_centroids))
spectral_bandwidth_2 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=2)[0]
spectral_bandwidth_3 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=3)[0]
spectral_bandwidth_4 = librosa.feature.spectral_bandwidth(y+0.01, sr=sr, p=4)[0]
feature_list.append(np.mean(spectral_bandwidth_2))
feature_list.append(np.std(spectral_bandwidth_2))
feature_list.append(np.mean(spectral_bandwidth_3))
feature_list.append(np.std(spectral_bandwidth_3))
feature_list.append(np.mean(spectral_bandwidth_3))
feature_list.append(np.std(spectral_bandwidth_3))
spectral_contrast = librosa.feature.spectral_contrast(y, sr=sr, n_bands = 6, fmin = 200.0)
feature_list.extend(np.mean(spectral_contrast, axis=1))
feature_list.extend(np.std(spectral_contrast, axis=1))
spectral_rolloff = librosa.feature.spectral_rolloff(y+0.01, sr=sr, roll_percent = 0.85)[0]
feature_list.append(np.mean(spectral_rolloff))
feature_list.append(np.std(spectral_rolloff))
mfccs = librosa.feature.mfcc(y, sr=sr, n_mfcc=20)
feature_list.extend(np.mean(mfccs, axis=1))
feature_list.extend(np.std(mfccs, axis=1))
chroma_stft = librosa.feature.chroma_stft(y, sr=sr, hop_length=1024)
feature_list.extend(np.mean(chroma_stft, axis=1))
feature_list.extend(np.std(chroma_stft, axis=1))
feature_list[1:] = np.round(feature_list[1:], decimals=3)
except:
pass
df = df.append(pd.DataFrame(feature_list, index=col_names).transpose(), ignore_index=True)
# Save file
df.to_csv('df_features.csv', index=False)