MATLAB导出滤波器参数至ADSP中使用

一、matlab设计滤波器

使用filterDesigner命令打开滤波器设计模块

二、运行matlab脚本转换出C可以直接使用的系数

Matlab 复制代码
clc;
BIQUADS = 5; %对应ADI biquad SECTION数量
fp = fopen("lowpass.txt","w");
for i = 1:BIQUADS
    fprintf(fp, '%2.20e,\n', -SOS(i,6));
    fprintf(fp, '%2.20e,\n', -SOS(i,5));
    fprintf(fp, '%2.20e,\n', SOS(i,3)*G(i));
    fprintf(fp, '%2.20e,\n', SOS(i,2)*G(i));
    fprintf(fp, '%2.20e,\n', SOS(i,1)*G(i));
end

三、在C中使用

cpp 复制代码
/*****************************************************************************
 * biquad_test.c
 *****************************************************************************/

#include "adi_initialize.h"
#include "biquad_test.h"

#include <sys/platform.h>

/** 
 * If you want to use command program arguments, then place them in the following string. 
 */
char __argv_string[] = "";


#include <filter.h> //vector_version
#define NSECTIONS  5  //5个biquad级联使用
#define NSAMPLES  7 //数据长度64
#define NSTATE    (2*NSECTIONS) //state数组的长度 2的倍数

float input[NSAMPLES]={1,2,3,4,5,6,7};
float output[NSAMPLES];

float state[NSTATE];

//共25个系数 在数组中的顺序为
//[A2, A1, B2, B1, B0     A2, A1, B2, B1, B0   A2, A1, B2, B1, B0   A2, A1, B2, B1, B0 ...]
//第一个biquad            第二个biquad          第三个biquad          第四个biquad...
float pm coeffs[5*NSECTIONS]={
#include "lowpass.txt"
};

int main(int argc, char *argv[])
{
	int i;

	for (i = 0; i < NSTATE; i++){
		state[i] = 0;     /* initialize state array */
	}

	biquad(input, output, coeffs, state, NSAMPLES, NSECTIONS);  //调用完biquad后state数组不为0


	return 0;
}

四、验证

Matlab 复制代码
x = [1,2,3,4,5,6,7]';

%% 先用sos2tf转换为传递函数,再滤波

[b, a] = sos2tf(SOS, G); % b为分子系数, a为分母系数

y2 = filter(b, a, x);

相关推荐
kaixin_啊啊20 小时前
test_机器学习算法学习
学习·算法·机器学习
liulilittle20 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光20 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
一只齐刘海的猫21 小时前
【Leetcode】乘积最大子数组
算法·leetcode·职场和发展
余生皆假期-1 天前
傅里叶变换和拉普拉斯变换
人工智能·算法·机器学习
数模竞赛Paid answer1 天前
2026年五一杯数学建模C题边坡预警问题求解全过程文档及程序
算法·数学建模·五一杯
hold?fish:palm1 天前
20 旋转图像
c++·算法·leetcode
NoteStream1 天前
【C语言基础】分支和循环(上)
c语言·开发语言·c++·经验分享·笔记·算法·c#
白狐_7981 天前
408数据结构第7章:查找②——顺序、折半、分块查找秒杀 + 真题
数据结构·算法
卷心菜的学习路1 天前
基于多模态向量检索的数学相似题推荐系统:完整设计、算法与实验
java·python·算法·大模型·推荐算法·数学相似题