MATLAB 小波函数转成C语言
matlab产生一个带噪的信号
下面展示一些 内联代码片
。
// An highlighted block
t=0:0.1:512; %自己根据需要设置;
m=zeros(size(t)); %生成与矩阵t相同大小的全零矩阵;
for i=1:length(t) % 数组长度(即行数或列数中的较大值);
if (t(i)>=0)&&(t(i)<=256)
m(i)=sin(2*pi*5/320*t(i));
elseif (t(i)>256)&&(t(i)<=512)
m(i)=sin(2*pi*10/320*t(i));
end
end
y=awgn(m,20);
z = wden(y,'minimaxi','s','one',4,'db3');
subplot(2,1,1);
plot(t,y,'r',t,z,'k') % 'r'表示线为红色;
生成噪声信号记得把这一句删除z = wden(y,‘minimaxi’,‘s’,‘one’,4,‘db3’);
产生了一个double类型的数组长度为5121的带噪信号,我们采用的降低噪声小波函数是wden,不清楚wden怎么使用的可以再matlab按f1查看。
MATLAB转换成C
把上一个章节中的噪声信号,复制到m.txt文件里面。
新建matlab.m文件
输入以下代码,记得先把m.txt文件数据导入到工程里面,不然会编译错误。通过MATLAB上方的导入数据选项进行导入,选择数据矩阵
1、
2、
新建.m,输入代码
。
// An highlighted block
function [res]=li_woden(m)
res=wden(m,'modwtsqtwolog','s','mln',3,'db4');
end
3、
4、选择下面选项
5、
6、
7、
8、完成
导入VS,进行调试
当所有的函数(包括转换出来的main.c/main.h)都添加到VS以后,会出现这个下面这个错误,这个.h文件可以去MATLAB安装目录下面去找。找到后添加进去。
添加文件后编译通过
导入我们之前存储好的m.txt文件到VS工程目录下,通过修改main代码并可使用。
下面展示一些 内联代码片
。
// A code block
var foo = 'bar';
// An highlighted block
/*
* File: main.c
*
* MATLAB Coder version : 4.1
* C/C++ source code generated on : 25-Apr-2021 16:48:57
*/
/*************************************************************************/
/* This automatically generated example C main file shows how to call */
/* entry-point functions that MATLAB Coder generated. You must customize */
/* this file for your application. Do not modify this file directly. */
/* Instead, make a copy of this file, modify it, and integrate it into */
/* your development environment. */
/* */
/* This file initializes entry-point function arguments to a default */
/* size and value before calling the entry-point functions. It does */
/* not store or use any values returned from the entry-point functions. */
/* If necessary, it does pre-allocate memory for returned values. */
/* You can use this file as a starting point for a main function that */
/* you can deploy in your application. */
/* */
/* After you copy the file, and before you deploy it, you must make the */
/* following changes: */
/* * For variable-size function arguments, change the example sizes to */
/* the sizes that your application requires. */
/* * Change the example values of function arguments to the values that */
/* your application requires. */
/* * If the entry-point functions return values, store these values or */
/* otherwise use them as required by your application. */
/* */
/*************************************************************************/
/* Include Files */
#include "rt_nonfinite.h"
#include "li_woden.h"
#include "main.h"
#include "li_woden_terminate.h"
#include "li_woden_initialize.h"
#include <stdio.h>
#include <stdlib.h>
/* Function Declarations */
static void argInit_1x5121_real_T(double result[5121]);
static double argInit_real_T(void);
static void main_li_woden(void);
/* Function Definitions */
/*
* Arguments : double result[5121]
* Return Type : void
*/
static void argInit_1x5121_real_T(double result[5121])
{
int idx1;
/* Loop over the array to initialize each element. */
for (idx1 = 0; idx1 < 5121; idx1++) {
/* Set the value of the array element.
Change this value to the value that the application requires. */
result[idx1] = argInit_real_T();
}
}
/*
* Arguments : void
* Return Type : double
*/
static double argInit_real_T(void)
{
return 0.0;
}
/*
* Arguments : void
* Return Type : void
*/
static void main_li_woden(void)
{
static double dv3[5121];
double res[5121];
int n = 0;//输入信号长度
int i = 0;
char s[32];//从txt文件中读取一行数据
FILE* fp;
fp = fopen("../m.txt", "r");
if (fp == NULL) //如果读取失败
{
printf("错误!找不到要读取的文/n");
exit(1);//中止程序
}
while (fscanf(fp, "%s", s) != EOF) //读取长度n要设置得长一点,要保证读到回车符,这样指针才会定位到下一行?回车符返回的是零值?是,非数字字符经过atoi变换都应该返回零值
{
// fscanf(fp,"%d", &data[count]);//一定要有"&"啊!!!最后读了个回车符!适应能力不如atoi啊
dv3[n] = atof(s);
n++;
}
/* Initialize function 'li_woden' input arguments. */
/* Initialize function input argument 'm'. */
/* Call the entry-point 'li_woden'. */
//argInit_1x5121_real_T(dv3);
li_woden(dv3, res);
fp = fopen("data_output.txt", "w");
//打印一维小波变换后的结果
for (i = 0; i < n; i++)
{
printf("%f ", res[i]);
fprintf(fp, "%f ", res[i]);
}
}
/*
* Arguments : int argc
* const char * const argv[]
* Return Type : int
*/
int main(int argc, const char * const argv[])
{
(void)argc;
(void)argv;
/* Initialize the application.
You do not need to do this more than one time. */
li_woden_initialize();
/* Invoke the entry-point functions.
You can call entry-point functions multiple times. */
main_li_woden();
/* Terminate the application.
You do not need to do this more than one time. */
li_woden_terminate();
return 0;
}
/*
* File trailer for main.c
*
* [EOF]
*/
编译得到的结果文件导入到MATLAB中通过PLOT对比滤波结果。
效果图