【Matlab】用Matlab设计一个滤波器

程序1:
fs=22050;                  %语音信号采样频率为22050
x1=wavread('Windows Critical Stop.wav'); %读取语音信号的数据,赋给变量x1
sound(x1,22050);           %播放语音信号
y1=fft(x1,1024);           %对信号做1024点FFT变换
f=fs*(0:511)/1024;
figure(1)
plot(x1)                   %做原始语音信号的时域图形
title('原始语音信号');
xlabel('time n');
ylabel('fuzhi n');

figure(2)
freqz(x1)                  %绘制原始语音信号的频率响应图
title('频率响应图')

figure(3)
subplot(2,1,1);
plot(abs(y1(1:512)))       %做原始语音信号的FFT频谱图
title('原始语音信号FFT频谱')
subplot(2,1,2);
plot(f,abs(y1(1:512)));
title('原始语音信号频谱')
xlabel('Hz');
ylabel('fuzhi');

程序2:
fs=22050;                  %语音信号采样频率为22050
x1=wavread('Windows Critical Stop.wav'); %读取语音信号的数据,赋给变量x1
t=0:1/22050:(size(x1)-1)/22050;
y1=fft(x1,1024);           %对信号做1024点FFT变换
f=fs*(0:511)/1024;
x2=randn(1,length(x1));   %产生一与x长度一致的随机信号
sound(x2,22050);
figure(1)
plot(x2)                   %做原始语音信号的时域图形
title('高斯随机噪声');
xlabel('time n');
ylabel('fuzhi n');


randn('state',0);
m=randn(size(x1));
x2=0.1*m+x1;

sound(x2,22050);%播放加噪声后的语音信号
y2=fft(x2,1024);
figure(2)
plot(t,x2)
title('加噪后的语音信号');
xlabel('time n');
ylabel('fuzhi n');
figure(3)
subplot(2,1,1);
plot(f,abs(y2(1:512)));
title('原始语音信号频谱');
xlabel('Hz');
ylabel('fuzhi');
subplot(2,1,2);
plot(f,abs(y2(1:512)));
title('加噪后的语音信号频谱');
xlabel('Hz');
ylabel('fuzhi');

根据以上代码,你可以修改下面有错误的代码
程序3:双线性变换法设计Butterworth滤波器

fs=22050;
x1=wavread('h:\课程设计2\shuzi.wav');
t=0:1/22050:(size(x1)-1)/22050;
Au=0.03;
d=[Au*cos(2*pi*5000*t)]';
x2=x1+d;
wp=0.25*pi;
ws=0.3*pi;
Rp=1;
Rs=15;
Fs=22050;
Ts=1/Fs;
wp1=2/Ts*tan(wp/2);                 %将模拟指标转换成数字指标
ws1=2/Ts*tan(ws/2);
[N,Wn]=buttord(wp1,ws1,Rp,Rs,'s');  %选择滤波器的最小阶数
[Z,P,K]=buttap(N);                  %创建butterworth模拟滤波器
[Bap,Aap]=zp2tf(Z,P,K);
[b,a]=lp2lp(Bap,Aap,Wn);  
[bz,az]=bilinear(b,a,Fs);           %用双线性变换法实现模拟滤波器到数字滤波器的转换
[H,W]=freqz(bz,az);                 %绘制频率响应曲线
figure(1)
plot(W*Fs/(2*pi),abs(H))
grid
xlabel('频率/Hz')
ylabel('频率响应幅度')
title('Butterworth')
f1=filter(bz,az,x2);
figure(2)
subplot(2,1,1)
plot(t,x2)                          %画出滤波前的时域图
title('滤波前的时域波形');
subplot(2,1,2)
plot(t,f1);                         %画出滤波后的时域图
title('滤波后的时域波形');
sound(f1,22050);                    %播放滤波后的信号
F0=fft(f1,1024);
f=fs*(0:511)/1024;
figure(3)
y2=fft(x2,1024);
subplot(2,1,1);
plot(f,abs(y2(1:512)));             %画出滤波前的频谱图
title('滤波前的频谱')
xlabel('Hz');
ylabel('fuzhi');
subplot(2,1,2)
F1=plot(f,abs(F0(1:512)));          %画出滤波后的频谱图
title('滤波后的频谱')
xlabel('Hz');
ylabel('fuzhi');

程序4:窗函数法设计滤波器:

fs=22050;
x1=wavread('h:\课程设计2\shuzi.wav');
t=0:1/22050:(size(x1)-1)/22050;
Au=0.03;
d=[Au*cos(2*pi*5000*t)]';
x2=x1+d;
wp=0.25*pi;
ws=0.3*pi;
wdelta=ws-wp;
N=ceil(6.6*pi/wdelta);              %取整
wn=(0.2+0.3)*pi/2;
b=fir1(N,wn/pi,hamming(N+1));       %选择窗函数,并归一化截止频率
figure(1)
freqz(b,1,512)
f2=filter(bz,az,x2)
figure(2)
subplot(2,1,1)
plot(t,x2)
title('滤波前的时域波形');
subplot(2,1,2)
plot(t,f2);
title('滤波后的时域波形');
sound(f2,22050);                    %播放滤波后的语音信号
F0=fft(f2,1024);
f=fs*(0:511)/1024;
figure(3)
y2=fft(x2,1024);
subplot(2,1,1);
plot(f,abs(y2(1:512)));
title('滤波前的频谱')
xlabel('Hz');
ylabel('fuzhi');
subplot(2,1,2)
F2=plot(f,abs(F0(1:512)));
title('滤波后的频谱')
xlabel('Hz');
ylabel('fuzhi');

原文地址:https://www.cnblogs.com/xianghang123/p/1728885.html