**********e_STFT_librosa文件:********** import librosa import numpy as np from plot_set import plot_waveform,plot_spectrogram,plot_waveform_fft # 1.加载信号 wave_path=r"C:\Users\LENOVO\PycharmProjects\pythonProject3\Audio_Data\musicstyle\eletric_music.mp3" waveform,sr=librosa.load(wave_path,sr=None) # 2.设置参数,调用librosa.stft函数 n_fft = 1024 hop_size = 512 # def stft(y: ndarray, # *, # n_fft: int = 2048, # hop_length: Optional[int] = None, # win_length: Optional[int] = None, # window: Union[str, Tuple[Any, ...], float, (int) -> ndarray, _SupportsArray[dtype], _NestedSequence[_SupportsArray[dtype]], bool, int, complex, bytes, _NestedSequence[Union[bool, int, float, complex, str, bytes]]] = "hann", # center: bool = True, # dtype: Union[dtype, None, type, _SupportsDType[dtype], str, Tuple[Any, int], Tuple[Any, Union[SupportsIndex, Sequence[SupportsIndex]]], list, _DTypeDict, Tuple[Any, Any]] = None, # pad_mode: Union[Literal["constant", "edge", "linear_ramp", "reflect", "symmetric", "empty"], (...) -> Any] = "constant", # out: Optional[ndarray] = None) -> ndarray waveform_stft = librosa.stft(y=waveform,n_fft=n_fft,hop_length=hop_size) # 3.创建一个独立的绘图文件,方便调用 # -----> plot.set文件里 # 4.绘制stft plot_waveform(waveform,sr) plot_waveform_fft(waveform,sr,n_fft=1024) plot_spectrogram(np.abs(waveform_stft)) print("Congratulation!") **********plot_set文件:**********
# 画图 import librosa import numpy as np from matplotlib import pyplot as plt def plot_waveform(waveform,sr,title="waveform"): waveform = np.array(waveform) samples = waveform.size time_scale= np.linspace(0,samples/sr,num=samples) # np.linspace(2.0, 3.0, num=5) array([2., 2.25, 2.5 , 2.75, 3.]) plt.figure(figsize=(20,10)) plt.plot(time_scale,waveform,linewidth=1) plt.title(title) plt.grid(True) # 网格 plt.show() def plot_waveform_fft(waveform,sr,n_fft,title="waveform_ft"): waveform = np.array(waveform) samples = waveform.size waveform_fft = np.fft.rfft(waveform,n_fft) freq_scale=np.linspace(0,sr/2,num=int(n_fft/2)+1) plt.figure(figsize=(20,10)) plt.plot(freq_scale,waveform_fft,linewidth = 1) plt.title(title) plt.grid(True) plt.show() def plot_spectrogram(spectrogram,title="spectrogram(dB)"): plt.imshow(librosa.amplitude_to_db(spectrogram)) plt.title(title) plt.xlabel("frame/s") plt.ylabel("frequency/Hz") plt.colorbar() plt.show()