0
votes

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 output 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.

2

2 Answers

1
votes

This is wrong on many levels.

First of all, you never define the time vector inside a MATLAB Function, that's what the Simulink engine does. Pass time as an input to your MATLAB Function block and use a Clock block to generate the time input.

Second, the above is fine for simulation, but it sounds like you are generating C code from the Simulink model to run it (in real-time) on your DSP. This is not my area of expertise, but from memory, I think you need to enable "absolute time" or something similar for the above to work with code generation. However, I think this is target-dependent and so I'm not sure whether this will work on your DSP.

0
votes

In you function type plot(t,y) at the end. You are generating a 20khz square wave (assuming you are sampling at 1e-7). Essentially your generating it is working.

Now, what is the DSP board you are using/any information that is relevant to your problem?

I don't know what you are referring to when you say "Solver" either.

Is the "simulation" an oscilloscope or a program? Either way perhaps it is not triggering correctly? Is there an edge trigger option?