我正在制作简单的python音频播放器,但我在绘制我的wav文件时遇到问题。
我使用matplotlib和wave
代码段:
spf = wave.open('music.wav','r')
signal = spf.readframes(-1)
signal = np.fromstring(signal, dtype=int, sep=' ')
fs = spf.getframerate()
Time=np.linspace(0, len(signal)/fs, num=len(signal))
plt.figure(1)
plt.title('Signal Wave...')
plt.plot(Time,signal)
plt.show()
程序有效,但图表没有绘制,我认为问题可能在np.fromstring中,但我不太舒服。
答案 0 :(得分:1)
试试这个:
import matplotlib.pyplot as plt
import numpy as np
import wave
spf = wave.open('music.wav','r')
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int8')
plt.figure(1)
plt.title('Signal Wave...')
plt.plot(signal)
plt.show()
时间轴:
import matplotlib.pyplot as plt
import numpy as np
import wave
spf = wave.open('music.wav','r')
signal = spf.readframes(-1)
signal = np.fromstring(signal, 'Int8')
fs = spf.getframerate()
Time=np.linspace(0, len(signal)/fs, num=len(signal))
plt.figure(1)
plt.title('Signal Wave...')
plt.plot(Time,signal)
plt.show()