音频&深度学习Lesson6_时域,频域,时频域分析的绘图调用函数


**********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()

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值