利用Matlab工具箱设计FIR和IIR滤波器

FIR低通滤波器设计

在这里插入图片描述

生成系数

const int BL = 464;
const real32_T B[464] = {
  8.669180534e-05,2.788068377e-05,3.218713755e-05,3.677681525e-05,4.165340579e-05,
  4.677322067e-05,5.213804616e-05,5.771249926e-05,6.350613694e-05,6.947331713e-05,
  7.558634388e-05,8.175626863e-05,8.795419853e-05,9.414774831e-05, 0.000100335521,
  0.0001063660166,0.0001121259702,0.0001177662925,0.0001229849877,0.0001278363488,
  0.0001321963355,0.0001359695016, 0.000139088399,0.0001414458675,0.0001429676922,
   0.000143528363,0.0001430470584,0.0001414164872,0.0001385670912,0.0001343660115,
  0.0001287219784,0.0001215434168,0.0001127589858,0.0001022133001,8.989949129e-05,
  7.565880514e-05,5.946325473e-05,4.121305028e-05,2.086819586e-05,-1.650866693e-06,
  -2.637655598e-05,-5.336063987e-05,-8.260838513e-05,-0.0001141595858,-0.0001480082574,
  -0.0001841588528,-0.0002225704229,-0.0002632375981,-0.000306090049,-0.0003510724055,
  -0.0003981153714,-0.0004471126013,-0.0004979564692,-0.0005505274748,-0.0006046761409,
  -0.0006602567737,-0.0007170780445,-0.0007749674842,-0.000833709084,-0.0008930958575,
  -0.0009528772207, -0.00101282273,-0.001072656945,-0.001132114441,-0.001190904528,
  -0.001248744782, -0.00130530668,-0.001360305469,-0.001413399237,-0.001464285771,
  -0.001512619434,-0.001558086486, -0.00160034874,-0.001639090595,-0.001673978404,
  -0.001704707742,-0.001730960212,-0.001752444659,-0.001768867252,-0.001779967919,
  -0.001785474597,-0.001785159926,-0.001778801437, -0.00176620367,-0.001747193863,
  -0.001721625566,-0.001689378405,-0.001650367165,-0.001604527468, -0.00155184092,
  -0.001492309268,-0.001425981056, -0.00135293114,-0.001273284783,-0.001187188784,
  -0.001094840467,-0.0009964710334,-0.0008923534187,-0.0007827921654,-0.0006681427476,
  -0.0005487820017,-0.0004251426435,-0.0002976757532,-0.0001668831537,-3.328824823e-05,
  0.0001025461825,0.0002400322992,0.0003785476729,  0.00051745167,0.0006560774054,
   0.000793737534,0.0009297264041, 0.001063329517, 0.001193814795,  0.00132044882,
   0.001442493289, 0.001559207099, 0.001669859863, 0.001773722004,  0.00187008502,
   0.001958250301, 0.002037543571, 0.002107313601, 0.002166942693, 0.002215842018,
   0.002253463026, 0.002279298147, 0.002292887308, 0.002293816069, 0.002281730529,
   0.002256325213, 0.002217361704,  0.00216466072, 0.002098112367, 0.002017674735,
   0.001923376112, 0.001815317664, 0.001693679485, 0.001558712334,  0.00141075009,
   0.001250198926, 0.001077550347,0.0008933648933,0.0006982916384,0.0004930471187,
  0.0002784299722,5.530676208e-05,-0.0001753771503,-0.0004126156273,-0.000655326643,
  -0.0009023782331,-0.001152564189,-0.001404638402,-0.001657290966,-0.001909178798,
  -0.002158906078,  -0.0024050551,-0.002646166366,-0.002880767686,-0.003107361728,
  -0.003324450692,-0.003530521179,-0.003724076552,-0.003903616453,-0.004067672882,
  -0.004214789253,-0.004343551584,-0.004452575929,-0.004540533293, -0.00460613938,
  -0.004648179282,-0.004665495828,-0.004657014739,-0.004621731583,-0.004558741581,
   -0.00446721632,-0.004346439149,-0.004195784684,-0.004014744889,-0.003802914172,
  -0.003560011275,-0.003285864834,-0.002980436431,-0.002643799642,-0.002276167972,
  -0.001877869363,-0.001449372387,-0.0009912621463,-0.0005042651901, 1.07772612e-05,
  0.0005528874462, 0.001120973262, 0.001713812933, 0.002330075949, 0.002968308516,
   0.003626958234, 0.004304356873, 0.004998749588,  0.00570827676, 0.006431002636,
   0.007164903916, 0.007907892577, 0.008657809347, 0.009412444197,  0.01016953588,
    0.01092678867,  0.01168186869,   0.0124324318,   0.0131761115,  0.01391054969,
    0.01463338546,  0.01534228493,  0.01603493281,  0.01670905575,  0.01736242138,
    0.01799285598,  0.01859824918,  0.01917656325,  0.01972583681,  0.02024421096,
    0.02072990872,  0.02118127048,  0.02159674093,  0.02197489142,  0.02231441252,
    0.02261412516,     0.02287299,   0.0230901055,  0.02326471545,  0.02339620888,
    0.02348412946,  0.02352816425,  0.02352816425,  0.02348412946,  0.02339620888,
    0.02326471545,   0.0230901055,     0.02287299,  0.02261412516,  0.02231441252,
    0.02197489142,  0.02159674093,  0.02118127048,  0.02072990872,  0.02024421096,
    0.01972583681,  0.01917656325,  0.01859824918,  0.01799285598,  0.01736242138,
    0.01670905575,  0.01603493281,  0.01534228493,  0.01463338546,  0.01391054969,
     0.0131761115,   0.0124324318,  0.01168186869,  0.01092678867,  0.01016953588,
   0.009412444197, 0.008657809347, 0.007907892577, 0.007164903916, 0.006431002636,
    0.00570827676, 0.004998749588, 0.004304356873, 0.003626958234, 0.002968308516,
   0.002330075949, 0.001713812933, 0.001120973262,0.0005528874462, 1.07772612e-05,
  -0.0005042651901,-0.0009912621463,-0.001449372387,-0.001877869363,-0.002276167972,
  -0.002643799642,-0.002980436431,-0.003285864834,-0.003560011275,-0.003802914172,
  -0.004014744889,-0.004195784684,-0.004346439149, -0.00446721632,-0.004558741581,
  -0.004621731583,-0.004657014739,-0.004665495828,-0.004648179282, -0.00460613938,
  -0.004540533293,-0.004452575929,-0.004343551584,-0.004214789253,-0.004067672882,
  -0.003903616453,-0.003724076552,-0.003530521179,-0.003324450692,-0.003107361728,
  -0.002880767686,-0.002646166366,  -0.0024050551,-0.002158906078,-0.001909178798,
  -0.001657290966,-0.001404638402,-0.001152564189,-0.0009023782331,-0.000655326643,
  -0.0004126156273,-0.0001753771503,5.530676208e-05,0.0002784299722,0.0004930471187,
  0.0006982916384,0.0008933648933, 0.001077550347, 0.001250198926,  0.00141075009,
   0.001558712334, 0.001693679485, 0.001815317664, 0.001923376112, 0.002017674735,
   0.002098112367,  0.00216466072, 0.002217361704, 0.002256325213, 0.002281730529,
   0.002293816069, 0.002292887308, 0.002279298147, 0.002253463026, 0.002215842018,
   0.002166942693, 0.002107313601, 0.002037543571, 0.001958250301,  0.00187008502,
   0.001773722004, 0.001669859863, 0.001559207099, 0.001442493289,  0.00132044882,
   0.001193814795, 0.001063329517,0.0009297264041, 0.000793737534,0.0006560774054,
    0.00051745167,0.0003785476729,0.0002400322992,0.0001025461825,-3.328824823e-05,
  -0.0001668831537,-0.0002976757532,-0.0004251426435,-0.0005487820017,-0.0006681427476,
  -0.0007827921654,-0.0008923534187,-0.0009964710334,-0.001094840467,-0.001187188784,
  -0.001273284783, -0.00135293114,-0.001425981056,-0.001492309268, -0.00155184092,
  -0.001604527468,-0.001650367165,-0.001689378405,-0.001721625566,-0.001747193863,
   -0.00176620367,-0.001778801437,-0.001785159926,-0.001785474597,-0.001779967919,
  -0.001768867252,-0.001752444659,-0.001730960212,-0.001704707742,-0.001673978404,
  -0.001639090595, -0.00160034874,-0.001558086486,-0.001512619434,-0.001464285771,
  -0.001413399237,-0.001360305469, -0.00130530668,-0.001248744782,-0.001190904528,
  -0.001132114441,-0.001072656945, -0.00101282273,-0.0009528772207,-0.0008930958575,
  -0.000833709084,-0.0007749674842,-0.0007170780445,-0.0006602567737,-0.0006046761409,
  -0.0005505274748,-0.0004979564692,-0.0004471126013,-0.0003981153714,-0.0003510724055,
  -0.000306090049,-0.0002632375981,-0.0002225704229,-0.0001841588528,-0.0001480082574,
  -0.0001141595858,-8.260838513e-05,-5.336063987e-05,-2.637655598e-05,-1.650866693e-06,
  2.086819586e-05,4.121305028e-05,5.946325473e-05,7.565880514e-05,8.989949129e-05,
  0.0001022133001,0.0001127589858,0.0001215434168,0.0001287219784,0.0001343660115,
  0.0001385670912,0.0001414164872,0.0001430470584, 0.000143528363,0.0001429676922,
  0.0001414458675, 0.000139088399,0.0001359695016,0.0001321963355,0.0001278363488,
  0.0001229849877,0.0001177662925,0.0001121259702,0.0001063660166, 0.000100335521,
  9.414774831e-05,8.795419853e-05,8.175626863e-05,7.558634388e-05,6.947331713e-05,
  6.350613694e-05,5.771249926e-05,5.213804616e-05,4.677322067e-05,4.165340579e-05,
  3.677681525e-05,3.218713755e-05,2.788068377e-05,8.669180534e-05
};

