自学内容网 自学内容网

语音识别 语音识别项目相关笔记内容

语音识别应用范畴

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

语音识别框架

在这里插入图片描述

语音基本操作

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

使用scipy.io.wavfile读取wav音频文件获取采样率、长度、通道数

import scipy.io.wavfile as wavfile

# 读取 WAV 文件
file_path = 'path_to_your_audio_file.wav'
sampling_rate, data = wavfile.read(file_path)

# 获取采样率
print(f'Sampling Rate: {
     sampling_rate} Hz')

# 获取音频长度
# 音频长度 = 样本数 / 采样率
audio_length = data.shape[0] / sampling_rate
print(f'Audio Length: {
     audio_length:.2f} seconds')

# 获取通道数
# 如果音频是单声道,data.shape 将返回 (样本数,)
# 如果音频是多声道,data.shape 将返回 (样本数, 通道数)
if len(data.shape) == 1:
    channels = 1
else:
    channels = data.shape[1]

print(f'Number of Channels: {
     channels}')

使用numpy读取pcm格式音频文件

在这里插入图片描述

import numpy as np

# 定义PCM文件的路径
pcm_file_path = 'path/to/your/audio.pcm'

# 定义采样率和采样深度
sample_rate = 44100  # 例如,44.1 kHz
sample_depth = 16    # 例如,16-bit PCM

# 读取PCM文件
def read_pcm(file_path, sample_rate, sample_depth):
    # 根据采样深度设置数据类型
    if sample_depth == 16:
        dtype = np.int16
    elif sample_depth == 32:
        dtype = np.int32
    else:
        raise ValueError("Unsupported sample depth: {}".format(sample_depth))

    # 读取二进制PCM数据并转换为NumPy数组
    pcm_data = np.fromfile(file_path, dtype=dtype)

    # 返回音频数据和采样率
    return pcm_data, sample_rate

# 读取PCM文件数据
audio_data, sr = read_pcm(pcm_file_path, sample_rate, sample_depth)

# 打印音频数据和采样率
print("Sample Rate: {} Hz".format(sr))
print("Audio Data: ", audio_data)

在这里插入图片描述

读取wav音频文件,并绘制图像

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

# 读取 WAV 文件
sample_rate, audio_data = wavfile.read('your_audio_file.wav')

# 如果音频是立体声,将其转换为单声道
if audio_data.ndim > 1:
    audio_data = audio_data.mean(axis=1)

# 创建时间轴
time_axis = np.linspace(0, len(audio_data) / sample_rate, num=len(audio_data))

# 绘制音频波形
plt.figure(figsize=(15, 5))
plt.plot(time_axis, audio_data, label='Audio waveform')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Waveform of the audio file')
plt.legend()
plt.show()

读取双声道的wav音频文件,分别绘制不同声道的波形图

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile

# 读取 WAV 文件
sample_rate, audio_data = wavfile.read('your_stereo_audio_file.wav')

# 检查是否为双声道音频
if audio_data.ndim != 2 or audio_data.shape[1] != 2:
    raise ValueError("音频文件不是双声道文件")

# 分离左右声道
left_channel = audio_data[:, 0]
right_channel = audio_data[:, 1]

# 创建时间轴
time_axis = np.linspace(0, len(left_channel) / sample_rate, num=len(left_channel))

# 绘制左声道波形
plt.figure(figsize=(15, 5))
plt.subplot(2, 1, 1)
plt.plot(time_axis, left_channel, label='Left Channel', color='blue')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Left Channel Waveform')
plt.legend()

# 绘制右声道波形
plt.subplot(2, 1, 2)
plt.plot(time_axis, right_channel, label='Right Channel', color='red')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Right Channel Waveform')
plt.legend()

# 显示波形图
plt.tight_layout()
plt.show()

读取一个采样率为16k的音频,分别绘制出其时域与频域的图

import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from scipy.fftpack import fft

# 读取 WAV 文件
sample_rate, audio_data = wavfile.read('your_audio_file.wav')

# 检查采样率是否为16k
if sample_rate != 16000:
    raise ValueError("音频文件的采样率不是16k")

# 如果是立体声,取第一个通道
if audio_data.ndim == 2:
    audio_data = audio_data[:, 0]

# 创建时间轴
time_axis = np.linspace(0, len(audio_data) / sample_rate, num=len(audio_data))

# 绘制时域图
plt.figure(figsize=(12, 6))
plt.subplot(2, 1, 1)
plt.plot(time_axis, audio_data, label='Time Domain', color='blue')
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title('Time Domain Signal')
plt.legend()

# 计算音频的 FFT(快速傅里叶变换)
n = len(audio_data)
audio_fft = fft(audio_data)
audio_fft = np.abs(

原文地址:https://blog.csdn.net/guoqingru0311/article/details/140494995

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!