使用 python 进行音频处理
目录
使用 python 进行音频处理 1
实验目的及实验内容 1
实验目的: 1
实验内容: 1
原理分析: 1
实验环境 6
实验步骤及实验过程分析 8
解码结果: 56
实验结果总结 57
实验内容:
学习音频相关知识点,掌握 MFCC 特征提取步骤,使用给定的 chew.wav 音频文件进行特征提取。音频文件在实验群里下载。
部署 KALDI,简要叙述部署步骤运行 yes/no 项目实例,简要解析发音词典内容,画出初步的 WFST 图(按 PPT 里图的形式)。
调整并运行 TIMIT 项目,将命令行输出的过程与 run.sh 各部分进行对应,叙述顶层脚本run.sh 的各部分功能(不需要解析各训练过程的详细原理)。
实验环境
(本次实验所使用的器件、仪器设备等的情况)
处理器:Intel® Core™ i5-9300H CPU @ 2.40GHz 2.40 GHz
操作系统环境:
MFCC 特征提取:Windows 10 家庭中文版 x64 19042.867
Kaldi 的部署及测试:Ubuntu 18.04.5 LTS
编程语言:Python 3.8
其他环境:16 GB 运行内存(物理机),3GB 运行内存(VMWare 虚拟机)
IDE 及包管理器:JetBrains PyCharm 2020.1 x64,anaconda 3 for Windows(conda 4.9.0)
实验结果总结
(对实验结果进行分析,完成思考题目,总结实验的新的体会,并提出实验的改进意见)
librosa 是一个非常强大的 python 语音信号处理的第三方库,学会 librosa 后再也不用用python 去实现那些复杂的算法了,本文转载自http://www.biyezuopin.vip/onews.asp?id=16707只需要一句语句就能轻松实现。虽然如此,还是应该看看相关的文章,理解算法背后的原理。做 MFCC 提取时主要遇到的问题是 librosa 库安装时出现的问题,仅仅使用 pip install librosa 是没办法让程序正常运行的,因为 librosa 实际上还依赖了scipy、audioread、resampy、soundfile 等包,所以在运行前要检查完备再开始动手。
Kaldi 遇到的问题主要是部署的时候出现的,如果能够正常部署,后续应该不会出什么大问题。遇到问题要善于搜索,很多问题都是前人已经踩过的坑。TIMIT 可用的脚本函数库、数据语料语音的组织都有清晰的文件结构,函数脚本调用逻辑清晰,方便学习使用者学习和检索调用;其包含有函数处理流程结果的日志,便于查阅和分析。
虽然实验没有对算法理解做出要求,但多看看相关的博客以及文档、了解一下算法原理还是很有必要的。

# -*- coding: utf-8 -*-

import librosa
import librosa.display
import matplotlib.pyplot as plt
import numpy as np
import sklearn
import os

