MATLAB Answers

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

46 views (last 30 days)
Kenny on 10 Jan 2021
Edited: Kenny on 12 Jan 2021
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.
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)




Community Treasure Hunt

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

Start Hunting!