-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfeature_description.py
More file actions
310 lines (268 loc) · 9.56 KB
/
Copy pathfeature_description.py
File metadata and controls
310 lines (268 loc) · 9.56 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""
Created on Sat Apr 08 11:48:18 2018
@author: Akshita Gupta
Email - akshitadvlp@gmail.com
"""
import numpy as np
import os
from scipy import signal
from scikits.audiolab import wavread
import librosa
import wavio
def feature_normalize(feature_data):
"""
Input:
Output:
"""
N = feature_data.shape[0]
S1 = np.sum(feature_data, axis=0)
S2 = np.sum(feature_data ** 2, axis=0)
mean=S1/N
std=np.sqrt((N * S2 - (S1 * S1)) / (N * (N - 1)))
mean = np.reshape(mean, [1, -1])
std = np.reshape(std, [1, -1])
feature_data=((feature_data-mean)/std)
return feature_data
def convert_mono(wav,mono):
"""
Input:
Output:
"""
if mono and wav.ndim==2:
return np.mean( wav, axis=-1 )
else:
return wav
def read_audio(Type,path,dataset_type):
"""
Input: 'str','str','str'
Output: np.ndarray, int
"""
if Type == 'wavread':
wav,fs,enc = wavread(path)
return wav,fs,enc
elif Type == 'librosa' and dataset_type == 'chime_2016':
wav,fs = librosa.load(path,sr=16000.)
return wav,fs
elif Type == 'librosa':
wav,fs = librosa.load(path)
return wav,fs
elif Type =='readwav':
Struct = wavio.read( path )
wav = Struct.data.astype(float) / np.power(2, Struct.sampwidth*8-1)
fs = Struct.rate
return wav, fs
else:
print "not listed"
#def set_sampling_rate(sr):
def mel(features,path):
"""
This function extracts mel-spectrogram from audio.
Make sure, you pass a dictionary containing all attributes
and a path to audio.
"""
fsx=features['fs'][0]
n_mels=features['n_mels'][0]
#print n_mels
fmin=features['fmin'][0]
fmax=features['fmax'][0]
mono=features['mono'][0]
hamming_window=features['hamming_window'][0]
noverlap=features['noverlap'][0]
detrend=features['detrend'][0]
return_onesided=features['return_onesided'][0]
mode=features['mode'][0]
wav, fs, enc = read_audio('wavread',path)
#fsx = librosa.resample(wav,fs, 44100)
#wav, fs = librosa.load(path)
wav=convert_mono(wav,mono)
assert fs == fsx
ham_win = np.hamming(hamming_window)
[f, t, X] = signal.spectral.spectrogram(wav,fs, window=ham_win, nperseg=hamming_window, noverlap=noverlap, detrend=detrend, return_onesided=return_onesided, mode=mode )
X = X.T
# define global melW, avoid init melW every time, to speed up.
if globals().get('melW') is None:
global melW
melW = librosa.filters.mel( fs, n_fft=hamming_window, n_mels=n_mels, fmin=fmin, fmax=fmax )
melW /= np.max(melW, axis=-1)[:,None]
X = np.dot( X, melW.T )
X = X[:, 0:]
#X=feature_normalize(X)
return X
def logmel(features,path):
"""
This function extracts log mel-spectrogram from audio.
Make sure, you pass a dictionary containing all attributes
and a path to audio.
"""
fsx=features['fs'][0]
n_mels=features['n_mels'][0]
fmin=features['fmin'][0]
fmax=features['fmax'][0]
mono=features['mono'][0]
hamming_window=features['hamming_window'][0]
noverlap=features['noverlap'][0]
detrend=features['detrend'][0]
return_onesided=features['return_onesided'][0]
mode=features['mode'][0]
#wav, fs, enc = read_audio('wavread',path) for Dcase
wav, fs = read_audio('librosa',path,'chime_2016') # chime 2016 with different sampling rate at development
print "fs before mono",fs #[DEBUG]
wav=convert_mono(wav,mono)
assert fs==fsx #In case of Dcase fs=44100 and chime fs=16000
ham_win = np.hamming(1024)
[f, t, X] = signal.spectral.spectrogram(wav,fs, window=ham_win, nperseg=hamming_window, noverlap=noverlap, detrend=detrend, return_onesided=return_onesided, mode=mode )
X = X.T
# define global melW, avoid init melW every time, to speed up.
if globals().get('melW') is None:
global melW
melW = librosa.filters.mel( fs, n_fft=hamming_window, n_mels=n_mels, fmin=fmin, fmax=fmax )
melW /= np.max(melW, axis=-1)[:,None]
#print "mel"
X = np.dot( X, melW.T )
X = np.log( X + 1e-8)
X = X[:, 0:]
X=feature_normalize(X)
return X
def cqt(features,path):
"""
This function extracts constant q-transform from audio.
Make sure, you pass a dictionary containing all attributes
and a path to audio.
"""
fsx = features['fs'][0]
hop_length = features['hop_length'][0]
n_bins = features['n_bins'][0]
bins_per_octave = features['bins_per_octave'][0]
window = features['window'][0]
mono=features['mono'][0]
wav, fs, enc = read_audio('wavread',path)
wav=convert_mono(wav,mono)
assert fs==fsx
X=librosa.cqt(y=wav, hop_length=hop_length,sr=fs, n_bins=n_bins, bins_per_octave=bins_per_octave,window=window)
X=X.T
X=np.abs(np.log10(X))
return X
#def mfcc(features,path):
import scipy
def spectralCentroid(features,path):
#https://gist.github.com/endolith/359724/aa7fcc043776f16f126a0ccd12b599499509c3cc
fsx = features['fs'][0]
mono=features['mono'][0]
wav, fs, enc = read_audio('wavread',path)
wav=convert_mono(wav,mono)
assert fs==fsx
spectrum = abs(np.fft.rfft(wav))
normalized_spectrum = spectrum / sum(spectrum) # like a probability mass function
normalized_frequencies = np.linspace(0, 1, len(spectrum))
spectral_centroid = sum(normalized_frequencies * normalized_spectrum)
return spectral_centroid
def zcr(features,path):
fsx = features['fs'][0]
mono=features['mono'][0]
# nceps = features['nceps'][0]
frame_length = features['frame_length'][0]
hop_length = features['hop_length'][0]
center = features['center'][0]
pad = features['pad'][0]
wav, fs, enc = read_audio('wavread',path)
wav=convert_mono(wav,mono)
assert fs==fsx
X=librosa.feature.zero_crossing_rate(wav, frame_length=frame_length, hop_length=hop_length, center=center, pad=pad)
X=X.T
return X
def stft(features,path):
fsx = features['fs'][0]
window = features['window'][0]
mono=features['mono'][0]
# noverlap=features['noverlap'][0]
# detrend=features['detrend'][0]
# return_onesided=features['return_onesided'][0]
# nperseg = features['nperseg'][0]
# nfft = features['nfft'][0]
# boundary = features['boundary'][0]
# padded = features['padded'][0]
# axis = features['axis'][0]
wav, fs, enc = read_audio('wavread',path)
wav=convert_mono(wav,mono)
assert fs==fsx
ham_win = np.hamming(1024)
f,t,X = scipy.signal.stft(wav, fs, window=ham_win, nperseg=1024, noverlap=0, nfft=1024, detrend=False, return_onesided=True, boundary='zeros', padded=True, axis=0)
return X
#def spectralFlux(spectra, rectify=False):
# """
# Compute the spectral flux between consecutive spectra
# """
# spectralFlux = []
# Compute flux for zeroth spectrum
# flux = 0
# for bin in spectra[0]:
# flux = flux + abs(bin)
# spectralFlux.append(flux)
# Compute flux for subsequent spectra
# for s in range(1, len(spectra)):
# prevSpectrum = spectra[s - 1]
# spectrum = spectra[s]
# flux = 0
# for bin in range(0, len(spectrum)):
# diff = abs(spectrum[bin]) - abs(prevSpectrum[bin])
#
# # If rectify is specified, only return positive values
# if rectify and diff < 0:
# diff = 0
#
# flux = flux + diff
#
# spectralFlux.append(flux)
#
# return spectralFlux
def SpectralRolloff(features,path):
fsx = features['fs'][0]
mono=features['mono'][0]
noverlap=features['noverlap'][0]
detrend=features['detrend'][0]
return_onesided=features['return_onesided'][0]
mode=features['mode'][0]
# window = features['window'][0]
# noverlap=features['noverlap'][0]
# input_onesided=features['input_onesided'][0]
# nperseg = features['nperseg'][0]
# nfft = features['nfft'][0]
# boundary = features['boundary'][0]
# hop_length = features['hop_length'][0]
# roll_percent = features['roll_percent'][0]
# freq = features['freq'][0]
wav, fs, enc = read_audio('wavread',path)
wav=convert_mono(wav,mono)
print wav.shape
assert fs==fsx
# ham_win = np.hamming(1024)
# stft_matrix = stft(features,path)
# print stft_matrix.shape
ham_win = np.hamming(1024)
[f, t, X] = signal.spectral.spectrogram(wav,fs, window=ham_win, nperseg=1024, noverlap=noverlap, detrend=detrend, return_onesided=return_onesided, mode='psd' )
print X.shape
rolloff = librosa.feature.spectral_rolloff(wav, sr=fs, S=X, n_fft=1024, hop_length=512, freq=None, roll_percent=0.95)
rolloff = rolloff.T
return rolloff
def istft(features,path):
fsx = features['fs'][0]
mono=features['mono'][0]
# window = features['window'][0]
# noverlap=features['noverlap'][0]
# input_onesided=features['input_onesided'][0]
# nperseg = features['nperseg'][0]
# nfft = features['nfft'][0]
# boundary = features['boundary'][0]
# time_axis = features['time_axis'][0]
# freq_axis = features['freq_axis'][0]
#
wav, fs, enc = read_audio('wavread',path)
#wav=convert_mono(wav,mono)
assert fs==fsx
stft_matrix = stft(features,path)
t, X = scipy.signal.istft(stft_matrix, fs, window='hann', nperseg=None, noverlap=None, nfft=None, input_onesided=True, boundary=True, time_axis=-1, freq_axis=-2)
return X
#def neural_feature_extracter():
# hop length = winow length /4
# https://github.com/jsawruk/pymir/blob/master/pymir/SpectralFlux.py
#spectrogram is absolute of stft