if __name__ == '__main__':
    save_dir = './imgs'
    if not os.path.exists(save_dir):
        os.mkdir(save_dir)

    file = './data/chew.wav'
    x, sr = librosa.load(file, sr=22050)

    # 波形图 (Waveform)
    librosa.display.waveplot(x, sr=sr)

    tar = save_dir + '/wave.png'
    plt.savefig(tar)
    print('wave picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

    # 声谱图(spectrogram)
    D = librosa.amplitude_to_db(np.abs(librosa.stft(x)), ref=np.max)
    librosa.display.specshow(D, y_axis='linear')
    plt.colorbar(format='%+2.0f dB')
    plt.title('Linear-frequency power spectrogram of aloe')

    tar = save_dir + '/spectrogram.png'
    plt.savefig(tar)
    print('spectrogram picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

    # 过零率 (Zero Crossing Rate)
    zero_crossings = librosa.zero_crossings(x, pad=False)
    print('ALL Zero Crossing Rate: ', sum(zero_crossings))
    limit = (len(x) - 200, len(x) - 100)
    zero_crossings = librosa.zero_crossings(x[limit[0]:limit[1]], pad=False)
    print('Zero Crossing Rate in range({0}, {1}): '.format(limit[0], limit[1]), sum(zero_crossings))

    # 频谱质心 (Spectral Centroid)
    spectral_centroids = librosa.feature.spectral_centroid(x, sr)[0]
    print('Spectral Centroid\'s shape: ', spectral_centroids.shape)
    # 计算时间变量
    frames = range(len(spectral_centroids))
    t = librosa.frames_to_time(frames)
    # 归一化频谱质心
    normalized = sklearn.preprocessing.minmax_scale(spectral_centroids, axis=0)
    librosa.display.waveplot(x, sr=sr, alpha=0.4)
    plt.plot(t, normalized, color='r')

    tar = save_dir + '/Spectral_Centroid.png'
    plt.savefig(tar)
    print('Spectral Centroid picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

    # 声谱衰减 (Spectral Roll-off)
    spectral_rolloff = librosa.feature.spectral_rolloff(x+0.01, sr)[0]
    librosa.display.waveplot(x, sr=sr, alpha=0.4)
    normalized = sklearn.preprocessing.minmax_scale(spectral_rolloff, axis=0)
    plt.plot(t, normalized, color='b')

    tar = save_dir + '/Spectral_Roll-off.png'
    plt.savefig(tar)
    print('Spectral Roll-off picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

    # 色度频率 (Chroma Frequencies)
    hop_length = 512
    chromagram = librosa.feature.chroma_stft(x, sr=sr, hop_length=hop_length)
    plt.figure(figsize=(15, 5))
    librosa.display.specshow(chromagram, x_axis='time', y_axis='chroma',
                             hop_length=hop_length, cmap='coolwarm')

    tar = save_dir + '/Chroma_Frequencies.png'
    plt.savefig(tar)
    print('Chroma Frequencies picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

    # MFCC特征提取 ( Mel Frequency Cepstral Coefficents )
    mfccs = librosa.feature.mfcc(x, sr=sr)
    librosa.display.specshow(mfccs, sr=sr, x_axis='time')
    
    tar = save_dir + '/mfccs.png'
    plt.savefig(tar)
    print('mfcc feature picture has been saved as file: \'{}\''.format(tar))
    plt.show()
    plt.cla()

Python 声音特征处理 python声音分析_python


Python 声音特征处理 python声音分析_声音信号处理_02


Python 声音特征处理 python声音分析_语音信号处理_03


Python 声音特征处理 python声音分析_声音信号处理_04


Python 声音特征处理 python声音分析_声音信号处理_05


Python 声音特征处理 python声音分析_声音信号处理_06


Python 声音特征处理 python声音分析_声音信号处理_07


Python 声音特征处理 python声音分析_声音信号处理_08


Python 声音特征处理 python声音分析_语音特征提取_09


Python 声音特征处理 python声音分析_声音信号处理_10


Python 声音特征处理 python声音分析_声音信号处理_11


Python 声音特征处理 python声音分析_语音信号处理_12


Python 声音特征处理 python声音分析_语音信号处理_13


Python 声音特征处理 python声音分析_声音信号处理_14


Python 声音特征处理 python声音分析_python_15


Python 声音特征处理 python声音分析_语音信号处理_16


Python 声音特征处理 python声音分析_python_17


Python 声音特征处理 python声音分析_语音信号处理_18


Python 声音特征处理 python声音分析_语音特征提取_19


Python 声音特征处理 python声音分析_Python 声音特征处理_20


Python 声音特征处理 python声音分析_python_21


Python 声音特征处理 python声音分析_声音信号处理_22


Python 声音特征处理 python声音分析_Python 声音特征处理_23


Python 声音特征处理 python声音分析_声音信号处理_24


Python 声音特征处理 python声音分析_语音信号处理_25


Python 声音特征处理 python声音分析_Python 声音特征处理_26


Python 声音特征处理 python声音分析_语音特征提取_27


Python 声音特征处理 python声音分析_python_28


Python 声音特征处理 python声音分析_声音信号处理_29