作业帮 > 综合 > 作业

用matlab设计滤波器

来源:学生作业帮 编辑:搜搜考试网作业帮 分类:综合作业 时间:2024/06/28 21:37:41
用matlab设计滤波器
某合成信号,表达式如下:f=10cos(2pi*30t)+cos(2pi*150t)+5cos(2pi*600t),请设计三个滤波器,分别提取出信号中各频率分量,并分别绘制出通过这三个滤波器后信号的时域波形和频谱.
用matlab设计滤波器
这个信号的频率分量分别为30、150和600Hz,因此可分别设计一个低通、带通和高通的滤波器来提取.以FIR滤波器为例,程序如下:
clear;fs=2000;t=(1:1000)/fs;
x=10*cos(2*pi*30*t)+cos(2*pi*150*t)+5*cos(2*pi*600*t);
L=length(x);N=2^(nextpow2(L));Hw=fft(x,N);
figure(1);subplot(2,1,1);plot(t,x);
grid on;title('滤波前信号x');xlabel('时间/s');% 原始信号
subplot(2,1,2);plot((0:N-1)*fs/L,abs(Hw));% 查看信号频谱
grid on;title('滤波前信号频谱图');xlabel('频率/Hz');ylabel('振幅|H(e^jw)|');
%% x_1=10*cos(2*pi*30*t)
Ap=1;As=60;% 定义通带及阻带衰减
dev=[(10^(Ap/20)-1)/(10^(Ap/20)+1),10^(-As/20)];% 计算偏移量
mags=[1,0];% 低通
fcuts=[60,100];% 边界频率
[N,Wn,beta,ftype]=kaiserord(fcuts,mags,dev,fs);% 估算FIR滤波器阶数
hh1=fir1(N,Wn,ftype,kaiser(N+1,beta));% FIR滤波器设计
x_1=filter(hh1,1,x);% 滤波
x_1(1:ceil(N/2))=[];% 群延时N/2,删除无用信号部分
L=length(x_1);N=2^(nextpow2(L));Hw_1=fft(x_1,N);
figure(2);subplot(2,1,1);plot(t(1:L),x_1);
grid on;title('x_1=10*cos(2*pi*30*t)');xlabel('时间/s');
subplot(2,1,2);plot((0:N-1)*fs/L,abs(Hw_1));% 查看信号频谱
grid on;title('滤波后信号x_1频谱图');xlabel('频率/Hz');ylabel('振幅|H(e^jw)|');
%% x_2=cos(2*pi*150*t)
Ap=1;As=60;% 定义通带及阻带衰减
dev=[10^(-As/20),(10^(Ap/20)-1)/(10^(Ap/20)+1),10^(-As/20)];% 计算偏移量
mags=[0,1,0];% 带通
fcuts=[80,120,180,220];% 边界频率
[N,Wn,beta,ftype]=kaiserord(fcuts,mags,dev,fs);% 估算FIR滤波器阶数
hh2=fir1(N,Wn,ftype,kaiser(N+1,beta));% FIR滤波器设计
x_2=filter(hh2,1,x);% 滤波
x_2(1:ceil(N/2))=[];% 群延时N/2,删除无用信号部分
L=length(x_2);N=2^(nextpow2(L));Hw_2=fft(x_2,N);
figure(3);subplot(2,1,1);plot(t(1:L),x_2);
grid on;title('x_2=cos(2*pi*150*t)');xlabel('时间/s');
subplot(2,1,2);plot((0:N-1)*fs/L,abs(Hw_2));% 查看信号频谱
grid on;title('滤波后信号x_2频谱图');xlabel('频率/Hz');ylabel('振幅|H(e^jw)|');
%% x_3=5*cos(2*pi*600*t)
Ap=1;As=60;% 定义通带及阻带衰减
dev=[10^(-As/20),(10^(Ap/20)-1)/(10^(Ap/20)+1)];% 计算偏移量
mags=[0,1];% 高通
fcuts=[500,550];% 边界频率
[N,Wn,beta,ftype]=kaiserord(fcuts,mags,dev,fs);% 估算FIR滤波器阶数
hh2=fir1(N,Wn,ftype,kaiser(N+1,beta));% FIR滤波器设计
x_3=filter(hh2,1,x);% 滤波
x_3(1:ceil(N/2))=[];% 群延时N/2,删除无用信号部分
L=length(x_3);N=2^(nextpow2(L));Hw_3=fft(x_3,N);
figure(4);subplot(2,1,1);plot(t(1:L),x_3);
grid on;title('x_3=5*cos(2*pi*600*t)');xlabel('时间/s');
subplot(2,1,2);plot((0:N-1)*fs/L,abs(Hw_3));% 查看信号频谱
grid on;title('滤波后信号x_3频谱图');xlabel('频率/Hz');ylabel('振幅|H(e^jw)|');