def calc_power_spectrogram(audio_data, samplerate, n_mels=128, n_fft=512, hop_length=160):
"""
Calculate power spectrogram from the given raw audio data
Args:
audio_data: numpyarray of raw audio wave
samplerate: the sample rate of the `audio_data`
n_mels: the number of mels to generate
n_fft: the window size of the fft
hop_length: the hop length for the window
Returns: the spectrogram in the form [time, n_mels]
"""
spectrogram = librosa.feature.melspectrogram(audio_data, sr=samplerate, n_mels=n_mels, n_fft=n_fft, hop_length=hop_length)
# convert to log scale (dB)
log_spectrogram = librosa.logamplitude(spectrogram, ref_power=np.max)
# normalize
normalized_spectrogram = normalize(log_spectrogram)
return normalized_spectrogram.T
评论列表
文章目录