-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclassifying-heart-sounds-using-sklearn.py
42 lines (36 loc) · 1.46 KB
/
classifying-heart-sounds-using-sklearn.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
import os
import librosa
import numpy as np
from sklearn import svm
def decodeFolder(category):
print("Starting decoding folder "+category+" ...")
listOfFiles = os.listdir(category)
arrays_sound = np.empty((0,193))
for file in listOfFiles:
filename = os.path.join(category,file)
features_sound = extract_feature(filename)
arrays_sound = np.vstack((arrays_sound,features_sound))
return arrays_sound
def extract_feature(file_name):
print("Extracting "+file_name+" ...")
X, sample_rate = librosa.load(file_name)
stft = np.abs(librosa.stft(X))
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T,axis=0)
chroma = np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
mel = np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
contrast = np.mean(librosa.feature.spectral_contrast(S=stft, sr=sample_rate).T,axis=0)
tonnetz = np.mean(librosa.feature.tonnetz(y=librosa.effects.harmonic(X),sr=sample_rate).T,axis=0)
return np.hstack((mfccs,chroma,mel,contrast,tonnetz))
#train data
normal_sounds = decodeFolder("normal")
normal_labels = [0 for items in normal_sounds]
murmur_sounds = decodeFolder("murmur")
murmur_labels = [1 for items in murmur_sounds]
train_sounds = np.concatenate((normal_sounds, murmur_sounds))
train_labels = np.concatenate((normal_labels, murmur_labels))
#test_data
test_sound = decodeFolder("test")
clf =svm.SVC()
clf.fit(train_sounds,train_labels)
print("training done")
print(clf.predict(test_sound))