【speech】librosa

#调用音频处理库加画图
import librosa
from librosa import display
import numpy as np
import matplotlib.pyplot as plt
#音频播放
import pyaudio
import wave

clean = "/home/sundekai/桌面/clean.wav"
outpath = "/home/sundekai/桌面/out.wav"

#加载音频,x时间序列 sr采样率
x , sr = librosa.load(clean)
print(type(x), type(sr))
print(x.shape, sr)
#时长
print(librosa.get_duration(filename=clean))
'''y=None, sr=22050, S=None, n_fft=2048, hop_length=512, center=True,filename=clean'''
#采样率
print(librosa.get_samplerate(clean))
#输出
librosa.output.write_wav(outpath, x, sr, norm=False)
#过零率
print(librosa.feature.zero_crossing_rate(x, frame_length = 2048, hop_length = 512, center = True))
#画波形图
librosa.display.waveplot(x, sr=22050, x_axis='time', offset=0.0, ax=None)

#短时傅立叶变换(STFT),返回一个复数矩阵使得D(f,t)
#    复数的实部:np.abs(D(f,t))频率的振幅
#    复数的虚部:np.angle(D(f,t))频率的相位
'''
参数:

    y:音频时间序列
    n_fft:FFT窗口大小,n_fft=hop_length+overlapping
    hop_length:帧移,如果未指定,则默认win_length / 4。
    win_length:每一帧音频都由window()加窗。窗长win_length,然后用零填充以匹配N_FFT。默认win_length=n_fft。
    window:字符串,元组,数字,函数 shape =(n_fft, )
        窗口(字符串,元组或数字);
        窗函数,例如scipy.signal.hanning
        长度为n_fft的向量或数组
    center:bool
        如果为True,则填充信号y,以使帧 D [:, t]以y [t * hop_length]为中心。
        如果为False,则D [:, t]从y [t * hop_length]开始
    dtype:D的复数值类型。默认值为64-bit complex复数
    pad_mode:如果center = True,则在信号的边缘使用填充模式。默认情况下,STFT使用reflection padding。

返回:

    STFT矩阵,shape =(1 + $\frac{n_{fft} }{2}$,t)
    '''
stft_matrix = librosa.stft(x, n_fft=2048, hop_length=None, win_length=None, window='hann', center=True, pad_mode='reflect')

'''
短时傅立叶逆变换(ISTFT),将复数值D(f,t)频谱矩阵转换为时间序列y,窗函数、帧移等参数应与stft相同

参数:

    stft_matrix :经过STFT之后的矩阵
    hop_length :帧移,默认为$\frac{win_{length}}{4}$
    win_length :窗长,默认为n_fft
    window:字符串,元组,数字,函数或shape = (n_fft, )
        窗口(字符串,元组或数字)
        窗函数,例如scipy.signal.hanning
        长度为n_fft的向量或数组
    center:bool
        如果为True,则假定D具有居中的帧
        如果False,则假定D具有左对齐的帧
    length:如果提供,则输出y为零填充或剪裁为精确长度音频

返回:

    y :时域信号
'''
librosa.istft(stft_matrix, hop_length=None, win_length=None, window='hann', center=True, length=None)


'''
幅度转dB
将幅度频谱转换为dB标度频谱。也就是对S取对数。与这个函数相反的是librosa.db_to_amplitude(S)

参数:

    S :输入幅度
    ref :参考值,振幅abs(S)相对于ref进行缩放,$20*log_{10}(\frac{S}{ref})$

返回:

    dB为单位的S
'''
librosa.amplitude_to_db(S, ref=1.0)


'''
功率转dB
将功率谱(幅度平方)转换为分贝(dB)单位,与这个函数相反的是librosa.db_to_power(S)

参数:

    S:输入功率
    ref :参考值,振幅abs(S)相对于ref进行缩放,$10*log_{10}(\frac{S}{ref})$

返回:

    dB为单位的S
'''
librosa.core.power_to_db(S, ref=1.0)



plt.show()
plt.figure(figsize=(14, 5))
librosa.display.waveplot(x, sr=sr)


X = librosa.stft(x)
Xdb = librosa.amplitude_to_db(abs(X))
plt.figure(figsize=(14, 5))
librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='hz')
plt.colorbar()

librosa.display.specshow(Xdb, sr=sr, x_axis='time', y_axis='log')
plt.colorbar()


# 1. Get the file path to the included audio example
# Sonify detected beat events
y, sr = librosa.load(librosa.util.example_audio_file())
tempo, beats = librosa.beat.beat_track(y=y, sr=sr)
y_beats = librosa.clicks(frames=beats, sr=sr)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值