-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmgca.py
95 lines (72 loc) · 2.71 KB
/
mgca.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
#!usr/bin/python
from pysptk.sptk import *
from scipy.signal import hamming
import numpy.matlib
import scipy
import scipy.io.wavfile as wav
import numpy as np
import wave
from python_speech_features.sigproc import *
from math import *
def readWavFile(wav):
#given a path from the keyboard to read a .wav file
#wav = raw_input('Give me the path of the .wav file you want to read: ')
inputWav = '/home/gionanide/Theses_2017-2018_2519/MEEI-RainBow'+wav
return inputWav
#reading the .wav file (signal file) and extract the information we need
def initialize(inputWav):
rate , signal = wav.read(readWavFile(inputWav)) # returns a wave_read object , rate: sampling frequency
sig = wave.open(readWavFile(inputWav))
# signal is the numpy 2D array with the date of the .wav file
# len(signal) number of samples
sampwidth = sig.getsampwidth()
print 'The sample rate of the audio is: ',rate
print 'Sampwidth: ',sampwidth
return signal , rate
#implementation of the low-pass filter
def lowPassFilter(signal, coeff=0.97):
return np.append(signal[0], signal[1:] - coeff * signal[:-1]) #y[n] = x[n] - a*x[n-1] , a = 0.97 , a>0 for low-pass filters
def preEmphasis(wav):
#taking the signal
signal , rate = initialize(wav)
#Pre-emphasis Stage
preEmphasis = 0.97
emphasizedSignal = lowPassFilter(signal)
Time=np.linspace(0, len(signal)/rate, num=len(signal))
EmphasizedTime=np.linspace(0, len(emphasizedSignal)/rate, num=len(emphasizedSignal))
return emphasizedSignal, signal , rate
def writeFeatures(mgca_features,wav):
#write in a txt file the output vectors of every sample
f = open('mel_generalized_features.txt','a')#sample ID
#f = open('mfcc_featuresLR.txt','a')#only to initiate the input for the ROC curve
wav = makeFormat(wav)
np.savetxt(f,mgca_features,newline=",")
f.write(wav)
f.write('\n')
def makeFormat(wav):
#if i want to keep only the gender (male,female)
wav = wav.split('/')[1].split('-')[1]
#only to make the format for Logistic Regression
if (wav=='Female'):
wav='1'
else:
wav='0'
return wav
def mgca_feature_extraction(wav):
#I pre-emphasized the signal with a low pass filter
emphasizedSignal,signal,rate = preEmphasis(wav)
#and now I have the signal windowed
emphasizedSignal*=np.hamming(len(emphasizedSignal))
mgca_features = mgcep(emphasizedSignal,order=12)
writeFeatures(mgca_features,wav)
def mel_Generalized():
folder = raw_input('Give the name of the folder that you want to read data: ')
amount = raw_input('Give the number of samples in the specific folder: ')
print 'Mel-Generalized Cepstrum analysis github implementation '
for x in range(1,int(amount)+1):
wav = '/'+folder+'/'+str(x)+'.wav'
print wav
mgca_feature_extraction(wav)
def main():
mel_Generalized()
main()