scipy 如何在python中绘制wav文件中的波形?

x4shl7ld  于 2022-11-09  发布在  Python
关注(0)|答案(1)|浏览(222)
import os
import scipy.io
import scipy.io.wavfile
import numpy as np
import matplotlib.pyplot as plt
dataset_path = os.path.join(os.environ['HOME'], 'shared', 'data', 'assignment_1')
wavedata = os.path.join(dataset_path, 'example.wav')

fs, audio_buffer = scipy.io.wavfile.read(wavedata)

我有点不明白如何进一步使用matplotlib。pyplot将感谢任何建议或正确的文档!

h5qlskok

h5qlskok1#

这将把wav音频文件在其固有时间域中绘制为时间序列

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

myAudioFilename = 'aaa.wav'  #  plot this wav file     ~/audio/aaa.wav

dataset_path = os.path.join(os.environ['HOME'], 'audio') # homedir -> audiodir -> my wav files
wavedata = os.path.join(dataset_path, myAudioFilename)

sampleRate, audioBuffer = scipy.io.wavfile.read(wavedata)

duration = len(audioBuffer)/sampleRate

time = np.arange(0,duration,1/sampleRate) #time vector

plt.plot(time,audioBuffer)
plt.xlabel('Time [s]')
plt.ylabel('Amplitude')
plt.title(myAudioFilename)
plt.show()

请注意,该图准确地呈现了从定义采样率的WAV文件头导出的时间......沿着位深度和通道计数......这些属性给予代码能够逐字节解析二进制WAV文件,这些文件在显示的曲线上呈现为一系列点(每个点都是给定通道的音频样本)

相关问题