电力电子应用中,往往需要对电流、电压信号进行采样,滤除干扰频段的信号,或者保留、提取感兴趣的频段的信号。通过模拟滤波往往难以达到较好的效果,因此需要采用数字滤波器。
常用的数字滤波器有IIR、FIR滤波器两种。FIR 滤波器最重要的优点就是由于不存在系统极点,FIR 滤波器是绝对稳定的系统。FIR 滤波器还确保了线性相位,这在信号处理中也非常重要。此外,由于不需要反馈,FIR 滤波器的实现也比 IIR 滤波器简单。FIR 滤波器的缺点在于它的性能不如同样阶数的 IIR 滤波器,不过由于数字计算硬件的飞速发展,这一点已经不成为问题。再加上引入计算机辅助设计,FIR 滤波器的设计也得到极大的简化。基于上述原因,FIR 滤波器比 IIR 滤波器的应用更广泛。
这篇博文就通过Matlab仿真来直观的介绍FIR滤波器的用途,并将FIR滤波器移植到以STM32F4为基础的嵌入式平台中。

一、Matlab中的FIR实验

通过一个简单的实验来使用FIR滤波器:将200Hz正弦信号叠加到基波50Hz的正弦信号上,然后利用FIR滤波器提取200Hz信号。采样率设置为6400Hz。

1.搭建原始信号

在simulink搭建简易混合电路,将幅值为1的50Hz正弦波与幅值为1的200Hz正弦波混合。
首先设置simulink的仿真步长为固定步长,1/6400,那么就相当于采样率为6400Hz,如图所示。
在这里插入图片描述
搭建电路如下图所示。其中1为多路复合,为了在scope中分别查看50Hz和200Hz的原始信号,2为信号叠加混合,即50Hz混合200Hz的信号。
在这里插入图片描述
运行0.2秒,即10个50Hz周波,查看scope窗口如下图所示。可以看到上方为50Hz、200Hz的原始信号,下方为混合后的信号。我们现在要做的就是利用FIR滤波器将200Hz信号从下方的信号中提取出来。将scope数据导出到workspace待用。
在这里插入图片描述

2.设计FIR滤波器

在matlab命令行输入fdatool,打开滤波器设计工具。
1.选择带通滤波、FIR窗口滤波;
2.选择常用的汉宁窗;
3.指定FIR阶数,一般来讲,阶数越高,滤波效果越好,但运算量越大,需要在滤波效果和运行速度上做一个取舍,这里我们暂时设置为100阶;
4.设置采样频率为6400Hz,要与simulink中的相符;
5.设置带通范围,这里我们设置为带通范围180~220Hz;
6.点击Design Filter;
7.点击Group Delay Response可以查看滤波器的相位延迟,这里是50,这个数要记住,后边要用到。
在这里插入图片描述
在这里插入图片描述
生成滤波器后,我们将滤波器导出到workspace中。具体操作为:file->export->Export As选择Objects。默认的滤波器名字为Hd。

3.FIR滤波

至此,worksapce中已经有了scope中导出的原始波形、混杂波形,以及设计好的滤波器Hd。
在命令行中对混杂信号进行滤波:

fileter(Hd,rawSignal)

其中rawSignal为混杂信号,即scope中下方的波形。运行后,ans中就存储了滤波后的波形。

4.滤波前后对比

将滤波后的波形与原始的200Hz信号进行比对:

plot(1:200,ans(51:250),1:200,raw200Hz(1:200))

其中,滤波后的信号要从51开始,因为滤波器在设计时的FIR相位延迟为50,即第51个点才为滤波后的数据起始点。raw200Hz为从scope中导出的200Hz信号数据。绘图后如下图所示。可以看出FIR滤波震荡稳定后,能较好的带通200Hz信号,幅值、相位均与原信号相同,50Hz信号被滤掉。
在这里插入图片描述

二、嵌入式平台FIR滤波移植

1.arm-dsp库移植

arm-dsp库的移植可以参考我之前的博客【嵌入式】利用arm-DSP库进行FFT计算,获得信号的频谱、幅值及相位(上)中的二-1节内容。

2.获得FIR滤波系数

还是在fdatool中,当设计玩滤波器后,点击Targets->Generate C Header.
打开生成的fdacoefs.h,里边的BL即为FIR滤波器阶数。B数组即为FIR滤波器系数,将前边的数据类型real64_T改为float即可。

/*
 * Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool
 * Generated by MATLAB(R) 8.3 and the Signal Processing Toolbox 6.21.
 * Generated on: 28-Apr-2021 15:33:18
 */

/*
 * Discrete-Time FIR Filter (real)
 * -------------------------------
 * Filter Structure  : Direct-Form FIR
 * Filter Length     : 101
 * Stable            : Yes
 * Linear Phase      : Yes (Type 1)
 */

