Matlab Filter函数学习及C语言2阶实现
1、Matlab Filter
Matlab脚本
- 在Matlab中使用butter函数设计n阶滤波器b、a系数
- 在Matlab中使用将b、a系数传入filter函数实现滤波
clc;
clear;
close all;
fc = 100;
fs = 1000;
[b,a] = butter(2,fc/(fs/2))
freqz(b,a,[],fs)
subplot(2,1,1)
ylim([-100 20])
dataIn = randn(1000,1);
dataOut = filter(b,a,dataIn);
figure()
plot(dataIn)
hold on
plot(dataOut)
运行结果

函数说明(来源Matlab)
butter


filter


2、C语言实现
公式

C代码(2阶)
void filter_2order(double *b, double *a, double* x, double* y, unsigned int len)
{
unsigned int i = 0;
y[0] = b[0] * x[0];
y[1] = b[0] * x[1] + b[1] * x[0] - a[1] * y[0];
for (i = 2; i < len; i++)
{
y[i] = b[0] * x[i] + b[1] * x[i - 1] + b[2] * x[i - 2] - a[1] * y[i - 1] - a[2] * y[i - 2];
}
}
测试主函数
int main(void)
{
#define ORDER 2
#define LEN 1000
double b[ORDER + 1] = { 0.067455274,0.134910548,0.067455274 };
double a[ORDER + 1] = { 1,-1.142980503,0.412801598 };
double x[LEN] = { -0.491328111,0.657745314,1.436083027,-1.535033015,1.752844532,1.283050852,0.093642419,1.580098828,0.244783676,-1.020254356,1.127840682,2.232468702,-0.774247093,-1.001457286,1.138326349,-0.023951589,-1.363832296,1.236316183,-0.154075344,-0.795206586,-0.581572584,-0.497663996,0.937428006,-1.155727401,0.469223861,1.004411364,-0.859842623,0.334386523,-1.653469212,0.696878623,-1.122209427,0.806363297,0.433565065,-0.411399971,-1.479868178,1.331586826,0.152702847,1.677299219,0.008565093,-1.778385222,1.690066583,-0.291388735,0.924400207,-0.722240284,0.611834579,0.834988463,1.013086105,0.499048921,-0.040810929,0.144477744,0.302344993,-0.738461735,0.550423231,-1.064474418,-1.905377815,0.166565726,0.197731836,0.220736301,1.772843881,1.232882475,1.863792959,0.220350637,0.908635455,-0.591170221,-0.230979854,-1.747769492,0.069832209,0.926980897,-1.058176043,-0.222484554,-0.72408143,-0.808408292,0.902393481,0.067267077,0.739593243,0.39677531,0.935791523,-2.257203967,-1.022122876,-0.171884806,0.347912133,1.148432523,-0.639623721,-0.604947425,-0.285155015,0.695220483,0.106434776,1.125701654,-0.771931903,0.411133643,0.387188439,-1.644007767,0.296388813,1.739693661,-1.197785212,-0.637081262,-1.016757906,1.129067957,-0.556514075,0.751357989,-1.644759821,-1.822552249,0.997355788,0.593306266,0.280074227,0.967067587,0.067591032,0.139905171,-0.874892317,0.009505575,-1.100317252,-0.890699813,-0.477231952,0.13873541,1.602834872,0.377530026,0.406144873,0.543279428,1.203667788,0.580418912,0.063672442,-0.433042715,0.597220105,-1.68351719,0.041732906,0.943244353,-0.227951816,0.576819232,0.172430276,0.585028268,-0.49346731,1.67126214,0.396610858,0.141009748,-1.190598616,-0.193148666,-1.445605833,-0.288587492,-1.229140422,-0.063965837,0.152206915,-0.484546103,1.632897838,1.368895506,1.413900768,0.590728575,0.780002129,-0.657244217,0.131814263,-1.122417171,0.239846434,1.142156113,-1.100866604,-0.208882455,-1.46608136,0.606281003,1.230829436,1.14345173,-1.02566565,-0.709034818,-0.102158521,-0.395123393,-0.527554291,-2.524534946,0.636676728,-0.234422175,0.693361433,0.264528592,-2.978544004,-0.301313132,0.786084975,1.045165478,-0.317219122,0.520515639,0.237981084,0.921874425,1.963604137,-0.911435262,2.09993653,0.214464095,-0.799034553,-0.810339567,0.117768314,0.138022894,0.410911288,0.827006697,0.361593114,-1.024509991,-0.690159113,-0.758888632,-0.590452742,0.355152945,0.340715749,-0.227538168,-0.998835959,-0.325849588,0.597047559,-0.129605967,1.440339764,-0.818393473,0.294179209,1.15137207,-0.860265311,-0.05609512,-1.214747045,-0.931103506,-1.447286497,-0.93474905,0.367631235,-0.240151113,0.58303962,1.027739958,0.362556895,0.77548101,0.754981737,2.357318467,0.757204413,-1.49199876,0.295768561,0.667410122,0.942656022,0.817138566,-0.491906131,0.027691451,0.345195484,0.215736984,2.028995444,-1.604517315,0.855678863,1.186423616,-0.590599071,0.495655924,-0.089001675,-1.033427345,0.100672297,1.668133275,-0.399147114,0.627519816,-1.121339544,-0.921659114,-1.239045479,-1.434684338,-0.942524572,-0.899643027,-0.442063537,-1.643538186,-0.506853297,-0.774918685,-0.793732023,-0.316836213,1.171767508,-0.004797876,0.421027773,1.585497243,0.684602385,0.12938685,1.086017158,2.229872638,-0.516964715,0.848372855,-0.813994061,-1.248465244,-0.677408992,-0.073731963,-1.961731227,-1.598594703,-0.057066145,0.600563607,0.785452724,0.437340298,-0.179804694,0.530015843,2.492850202,1.976814728,-0.059381352,1.414432228,-0.62058383,0.350801627,0.14442274,0.763297178,-1.214938461,0.624257038,0.168626236,0.328758593,-1.02861151,-0.393576332,-1.544140356,-0.733862581,-0.969899942,-0.3496269,0.169241311,0.847958684,-1.06906282,0.293622184,1.086485561,0.961315233,-1.594446358,0.732017984,-0.168767033,1.381407728,0.929302073,-0.348845746,0.269422539,0.510851504,-2.461178451,-0.613988223,-0.39544689,-1.144753672,-2.23236833,0.840037507,-0.613785968,1.17215934,-0.958240409,0.953125795,0.703446627,0.559895045,-1.037997901,-0.178315643,-0.469938546,-0.959886736,-0.445025298,-1.621173741,-0.157394384,-0.197458254,-0.831367451,0.637333937,-1.048598681,-0.248183185,0.524913159,0.213223909,-2.397377049,-0.772418839,-0.406103307,-0.553319721,-1.352965973,-0.043116229,-0.167630791,1.279593129,0.112219107,0.705481392,-0.130396955,-1.825464429,-1.473279377,-0.751403919,-0.433729916,0.576351041,-0.834456151,-0.628795362,0.255851793,-0.562049583,-1.228419863,-0.554040938,-0.680275588,0.691164599,0.221938054,0.545931143,-0.459746999,2.116712668,0.109101493,-0.919826045,-1.323605998,0.098650184,-0.406640029,-1.351891832,-0.469815207,-1.628010774,0.682417154,1.94370839,0.740237955,-0.323177184,1.359645942,0.291030256,-2.218994711,-0.144627255,0.078700061,0.562218061,-0.164275808,1.173703169,-1.328395111,2.459213462,1.352204826,-0.246773287,0.288016672,-0.872674102,-0.930755707,0.076155378,0.985664468,0.925841557,2.13149973,-0.325832926,-0.223013464,-2.734301273,-0.308548313,0.146108342,0.037732668,-0.647646618,-1.287407016,-0.4110156,2.200545337,-0.929047088,0.002664404,-1.157621762,1.164531251,0.786436018,0.160341671,-0.047538593,1.478874775,0.577770824,-0.003531752,0.13666497,1.538983795,0.494838381,0.268859574,0.568572737,-0.098590958,-0.987223016,0.367607954,-1.622299684,-0.694147742,1.057942441,-0.69457826,0.270112675,-0.215269438,0.277456758,0.42359979,-0.500923398,-2.124567616,1.021152744,-0.557920051,0.284803326,0.969564999,-1.082401244,1.256167811,0.74116558,0.122380088,-0.358789971,1.205195,0.44578344,0.532335163,2.692884249,0.554637302,1.176518451,-0.050684187,1.802265549,-0.095097483,-0.994993902,-0.016643083,-1.716047025,1.257595747,-1.184155843,0.281644962,0.914349459,0.482747658,0.373550608,-0.86447642,-0.055050523,0.930401082,-0.763580794,-1.235697806,-0.698666764,0.854441034,-0.274565612,0.222871093,-1.744834536,-0.772631263,-0.69948051,-1.265930373,1.173968526,1.039894421,-1.567216578,0.249868699,-0.965552625,-0.165745678,0.04567878,1.709555166,0.37043645,-0.656453105,0.831383083,0.876000852,0.138307983,2.242231434,-0.166766685,-0.459046415,0.546193455,-1.302302799,-0.411524019,-0.540077676,1.366469917,0.706849198,0.607570216,0.919815411,0.899393277,0.032717379,-1.415856974,-1.68086021,0.037781186,1.663462461,0.830999694,1.04765464,1.793817583,-0.485121788,-0.63362915,1.101900362,-0.717021174,2.200021478,0.890616836,-0.635431755,-0.373502709,0.521876378,0.71289943,1.168406212,0.339968228,-1.014729752,0.09314554,-1.536205299,0.576217657,0.572062119,0.146680068,-0.92572349,1.273140581,0.987920866,-0.711861835,-1.334303091,1.004236668,0.608510675,0.035924424,0.255350054,0.697162196,-0.431179392,1.553913167,0.346650755,-0.7664851,-0.4934522,1.50453667,-0.649856722,-1.060486854,1.154726822,0.319256968,2.203528463,-0.267906786,0.63805474,-0.019369154,0.432255194,-1.589741184,0.249650474,0.763109203,-0.005732262,-0.359689848,0.025904465,-2.089257026,1.241868282,0.34214261,0.550592771,0.654700749,0.286811201,1.418357598,-1.118931816,-0.074687678,1.084119486,-0.014549562,0.642925541,-1.619869243,0.348747529,1.083413492,-2.240119863,-0.23564428,-1.253171284,-1.030196067,0.592835692,1.090252577,-0.209461404,-0.519221212,1.802246085,-0.086125157,0.38061043,-0.347925496,0.507329688,-1.222167503,0.021564301,-0.885708334,-0.191622817,0.689514553,1.959331996,-0.452041425,0.533254627,-0.457055444,0.089446132,2.782586116,0.181329135,-0.596945151,1.002224384,0.9248121,-1.521679473,-0.640085338,-0.44801521,-0.253666683,0.104909317,-0.56036175,0.433441572,0.470756352,-0.842570453,0.375919009,-1.121856471,0.824889591,-0.213846762,0.720025224,-0.993468251,0.781864817,-0.276267193,-0.07264129,-0.271723747,0.67124097,1.80271974,0.896098517,0.029449916,-0.747576218,0.084047227,-0.997619595,-0.087357191,0.942072918,-0.08749846,1.192776006,-0.498168215,0.43754627,0.330044189,-1.40014186,-0.521881084,1.004918394,-1.137789386,-0.153247998,-0.974249346,-0.979501189,-0.921452084,0.823138388,0.006565077,0.374132782,-0.555195219,-0.124638466,0.83697425,-1.287476203,-0.758512754,1.02057709,1.694427392,-0.86318828,0.439280578,-1.246878039,0.657701772,0.133127765,-1.013790064,0.508966618,1.264501025,-0.157605026,-0.898631249,-1.117966299,-1.241883403,0.3984553,0.339393133,-0.783577083,0.754028533,-1.065583718,1.487362837,-2.127732426,-0.273774444,0.753309636,0.01227506,0.049070104,-0.508520216,1.965257548,-0.46033512,0.697998178,1.211256588,-0.339759121,1.44864558,0.443910564,-1.487802376,-0.471275689,-0.145215111,0.637923187,0.712707186,0.697064159,-1.446998808,-1.525752056,1.345511655,0.823446603,0.412184549,1.546189754,1.846948718,-0.772296031,0.041902847,0.431067422,0.120365366,0.202805281,0.194154727,1.086571339,-2.748723641,-0.005891803,-0.250432973,-0.468704021,0.144009406,0.534210961,-0.284940424,0.018133549,-1.649917809,-1.11429094,-0.373310711,-0.170381047,-1.066977132,-1.180644346,0.612916815,0.134828653,1.37690327,0.545738785,-1.165635854,-2.218573279,0.139236703,-0.227141607,0.554559504,0.913972183,-0.20018341,0.148786992,-0.721357483,1.201293097,0.051885204,-0.532589247,-0.457453476,1.818272819,0.507958614,0.420440281,0.336928312,0.454330108,0.47225436,0.006376235,1.711590607,-0.239429046,-0.843438104,-0.412751293,1.182966073,0.781520643,1.171962011,-0.214152933,0.150936525,-1.814309792,0.945596698,-0.107487176,0.056038544,1.013494534,-0.273372627,0.43084131,-0.717847862,-0.531839352,0.541662509,-0.211772195,-1.565650524,-0.537865066,2.90950916,0.492827187,0.215098339,-0.409014744,-1.819450969,0.135598307,-3.078963218,-0.923381458,-1.455122468,-0.21329385,1.073617254,0.515949709,-0.443665953,-1.626537958,-0.533168111,-1.185578326,0.429481839,-0.585620615,1.41674407,-0.117376955,-1.518860187,0.07923613,0.717367668,-0.495938824,0.617738375,0.339304058,0.384447388,-0.326549398,-0.127644533,-0.13426739,-0.128478779,-1.45573385,-0.01826599,-1.19043682,0.12794658,-1.166445333,1.362765218,0.820975156,-0.539193125,2.064612632,1.139357119,0.853397161,-2.072750727,-1.265754006,-0.702280836,-0.327693686,-1.311551364,0.655080617,-0.522712044,-0.165371801,-0.5485457,-0.088259383,1.37217995,-1.830471441,-0.961464463,-0.470914615,0.554739931,-1.023085139,-0.524780785,0.703483761,-1.227934718,1.047608985,0.342178668,0.569912411,-0.014888969,-0.997451958,-1.599527645,0.153380702,0.715557353,0.724202711,-0.086045748,1.648136441,0.185027816,-0.40298541,0.105222787,-0.615970147,0.68753055,1.556752774,1.040086182,0.248261615,-1.360803628,-0.636671631,0.403044333,-0.076599974,1.382435835,0.660856079,-1.433693611,0.632975923,0.668609507,1.75070022,0.085325717,0.263015304,-1.575409126,-0.460339097,-1.49354921,-0.14796502,-0.779475521,-1.288077161,-0.004485191,-0.840699961,1.267781171,2.227383528,0.152662012,0.2822603,-1.958344949,1.497394054,-0.623825627,0.390898555,-0.201298041,0.343561984,-0.086392273,0.130692158,-1.06405666,0.132694805,-1.488370029,-0.10856655,0.182589623,-0.278445613,0.32571953,0.88499534,-1.593998522,-1.455360674,-0.619775689,0.428207744,0.345703163,-0.355340755,0.58010478,2.078772331,-1.079049363,-0.846084931,-1.829996146,0.133610745,0.794793543,-0.371500827,1.090049088,-0.231560755,0.24370435,1.01388343,0.587913047,0.315685112,1.426476733,-0.914058513,-0.464747796,0.296200816,-0.357042534,-0.563856672,0.720381995,-0.507635058,0.395061516,-0.183371272,-0.435802439,0.457254173,0.291098345,-1.973707065,-0.339056555,-1.078613093,0.111895665,-0.717465469,-0.01415907,1.253577957,1.701752828,0.798545863,-1.598119744,1.467198682,1.075071621,0.253438094,2.430472185,-0.46095206,1.793683151,-1.106499322,-0.39723199,-1.1725545,0.562614264,0.363942576,-0.65121786,-1.40697133,-2.697613585,-0.953621302,-1.434529278,0.661083447,1.07742772,-2.002162696,-1.747387721,-0.47894817,0.947337328,1.253198246,1.470473077,-0.396390494,-0.657613698,-1.033756244,0.55093,0.76479892,-1.5301454,-0.139826781,-0.531307735,0.602158531,1.143889184,1.199512308,0.487490676,-0.418717769,-0.477655685,-1.174335305,1.16434779,-0.898357908,-1.947341308,-0.82701207,1.451055598,0.252831523,-0.3667585,0.204297918,0.38843925,0.437946869,-0.180282088,2.284422645,1.05823318,-2.47694656,-0.951413362,-0.032491425,0.497178072,-0.284751085,-0.045864606,-0.32491531,-1.316365794,2.129761372,-0.196122723,2.324657676,-0.890219012,-0.257184976,-1.308841257,-0.855603814,0.902037749,0.755764983,-0.875472247,0.150960373,-0.440395044,0.827646034,-0.769324688,1.55577598,-1.550351366,0.290746016,-1.513024681,-0.725653515,-1.075608661,1.548817553,-0.162520912,0.325820798 };
double y[LEN] = { 0 };
filter_2order(b, a, x, y, LEN);
FILE* fd = fopen("filter_demo.csv", "w");
for (int i = 0; i < LEN; i++)
{
fprintf(fd, "%f,%f\n", x[i], y[i]);
}
fclose(fd);
}
运行结果
结果与Matlab计算的一样

本文介绍了如何在Matlab中使用butter函数设计滤波器,并通过filter函数进行滤波操作。接着,详细展示了C语言如何实现2阶滤波器,包括公式推导、代码实现及测试,最终运行结果与Matlab计算一致。
741

被折叠的 条评论
为什么被折叠?



