-
Notifications
You must be signed in to change notification settings - Fork 1
/
features.py
67 lines (51 loc) · 2.19 KB
/
features.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
import librosa
import numpy as np
#import pandas as pd
import matplotlib.pyplot as plt
def sample_graph(samples,sample_rate):
fig, ax = plt.subplots(figsize=(10,10))
librosa.display.waveplot(samples, sr=sample_rate)
ax.label_outer()
ax.set(title='Data Respresentation')
plt.show()
def MFCC_graph(samples):
fig, ax = plt.subplots(figsize=(10,10))
img = librosa.display.specshow(samples, x_axis='time', ax=ax)
ax.set(title='MFCC')
ax.label_outer()
plt.show()
def melspectrogram_graph(data):
fig, ax = plt.subplots(figsize=(10,10))
S_dB = librosa.power_to_db(data, ref=np.max)
img = librosa.display.specshow(S_dB, x_axis='time',
y_axis='mel', sr=16000,
fmax=8000, ax=ax)
ax.set(title='Mel-frequency spectrogram')
ax.label_outer()
plt.show()
def poly_graph(data):
fig, ax = plt.subplots(figsize=(10,10))
times = librosa.times_like(data)
ax.plot(times, data[1].T, alpha=0.8, label='Poly Feature')
ax.legend()
ax.label_outer()
plt.show()
def zero_crossing_rate_graph(data):
fig, ax = plt.subplots(figsize=(10,10))
times = librosa.times_like(data)
ax.plot(times, data[0], label='zero crossing rate')
ax.legend()
ax.label_outer()
plt.show()
def mfcc_feature(audio, sample_rate):
mfcc = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
return mfcc # it returns a np.array with size (40,'n') where n is the number of audio frames.
def melspectrogram_feature(audio, sample_rate):
melspectrogram = librosa.feature.melspectrogram(y=audio, sr=sample_rate, n_fft=2048)
return melspectrogram # it returns a np.array with size (128,'n') where n is the number of audio frames.
def poly_feature(audio, sample_rate):
poly_features = librosa.feature.poly_features(y=audio, sr=sample_rate, n_fft=2048)
return poly_features # it returns a np.array with size (2,'n') where n is the number of audio frames.
def zero_crossing_rate_features(audio):
zero_crossing_rate = librosa.feature.zero_crossing_rate(y=audio)
return zero_crossing_rate # it returns a np.array with size (1,'n') where n is the number of audio frames.