Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Filter design in Matlab Filter Designer?

Status
Not open for further replies.

Javert

Member level 2
Joined
Sep 14, 2019
Messages
44
Helped
0
Reputation
0
Reaction score
1
Trophy points
8
Activity points
551
Sorry for the beginner question, this is my first step with digital filters. I want to try to apply a digital filter to the signal sampled by the ADC in the STM32 MCU..I used Filter Designer from Matlab to design the filter.
I expected FD to be able to generate c code directly, similar to what Nuhertz's FilterSolution does. FD, but only generates .h with constants for the filter.
I'm a little confused about this Can anyone find an example of filter design in FD matlab for MCU?

Thanks
 

The code for FIR or cascaded BiQuads pretty simple.

Whats your specs for the filter, there are solutions out there, ARM based,
that do everything in HW on chip ?For example :

1665061358927.png

The above, DelSig, can be configed up to 20 bits. The above is single chip, using a small
number of resources this family has. No CPU intervention, operates in background. IDE
has its own filter design tool.


Regards, Dana.
 
Last edited by a moderator:

There are free online filter design tools other than the one provided by MATLAB which generate source code from specifications. Keep in mind that since C code is platform independent you can chose any. Have a try at this on for example: http://t-filter.engineerjs.com/
 

Thanks for the comments.
I am not talking about the design of a specific filter or other design software.
I am talking about Matlab, I want to understand Filter Design and possibly compare it with Filer Solution.

Up to speed in Filter Solution



FS.jpg


and return c code see Filter.zip

Matlab

FD.jpg


and return only .h with consttn without c code,

I think I'm doing something wrong in FD,
Matlab can generate HDL, Filter Solution can't, but support for C is listed by both.
 

Attachments

  • Filter.zip
    1.8 KB · Views: 145

Just making a checklist:

1. Are you using the Trial version of Nuhertz's FilterSolution ? If so, I'm not sure if they provide the generated full code at this modality.
2. Did you check all dependencies on the .H file, such as for example if there are included files on which the actual function is implemented ?
 

MATLAB has toolboxes for embedded code generation but no equivalent to the simple floating point filter code provided by FilterSolutions. It's however easy to write similar code from the scratch, in my view it's more a demonstration of generated filter coefficients than an embedded coding tool. Nevertheless there may be cases where you can the generated code in your application, if the sampling rate is low and you aren't heading for particular high performance.
--- Updated ---

Also the HDL coding feature isn't provided by MATLAB Filter Designer itself but additional toolboxes with separate licences.
 
Last edited:

Sorry, I have no experience with digital filters.
I look at the filter from Filter Solution and ..

1. The code begins
float DigFil(invar, setic)
float invar; int setic;
{}

i think it's a erroe and and it should be
float DigFil( float invar, int setic)
{}


2. Use
FS=1000;
F= 187;
N=100;
inputbuffer[100];
outputbuffer[100];

//generate 187Hx sampling f 1kHz signal
for(x=0;x<N;x++)
(
inputbuffer[x]=sin(2*PI*F*x/Fs);
}

//use filter
DigFil(0, 1); //initialize the filter to zero

for(x=0;x<N;x++)
(
outputbuffer[x]=DigFil(inputbuffer[x],0);
}

note on the introduction
- for the case when there are N samples in the buffer, this method is not very efficient, the function is called 100x better to override and pass the entire inputbuffer and outputbuffer fields

The main thing, the inputbuffer only contains a 187 Hz signal, so I would expect a very similar signal in the outputbuffer because Pass Band Attenuation = 1 dB

Naybe the first few samples before the filter fills to zero, maybe the signal will be shifted, but the result should be a similar 187 Hz signal
Reality
input signal sin in the range 0 to 1
output signal sin in the typical x.xE-6
What did I not understand or what is wrong?
Maybe states[] is defined as a local variable in the DigFil function, i.e. it is initialized every time it is called That code is weird
 

