Sample and hold block (Simulink) - initial value dominates at t = 0?

17 views (last 30 days)
Hi all !
In my testing of a simulink sample and hold block, I notice that the time (t = 0) output of the simulink sample and hold block (during a simulink simulation) will be equal to the 'initial value' setting (such as the default initial value of 'zero'). This is probably expected behaviour.
However, if I have some applications (simulations) where I would like the sample-and-hold block to capture the sample-and-hold's input value at t = 0 (such as an input value of '1'), then is there a method or setting that overrides the 'initial value' of the sample-and-hold block?
I've provided an example of my current situation here. The simulink file is from Matlab R2013b.
The simulation (running with fixed-step solver) simply involves a counter (that counts from 1 to 7). The counter just counts the rising edge events of the pulse generator. The pulse generator produces only regular squarewaves. The rising edge of those square-waves triggers the counts of the counter.
The sample and hold block is set up to sample the output value of the counter at each rising-edge trigger event of the counter's input.
The following behaviour is noticed with the simulation:
When the simulation runs ----- the output of the counter at time t = 0 will be '1', so that there is a step of '1' at time t = 0 for the output of the counter.
The sample-and-hold does not capture the output = 1 value of the counter associated with time t = 0.
Instead, the sample-and-hold block at time t = 0 captures the 'initial value setting' of the sample-and-hold block (ie. setting of '0'). And if I had chosen to change the initial value of the sample-and-hold block to some other value (such as -9), then the output of the sample-and-hold block would be some other value (such as -9).
Extra notes: The simulink fixed-step sampling period is 'base_sample_period, set to 5 nanosecond' (which is set in View -> Model Explorer -> Callbacks -> InitFcn*). And the pulse generator produces regular square-waves with a period of 5 microsecond.
I've included a couple of screen-shots. The first image shows the counter output (seen with a 'scope') - showing a step of '1' at time t = 0.
The second image shows the output of the sample-and-hold block, where the step of '1' is not there at time t = 0. That is, the sample-and-hold is not capturing the t = 0 step jump of the counter?
Is there a general method that everybody uses where we can get the sample-and-hold to capture the t = 0 value of the counter?
Thanks very much for any help in advance!

Accepted Answer

Pat Gipper
Pat Gipper on 11 Jan 2021
I don't have the DSP System Toolbox so have replaced the Sampe and Hold with a Triggered Subsystem which exhibited the same problem you describe when it uses its Trigger default "compatibility (no trigger on first evaluation)" Instead if you set the Trigger to "zero" it will sample at time zero.
  5 Comments
Pat Gipper
Pat Gipper on 12 Jan 2021
Scope exhibits the same behavior in 2019b. Pretty weird isn't it. It will still do this even if you insert a unit delay before the STOP. But if you manually change the time span on the time axis of the scope to something much smaller (1e-8) the trace will show up as long as there are at least two sample. If you select the scope to log the data to the workspace you will see that the time zero values are what you wanted.
Kenny
Kenny on 12 Jan 2021
Edited: Kenny on 12 Jan 2021
Thanks again Pat! I confirm your observations. You're absolutely right there. I agree - very interesting.
The 'display' blocks will register the value for a sample occurring at t = 0, while the scope displays no spike at t = 0 for a simulation having just a single sample taken at t = 0.
And the scope will display what I want to see (graphically) when there are at least two samples taken.
Thanks for the nice recommendation about using the scope to log the data to workspace! I didn't think of doing that before. And sure enough ----- for a single sample taken at time t = 0 only ---- the workspace results is showing (in the logged scope array) a value of 1 associated with time t = 0. The scope detects the value, and logs it, and is just not plotting it.
Greatly appreciated! Thanks Pat!

Sign in to comment.

More Answers (0)

Categories

Find more on Event Functions in Help Center and File Exchange

Products


Release

R2013b

Community Treasure Hunt

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

Start Hunting!