-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurbansound8k_spectrogram.py
88 lines (66 loc) · 2.25 KB
/
urbansound8k_spectrogram.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
'''
Created on Dec 18, 2018
Updated on Mar 15th, 2019
@author: Deborah Magalhaes
@collaborators: Flavio Henrique, Myllena, Jederson e Fatima Sombra
'''
import pandas as pd
import numpy as np
import os
import struct
import glob
import re #regex
from scipy.io import wavfile as wav
import subprocess
import librosa
from scipy import signal
import pickle
#Caminho do conjunto de teste e conjunto de treino
path_database = '/data/deborah/UrbanSound8K/teste/'
audio_format = 'wav'
#Parametros do espectrograma
sr=44100
hop_length=1024
n_fft=2048
window = signal.blackmanharris(2048)
n_mels=100
#Listas que armazenarao os espectrogramas e classe de cada audio
specs = []
labels = []
#Função que normaliza os dados do espectrogram
def normalization(data):
data = np.log10(10000*data+1)
data = (data-np.mean(data))/np.std(data)
return data
def run():
#Capturando o nome das pastas
names_folders = glob.glob(path_database + '*')
for names_ in names_folders:
#Capturando o caminho completo do audio
path_audios = glob.glob(names_ + '/*.' + audio_format)
for path_ in path_audios:
#Captura o nome do audio
match_obj = re.sub(names_, "", path_)
match_obj = re.sub(r'/', "", match_obj)
match_obj2 = re.sub(match_obj, "", path_)
#Captura o label da classe
label = int(match_obj.split('-')[1])
os.chdir(match_obj2)
#Captura os primeiros 2.3 segundos do audio
y, sr = librosa.load(match_obj, sr=None,duration=2.3)
#Aplica a transformada STFT
S = librosa.stft(y, n_fft= n_fft, hop_length= hop_length, window=window)
S = np.abs(S)
#Gera o espectrograma com 100 bandas
X = librosa.feature.melspectrogram(y=y, sr=sr, S=S, n_mels=n_mels)
#Aplica a normalizacao dos dados
log_spectrogram = normalization(X)
specs.append(log_spectrogram)
labels.append(label)
os.chdir(path_database)
#Salvando as listas em disco na forma de objetos
with open('log_specs_100_teste.pickle', 'bw') as f:
pickle.dump(specs, f)
with open('labels_100_teste.pickle', 'bw') as f:
pickle.dump(labels, f)
run()