-
Notifications
You must be signed in to change notification settings - Fork 0
/
glosy.py
135 lines (110 loc) · 3.65 KB
/
glosy.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
133
134
135
from __future__ import division
import scipy.io.wavfile
import struct
from pylab import *
from numpy import *
from scipy import *
from os import listdir
from os.path import isfile, join, splitext
from scikits.audiolab import wavread
def makePlots(samples):
'''makes plots for a collection of samples
argument: dictionary returned by loadFiles
'''
print "drowing plots..."
for s in samples:
print "processing " + s['name']
w = s['sampleRate']
T = 20
n = len(s['signal'])
signal = s['signal'][0:n] # funkcja sprobkowana
print "......fft"
signal1 = fft(signal)
signal1 = abs(signal1)
print "......plot"
freqs = linspace(0, w, n, endpoint=False)
plot(freqs[:int(len(freqs)/2)], signal1[:int(len(freqs)/2)], '-')
xlabel("czestotliwosc probkowania")
ylabel("liczba probek/stala")
xscale('linear', rotation=45)
print "...saveFig"
savefig("plots/" + s['name'] + ".pdf")
def loadFiles(path):
"""reads wave files from path and returns dictionary with fields:
- "name" - name of file
- "nameGender" - a sex readed from filename
- "signal" - numpy array with sound signal readed from file
- "sampleRate" - sample rate of the file
and dictionary that contains numbers of male and female voices
"""
print "reading files..."
files = [ f for f in listdir(path) if isfile(join(path,f)) and splitext(f)[1] == ".wav" ]
samples = []
maleCount = 0
femaleCount = 0
for f in files:
p = path + '/' + f
print "...", f
data,rate,encoding=wavread(p)
sig=[mean(d) for d in data]
samples.append({'name': f, 'nameGender': f[-5:-4], 'signal': sig, 'sampleRate': rate})
if f[-5:-4] == "M":
maleCount += 1
else:
femaleCount += 1
counters = {"maleCount":maleCount, "femaleCount":femaleCount}
return samples, counters
def recognizeGender(sample):
#This function recognizes the sex of person who is speaking
# argument: single sample from dictionary that is returned by loadFiles
# returns: string - 'M' i a man is speaking, 'K' if a woman is speaking
t=3
w=sample['sampleRate']
n=w*t #t*w
signal=sample['signal']
nframe=len(signal)
if n>nframe:
n=nframe
frequency=linspace(0,w,n)
spectrum=fft(signal[0:n])
spectrum=abs(spectrum)
amp,freq=[],[]
for i in range(len(frequency)):
if 85 < frequency[i] < 255:
freq.append(frequency[i])
amp.append(spectrum[i])
index=amp.index(max(amp))
avg_freq=freq[index]
if avg_freq<175:
return 'M'
else:
return 'K'
def launchAlgorithm(samples, counters):
recognizedMale = 0
recognizedFemale = 0
wellRecognized = 0
print "Launching algorithm..."
for s in samples:
gender = recognizeGender(s)
if gender == s['nameGender']:
wellRecognized += 1
if gender == "M":
recognizedMale += 1
elif gender == "K":
recognizedFemale += 1
else:
print "...algorithm returned wrong value: ", s['name']
print "...", s['name'], "...ok!"
else:
print "...", s['name'], "...not so good"
samplesCount = counters['maleCount'] + counters['femaleCount']
print "\nStatistics..."
print "...Well recognized Male: ", recognizedMale, "/", counters['maleCount']
print "...Well recognized Female: ", recognizedFemale, "/", counters['femaleCount']
print "...Total: ", wellRecognized, "/", samplesCount, " (", wellRecognized/samplesCount*100, "%)"
if __name__ == '__main__':
samples, counters = loadFiles("train")
#print samples
print counters
#makePlots(samples)
launchAlgorithm(samples, counters)