/* General type conversion for MATLAB generated C-code  */
#include "tmwtypes.h"
/* 
 * Expected path to tmwtypes.h 
 * F:\MatLab\extern\include\tmwtypes.h 
 */
const int BL = 101;
const real64_T B[101] = {
  -0.002364379994766,-0.002555816813742, -0.00271143828077,-0.002821346700614,
  -0.002865814202786,-0.002816683118228,-0.002640000779638,-0.002299699197689,
  -0.001762028066288,-0.001000368319048,4.399291056296e-18, 0.001237622441001,
   0.002691519514449, 0.004319142530531, 0.006055934652271, 0.007816685262717,
   0.009498734985135,  0.01098694698435,  0.01216021777687,  0.01289916830821,
    0.01309454373426,  0.01265576722454,  0.01151904640175, 0.009654425543614,
   0.007071214384679, 0.003821304224151,-5.964020643576e-18,-0.004255853855223,
  -0.008772504883931, -0.01334655058446, -0.01775451146912, -0.02176424083345,
   -0.02514755140538, -0.02769333338392, -0.02922037668388,  -0.0295890978537,
   -0.02871141129903, -0.02655807388539, -0.02316296707884, -0.01862395375005,
   -0.01310014718858, -0.00680564532364,-1.977442795623e-17, 0.007024104515362,
    0.01395531112556,  0.02047939405639,  0.02629552027152,  0.03113204072429,
    0.03476090062898,  0.03700983073789,  0.03777160643401,  0.03700983073789,
    0.03476090062898,  0.03113204072429,  0.02629552027152,  0.02047939405639,
    0.01395531112556, 0.007024104515362,-1.977442795623e-17, -0.00680564532364,
   -0.01310014718858, -0.01862395375005, -0.02316296707884, -0.02655807388539,
   -0.02871141129903,  -0.0295890978537, -0.02922037668388, -0.02769333338392,
   -0.02514755140538, -0.02176424083345, -0.01775451146912, -0.01334655058446,
  -0.008772504883931,-0.004255853855223,-5.964020643576e-18, 0.003821304224151,
   0.007071214384679, 0.009654425543614,  0.01151904640175,  0.01265576722454,
    0.01309454373426,  0.01289916830821,  0.01216021777687,  0.01098694698435,
   0.009498734985135, 0.007816685262717, 0.006055934652271, 0.004319142530531,
   0.002691519514449, 0.001237622441001,4.399291056296e-18,-0.001000368319048,
  -0.001762028066288,-0.002299699197689,-0.002640000779638,-0.002816683118228,
  -0.002865814202786,-0.002821346700614, -0.00271143828077,-0.002555816813742,
  -0.002364379994766
};

3.基于arm-dsp的FIR程序

1.将混杂信号采样值复制到C数组raw中。