生成FIR滤波器调用函数

function Hd = fir_designer
%FIR_DESIGNER Returns a discrete-time filter object.

% MATLAB Code
% Generated by MATLAB(R) 9.1 and the DSP System Toolbox 9.3.
% Generated on: 19-Aug-2023 21:49:22

% Equiripple Lowpass filter designed using the FIRPM function.

% All frequency values are in Hz.
Fs = 6400;  % Sampling Frequency

Fpass = 65;              % Passband Frequency
Fstop = 100;             % Stopband Frequency
Dpass = 0.057501127785;  % Passband Ripple
Dstop = 0.0001;          % Stopband Attenuation
dens  = 20;              % Density Factor

% Calculate the order from the parameters using FIRPMORD.
[N, Fo, Ao, W] = firpmord([Fpass, Fstop]/(Fs/2), [1 0], [Dpass, Dstop]);

% Calculate the coefficients using the FIRPM function.
b  = firpm(N, Fo, Ao, W, {dens});
Hd = dfilt.dffir(b);

% [EOF]

IIR低通滤波器设计

在这里插入图片描述

生成系数

截止频率:65Hz

SOS Matrix:                                                 
1  2  1  1  -1.9483812279206605  0.95235504547143668        
1  2  1  1  -1.8848795046449112  0.88872380736622714        
                                                            
