Matteo Maggioni
Matteo Maggioni

Reputation: 71

Creating standalone butter filter with Matlab coder

i'm trying to compile with Matlab Coder a custom function which includes a butterworth filter. I've extracted the lines that give me problem with the Matlab coder function.

function [output] = myfilter(input,fs) %#codegen
f1 = 5;         % cuttoff low frequency to get rid of baseline wander
Wn = f1.*2./fs;   % cutt off based on fs
N  = 3;         % order of 3 less processing
[a,b] = butter(N,Wn); % bandpass filtering
output = filtfilt(a,b,input); % filtering
output = output/max(abs(output));

I'm getting an error when I launch the command to compile it:

codegen -config:lib -launchreport myfilter -args {zeros(1,100),10}

The output error is: All inputs must be constant. I tried to use the function coder.const modifying the code as follows, but still have the same problem:

function [output] = myfilter(input,fs) %#codegen
f1 = 5;         % cuttoff low frequency to get rid of baseline wander
Wn = f1.*2./fs;   % cutt off based on fs
N  = 3;         % order of 3 less processing
[a,b] = coder.const(@butter,N,Wn); % bandpass filtering
a = coder.const(a);
b = coder.const(b);
output = filtfilt(a,b,input); % filtering
output = output/max(abs(output));

Can somebody help me with this issue? I'm new to Matlab Coder. Thanks in advance!

Upvotes: 1

Views: 801

Answers (1)

Ryan Livingston
Ryan Livingston

Reputation: 1928

The function butter needs to have constant inputs. This means that when generating code MATLAB Coder must be able to determine values for all of them.

In this case, the code passes Wn which is computed from the top input fs. Because it depends on a top level input, Wn is not a constant.

Two options to consider:

  1. Pass a literal constant for Wn in the call to butter like you did for N

  2. Pass a constant to the codegen call for fs so that the computation for Wn can be constant folded

     codegen -config:lib -launchreport myfilter -args {zeros(1,100),coder.Constant(10)}
    

Upvotes: 1

Related Questions