python(3-1):librosa函数的使用

一、数据手册

函数手册

二、基本使用

基本使用

三、用librosa提取MFCC特征例子

import scipy.io.wavfile as wav
from python_speech_features import mfcc
import librosa
from librosa import display
from matplotlib import pyplot as plt
import numpy as np
# audio
import librosa
import librosa.display

# normalization
import sklearn
import warnings

warnings.filterwarnings("ignore", message="Numerical issues were encountered ")

# 采样率320004s为128000
# 将数据全部调整为4s[0     取前4s]
def load_clip(filename):
    x, sr = librosa.load(filename, sr=32000)
    L = sr * 4 - 1
    signal_length = x.shape[0]
    if signal_length < L:
        # pad by repeating signal
        x = np.pad(x, (0, L - signal_length), mode='wrap')
    elif signal_length > L:
        x = x[:L]
    return x, sr


# 提取MFCC特征
def extract_feature(filename):
    x, sr = load_clip(filename)
    # 信号,采样率,窗口大小,移动长度,MFCC特征数[从一个窗口中获取的,纵长]
    mfccs = librosa.feature.mfcc(y=x, sr=sr, n_fft=1024, hop_length=512, n_mfcc=128)  
    norm_mfccs = sklearn.preprocessing.scale(mfccs, axis=1)
    print(norm_mfccs.shape)
    # out:[128,250]
    print(norm_mfccs.shape[0], norm_mfccs.shape[1])
    norm_mfccs = norm_mfccs.reshape(norm_mfccs.shape[1], norm_mfccs.shape[0])
    print()
    print(norm_mfccs.shape)
    # out 
    print(norm_mfccs.shape[0], norm_mfccs.shape[1])
    # norm_mfccs = norm_mfccs.reshape(1, norm_mfccs.shape[0], norm_mfccs.shape[1], 1)
    # print()
    # print(norm_mfccs.shape)
    return norm_mfccs

# 测试extract_feature是否正常工作

mfccs = extract_feature('./123.wav')
plt.figure(figsize=(20, 5))
librosa.display.specshow(mfccs, sr=32000, x_axis='time', cmap='viridis')
plt.colorbar()
plt.show()
print(mfccs.var(axis=1))
print(mfccs.mean(axis=1))
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值