const float raw[1280] = {0,0.244157996343546,0.480700572694650,0.702300707474964,0.902197103202676,1.07444979220581,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,
1.39527626933728,1.34557235649577,1.26267701420615,1.15126953751204,1.01707671652874,0.866649276863147,0.707106781186548,0.545860803338831,0.390327020997647,0.247637298461043,
0.124362831115998,0.0262589976977269,-0.0419582681629318,-0.0767959872797872,-0.0761204674887133,-0.0392412152202096,0.0330608032209223,0.138561640891999,0.273678499216683,0.433606276945179,
0.612501294307107,0.803705134189045,1.00000000000000,1.19388577822130,1.37786815903729,1.54474674298438,1.68789206158978,1.80150086549709,1.88081986824350,1.92232934558625,
1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230282,1.53857639348909,1.35877776450025,1.15569388572783,0.936041447371087,0.707106781186548,0.476468632830889,
0.251709851798556,0.0401290714728317,-0.151536548166946,-0.317366868109323,-0.452482795685290,-0.553230186972949,-0.617316567634911,-0.643895427011010,-0.633594855256824,-0.588489432399282,
-0.512016459170419,-0.408839758564241,-0.284666292035529,-0.146022647688710,-3.67394039744206e-16,0.146022647688711,0.284666292035529,0.408839758564241,0.512016459170420,0.588489432399282,
0.633594855256825,0.643895427011010,0.617316567634910,0.553230186972948,0.452482795685289,0.317366868109324,0.151536548166945,-0.0401290714728307,-0.251709851798557,-0.476468632830890,
-0.707106781186549,-0.936041447371088,-1.15569388572783,-1.35877776450025,-1.53857639348909,-1.68919822230282,-1.80580079685964,-1.88477457352667,-1.92387953251129,-1.92232934558625,
-1.88081986824350,-1.80150086549709,-1.68789206158978,-1.54474674298438,-1.37786815903729,-1.19388577822130,-1.00000000000000,-0.803705134189046,-0.612501294307106,-0.433606276945178,
-0.273678499216683,-0.138561640891999,-0.0330608032209226,0.0392412152202102,0.0761204674887134,0.0767959872797871,0.0419582681629319,-0.0262589976977280,-0.124362831115999,-0.247637298461043,
-0.390327020997648,-0.545860803338834,-0.707106781186550,-0.866649276863148,-1.01707671652874,-1.15126953751204,-1.26267701420615,-1.34557235649577,-1.39527626933728,-1.40834037383351,
-1.38268343236509,-1.31767513379545,-1.21416420976575,-1.07444979220581,-0.902197103202674,-0.702300707474963,-0.480700572694650,-0.244157996343547,-1.22464679914735e-15,0.244157996343549,
0.480700572694652,0.702300707474965,0.902197103202676,1.07444979220581,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649577,
1.26267701420615,1.15126953751204,1.01707671652874,0.866649276863147,0.707106781186549,0.545860803338832,0.390327020997646,0.247637298461042,0.124362831115998,0.0262589976977273,
-0.0419582681629314,-0.0767959872797872,-0.0761204674887132,-0.0392412152202096,0.0330608032209218,0.138561640892000,0.273678499216684,0.433606276945180,0.612501294307107,0.803705134189047,
1.00000000000000,1.19388577822130,1.37786815903729,1.54474674298439,1.68789206158978,1.80150086549709,1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352667,
1.80580079685964,1.68919822230282,1.53857639348909,1.35877776450025,1.15569388572782,0.936041447371084,0.707106781186545,0.476468632830888,0.251709851798555,0.0401290714728308,
-0.151536548166945,-0.317366868109323,-0.452482795685288,-0.553230186972950,-0.617316567634911,-0.643895427011010,-0.633594855256824,-0.588489432399281,-0.512016459170419,-0.408839758564240,
-0.284666292035529,-0.146022647688706,-1.10218211923262e-15,0.146022647688709,0.284666292035527,0.408839758564238,0.512016459170421,0.588489432399282,0.633594855256825,0.643895427011010,
0.617316567634910,0.553230186972946,0.452482795685290,0.317366868109319,0.151536548166948,-0.0401290714728279,-0.251709851798559,-0.476468632830885,-0.707106781186550,-0.936041447371081,
-1.15569388572783,-1.35877776450025,-1.53857639348909,-1.68919822230282,-1.80580079685964,-1.88477457352667,-1.92387953251129,-1.92232934558625,-1.88081986824350,-1.80150086549709,
-1.68789206158978,-1.54474674298438,-1.37786815903728,-1.19388577822130,-0.999999999999995,-0.803705134189046,-0.612501294307103,-0.433606276945176,-0.273678499216681,-0.138561640891998,
-0.0330608032209183,0.0392412152202098,0.0761204674887133,0.0767959872797871,0.0419582681629321,-0.0262589976977290,-0.124362831115996,-0.247637298461045,-0.390327020997644,-0.545860803338833,
-0.707106781186549,-0.866649276863148,-1.01707671652874,-1.15126953751204,-1.26267701420615,-1.34557235649577,-1.39527626933728,-1.40834037383351,-1.38268343236509,-1.31767513379545,
-1.21416420976575,-1.07444979220581,-0.902197103202675,-0.702300707474956,-0.480700572694651,-0.244157996343539,-2.44929359829471e-15,0.244157996343543,0.480700572694655,0.702300707474960,
0.902197103202678,1.07444979220581,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649577,1.26267701420615,1.15126953751204,
1.01707671652873,0.866649276863145,0.707106781186546,0.545860803338830,0.390327020997642,0.247637298461043,0.124362831115994,0.0262589976977277,-0.0419582681629329,-0.0767959872797874,
-0.0761204674887129,-0.0392412152202090,0.0330608032209198,0.138561640892000,0.273678499216683,0.433606276945179,0.612501294307106,0.803705134189050,0.999999999999998,1.19388577822130,
1.37786815903728,1.54474674298439,1.68789206158978,1.80150086549709,1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230281,
1.53857639348910,1.35877776450024,1.15569388572782,0.936041447371085,0.707106781186546,0.476468632830881,0.251709851798556,0.0401290714728245,-0.151536548166944,-0.317366868109322,
-0.452482795685292,-0.553230186972947,-0.617316567634911,-0.643895427011010,-0.633594855256824,-0.588489432399281,-0.512016459170419,-0.408839758564241,-0.284666292035525,-0.146022647688712,
3.49210031947972e-15,0.146022647688708,0.284666292035531,0.408839758564242,0.512016459170420,0.588489432399282,0.633594855256826,0.643895427011010,0.617316567634909,0.553230186972949,
0.452482795685286,0.317366868109326,0.151536548166942,-0.0401290714728269,-0.251709851798558,-0.476468632830900,-0.707106781186549,-0.936041447371096,-1.15569388572783,-1.35877776450025,
-1.53857639348909,-1.68919822230282,-1.80580079685964,-1.88477457352667,-1.92387953251129,-1.92232934558625,-1.88081986824350,-1.80150086549709,-1.68789206158977,-1.54474674298438,
-1.37786815903728,-1.19388577822130,-0.999999999999996,-0.803705134189033,-0.612501294307104,-0.433606276945171,-0.273678499216682,-0.138561640891994,-0.0330608032209219,0.0392412152202076,
0.0761204674887132,0.0767959872797880,0.0419582681629324,-0.0262589976977287,-0.124362831115996,-0.247637298461045,-0.390327020997654,-0.545860803338832,-0.707106781186554,-0.866649276863147,
-1.01707671652874,-1.15126953751203,-1.26267701420615,-1.34557235649577,-1.39527626933728,-1.40834037383351,-1.38268343236509,-1.31767513379545,-1.21416420976575,-1.07444979220580,
-0.902197103202676,-0.702300707474957,-0.480700572694636,-0.244157996343540,-3.67394039744206e-15,0.244157996343551,0.480700572694645,0.702300707474966,0.902197103202670,1.07444979220581,
1.21416420976574,1.31767513379545,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649576,1.26267701420615,1.15126953751203,1.01707671652873,0.866649276863140,
0.707106781186547,0.545860803338837,0.390327020997637,0.247637298461039,0.124362831115999,0.0262589976977311,-0.0419582681629346,-0.0767959872797873,-0.0761204674887137,-0.0392412152202092,
0.0330608032209192,0.138561640891999,0.273678499216688,0.433606276945178,0.612501294307099,0.803705134189056,1.00000000000000,1.19388577822130,1.37786815903728,1.54474674298439,
1.68789206158978,1.80150086549709,1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230281,1.53857639348908,1.35877776450024,
1.15569388572783,0.936041447371070,0.707106781186539,0.476468632830891,0.251709851798565,0.0401290714728182,-0.151536548166950,-0.317366868109321,-0.452482795685291,-0.553230186972947,
-0.617316567634911,-0.643895427011011,-0.633594855256824,-0.588489432399284,-0.512016459170413,-0.408839758564237,-0.284666292035531,-0.146022647688717,8.08638275819206e-15,0.146022647688712,
0.284666292035526,0.408839758564242,0.512016459170423,0.588489432399281,0.633594855256825,0.643895427011010,0.617316567634909,0.553230186972943,0.452482795685287,0.317366868109326,
0.151536548166930,-0.0401290714728405,-0.251709851798557,-0.476468632830883,-0.707106781186548,-0.936041447371095,-1.15569388572783,-1.35877776450025,-1.53857639348909,-1.68919822230282,
-1.80580079685965,-1.88477457352667,-1.92387953251129,-1.92232934558625,-1.88081986824349,-1.80150086549709,-1.68789206158978,-1.54474674298437,-1.37786815903728,-1.19388577822130,
-0.999999999999997,-0.803705134189034,-0.612501294307105,-0.433606276945171,-0.273678499216682,-0.138561640891994,-0.0330608032209160,0.0392412152202113,0.0761204674887130,0.0767959872797880,
0.0419582681629289,-0.0262589976977282,-0.124362831115995,-0.247637298461044,-0.390327020997654,-0.545860803338831,-0.707106781186553,-0.866649276863146,-1.01707671652874,-1.15126953751203,
-1.26267701420615,-1.34557235649577,-1.39527626933728,-1.40834037383351,-1.38268343236509,-1.31767513379545,-1.21416420976574,-1.07444979220580,-0.902197103202677,-0.702300707474974,
-0.480700572694637,-0.244157996343542,-4.89858719658941e-15,0.244157996343549,0.480700572694644,0.702300707474965,0.902197103202683,1.07444979220581,1.21416420976574,1.31767513379546,
1.38268343236509,1.40834037383351,1.39527626933729,1.34557235649576,1.26267701420615,1.15126953751204,1.01707671652873,0.866649276863141,0.707106781186548,0.545860803338826,
0.390327020997649,0.247637298461040,0.124362831115991,0.0262589976977254,-0.0419582681629305,-0.0767959872797885,-0.0761204674887125,-0.0392412152202095,0.0330608032209189,0.138561640891998,
0.273678499216687,0.433606276945177,0.612501294307111,0.803705134189041,1.00000000000000,1.19388577822131,1.37786815903729,1.54474674298438,1.68789206158979,1.80150086549709,
1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230282,1.53857639348909,1.35877776450025,1.15569388572782,0.936041447371087,
0.707106781186540,0.476468632830875,0.251709851798550,0.0401290714728340,-0.151536548166936,-0.317366868109331,-0.452482795685291,-0.553230186972946,-0.617316567634910,-0.643895427011011,
-0.633594855256825,-0.588489432399280,-0.512016459170420,-0.408839758564238,-0.284666292035522,-0.146022647688708,-3.30654635769785e-15,0.146022647688722,0.284666292035535,0.408839758564241,
0.512016459170416,0.588489432399286,0.633594855256825,0.643895427011011,0.617316567634909,0.553230186972944,0.452482795685287,0.317366868109316,0.151536548166944,-0.0401290714728249,
-0.251709851798572,-0.476468632830898,-0.707106781186546,-0.936041447371077,-1.15569388572784,-1.35877776450025,-1.53857639348909,-1.68919822230282,-1.80580079685965,-1.88477457352667,
-1.92387953251129,-1.92232934558625,-1.88081986824349,-1.80150086549709,-1.68789206158977,-1.54474674298439,-1.37786815903728,-1.19388577822129,-0.999999999999998,-0.803705134189049,
-0.612501294307093,-0.433606276945172,-0.273678499216683,-0.138561640892004,-0.0330608032209163,0.0392412152202111,0.0761204674887129,0.0767959872797868,0.0419582681629329,-0.0262589976977278,
-0.124362831116003,-0.247637298461043,-0.390327020997642,-0.545860803338842,-0.707106781186552,-0.866649276863145,-1.01707671652873,-1.15126953751204,-1.26267701420615,-1.34557235649577,
-1.39527626933728,-1.40834037383351,-1.38268343236509,-1.31767513379545,-1.21416420976575,-1.07444979220580,-0.902197103202664,-0.702300707474959,-0.480700572694655,-0.244157996343525,
1.16403343982657e-14,0.244157996343548,0.480700572694643,0.702300707474964,0.902197103202682,1.07444979220581,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,
1.39527626933728,1.34557235649577,1.26267701420614,1.15126953751203,1.01707671652874,0.866649276863153,0.707106781186537,0.545860803338827,0.390327020997649,0.247637298461050,
0.124362831115992,0.0262589976977259,-0.0419582681629302,-0.0767959872797860,-0.0761204674887126,-0.0392412152202098,0.0330608032209184,0.138561640892006,0.273678499216686,0.433606276945176,
0.612501294307124,0.803705134189054,1.00000000000000,1.19388577822132,1.37786815903730,1.54474674298439,1.68789206158978,1.80150086549710,1.88081986824350,1.92232934558625,
1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230282,1.53857639348910,1.35877776450023,1.15569388572782,0.936041447371089,0.707106781186558,0.476468632830876,
0.251709851798551,0.0401290714728351,-0.151536548166935,-0.317366868109331,-0.452482795685290,-0.553230186972946,-0.617316567634914,-0.643895427011011,-0.633594855256825,-0.588489432399284,
-0.512016459170414,-0.408839758564238,-0.284666292035532,-0.146022647688698,6.61680659921524e-15,0.146022647688711,0.284666292035544,0.408839758564249,0.512016459170422,0.588489432399281,
0.633594855256827,0.643895427011010,0.617316567634910,0.553230186972950,0.452482795685279,0.317366868109317,0.151536548166945,-0.0401290714728239,-0.251709851798539,-0.476468632830897,
-0.707106781186545,-0.936041447371076,-1.15569388572781,-1.35877776450025,-1.53857639348909,-1.68919822230281,-1.80580079685965,-1.88477457352667,-1.92387953251129,-1.92232934558625,
-1.88081986824349,-1.80150086549709,-1.68789206158977,-1.54474674298437,-1.37786815903728,-1.19388577822128,-0.999999999999984,-0.803705134189036,-0.612501294307107,-0.433606276945161,
-0.273678499216673,-0.138561640891996,-0.0330608032209233,0.0392412152202067,0.0761204674887143,0.0767959872797869,0.0419582681629331,-0.0262589976977213,-0.124362831116002,-0.247637298461043,
-0.390327020997641,-0.545860803338818,-0.707106781186552,-0.866649276863144,-1.01707671652873,-1.15126953751204,-1.26267701420615,-1.34557235649577,-1.39527626933729,-1.40834037383351,
-1.38268343236509,-1.31767513379544,-1.21416420976574,-1.07444979220581,-0.902197103202652,-0.702300707474945,-0.480700572694639,-0.244157996343544,-7.34788079488412e-15,0.244157996343564,
0.480700572694659,0.702300707474963,0.902197103202668,1.07444979220582,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649577,
1.26267701420616,1.15126953751203,1.01707671652874,0.866649276863154,0.707106781186538,0.545860803338828,0.390327020997650,0.247637298461031,0.124362831115993,0.0262589976977263,
-0.0419582681629376,-0.0767959872797883,-0.0761204674887126,-0.0392412152202022,0.0330608032209309,0.138561640892006,0.273678499216686,0.433606276945175,0.612501294307123,0.803705134189053,
1.00000000000000,1.19388577822129,1.37786815903727,1.54474674298437,1.68789206158980,1.80150086549710,1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352668,
1.80580079685965,1.68919822230282,1.53857639348907,1.35877776450023,1.15569388572782,0.936041447371090,0.707106781186559,0.476468632830878,0.251709851798552,0.0401290714728360,
-0.151536548166934,-0.317366868109330,-0.452482795685289,-0.553230186972957,-0.617316567634913,-0.643895427011011,-0.633594855256825,-0.588489432399285,-0.512016459170428,-0.408839758564222,
-0.284666292035513,-0.146022647688699,5.88201851972683e-15,0.146022647688710,0.284666292035524,0.408839758564231,0.512016459170409,0.588489432399289,0.633594855256827,0.643895427011010,
0.617316567634910,0.553230186972951,0.452482795685297,0.317366868109318,0.151536548166946,-0.0401290714728229,-0.251709851798570,-0.476468632830896,-0.707106781186544,-0.936041447371108,
-1.15569388572784,-1.35877776450025,-1.53857639348909,-1.68919822230281,-1.80580079685965,-1.88477457352668,-1.92387953251129,-1.92232934558625,-1.88081986824349,-1.80150086549709,
-1.68789206158979,-1.54474674298440,-1.37786815903726,-1.19388577822128,-0.999999999999985,-0.803705134189037,-0.612501294307108,-0.433606276945186,-0.273678499216695,-0.138561640891996,
-0.0330608032209108,0.0392412152202144,0.0761204674887142,0.0767959872797869,0.0419582681629335,-0.0262589976977330,-0.124362831116002,-0.247637298461042,-0.390327020997640,-0.545860803338840,
-0.707106781186551,-0.866649276863166,-1.01707671652875,-1.15126953751204,-1.26267701420615,-1.34557235649576,-1.39527626933728,-1.40834037383351,-1.38268343236508,-1.31767513379544,
-1.21416420976574,-1.07444979220581,-0.902197103202680,-0.702300707474977,-0.480700572694674,-0.244157996343510,2.69546091939735e-14,0.244157996343563,0.480700572694657,0.702300707474962,
0.902197103202667,1.07444979220582,1.21416420976575,1.31767513379545,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649577,1.26267701420614,1.15126953751203,
1.01707671652874,0.866649276863155,0.707106781186539,0.545860803338829,0.390327020997629,0.247637298461032,0.124362831115993,0.0262589976977268,-0.0419582681629297,-0.0767959872797858,
-0.0761204674887100,-0.0392412152202024,0.0330608032209304,0.138561640892005,0.273678499216685,0.433606276945174,0.612501294307095,0.803705134189024,1,1.19388577822132,
1.37786815903730,1.54474674298439,1.68789206158978,1.80150086549708,1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352667,1.80580079685964,1.68919822230282,
1.53857639348908,1.35877776450023,1.15569388572782,0.936041447371091,0.707106781186560,0.476468632830879,0.251709851798522,0.0401290714728076,-0.151536548166959,-0.317366868109329,
-0.452482795685289,-0.553230186972945,-0.617316567634907,-0.643895427011009,-0.633594855256821,-0.588489432399276,-0.512016459170415,-0.408839758564239,-0.284666292035533,-0.146022647688720,
5.14723044023841e-15,0.146022647688710,0.284666292035543,0.408839758564248,0.512016459170421,0.588489432399280,0.633594855256827,0.643895427011010,0.617316567634910,0.553230186972951,
0.452482795685281,0.317366868109318,0.151536548166920,-0.0401290714728512,-0.251709851798569,-0.476468632830895,-0.707106781186543,-0.936041447371074,-1.15569388572781,-1.35877776450028,
-1.53857639348911,-1.68919822230283,-1.80580079685965,-1.88477457352667,-1.92387953251129,-1.92232934558625,-1.88081986824349,-1.80150086549708,-1.68789206158977,-1.54474674298438,
-1.37786815903729,-1.19388577822131,-0.999999999999986,-0.803705134189038,-0.612501294307109,-0.433606276945162,-0.273678499216675,-0.138561640891997,-0.0330608032209242,0.0392412152202141,
0.0761204674887140,0.0767959872797871,0.0419582681629337,-0.0262589976977204,-0.124362831116001,-0.247637298461061,-0.390327020997661,-0.545860803338840,-0.707106781186550,-0.866649276863143,
-1.01707671652873,-1.15126953751202,-1.26267701420616,-1.34557235649577,-1.39527626933729,-1.40834037383351,-1.38268343236509,-1.31767513379545,-1.21416420976576,-1.07444979220581,
-0.902197103202654,-0.702300707474947,-0.480700572694641,-0.244157996343546,-9.79717439317883e-15,0.244157996343562,0.480700572694656,0.702300707474961,0.902197103202666,1.07444979220582,
1.21416420976575,1.31767513379546,1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649577,1.26267701420616,1.15126953751201,1.01707671652872,0.866649276863133,
0.707106781186540,0.545860803338829,0.390327020997652,0.247637298461052,0.124362831116010,0.0262589976977150,-0.0419582681629370,-0.0767959872797882,-0.0761204674887128,-0.0392412152202106,
0.0330608032209170,0.138561640892005,0.273678499216684,0.433606276945174,0.612501294307121,0.803705134189051,0.999999999999999,1.19388577822132,1.37786815903730,1.54474674298439,
1.68789206158978,1.80150086549708,1.88081986824350,1.92232934558625,1.92387953251128,1.88477457352667,1.80580079685964,1.68919822230282,1.53857639348910,1.35877776450026,
1.15569388572779,0.936041447371059,0.707106781186528,0.476468632830880,0.251709851798555,0.0401290714728381,-0.151536548166932,-0.317366868109328,-0.452482795685288,-0.553230186972956,
-0.617316567634913,-0.643895427011011,-0.633594855256825,-0.588489432399276,-0.512016459170415,-0.408839758564240,-0.284666292035534,-0.146022647688700,4.41244236075000e-15,0.146022647688730,
0.284666292035542,0.408839758564247,0.512016459170421,0.588489432399280,0.633594855256823,0.643895427011010,0.617316567634904,0.553230186972940,0.452482795685281,0.317366868109319,
0.151536548166947,-0.0401290714728209,-0.251709851798536,-0.476468632830893,-0.707106781186575,-0.936041447371106,-1.15569388572784,-1.35877776450025,-1.53857639348909,-1.68919822230281,
-1.80580079685964,-1.88477457352667,-1.92387953251129,-1.92232934558625,-1.88081986824349,-1.80150086549709,-1.68789206158977,-1.54474674298438,-1.37786815903729,-1.19388577822131,
-0.999999999999987,-0.803705134189039,-0.612501294307083,-0.433606276945163,-0.273678499216675,-0.138561640891997,-0.0330608032209246,0.0392412152202060,0.0761204674887113,0.0767959872797847,
0.0419582681629264,-0.0262589976977321,-0.124362831116000,-0.247637298461041,-0.390327020997639,-0.545860803338816,-0.707106781186549,-0.866649276863165,-1.01707671652875,-1.15126953751204,
-1.26267701420615,-1.34557235649576,-1.39527626933729,-1.40834037383351,-1.38268343236509,-1.31767513379544,-1.21416420976574,-1.07444979220581,-0.902197103202655,-0.702300707474948,
-0.480700572694643,-0.244157996343548,-1.10218211923262e-14,0.244157996343526,0.480700572694688,0.702300707474991,0.902197103202692,1.07444979220582,1.21416420976575,1.31767513379545,
1.38268343236509,1.40834037383351,1.39527626933728,1.34557235649576,1.26267701420614,1.15126953751203,1.01707671652874,0.866649276863156,0.707106781186540,0.545860803338830,
0.390327020997631,0.247637298461033,0.124362831115994,0.0262589976977276,-0.0419582681629368,-0.0767959872797881,-0.0761204674887129,-0.0392412152202110,0.0330608032209165,0.138561640892004,
0.273678499216705,0.433606276945197,0.612501294307120,0.803705134189050,0.999999999999998,1.19388577822129,1.37786815903727,1.54474674298441,1.68789206158979,1.80150086549710,
1.88081986824350,1.92232934558625,1.92387953251129,1.88477457352668,1.80580079685964,1.68919822230280,1.53857639348908,1.35877776450024,1.15569388572782,0.936041447371093,
0.707106781186563,0.476468632830881,0.251709851798556,0.0401290714728096,-0.151536548166957,-0.317366868109327,-0.452482795685288,-0.553230186972956,-0.617316567634913,-0.643895427011011,
-0.633594855256825,-0.588489432399285,-0.512016459170416,-0.408839758564224,-0.284666292035515,-0.146022647688701,3.67765428126159e-15,0.146022647688708,0.284666292035522,0.408839758564230,
0.512016459170434,0.588489432399288,0.633594855256827,0.643895427011010,0.617316567634910,0.553230186972952,0.452482795685299,0.317366868109320,0.151536548166922,-0.0401290714728491,
-0.251709851798567,-0.476468632830892,-0.707106781186541,-0.936041447371104,-1.15569388572783,-1.35877776450025,-1.53857639348909,-1.68919822230283,-1.80580079685964,-1.88477457352668,
-1.92387953251129,-1.92232934558625,-1.88081986824350,-1.80150086549709,-1.68789206158979,-1.54474674298436,-1.37786815903726,-1.19388577822128,-0.999999999999988,-0.803705134189040,
-0.612501294307111,-0.433606276945189,-0.273678499216698,-0.138561640891980,-0.0330608032209121,0.0392412152202136,0.0761204674887138,0.0767959872797873,0.0419582681629341,-0.0262589976977317,
-0.124362831116000,-0.247637298461040,-0.390327020997660,-0.545860803338838,-0.707106781186548,-0.866649276863164,-1.01707671652875,-1.15126953751204,-1.26267701420615,-1.34557235649576,
-1.39527626933729,-1.40834037383351,-1.38268343236508,-1.31767513379544,-1.21416420976574,-1.07444979220581,-0.902197103202683,-0.702300707474980,-0.480700572694610,-0.244157996343514};

