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.
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.
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?
Related
I have a Matlab function (created by me) that must be evaluated only at a given rate. I would like to sample the value of a signal, give to this function (discrete values) and then, the calculated output must be hold until the next value is available. Is there a way in simulink to do this? All answers I have found use quantizer + ZOH but in this case I still get "a continuum" (or almost it) of points to be evaluated by thsi function which is really slow. Changing the rate of simulink's solver is also not an option as the result of this function will be given for a continuous time system.
Any help will be highly appreciatted!
Thank you
Assuming by Matlab function you mean a MATLAB Function block, then it sounds as if all you need to do is make the block discrete. Do that by right-clicking on the block, going down to Block Properties and then in the resulting dialog enter your required sample time.
The block will then sample its input and generate an output (which is held between sample times) at each sample time.
I'm trying to generate a sine wave without using any standard blocks available in Simulink. Right now I'm using constants for phase angle and frequency but eventually I want to vary this and hence not using the standard blocks. Here is the Simulink model
Here is the scope output
The output is not a sinusoidal wave. What am I screwing up here?
I'm using a fixed step auto solver.
As I'm currently not in the possession of Matlab/Simulink so I can't construct a working example. But I think the best solution for your problem is using a Simulink function block. In this block you can define a Matlab function of your own design and it will allow you to generate any signal you want.
Mathworks example
I've been trying to use Simulink for modelling an oscilloscope (including DSP). I am still very novice so please forgive me if the following question is way too easy: How do I implement AC coupling (remove DC component) in Simulink while using analog signals? I know there is a DC blocker, but it only accepts discrete signals and I want to discretize my signal later with an imperfect ADC.
I'm trying to use a MATLAB function for the AC coupling, but I cannot find a way to use the existing analog signal gained from the function declaration. Instead, all MATLAB documentary seems to want me to use an analog device on my computer (https://mathworks.com/help/daq/analog-data-acquisition.html).
Thank you for any efforts and/or suggestions! :)
I don't think using the Matlab Function Block will work since you need to hold a state. To simulate AC coupling, I believe that you need to create a high pass filter. This removes DC from the signal so that the waveform is centered around zero. The corner frequency generally needs to be very low so that there is no distortion of the waveform being measured. You can build this without any special toolboxes. You just need an integrator block, gain block and some summation blocks.
An example is provided below:
This example applies a DC step to the waveform at 1 second. The figure below provides the output
In the upper figure (input) the offset remains, but in the lower figure (output) the offset is removed.
If you want using the matlab AC Coupling function, try using the Matlab Function Block : https://fr.mathworks.com/help/simulink/ug/creating-an-example-model-that-uses-a-matlab-function-block.html . You can use it like any matlab function, i.e. function [out1, out2] = my_function(in1) but it will be defined in a simulink block.
I believe I am doing something fundamentally wrong when trying to import and test a transfer function in Simulink which was created within the System Identification Toolbox (SIT).
To give a simple example of what I am doing.
I have an input which is an offset sinusoidal wave from 12 seconds to 25 seconds with an amplitude of 1 and a frequency of 1.5rad/s which gives a measured output.
I have used SIT to create a simple 2 pole 1 zero transfer function which gives the following agreement:
I have then tried to import this transfer function into Simulink for investigation in the following configuration which has a sinusoidal input of frequency 1.5rad/s and a starting t=12. The LTI system block refers to the transfer function variable within the workspace:
When I run this simulation for 13 seconds the input to the block is as expected but the post transfer function signal shows little agreement with what would be expected and is an order of magnitude out.
pre:
post:
Could someone give any insight into where I am going wrong and why the output from the tf in simulink shows little resemblance to the model output displayed in the SIT. I have a basic grasp of control theory but I am struggling to make sense of this.
This could be due to different initial conditions used in SimuLink and the SI Toolbox, the latter should estimate initial conditions with the model, while Simulink does nothing special with initial conditions unless you specify them yourself.
To me it seems that your original signals are in periodic regime, since your output looks almost like a sine wave as well. In periodic regime, initial conditions have little effect. You can verify my assumption by simulating your model for a longer amount of time: if at the end, your signal reaches the right amplitude and phase lag as in your data, you will know that the initial conditions were wrong.
In any case, you can get the estimated initial state from the toolbox, I think using the InitialState property of the resulting object.
Another thing that might go wrong, is the time discretization that you use in Simulink in case you estimated a continuous time model (one in the Laplace variable s, not in z or q).
edit: In that case I would recommend you check what Simulink uses to discretize your CT model, by using c2d in MATLAB and a setup like the one below in Simulink. In MATLAB you can also "simulate" the response to a CT model using lsim, where you have to specify a discretization method.
This set-up allows you to load in a CT model and a discretized variant (in this case a state-space representation). By comparing the signals, you can see whether the discretization method you use is the same one that SimuLink uses (this depends on the integration method you set in the settings).
I'm running a SimEvents simulation using Variable-step discrete solver. I save a signal data using a 'to Workspace' block, but the plot that I obtain is different from the one shown in the 'Scope' block inside the model.
The original signal remains constant between t=64[h] and t=65.4[h] (and this seems to be correctly done also in the matlab plot), while elsewhere it is like the plot command and the 'scope' block are working with different "sample times".
I'd like to obtain a plot showing the typical "step shape" of a discrete signal rather than a "nearly continuous" signal.
I've used the Scope 'save data to workspace' as well, but I didn't solve the problem.
I would have attached some screenshots in order to make the question more complete, but this is my first question so I have not enough reputation to post images. If you need, I can send them to you via e-mail.
Thank you in advance!
There are no screenshots to look at, but my guess would be that the signal is discrete, and the Simulink scope block, knowing this, is only changing at the discrete time points.
However, if you are plotting the data dumped to MATLAB using the standard plot function then it treats the data as continuous and joins successive points with a (non-constant) line.
If that's the case, you most likely want to be using the stairs function to do your MATLAB plottig.