How can I generate these signals in Simulink? - matlab

The signals are:
Tb is bit time = 0.001s. Basically, first is square wave with 50% duty cycle and second is with 100 percent duty cycle. I tried using Pulse Generator, it is not giving me negative output and it does not let me choose 100 for duty cycle.
How can I do this in Simulink?

Behold, the Simulink model that generates the first signal:
The amplitude of the Pulse Generator is set to 2/sqrt(Tb), the duty cycle to 50%, and the period to Tb, where Tb is a variable in the (model) workspace.
As for the second graph; a square wave with 100% duty cycle:
There is, of course, no difference between a constant-valued signal and a square wave with 100% duty cycle, which is what Simulink nags about. I don't know why the Mathworks decided to explicitly forbid 100% duty cycles from Pulse Generator blocks, but oh well.

Related

Create a PWM signal based on HI time

I am using a matlab simulink hardware in the loop setup with a DAQ with digital output. It controls an RC servo.
It is a 50hz pwm. 90 degrees is a certain time HI, -90 is a different time HI. Correlate to 5 percent to 25 percent duty cycle.
In simulink, is it possible to like scale the input (or pass it thru an arithmetic formula) so a zero duty cycle produces 5% and a 100 percent duty cycle produces 25%?
Thanks
I am a college student with only simulated experience

How to convert a continous time simulink model to discrete time model in Simulink?

For example, I have a controller consisting of integrator and sine web signal combined in the input. My question may be very naive to you, please help me to understand.
In the picture I have a frequency which is the input of an integrator then two cos and sine block. How to make each Simulink block discrete using ZOH and rate transition block in Simulink.
Two potential solutions
Change the sample time of the sinewave to something that is non-zero. This will make it a discrete signal
Use a Zero Order Hold (as you've mentioned) to convert the signal to discrete, you will need to specify a sample time for this block to perform the conversion. Setting the time to 0.5 will sample the signal every half second for example.

Using low frequency data to calibrate high frequency data

I have a 10 Hz time series measured by a fast instrument and a 1 minute time series measured by a slow reference instrument. The data consists of a fluctuating meteorological parameter. The slow reference instrument is used to calibrate the fast instrument measurements. Both time series are synchronised.
My idea:
Average the 10 Hz data into 1 minute blocks.
Take 5 one minute block from each time series and calculate the linear regression equations.
Use the regression equations to calibrate the 10 Hz data in 5 minute blocks (3000 data points).
What would be the best way to match (calibrate) the high frequency data using the low frequency data? I use MATLAB.
More background: The fast instrument outputs a fluctuating voltage signal while the slow instrument outputs the true value of a trace gas concentration in ppb (parts per billion). The slow instrument samples every ten seconds and outputs the average every one minute.
In short I would like to have my fast signal also in ppb but without losing it's integrity (I need the turbulent fluctuations to remain unfiltered), hence the need to use a linear fit.
Here's my approach and the results I got...
I modelled the problem as there being
a real (unmeasured by instruments) signal.
Let's call this real.
a slow signal - which is just the real signal sampled once a minute.
Let's call this lf (short for low frequency).
a fast signal - real signal + noise + signal drift.
Let's call this hf (short for high frequency).
The task was to take the slow and fast signals and try to reconstruct the real signal.
(Using least squares as a scoring metric)
Strategy:
Define a "piecewise linear filter" - this takes a signal, and returns a piecewise version of it. (With each piecewise part occurring where the slow signal is measured.)
NOTE: The slow signal is considered piecewise anyway.
Define a forwards-backwards low pass filter.
Define "uncertainty" to be 0 at the points where the low frequency signal is measured. It linearly increases to 1 when the timestamp is halfway between low frequency signal measurements.
Now, take your high frequency signal and filter it with the low pass filter.
Let's call this hf_lp
Take hf_lp and apply the "piecewise linear filter" to it.
Let's call this hf_lp_pl
Subtract the last two from each other.
I.e. hf_diff = hf_lp - hf_lp_pl.
You now want to find some function that estimates how by how much hf_diff should be added to the low frequency signal (lf) such that the squared error between real_estimated and real is minimized.
I fitted a function along the lines of real_estimated = lf + diff.*(a1*uncertainty + a2*uncertainty.^2 + a3*uncertainty.^3)
Use fminsearch or other optimization techniques to get a1, a2, a3...
Here is a sample plot of my results - you can see that real_estimated is much closer to real than the slow signal lf.
Closing thoughts...
The fast signal contains too much very low frequency (drift) and too much
very high frequency (noise) components.
But it has valuable medium frequency info.
The slow signal has perfect low frequency information, but no medium frequency info.
The strategy above is really just one way of extracting the medium frequencies from the fast signal and adding it to the low frequency signal.
This way, we get the best of all worlds: low frequencies, medium frequencies and low noise.

Matlab: How does Simulink simulate model with block computation time much longer than clock time period?

I have a Simulink model with master clock value of 4410 Hz. I know for a fact that computation time of some algorithms (e.g. cubic spline interpolation on a 4410 sample frame being accumulated in real-time) is much longer than the master time period (i.e. computation time of spline is cca 0.7 seconds). I would expect Simulink to output frame elements AFTER initial 1 second + propagation time delay (like in hardware languages, e.g. VHDL), but it actually starts outputting the elements of the frame just after one seconds (which is the length of frame, 4410/4410 seconds). This wouldn't be a problem if my output values weren't unexpected/wrong.
How does Simulink build the simulation in this case? It would appear that it stops the simulation for larger computation times, then continues it afterwards.
A simulink simulation assumes infinite computation capacity, it does not simulate computation times. It does not stop the simulation, it does not use a real clock at all. While simulink is a bit more complicated with the different solvers, you can take a look at discrete event simulation which should give a simple example of isolating the simulation clock form your real clock.

sampling time counter in simulink

how can I take simulink time from "clock" block in simulink and then increase a counter (k)
in a matlab embedded function at every sampling period Ts?
let us say if simulink clock time is t, then we may consider
if mod(t, Ts)==0
k=k+1;
end
but this will not work since simulink time is variable.
Any idea? Thanks.
I'm assuming that you are only interested in doing this for variable step solvers. Let us assume that your sampling time is every two seconds, and your solver is ode23t, with the simulation running for ten seconds. Then, you expect to have the value of your variable be 5.
Now, the way you set up the model is in the screenshot below. Constant is your sampling time, Clock is the source for the time. You need a Rate Transition block to ensure a periodic output for a non-periodic clock input into it. I've set a sampel time of 1 for it.
Finally, in your code, you need a persistent variable. You could also use Simulink's Data Store Read and Write blocks, but this seems simpler to me.