Problem Compiling function subset with EMLC

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?

 Accepted 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.

6 Comments

1. Yes i have missed out that one.
2. Where in the help is this written?
I have changed the code and entered the command line emlc Lowpassfilter.m -eg {emlcoder.egc('high')}
The error is "Function 'Lowpassfilter' resolved to 'C:/Program Files/Matlab_2/toolbox/filterdesign/filterdesgin/Lowpassfilter.m' on the Matlab path, but it is not on the Embedded Matlab path. To compile this function, add its path to the Embedded Matlab path using the '-l' compiler option."
What is the Embedded Matlab path? Is it the path for the Embedded Matlab coder toolbox? Please note that Matlab is not installed in the default folder, but in a folder called Matlab_2.
Thank you
Regarding the use of "filter", I had linked the documentation to both functions in my answer (the word "filter" is hyperlinked).
Yes, the path the error refers to is the path for the Embedded MATLAB coder.
Also, it looks like you have placed you function Lowpassfilter.m in the same directory as a MATLAB toolbox - this is not good practice, because:
1) These directories are reserved for MathWorks' files, so it may cause confusion if you put your files within the MATLAB installation direcrory.
2) When you update MATLAB and install in the same directory, the folders get overwritten, and you will lose your files.
Therefore, I would recommend moving your file to your personal directory and running emlc from that directory.
My folder structure is the following:
Matlab & Toolboxes are installed in a folder called C:\Program Files\Matlab_2
All Projects are installed in a folder ...\Username\Documents\Matlab (standard folder)
So the file Lowpassfilter.m wasn't in the same folder as the toolbox. However there is a file also called lopwassfilter.m in the folder toolbx\filterdesign. So my function name wasn't chosen very well.
I have created a new file called LPfcn.m in the Projects standard folder.
The code stayed the same
%#eml
function [ output ] = LPfcn( input )
assert ( ~isreal (input) ); %complex input
n = 5; wn= 0.041;
A = zeros(1, n+1);
B = zeros(1, n+1);
[B A]= butter(n, wn, 'low');
output = filter(B,A,input);
end
the command line was:
emlc Lowpassfilter.m -eg {emlcoder.egc('high')}
The error messages were:
-Function 'butter' imlicitly resolved in the Matlab workspace. Implicit evaluation in Matlab is not supported.
-Undefined function or variable 'A' and 'B'. The first assignment to a local variable determines its class
What is wrong?
What version of MATLAB are you using? Could you browse to your Help window (type "doc" at command prompt to open) to Embedded MATLAB->User's Guide->Working with the Embedded MATLAB Subset->Embedded MATLAB Function Library Reference and see if "butter" is listed in the table?
I am using Matlab R2008a.
Thank you asking me. I looked up on the web if butter is a subset function and by default R2010b as help is set, but in R2008a this function is not a subset of the Embbeded Coder.
One final question about the definition of variables:
I want the input and output to interface with my DSP programming, which is int32. You said the function filter doesn't take int32 according to the link. I really don't see where you get that from.
Besides that how can i interface this with my DSP? The input from the generated code offers me: cons mxArray *eml_input and real_t eml_b_input, but I do not know how to handle the output. The code snippet is:
/* Marshall function outputs*/
eml_b_y = Null;
eml_m0 = mxCreateDoubleScalar (eml_dbuffer[0]);
emlrtAssign(&eml_b_y, eml_m0);
eml_plhs[0] = eml_b_y;
MATLAB in general does not handle fixed-point data - almost all functions expect floating-point data unless otherwise mentioned in the documentation. You will even see an error when you try something like:
>> filter([1 2],1, int32([1:4]))
Undefined function 'filter' for input arguments of type 'int32'.
Regarding the generated code, it seems like you are looking at the LPfcn_api.c file, which demonstrates how you can pass in data obtained from the MATLAB command prompt (which are of type mxArray). Since you don't need to use mxArrays, you can directly look at LPfcn.h - that will show you the actual function prototype.

Sign in to comment.

More Answers (1)

As explained in the entry for butter in the Embedded MATLAB Function Library Reference, you need to provide constant inputs to the function. In your function, 'input' is a non-const variable. To specify it as constant, you can use something like:
emlc mytest.m -eg {emlcoder.egc('high')}

1 Comment

Sorry for the late answer:
I have understood that 'input' needs to be specified as constant.
I have tried you emlc Lowpassfilter -eg 'high', giving me the the error "failed to evaluate "high" to non-empty array in the base word"
What does this mean?
I have tried to handel the 'input' parameter problem by defining it in the m-file:
%Code
%#eml
function [ output ] = Lowpassfilter ( input )
assert(isa(input,'int32'));
assert( *isreal(input)); %defines input as complex; dont know if that is correct
n = 5; Wn = 0.5;
[b a] = butter (n, Wn, 'low');
output = filter(b, a, input);
If I type emlc Lowapssfilter, it gives me the error "function butter implicitly resolved in the Matlab workspace" & "undefined variable for A and B"
Please help me with this problem

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!