Compiling Square wave function to C language

4 Ansichten (letzte 30 Tage)
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh am 31 Dez. 2013
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

Antworten (1)

Walter Roberson
Walter Roberson am 31 Dez. 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 Kommentare
Walter Roberson
Walter Roberson am 31 Dez. 2013
50% duty cycle square wave can be done as sign(sin(t + shift))
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu DSP Algorithm Acceleration finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by