matlab处理语音信号(一)

下面是程序的主要部分,总程序可以在我的资源中下载《用matlab处理语音信》。

[y,fs]=audioread('open.wav')
sound(y,fs)
N=length(y)     %信号的长度
t=0:1/fs:(N-1)/fs
figure(1)
subplot(2,2,1)
plot(t,y)       %音频信号时域图
title('音频时域图')
Y=fft(y)
subplot(2,2,2)
df=fs/length(Y) %计算谱线间隔
f=0:df:(fs/2-df) %频谱范围,截取前半段(抽样频率高于最大频率的2倍)
Yf=abs(Y)   %幅度
Yf=Yf(1:length(Yf)/2)   %截取一半
plot(f,Yf)   %音频信号频谱图
title('音频频谱图')

y=y(:,1) %第一列
y=y'
x=0.5*sin(2*pi*500*t)    %500HZ正弦干扰
s=y+x     %加入噪音的音频
sound(s,fs)
subplot(2,2,3)
plot(t,s)   %加入噪声的音频时域图
title('加入噪声的音频')
subplot(2,2,4)
S=fft(s)
Sf=abs(S)   %幅度
Sf=Sf(1:length(Sf)/2)
plot(f,Sf)
title('加入噪声的频谱(500HZ处有脉冲)')

%设计带阻滤波器
fpd=400,fsd=450,fsu=550,fpu=600,As=50
%转换成数字频率
wpd=fpd*2*pi/fs,wsd=fsd*2*pi/fs,wsu=fsu*2*pi/fs,wpu=fpu*2*pi/fs
%计算过滤带宽(与N成反比,因此取小的)
dw=min((wsd-wpd),(wpu-wsu))
%计算上下带中心频率
wcu=(wpu+wsu)/2,wcd=(wsd+wpd)/2
%由于As=50,查表可知选海明窗,计算海明窗的长度
Nh=ceil(6.6*pi/dw)
%由于是带阻滤波器,因此确保Nh为奇数
Nh=Nh+mod(Nh+1,2)
nh=0:Nh-1
%根据带阻与低通滤波器的关系求带阻滤波器的脉冲响应
hd_bs=ideallp(pi,Nh)-ideallp(wcu,Nh)+ideallp(wcd,Nh)
%由于输出是中心值归一化为1的窗函数序列,它是列向量,因此需要转置
w_ham=(hamming(Nh))'
%利用窗函数法计算实际滤波器脉冲响应
h_bs=hd_bs.*w_ham
%计算频率特性
[db,mag,pha,grd,w]=freqz_m(h_bs,[1])
figure(2)
%汉明窗
subplot(3,2,1)
plot(nh,w_ham)
title('汉明窗')
subplot(3,2,2)
plot(w/pi,db)
title('相对幅频响应')
subplot(3,2,3)
plot(w/pi,mag)
title('绝对幅频响应')
subplot(3,2,4)
plot(nh,hd_bs)
title('理想带阻滤波器时域 ')
[NH,whf]=freqz(hd_bs)
subplot(3,2,5)
plot(whf*fs/2/pi,abs(NH))
title('理想带阻滤波器频域')

y_fil=fftfilt(h_bs,s)
sound(y_fil,fs)
figure(3)
Y_fil=abs(fft(y_fil))
Y_fil=Y_fil(1:length(Y_fil)/2)
subplot(2,2,2)
plot(f,Y_fil)
title('去噪频域图')
subplot(2,2,1)
plot(t,y_fil)
title('去噪时域图')

猜你喜欢

转载自blog.csdn.net/jxqbuct/article/details/84917902