1、matlab 中利用窗函数设计单一频率滤波器的应用实例如下:%本函数利用窗函数法设计带通滤波器,主要用来滤出单一频率,即中心频率%data 是输入的数据, centerFre 是带通的中心频率, offsetFre 是频偏 ,最终带通为 centerFre +- offsetFre/2%,sampFre 是采样率function y = BPassFilter(data, centerFre, offsetFre, sampFre)%设计 I 型带通滤波器M = 0 ; %滤波器阶数(必须是偶数)Ap = 0.82; %通带衰减As = 45; %阻带衰减Wp1 = 2*pi*(centerF
2、re - offsetFre)/sampFre; %算出下边频Wp2 = 2*pi*(centerFre + offsetFre)/sampFre; %算出上边频% (1)矩形窗N = ceil(3.6*sampFre/offsetFre); %计算滤波器阶数,采用矩形窗,3dB 截频在中心频率到上下边频的中点M = N - 1;M = mod(M,2) + M ; %使滤波器为 I 型(偶数)%单位脉冲响应的下脚标h = zeros(1,M+1); %单位冲击响应变量赋初值for k = 1:(M+1);if ( k -1 - 0.5*M)=0)h(k) = Wp2/pi - Wp1/pi;
3、elseh(k) = Wp2*sin(Wp2.*(k - 1 - 0.5*M)/(pi*(Wp2*(k -1 - 0.5*M) - Wp1*sin(Wp1*(k - 1 - 0.5*M)/(pi*(Wp1*(k -1 - 0.5*M);endend% (2) Hann Window% N = ceil(12.4*sampFre/offsetFre); %计算滤波器阶数,采用矩形窗,3dB 截频在中心频率到上下边频的中点% M = N - 1;% M = mod(M,2) + M ; %使滤波器为 I 型( 偶数)% h = zeros(1,M+1); %单位冲击响应变量赋初值% for k =
4、 1:(M+1);% if ( k -1 - 0.5*M)=0)% h(k) = Wp2/pi - Wp1/pi;% else% h(k) = Wp2*sin(Wp2.*(k - 1 - 0.5*M)/(pi*(Wp2*(k -1 - 0.5*M) - Wp1*sin(Wp1*(k - 1 - 0.5*M)/(pi*(Wp1*(k -1 - 0.5*M);% end% end% K = 0:M;% w = 0.5 - 0.5*cos(2*pi*K/M);% h = h.*w;% (3)Hamming Window% N = ceil(14*sampFre/offsetFre); %计算滤波器阶
5、数,采用矩形窗,3dB 截频在中心频率到上下边频的中点% M = N - 1;% M = mod(M,2) + M ; %使滤波器为 I 型( 偶数)% h = zeros(1,M+1); %单位冲击响应变量赋初值% for k = 1:(M+1);% if ( k -1 - 0.5*M)=0)% h(k) = Wp2/pi - Wp1/pi;% else% h(k) = Wp2*sin(Wp2.*(k - 1 - 0.5*M)/(pi*(Wp2*(k -1 - 0.5*M) - Wp1*sin(Wp1*(k - 1 - 0.5*M)/(pi*(Wp1*(k -1 - 0.5*M);% end
6、% end% K = 0:M;% w = 0.54 - 0.46*cos(2*pi*k/M);% h = h.*w;% (4)Blackman window% N = ceil(22.8*sampFre/offsetFre); %计算滤波器阶数,采用矩形窗,3dB 截频在中心频率到上下边频的中点% M = N - 1;% M = mod(M,2) + M ; %使滤波器为 I 型( 偶数)% h = zeros(1,M+1); %单位冲击响应变量赋初值% for k = 1:(M+1);% if ( k -1 - 0.5*M)=0)% h(k) = Wp2/pi - Wp1/pi;% else% h(k) = Wp2*sin(Wp2.*(k - 1 - 0.5*M)/(pi*(Wp2*(k -1 - 0.5*M) - Wp1*sin(Wp1*(k - 1 - 0.5*M)/(pi*(Wp1*(k -1 - 0.5*M);% end% end% K = 0:M;% w = 0.42 - 0.5*cos(2*pi*K/M) + 0.08*cos(4*pi*K/M);% h = h.*w;y = filter(h,1,data);