Scale Values:                                               
0.0009934543876940359                                       
0.00096107568032897277   

截止频率:75Hz

SOS Matrix:                                                 
1  2  1  1  -1.9399670771829454  0.94523778322684693        
1  2  1  1  -1.867646689657416   0.8727209080865993         
                                                            
Scale Values:                                               
0.0013176765109754376                                       
0.0012685546072958092  

截止频率:85Hz

SOS Matrix:                                                 
1  2  1  1  -1.9314328304904613  0.93817732047303393        
1  2  1  1  -1.8505362037561721  0.85699820579506891        
                                                            
Scale Values:                                               
0.001686122495643158                                        
0.0016155005097241886 

截止频率:95Hz

SOS Matrix:                                                 
1  2  1  1  -1.9227805373301849  0.93117365861042944        
1  2  1  1  -1.8335462899281088  0.84154989516829737        
                                                            
Scale Values:                                               
0.002098280320061078                                        
0.0020009013100471622     

截止频率:105Hz

SOS Matrix:                                                 
1  2  1  1  -1.9140122208955201  0.92422678970950123        
1  2  1  1  -1.8166752155117512  0.82637032288824241        
                                                            
Scale Values:                                               
0.0025536422034952134                                       
0.0024237768441227759   

生成IIR滤波器调用函数

function Hd = iir_designer
%IIR_DESIGNER Returns a discrete-time filter object.

% MATLAB Code
% Generated by MATLAB(R) 9.1 and the DSP System Toolbox 9.3.
% Generated on: 19-Aug-2023 21:26:11

% Butterworth Lowpass filter designed using FDESIGN.LOWPASS.

% All frequency values are in Hz.
Fs = 6400;  % Sampling Frequency

N  = 4;   % Order
Fc = 65;  % Cutoff Frequency

% Construct an FDESIGN object and call its BUTTER method.
h  = fdesign.lowpass('N,F3dB', N, Fc, Fs);
Hd = design(h, 'butter');

% [EOF]

滤波器验证比较

%%
close all;
clc;
clear;
f = 50;
fs = 6400; %采样频率
time = 1 / fs;
t = 0:time:0.1;
y1 = sin(2 * pi * f * t) + sin(2 * pi * 13 * f * t);
figure('color',[1,1,1]);
subplot(3,1,1);
plot(t,y1);
title('滤波前波形(基波50Hz+13次谐波)');
%%
y2 = filter(fir_designer,y1);  % 直接使用设计好的滤波器进行滤波,fir_designer函数是滤波函数
subplot(3,1,2);
plot(t,y2);
title('FIR滤波后波形');

%%
y3 = filter(iir_designer,y1);  % 直接使用设计好的滤波器进行滤波,iir_designer函数是滤波函数
subplot(3,1,3);
plot(t,y3);
title('IIR滤波后波形');

在这里插入图片描述
总结:
1、FIR所需阶数较高,即使采用最小阶数仍高达463阶(由此带来很大的时延),而IIR仅需4阶即可达到设想滤波效果,计算简单。
2、FIR滤波器在通带区域提供完美的线性相位特性,从而避免了相位失真。所有IIR滤波器都提供非线性相位特性。如果一个实时应用需要零相位失真,FIR滤波器是设计的直接选择。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值