Mercurial > hg > chime-home-dataset-annotation-and-baseline-evaluation-code
view gmm_baseline_experiments/extract_features.py @ 5:b523456082ca tip
Update path to dataset and reflect modified chunk naming convention.
author | peterf |
---|---|
date | Mon, 01 Feb 2016 21:35:27 +0000 |
parents | cb535b80218a |
children |
line wrap: on
line source
# # extract_features.py: # Extract features for a given audio file # # Copyright (c) 2012 Dan Stowell and Queen Mary University of London # Copyright (c) 2014 Peter Foster and Queen Mary University of London # # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. import os.path import numpy as np import external_libs.librosa.librosa as librosa N_MELFILTERS = 40 class FeatureExtractor: def __init__(self, samplingRate=44100, frameLength=1024, hopLength=512): if not(samplingRate == None): self.SAMPLING_RATE = samplingRate if not(frameLength == None): self.FRAME_LENGTH = frameLength if not(hopLength == None): self.HOP_LENGTH = hopLength self.window = np.hamming(self.FRAME_LENGTH) def file_to_features(self, wavpath): "Reads through a mono WAV file, converting each frame to the required features. Returns a 2D array." print('Extracting features for file ' + wavpath) if not os.path.isfile(wavpath): raise ValueError("path %s not found" % wavpath) features = {} #Compute features based on overlapping frames y, sr = librosa.load(wavpath, sr=None) if sr != self.SAMPLING_RATE: raise ValueError("wanted sample rate %g - got %g." % (self.SAMPLING_RATE, sf.samplerate)) STFT = librosa.stft(y, n_fft=self.FRAME_LENGTH, hop_length=self.HOP_LENGTH, window=self.window) magSpectrum = np.abs(STFT) magSpectrum = magSpectrum.astype(np.float32) features['librosa_magspectrum'] = magSpectrum.T features['librosa_melspectrum'] = librosa.feature.melspectrogram(S=magSpectrum ** 2, sr=sr, n_mels=N_MELFILTERS, fmax=sr/2.0).T features['librosa_mfccs'] = librosa.feature.mfcc(S=librosa.logamplitude(features['librosa_melspectrum'].T)).T features['librosa_mfccs'] = features['librosa_mfccs'][:,1:14] return features def files_to_features(self, files): featureList = [self.file_to_features(f) for f in files] return featureList