import librosa
# to install librosa package
# > conda install -c conda-forge librosa
import librosa.display
import IPython.display as ipd
import matplotlib.pyplot as plt
plt.style.use('ggplot')
import numpy as np
filename = 'ClapSound.wav'
newFilename = 'ClapSound_8k.wav'
y, sr = librosa.load(filename, sr=48000)
y_8k = librosa.resample(y,sr,8000)
# print(y.shape, y_8k.shape)
librosa.output.write_wav(newFilename, y_8k, 8000)
ipd.Audio(filename)
ipd.Audio(newFilename)
D = librosa.amplitude_to_db(np.abs(librosa.stft(y)), ref=np.max)
plt.figure(figsize=(20,5))
librosa.display.specshow(D, y_axis='log')
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
plt.show()
D_8k = librosa.amplitude_to_db(np.abs(librosa.stft(y_8k)), ref=np.max)
plt.figure(figsize=(20,5))
librosa.display.specshow(D_8k, y_axis='log')
plt.colorbar(format='%+2.0f dB')
plt.title('Log-frequency power spectrogram')
plt.show()
这里频谱模糊的原因是因为我们降低了采样率,而在进行stft的时候,librosa.stft函数默认窗的长度是一致的,因此每个窗在时域上时间就变长了,使得频谱看起来模糊。