MATLAB: Problem Compiling function subset with EMLC

emlc butter compiling dsp

Hello everybody,
I have problems with the emlc compiler. I want to generate c code for a lowpass filter for a real time DSP. I followed the Matlab tutorial and created a M-file with the code:
function [ output ] = Lowpassfilter ( input ) %#emlc
n = 5; Wn = 0.5;
[b a] = butter (n, Wn, input);
output = filter(b, a, input);
%input comes from the ADC and is int32 in the DSP program
%the ADC samples at 48kHz and provides new data through the bus every sample time
I have tried the command lines: emlc Lowpassfilter and some derivations (-c -t RTW). The error message is: "Function 'butter' imlicitly resolved in the Matlab workspace…" How can i use matlab subset functions, when compiling them with emlc? Do i need to add -eg declarations for the DSP input and how do i do that?

Best Answer

Sorry about that. Since 'high' is a string input, it works better if you use:
emlc Lowpassfilter.m -eg {emlcoder.egc('high')}
I fixed my answer above also.
I corrected your code slightly, and this works
%#eml
function [ output ] = Lowpassfilter ( input )
%assert(isa(input,'int32'));
assert( ~isreal(input));
n = 5; Wn = 0.5;
a = zeros(1,n+1);
b = zeros(1,n+1);
[b a] = butter (n, Wn, 'low');
output = filter(b, a, input);
You had two errors:
  1. Variables 'a' and 'b' must be pre-allocated, so I used ZEROS to do this
  2. The function filter cannot take int32 inputs, so I removed the assert for datatype - emlc will probably assume a floating point input now. There is filter in Fixed-Point Toolbox that does allow int32 inputs, but the second parameter should always be 1.