Generating square wave using an embeded matlab function

Hi,
I intend to generate a square wave which is applied on a DSP.
I have written these codes and put them in an embeded Matlab function.
function y = fcn(u)
%#eml
t=0:0.001:1
h = sign(sin(125600*t+u));
y= (h+1)/2
where, u is a constant value of 0.582 which is used for shifting the square wave.
The problem is at the out put in the simulation, instead of getting a square wave, I see only two straight lines of y=o and y=1.
Please let me know where is the problem that I can not get the square wave?
Note that the frequency of square wave must be 20 kHz. Therefore, I adjust the sampling time as 1e-7 s. And also its amplitude is between 0 and 1 In addition, due to this signal must be transferred to a DSP board, in the "solver option" I chose the type: " Fixed-step" and for the Solver: "Discrete (no continues state)".
Thanks a lot.

1 件のコメント

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 21 日
Sayedfoad, I suppose that you are accepting answers randomly.

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

 採用された回答

Walter Roberson
Walter Roberson 2014 年 1 月 21 日

0 投票

Your function is fine. You are not zoomed in enough to see the wave transitions.

1 件のコメント

Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2014 年 1 月 21 日
Thanks a lot for your reply, Actually, when I zoom in I can not see the output. Even when I convert this system using a compiler on a DSP board, it is compiled completely, but I can not see the waveform on Oscilloscope as well.
Please let me know if there is a solution.

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

その他の回答 (1 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 21 日
編集済み: Azzi Abdelmalek 2014 年 1 月 21 日

1 投票

Seyedfoad, You can't define the time inside the Embedded function, you can notice that your output y is not a scalar, but a vector with length equal to numel(0:0.001:1). The two lines you are seeing, are the 51 output of your embedded function that are equal to 1 for some of them and 0 for the others, you can check this by using a [to workspace] block.
But the question is, why not to just use a pulse generator block?
You can use an embedded function with an input signal t coming from a clock block. and the code is the same as yours without the vector time
function y = fcn(t)
u=0.582;
h = sign(sin(125600*t+u));
y= (h+1)/2;

5 件のコメント

Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2014 年 1 月 21 日
Highly appreciated for your useful answer. You opened my eyes. I tried it on simulation and it worked. As I mentioned, I must transfer this system on a DSP board and the DSP board accepts only discrete time blocks. I do not know the "clk" can be accepted by DSP or no. I have to test it.
Thanks again for your useful answer.
Regards Foad
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2014 年 1 月 22 日
Dear Mr. Azzi Abdelmalek
I tried the "clock" block in the simulation and could get the square wave signal as an output. In the simulation, it is fine. But when I try to transfer it on DSP the compiler reports this error:
---------
Block 'Final2/Clock' uses continuous time, which is not supported with the current configuration. Consider selecting the support 'continuous time' option on the Configuration Parameters > Real-Time Workshop > Interface page.
---------
Then I followed the error and I went to Configuration Parameters > Real-Time Workshop > Interface page and ticked the "continuous time". But after doing this, when I tried again, I received this new error:
--------------
The Embedded-C code format does not support continuous sample time blocks.
--------------
Please let me know if you have any idea. Certainly, it would be very effective to solve my problem.
Regards
Foad
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 1 月 22 日
I suppose you are working with a fixed-step, Try a digital clock block instead of a clock
Seyedfoad Taghizadeh
Seyedfoad Taghizadeh 2014 年 1 月 22 日
Thanks alot, I used the block and it worked.
Regards Foad
huynh quyen
huynh quyen 2016 年 8 月 19 日
dear Foad, I want to generate 40khz pulse wave on raspberry pi , I used your block as the figure following but not accurately, simulation is display precisely but when I measure on ocsiloscope is wrong display. Please, help me.

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by