赞
踩
import numpy as np import glob import librosa import librosa.display import matplotlib.pyplot as plt import cv2 path_dir = 'C:/Users/Chengguo/Desktop/声音_行为/wav/' for path in glob.glob(path_dir+"*.wav"): # path = "C:/Users/Chengguo/Desktop/声音_行为/wav/尖叫.wav" # print(path) for i in range(50): y, sr = librosa.load(path,sr=None,offset=i*1.0, duration=1.0)#librosa.ex('trumpet'))#声音强度、声音采样频率 # print(len(y),sr) # plt.figure(figsize=(16,6)) # librosa.display.waveplot(y, sr=sr) # plt.title('Signal'); # plt.xlabel('Time (samples)') # plt.ylabel('Amplitude') '''FFT变换''' # n_fft = 2048 # ft = np.abs(librosa.stft(y[:n_fft], hop_length = n_fft+1)) # plt.figure() # plt.plot(ft); # plt.title('Spectrum'); # plt.xlabel('Frequency Bin'); # plt.ylabel('Amplitude'); '''stft频谱变换''' # spec = np.abs(librosa.stft(y, hop_length=512)) # spec = librosa.amplitude_to_db(spec, ref=np.max) # plt.figure(figsize=(16,6)) # librosa.display.specshow(spec, sr=sr, x_axis='time', y_axis='log'); # plt.colorbar(format='%+2.0f dB'); # plt.title('Spectrogram'); '''Mel频谱变换''' mel_spect = librosa.feature.melspectrogram(y=y, sr=sr, n_fft=2048, hop_length=1024) mel_spect = librosa.power_to_db((mel_spect), ref=np.max) plt.figure(figsize=(16,8)) # kernel = np.ones((3,3),np.uint8) , # mel_spect = cv2.morphologyEx(mel_spect, cv2.MORPH_OPEN, kernel) librosa.display.TimeFormatter(lag=True) mel_img=librosa.display.specshow(mel_spect, y_axis='mel', x_axis='s')#, fmax=8000 plt.title(f'Mel-Spectrogram-{i*1}') plt.colorbar(mel_img,format='%+2.0f dB') plt.savefig(f"{path[:-4]}/Log-Mel-Spectrogram-{i*1}.png") plt.close()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。