-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStandard_Mel_PCEN_Spectrograms.py
50 lines (43 loc) · 2.27 KB
/
Standard_Mel_PCEN_Spectrograms.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
import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
# Load audio file
y, sr = librosa.load(librosa.ex('robin'))
#############################################################################################
#############################################################################################
#Compute Standard Spectrogram
X_STFT = librosa.stft(y, n_fft = 2048, hop_length=512)
X_log_scale = librosa.power_to_db(np.abs(X_STFT)**2)
#############################################################################################
#############################################################################################
# Compute mel-spectrogram
spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=512)
# The mel-spectrogram computed by librosa.feature.melspectrogram() is already a power
# spectrogram (as it is default power=2.0), so there is no need to square it before computing
# the PCEN spectrogram
log_spectrogram = librosa.power_to_db(spectrogram, ref=np.max)
#############################################################################################
#############################################################################################
# Compute PCEN spectrogram
pcen_spectrogram = librosa.pcen(spectrogram*(2**31), sr=sr)
#############################################################################################
#############################################################################################
# Plot standard spectrogram
plt.figure(figsize=(10, 4))
librosa.display.specshow(X_log_scale, x_axis='time', y_axis='linear', sr=sr, n_fft = 2048, hop_length=512)
plt.colorbar(format='%+2.0f dB')
plt.title('Standard Spectrogram\n(log of squared magnitude)')
plt.tight_layout()
# Plot Mel-spectrogram
plt.figure(figsize=(10, 4))
librosa.display.specshow(log_spectrogram, x_axis='time', y_axis='mel', sr=sr, n_fft = 2048, hop_length=512)
plt.colorbar(format='%+2.0f dB')
plt.title('Mel-spectrogram')
plt.tight_layout()
# Plot PCEN spectrogram
plt.figure(figsize=(10, 4))
librosa.display.specshow(pcen_spectrogram, x_axis='time', y_axis='mel', sr=sr, n_fft = 2048, hop_length=512)
plt.colorbar(format='%+2.0f dB')
plt.title('PCEN Spectrogram')
plt.tight_layout()