2.利用arm_fir_init_f32和arm_fir_f32进行FIR计算。
首先讲解arm_fir_init_f32 FIR初始化函数,其函数原型为

void arm_fir_init_f32(
  arm_fir_instance_f32 * S,
  uint16_t numTaps,
  float32_t * pCoeffs,
  float32_t * pState,
  uint32_t blockSize)

其中:S为fir实例指针;numTaps为FIR阶数;pCoeffs为FIR系数指针;pState为状态数组指针,其实就是一个计算过程中需要开辟的缓存,用于计算,其长度为numTaps+blockSize-1;blockSize为计算块的长度,大于1,小于等于原始数据的总长度即可。

配置好arm_fir_init_f32后,即可使用arm_fir_f32进行FIR计算,arm_fir_f32的函数原型为

void arm_fir_f32(
const arm_fir_instance_f32 * S,
float32_t * pSrc,
float32_t * pDst,
uint32_t blockSize)

由于设置了blockSize,即每次调用arm_fir_f32计算的数据长度,如blockSize设置为128时,要对总长度为1280的原始数据进行计算,就要循环调用10次arm_fir_f32,其中pSrc和pDst指针也要随着循环进行后移。
具体FIR程序如下所示:

float32_t output[1280] = {0};
void FIR()
{
	uint32_t timeTick = HAL_GetTick();		//函数进入时间戳
	uint32_t timePass = 0;					//计算用时
	uint32_t blockSize = 128;				//块长度,原始数据长度为1280
	uint32_t numOfBlock = 1280 / blockSize;	//
	uint32_t i;
	arm_fir_instance_f32 S;

	float32_t *pState;
	
	pState = (float32_t *)Mem.malloc((blockSize + BL - 1) * sizeof(float32_t));

	if(pState != NULL)
	{
		arm_fir_init_f32(&S,BL,(float32_t *)B,pState,blockSize);
		for(int i=0; i < numOfBlock; i++)
		{
			arm_fir_f32(&S, raw + (i * blockSize), output + (i * blockSize), blockSize);
		}
	}

    timePass = HAL_GetTick() - timeTick;

	Mem.free(pState);
    
}

其中,timePass保存了计算所需要的时间;output为FIR滤波后的数据输出数组;设置blockSize为128,则numOfblock块个数就是raw数组的长度1280除以128;pState为临时使用内存池申请的缓存,如果你的程序中没有内存池,可以直接开辟一个长度为1280的float数组,或者直接使用malloc动态申请。
运行后,timePass等于2,说明对1280个点进行100阶的FIR滤波只需要2ms,速度已经非常快了。
在这里插入图片描述

4.滤波结果-与Matlab比对

将output数组数据导出,导入到matlab中,与matlab的FIR滤波结果进行比对,发现一模一样完全重合,证明了stm32调用arm-dsp库FIR滤波的准确性、可靠性和高效性。

 plot(1:1280,ans(1:1280),1:1280,stm32out)

在这里插入图片描述

三、总结

实际工程应用中,往往是单片机对信号进行采样,将波形保存在一个数组中,然后再使用FIR进行滤波处理。一般的工程开发过程为:
1.分析原始信号,确定滤波目标;
2.导出单片机采样数据,在matlab中设计合适的FIR滤波器;
3.导出Matlab滤波器系数到单片机程序;
4.利用arm-dsp库进行FIR滤波计算。

Logo

开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!

更多推荐