使用Python绘制语音信号的波形图

# improt library
import numpy as np
import wave
import pylab as pl
# download open-souce audio in
# http://www.voiptroubleshooter.com/open_speech/american.html


def plot_audio(audio_file='OSR_us_000_0010_8k.wav'):
    # load audio
    audio = wave.open(audio_file, 'rb')

    # read format information
    params = audio.getparams()
    n_channels, _, sample_rate, n_frames = params[:4]

    # read waveform data
    str_wave_data = audio.readframes(n_frames)
    audio.close()

    # tranform waveform to array, then plot
    time = np.arange(0, n_frames) * (1.0 / sample_rate)
    wave_data = np.frombuffer(str_wave_data, dtype=np.short)
    if n_channels == 1:
        wave_data = np.reshape(wave_data, [-1, 1]).T

        pl.subplot(1, 1, 1)
        pl.plot(time, wave_data[0])
        pl.xlabel('time (seconds)')
        pl.ylabel('amplitude')
        pl.show()

    elif n_channels == 2:
        wave_data = np.reshape(wave_data, [-1, 2]).T

        pl.subplot(2, 1, 1)
        pl.plot(time, wave_data[0])
        pl.plot(2, 1, 2)
        pl.plot(time, wave_data[1], c='r')

        pl.xlabel('time (seconds)')
        pl.ylabel('amplitude')
        pl.show()
    else:
        raise ValueError('Only 1 or 2 channels supported!')
使用Python绘制语音信号的波形图_第1张图片

你可能感兴趣的:(语音信号处理,python,pylab,numpy,信号处理)