design filter to implement in microcontroller

Hello :)
I want to design a filter that attenuate some frequencies and has minimun delay to the signal.
I'm building quacoopter controller, and I want to design a IIR or FIR filter to implement later in C code in the microcontroller.
is there some exaple or explanation on how could i desgin the filter and implement it in C code?
Thank you very much!

 Accepted Answer

An IIR filter would be more efficient, and an elliptic filter the most efficient of those. The designfilt function may be the easiest way to create the filter you want, however it outputs a digitalFilter object that is a second-order-section implementation of the filter, not a transfer function, so it would then require the sos2tf function.
Since you want a transfer function, another way to design the filter using command-line functions would be something like this —
Fs = 1000; % Sampling Frequency (Hz)
Fn = Fs/2; % Nyquist Frequency (Hz)
Wp = 100/Fn; % Passband Frequency (Normalised)
Ws = 110/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple (Attenuation)
Rs = 60; % Stopband Ripple (Attenuation)
[n,Wn] = ellipord(Wp,Ws,Rp,Rs); % Calculate Optimal Filter Order
[b,a] = ellip(n,Rp,Rs,Wp,'low') % Transfer Function
b = 1×9
0.0025 -0.0094 0.0198 -0.0279 0.0313 -0.0279 0.0198 -0.0094 0.0025
a = 1×9
1.0000 -6.5233 19.4448 -34.4236 39.4818 -29.9978 14.7365 -4.2805 0.5636
figure
freqz(b,a, 2^16, Fs) % Filter Bode Plot
See ellipord and ellip for more information.
Explore the available options to design the filter you want.
.

4 Comments

Thank you very much, the elliptic filter has nice magnitude response, but is there an option to make the phase response smaller?
If I understood coretly if my system natural frequancy is 80hz that will give me 9.5[ms] delay time.
what is the best why to make the delay time as small as posible?
My pleasure!
I am not certain that it is possible to eliminate the delay in any filter.
The only phase-neutral filter I am aware of is the Bessel design, however it only works correctly when implemented in hardware as an analog filter, and it cannot be implemented as a digital filter and retain its phase-neutral property. All other filters have some phase delay (and therfore phase distortion) inherent in them.
MATLAB solves the phase-distortion problem with the filtfilt function that does forward-backward digital filtering. The Extended Capabilities section oif the documentation mentions only digitalFilter objects, not filtfilt itself (unless I am missing something, which is certainly a possibility), so it may be possible to realise filtfilt using the MATLAB Coder. I have not done any C/C++ coding in a while (and never with respect to MATLAB) so I cannot help with that.
This would require processing time in any event, so there would be an inherent time delay.
thank you very much, i have succefuly succeded to implement the filter (:
As always, my pleasure!
Congratulations!
If you believe it would be appropriate to share your results, please post them (or a link to them).

Sign in to comment.

More Answers (1)

You find C code implementation of filter() here: https://www.mathworks.com/matlabcentral/fileexchange/32261-filterm
Do you want to rewrite the tool to design of the filter parameters also?

2 Comments

I don't want to rewrite the tool, just to find the filter coefficients according to my sampling frequency and cut-off frequency desired, and then use this to get a differece equation in order to write a C function that gets the current sampled variable and calculates the next output.
I saw that in the example you shared the function gets the full vector of x but my system will run in real-time so each time ill send the next input to get the next output. how can I implement this? I'm new to designing filters in MatLab so sorry if the answer is obvious. thanks for the help(:
Then you have to insert the code to obtain the next value into the loop for filtering.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!