Generating square wave using an embeded matlab function
4 views (last 30 days)
Show older comments
Seyedfoad Taghizadeh
on 21 Jan 2014
Commented: huynh quyen
on 19 Aug 2016
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 Comment
Accepted Answer
Walter Roberson
on 21 Jan 2014
Your function is fine. You are not zoomed in enough to see the wave transitions.
More Answers (1)
Azzi Abdelmalek
on 21 Jan 2014
Edited: Azzi Abdelmalek
on 21 Jan 2014
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 Comments
huynh quyen
on 19 Aug 2016
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!