MATLAB Answers

designfilt and get the numerator and denominator coefficients of my filter

10 views (last 30 days)
昊 赵
昊 赵 on 18 Sep 2021
Answered: Paul on 19 Sep 2021
Hello,I have design an filter by the function designfilt and I have get the filter object. And I need to conver the filter to a C language version, so I plan to get the numerator(b) and denominator(a) coefficients of my filter and then implement the filter by C language base the coeficients. But after I get the numerator and denominator coefficients of filter object by the function "tf" and I filter the orgin signal by "filter(b,a,signal)" on matlab, it is not equivalent to using filter object directly. Here is my source code. Dose anyone know how to fix this?
%% initialize
close all;
% set the sample rate as 200Hz
fs = 200;
% set time axis
t = 1 / fs:1 / fs:10;
% the first signal | 5Hz
sig1 = sin(2 * pi * 5 * t);
% the second signal | 20Hz
sig2 = cos(2 * pi * 20 * t);
% plus
signal = sig1 + sig2;
% design a lp filter and generate a filter object:lpFilt
lpFilt = designfilt('lowpassiir', 'PassbandFrequency', 8, ...
'StopbandFrequency', 10, 'PassbandRipple', 0.5, 'SampleRate', fs, ...
'StopbandAttenuation', 65, 'DesignMethod', 'butter');
% use tf function to converts the digital filter, lpFilt, to numerator and denominator vectors
[b, a] = tf(lpFilt);
% filter the signal by filter object and get the ouput
dataFilteredOut1 = filter(lpFilt, signal);
% filter the signal by filter numerator and denominator vectors and get the ouput
dataFilteredOut2 = filter(b, a, signal);
%% view the copared output
figure("Name", "wave-compare")
subplot(3, 1, 1)
plot(t, signal);
subplot(3, 1, 2)
plot(t, dataFilteredOut1);
subplot(3, 1, 3)
plot(t, dataFilteredOut2);

Answers (1)

Paul on 19 Sep 2021
Trying to convert such a high order filter to a tf representation is likely not going to work well. Maybe consider implementing the filter in terms of the second-order-section coefficients in lpFilt as in sosfilt(), which is what filter(lpFilt,signal) actually does. Unfortunately, the Matlab implementation of sosfilt() for an iir filter is in a mexfile. But you can probably find some references.

Community Treasure Hunt

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

Start Hunting!