1.读取wav文件
# -*- coding: utf-8 -*-
import wave
import pylab as pl
import numpy as np
# 打开WAV文档
f = wave.open(r"c:\WINDOWS\Media\ding.wav", "rb")
# 读取格式信息
# (nchannels, sampwidth, framerate, nframes, comptype, compname)
params = f.getparams()
nchannels, sampwidth, framerate, nframes = params[:4]
# 读取波形数据
str_data = f.readframes(nframes)
f.close()
#将波形数据转换为数组
wave_data = np.fromstring(str_data, dtype=np.short)
wave_data.shape = -1, 2
wave_data = wave_data.T
time = np.arange(0, nframes) * (1.0 / framerate)
# 绘制键闹悉波形
pl.subplot(211)
pl.plot(time, wave_data[0])
pl.subplot(212)
pl.plot(time, wave_data[1], c="g")
pl.xlabel("time (seconds)")
pl.show()
2.观察信号频谱
# -*- coding: utf-8 -*-
import numpy as np
import pylab as pl
sampling_rate = 8000
fft_size = 512
t = np.arange(0, 1.0, 1.0/sampling_rate)
x = np.sin(2*np.pi*156.25*t) + 2*np.sin(2*np.pi*234.375*t)
xs = x[:fft_size]
xf = np.fft.rfft(xs)/fft_size
freqs = np.linspace(0, sampling_rate/2, fft_size/2+1)
xfp = 20*np.log10(np.clip(np.abs(xf), 1e-20, 1e100))
pl.figure(figsize=(8,4))
pl.subplot(211)
pl.plot(t[:fft_size], xs)
pl.xlabel(u"时弯脊间(秒)")
pl.title(u"156.25Hz和234.375Hz的波形和频谱")
pl.subplot(212)
pl.plot(freqs, xfp)
pl.xlabel(u"频率(Hz)")
pl.subplots_adjust(hspace=0.4)
pl.show()
............X=fft(x)X1=fft(x1)% 求信号的频谱
X=X(1:length(X)/2)X1=X1(1:length(X1)/2) % 截取前半部分
deltaf=fs/2/length(X)% 计算频谱的谱线间隔
f=0:deltaf:fs/2-deltaf % 计算频谱信悄猛频率范围
subplot(222)plot(f,abs(X)) % 绘制频谱图
title('语音信号幅度谱图')xlabel('频率(单位:Hz)')ylabel('幅度谱') axis tight
subplot(224)plot(f,abs(X1)) % 绘制运芦频谱图
title('加入干扰后的语音信号幅度谱图')xlabel('频率(单位:Hz)')ylabel('幅度谱') axis tight
PS:这本书还没有出版滑桥,我只能给你透露这么多了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)