-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathfeatureExtraction.py
More file actions
43 lines (34 loc) · 1.41 KB
/
Copy pathfeatureExtraction.py
File metadata and controls
43 lines (34 loc) · 1.41 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
# -*- coding: utf-8 -*-
import librosa
from madmom.audio.signal import *
from pathlib import Path
import matplotlib.pyplot as plt
def spec_extraction(file_name, win_size):
currentFilePath = str(Path(__file__).resolve().parent)
# print(currentFilePath)
x_test = []
# y, sr = librosa.load(file_name, sr=8000)
# *********** madmom.Signal() is faster than librosa.load() ***********
y = Signal(file_name, sample_rate=8000, dtype=np.float32, num_channels=1)
S = librosa.core.stft(y, n_fft=1024, hop_length=80*1, win_length=1024)
x_spec = np.abs(S)
x_spec = librosa.core.power_to_db(x_spec, ref=np.max)
x_spec = x_spec.astype(np.float32)
num_frames = x_spec.shape[1]
# for padding
padNum = num_frames % win_size
if padNum != 0:
len_pad = win_size - padNum
padding_feature = np.zeros(shape=(513, len_pad))
x_spec = np.concatenate((x_spec, padding_feature), axis=1)
num_frames = num_frames + len_pad
for j in range(0, num_frames, win_size):
x_test_tmp = x_spec[:, range(j, j + win_size)].T
x_test.append(x_test_tmp)
x_test = np.array(x_test)
# for normalization
x_train_mean = np.load(currentFilePath+'/x_data_mean_total_31.npy')
x_train_std = np.load(currentFilePath+'/x_data_std_total_31.npy')
x_test = (x_test-x_train_mean)/(x_train_std+0.0001)
x_test = x_test[:, :, :, np.newaxis]
return x_test, x_spec