当前位置:   article > 正文

Matlab Filter函数学习及C语言2阶实现_matlab 二阶滤波

matlab 二阶滤波

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

运行结果

函数说明(来源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];
	}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

测试主函数

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,-
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/552149
推荐阅读
相关标签
  

闽ICP备14008679号