1. The code begins
float DigFil(invar, setic)
float invar; int setic;
{}

i think it's a erroe and and it should be
float DigFil( float invar, int setic)
{}
The former is legal C syntax, although rather unusual. If your C compiler is compliant to C-standard it should accept the code. You can select "C++" style in FiltersSolutions code generator to get the latter parameter declaration form.
note on the introduction
- for the case when there are N samples in the buffer, this method is not very efficient, the function is called 100x better to override and pass the entire inputbuffer and outputbuffer fields
Not particularly. The main calculation effort is the sumden and sumnum loop which has to be performed for each time step. Due to the recursive operation of IIR, little would be saved if you execute the filter function for a block of N samples.

Providing the filter function for one input sample also gives the option to execute the filter in real time.
The main thing, the inputbuffer only contains a 187 Hz signal, so I would expect a very similar signal in the outputbuffer because Pass Band Attenuation = 1 dB
Failed expectation. You overlook the very small filter bandwidth which results in long settling time (many seconds) until the filter reaches steady state.
 

>C vs C++
You are right, I tried it in Visual Studio (deflt compiler C++) In C, I already use the C++ syntax allowed by later C standards. Sadly, I don't even remember the original C syntax anymore. Sadly, I don't even remember the original C syntax anymore and my first computer was a Commodore 16


>Filter Delay
Again you are right, in the picture here in the thread you can see a group delay of around 400ms.
 

FilterSolution designates function parameters with type as C++ but it's genuine standard C. You can refer e.g. to Kernighan Ritchie, The C programming language 2nd edition. The correct naming is old style versus new style function declaration. New style has been introduced with ANSI C and is the preferred form in K+R 2nd edition of 1988. K+R 1st edition of 1978 still uses old style.
 

I've been playing around with digital filters from Filter Solution again and I understand it less and less
I try
8th Order High Pass Butterworth
Sample Frequency Fs= 4.000 KHz */
Pass Band Frequency = 186.66 Hz
Pass Band Attenuation = 1 dB
f.png

see the attachment for the generated code

I used the code in the example like this

Code:
float Fs = 4000.0;
   float F = 186.66;
   const int N = 240;
   float wave[N];
   float filter;

 for (int i = 0; i < N; i++)
    {
        wave = sin(2 * PI * F * i / Fs);
        filter = DigFil(wave, 0, 0);
        fs << wave <<" ; " << filter << std::endl;
    }

Output is 240 sample of wave f=186.66 FS 4000Hz
and 249 value after passing through the filter
graphical representation of wave and filter

O.png


what the hell am I doing stupidly?
 

Attachments

  • code.zip
    1,008 bytes · Views: 162
Last edited by a moderator:

Simple problem, too few significant coefficient digits. Need at least 8 digits to use float resolution fully, which is required for a high order recursive filter. Better use double type for the filter, Matlab always does.
 

You are right, it was about the accuracy of the coefficients. Both the size of the variable and the accuracy of the coefficient can be set in Filter Solution. I kept the float and raised the precision from 4 digits to 8 and here is an example
top image
blue wave pure 50Hz
red wave output of digital filter with attenuation 50Hz -80db
a bit too much noise for my taste, maybe the effect of float accuracy, I can't judge

bottom image
blue wave = 50Hz +186.7Hz
red wave output of digital filter, i.e 186.7Hz



f50.jpg
 

I feel that about +/- 0.2 "numerical" noise is quite a lot. In fixed point filters, we can correct integer coefficients to minimize the error. In a floating point design, you should switch to double.
 

Same in double with 10 digit coefficient
The noise is removed, the only thing that maybe surprises me is the shift of 186.7 at the output of the filter towards positive values, see the bottom picture.
F50d.jpg
 

Same in double with 10 digit coefficient
The noise is removed, the only thing that maybe surprises me is the shift of 186.7 at the output of the filter towards positive values, see the bottom picture.
View attachment 179107
I don't see any shift(if you mean dc offset). Check zero line carefully
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top