Last active
August 29, 2018 04:16
-
-
Save rain1024/7c54e81da788c3ba49691af76361852d to your computer and use it in GitHub Desktop.
Feature Extraction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def windows(data, window_size): | |
start = 0 | |
while start < len(data): | |
yield int(start), int(start + window_size) | |
start += (window_size / 2) | |
def extract_features(filepath, label, bands=60, frames=41): | |
window_size = 512 * (frames - 1) | |
log_specgrams = [] | |
labels = [] | |
sound_clip,s = librosa.load(filepath) | |
for (start,end) in windows(sound_clip,window_size): | |
if(len(sound_clip[start:end]) == window_size): | |
signal = sound_clip[start:end] | |
melspec = librosa.feature.melspectrogram(signal, n_mels = bands) | |
logspec = librosa.logamplitude(melspec) | |
logspec = logspec.T.flatten()[:, np.newaxis].T | |
log_specgrams.append(logspec) | |
labels.append(label) | |
log_specgrams = np.asarray(log_specgrams).reshape(len(log_specgrams),bands,frames,1) | |
features = np.concatenate((log_specgrams, np.zeros(np.shape(log_specgrams))), axis = 3) | |
for i in range(len(features)): | |
features[i, :, :, 1] = librosa.feature.delta(features[i, :, :, 0]) | |
return np.array(features), np.array(labels, dtype = np.int) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment