Compiling Square wave function to C language

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

回答 (1 件)

Walter Roberson
Walter Roberson 2013 年 12 月 31 日

1 投票

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 件のコメント

Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2013 年 12 月 31 日
編集済み: Seyedfoad Taghizadeh 2013 年 12 月 31 日
Thanks for your answer. However, I have still some questions on your answer. Highly appreciated if you answer them.
- How can I generate myself the square waves by coding and without the Matlab Square() function which can be run on DSP? Please note that the square wave must be able to be shifted and be made in 20 kHz frequency which means one period takes 5*10^5 seconds. And also the duty cycle is 50%.
Answer to your question: c,c1,c2,c3 are four square wave signals which are going to be applied for switching of four Mosfete switches. This switches make two bridges.
Many thanks if you give me your useful answers.
Thanks
Walter Roberson
Walter Roberson 2013 年 12 月 31 日
50% duty cycle square wave can be done as sign(sin(t + shift))
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2014 年 1 月 2 日
Thanks a lot for your useful comment. I used it and could compile the Matlab codes to C language codes.
Highly appreciated, Regards

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMATLAB Coder についてさらに検索

質問済み:

2013 年 12 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by