Compiling Square wave function to C language

6 views (last 30 days)
Hi friends,
I have written a program via coding in Matlab to generate four Square waves including phase shift. This program has four outputs: c, c1, c2 and c3. The frequency is 20 kHz and the duty cycle is 50%. I intend to apply this program for a DSP. Therefore, I have to convert it to C language first. I used a a compiler for this issue. But the compiler can not convert the Square wave function of Matlab to C language completely. This is the report that appears after compiling:
------------------------------------
>> coder('-build', 'C language.prj')
??? Failed to eliminate a call to the MATLAB function 'square'. For non-simulation builds, calls to unsupported MATLAB functions are eliminated if they do not affect function outputs.
Error in ==> TestC1 Line: 12 Column: 3
Code generation failed: Open error report.
------------------------------------
According to some solutions, I should tell MATLAB the type and size of the extrinsic function's output before calling the extrinsic function. I have done this by pre-alocating the variable with the same type and size of the (expected) output. But unfortunately it did not work. Please let me know how I can compile this function (square wave) to C language using a compiler, For your consideration the codes are as follow:
---------------------------------------
function c = output__rg( ~ )
%UNTITLED2 Summary of this function goes here % Detailed explanation goes here
%#eml
eml.extrinsic('square');
t=0:.0000001:1;
% Pre-allocate
j = t;
% THEN assign
j=square(125600*t-0.469,50);
c= (j+1)/2
% Pre-allocate
j1 = t;
% THEN assign
j1=square(125600*(t-0.000026)-0.469,50);
c1= (j1+1)/2
% Pre-allocate
j2 = t;
% THEN assign
j2=square(125600*t,50);
c2= (j2+1)/2
% Pre-allocate
j3 = t;
% THEN assign
j3=square(125600*(t-0.000026),50);
c3= (j3+1)/2
end
-----------------------------------------
Thanks

Answers (1)

Walter Roberson
Walter Roberson on 31 Dec 2013
You cannot generate C code for square(). You need to supply the code yourself if the code is going to be run on the DSP.
The closest you can get is that if you were compiling in simulation mode, then you could use the extrinsic function facility to pretend you had supplied the code, with the pretense only being executable in simulation mode.
Question: what is going to be done with c1, c2, c3 after you calculate them? You are not returning them back from the function.
  3 Comments
Walter Roberson
Walter Roberson on 31 Dec 2013
50% duty cycle square wave can be done as sign(sin(t + shift))
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh on 2 Jan 2014
Thanks a lot for your useful comment. I used it and could compile the Matlab codes to C language codes.
Highly appreciated, Regards

Sign in to comment.

Categories

Find more on Application Deployment in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!