fdatool, how to use generated C header file

23 views (last 30 days)
David
David on 24 Sep 2012
Commented: Andrea Bettati on 20 May 2016
I used to fdatool in MATLAB to generate a digital lowpass filter and exported it as a C header file but I am not sure how to exactly incorporate it into my project. I need it to filter readings from a digital gyroscope, and we are using MPIDE to talk to it (MPIDE is similar to the arduino platform). I was wondering if anyone had any advice, or sample .cpp files I could go off of to get a start on trying to use the filter. The output file is below:
Thanks! David
//////
/*
* Filter Coefficients (C Source) generated by the Filter Design and Analysis Tool
*
* Generated by MATLAB(R) 7.14 and the Signal Processing Toolbox 6.17.
*
* Generated on: 24-Sep-2012 10:49:22
*
*/
/*
* Discrete-Time IIR Filter (real)
* -------------------------------
* Filter Structure : Direct-Form II, Second-Order Sections
* Number of Sections : 5
* Stable : Yes
* Linear Phase : No
*/
/* General type conversion for MATLAB generated C-code */
#include "tmwtypes.h"
/*
* Expected path to tmwtypes.h
* C:\Program Files\MATLAB\R2012a\extern\include\tmwtypes.h
*/
/*
* Warning - Filter coefficients were truncated to fit specified data type.
* The resulting response may not match generated theoretical response.
* Use the Filter Design & Analysis Tool to design accurate
* single-precision filter coefficients.
*/
#define MWSPT_NSEC 11
const int NUM_LENGTH[MWSPT_NSEC] = { 1,3,1,3,1,3,1,3,1,3,1 };
const real32_T NUM[MWSPT_NSEC][3] = {
{
0.006008784287, 0, 0
},
{
1, 2, 1
},
{
0.005747633986, 0, 0
},
{
1, 2, 1
},
{
0.005542717408, 0, 0
},
{
1, 2, 1
},
{
0.005402769428, 0, 0
},
{
1, 2, 1
},
{
0.005331991706, 0, 0
},
{
1, 2, 1
},
{
1, 0, 0
}
};
const int DOM_LENGTH[MWSPT_NSEC] = { 1,3,1,3,1,3,1,3,1,3,1 };
const real32_T DEN[MWSPT_NSEC][3] = {
{
1, 0, 0
},
{
1, -1.92819047, 0.9522256255
},
{
1, 0, 0
},
{
1, -1.844388604, 0.8673791885
},
{
1, 0, 0
},
{
1, -1.778631806, 0.8008026481
},
{
1, 0, 0
},
{
1, -1.733723164, 0.7553342581
},
{
1, 0, 0
},
{
1, -1.711010933, 0.7323389053
},
{
1, 0, 0
}
};

Answers (2)

Ahmad
Ahmad on 5 Sep 2013
Hi David,
I am doing a project where I have to use filters, and was having the same question you did. Here is a link to a PDF that graphically shows what coefficients are used where.
Ahmad
  1 Comment
Andrea Bettati
Andrea Bettati on 20 May 2016
Hi, I have the same exact problem! Could you please provide any link or reference on how understand the FDATools' representation of the coeffs? I'd like to use them to describe the Transfer Function of 4 order Butterworth filter so I need something like a1, a2, a3, a4 and b1, b2, b3, b4. Any help would be higly appreciated!!

Sign in to comment.


Wayne King
Wayne King on 24 Sep 2012
Edited: Wayne King on 24 Sep 2012
It's giving you the filter coefficients declared as constants so that you can use them in a C program to filter data. Did you write the C code to actually implement the filter? Presumably you would have some for loop in your C code that actually implements the filter. In that for loop you need the filter coefficients.
So you need to include the fdacoefs.h file in your project so that your C filtering program knows how the filter coefficients are defined when you use Num and Den
  1 Comment
David
David on 24 Sep 2012
Perhaps my question was not clear. I am aware that it is generating values and that I can use those values in a C program. My data needs filtering, that is why I generated the numbers, so I do not already have C code to implement the filter. I do not know how to use the header, that is why I am asking about sample programs or .cpp files that exist so that I could look off of it. In the code you can see there is an 11X3 matrix of values for the numerator and denominator. Does this mean I take in 33 values from the gyro and then then multiply them by the numerator matrix, and then divide them by the denominator matrix? These are a few of the questions I was hoping could be answered by a sample program.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!