-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_extraction.py
88 lines (72 loc) · 2.04 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
import os
import librosa
from librosa import feature
import numpy as np
import csv
DATA_PATH = '/home/konstantis/Nextcloud/ΤΗΜΜΥ/Thesis/Data/ACE/script-output/Dev/Speech'
def walk(dir, RETURN_PATH):
files = []
for p, d, f in os.walk(dir):
for file in f:
if file.endswith('.wav'):
if (RETURN_PATH == True):
files.append(p + '/' + file)
else:
files.append(file)
return files
audio_files = walk(DATA_PATH, RETURN_PATH=True)
filenames = walk(DATA_PATH, RETURN_PATH=False)
fn_list_i = [
feature.chroma_stft,
feature.chroma_cqt,
feature.chroma_cens,
feature.mfcc,
feature.spectral_centroid,
feature.spectral_bandwidth,
feature.spectral_contrast,
feature.spectral_rolloff
]
fn_list_ii = [
feature.rms,
feature.zero_crossing_rate,
feature.spectral_flatness
]
def get_feature_vector(y, sr):
feat_vect_i = [np.mean(funct(y=y, sr=sr)) for funct in fn_list_i]
feat_vect_ii = [np.mean(funct(y=y)) for funct in fn_list_ii]
feature_vector = feat_vect_i + feat_vect_ii
return feature_vector
audio_features = []
for file in audio_files:
y, sr = librosa.load(file, sr=None)
feature_vector = get_feature_vector(y, sr)
audio_features.append(feature_vector)
output_csv = 'audio_features.csv'
header = [
'file',
'filename',
'chroma_stft',
'chroma_cqt',
'chroma_cens',
'mfcc',
'spectral_centroid',
'spectral_bandwidth',
'spectral_contrast',
'spectral_flatness',
'spectral_rolloff',
'rmse',
'zero_crossing_rate'
]
# Append the file names and the full paths to the audio features list that will be written to the csv
index = 0
for sublist in audio_features:
sublist.insert(0, filenames[index])
index += 1
index = 0
for sublist in audio_features:
sublist.insert(0, audio_files[index])
index += 1
with open(output_csv, '+w') as f:
csv_writer = csv.writer(f, delimiter=',')
csv_writer.writerow(header)
csv_writer.writerows(audio_features)