-
Notifications
You must be signed in to change notification settings - Fork 0
/
boy_vs_girl_knn_fft_training.py
executable file
·132 lines (117 loc) · 4.08 KB
/
boy_vs_girl_knn_fft_training.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# import libraries
from __future__ import print_function
import scipy.io.wavfile as wavfile
import scipy
import scipy.fftpack
import numpy as np
from matplotlib import pyplot as plt
from sklearn import svm
import os
import pickle
from sklearn.neighbors import KNeighborsClassifier
# use array for data manipulation
knndata = []
label = []
# looping of the dataset folder
folder = r"D:\Projects\Thesis\Dataset\BOY"
for filename in os.listdir(folder):
# reading wav file in the dataset
fs_rate, signal = wavfile.read(os.path.join(folder, filename))
print("Frequency sampling", fs_rate)
# resizing the signal length
signal = np.resize(signal, (130000, 2))
l_audio = len(signal.shape)
print("Channels", l_audio)
if l_audio == 2:
signal = signal.sum(axis=1) / 2
N = signal.shape[0]
print("Complete Samplings N", N)
secs = N / float(fs_rate)
print("secs", secs)
# sampling interval in time
Ts = 1.0 / fs_rate
print("Timestep between samples Ts", Ts)
# time vector as scipy arange field / numpy.ndarray
t = np.arange(0, secs, Ts)
t = np.resize(t, (130000,))
FFT = abs(scipy.fft.fft(signal))
FHT = scipy.fft.fft(signal) * np.array([1 + 1j])
# one side FFT range
FFT_side = FFT[range(N // 2)]
freqs = scipy.fftpack.fftfreq(signal.size, t[1] - t[0])
fft_freqs = np.array(freqs)
freqs_side = freqs[range(N // 2)]
# one side frequency range
fft_freqs_side = np.array(freqs_side)
# uncomment this part to show the plots using matplotlib
'''plt.subplot(311)
p1 = plt.plot(t, signal, "g")
# plotting the signal
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(312)
# plotting the complete fft spectrum
p2 = plt.plot(freqs, FFT, "r")
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.subplot(313)
# plotting the positive fft spectrum
p3 = plt.plot(freqs, abs(FHT.real), "b")
plt.xlabel('Frequency (Hz)')
plt.ylabel('Magnitude')
plt.show()'''
# append the data in one single array
# we use the Fast Fourier Transform
knndata.append(FFT)
folder = r"D:\Projects\Thesis\Dataset\GIRL"
for filename in os.listdir(folder):
fs_rate, signal = wavfile.read(os.path.join(folder, filename))
print("Frequency sampling", fs_rate)
signal = np.resize(signal, (130000, 2))
l_audio = len(signal.shape)
print("Channels", l_audio)
if l_audio == 2:
signal = signal.sum(axis=1) / 2
N = signal.shape[0]
print("Complete Samplings N", N)
secs = N / float(fs_rate)
print("secs", secs)
Ts = 1.0 / fs_rate
print("Timestep between samples Ts", Ts)
t = np.arange(0, secs, Ts)
t = np.resize(t, (130000,))
FFT = abs(scipy.fft.fft(signal))
FHT = scipy.fft.fft(signal) * np.array([1 + 1j])
FFT_side = FFT[range(N // 2)]
freqs = scipy.fftpack.fftfreq(signal.size, t[1] - t[0])
fft_freqs = np.array(freqs)
freqs_side = freqs[range(N // 2)]
fft_freqs_side = np.array(freqs_side)
'''plt.subplot(311)
p1 = plt.plot(t, signal, "g")
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.subplot(312)
p2 = plt.plot(freqs, FFT, "r")
plt.xlabel('Frequency (Hz)')
plt.ylabel('Count dbl-sided')
plt.subplot(313)
p3 = plt.plot(freqs, abs(FHT.real), "b")
plt.xlabel('Frequency (Hz)')
plt.ylabel('Count single-sided')
plt.show()'''
knndata.append(FFT)
# labels used for prediction
for y in range(0, 16):
label.append('Boy')
for y in range(0, 16):
label.append('Girl')
# set the KNN Classifier and it's parameters (you can adjust the parameters as you wish)
classifier = KNeighborsClassifier(n_neighbors=5, metric = 'minkowski', p = 2)
classifier.fit(knndata,label)
decision_tree_pkl_filename = 'boy_vs_girl_knn_fft_model.pkl'
# Open the file to save as pkl file
decision_tree_model_pkl = open(decision_tree_pkl_filename, 'wb')
pickle.dump(classifier, decision_tree_model_pkl)
# Close the pickle instances
decision_tree_model_pkl.close()