Simulink configuration option "Treat each discrete rate as a separate task" auto-generates a rate transition at ZOH block when going from lower to higher frequency

48 views (last 30 days)
Hello all,
Im preparing a simuling discrete model for an aplication that requires autocoding. For that I need to tick the option in configuration parameter 'Treat each discrete rate as a separate task'. However, when I try to run it it doesn't compile due to the different sample times the signals in the model have. Prior selecting that option the model was running fine.
I have created a simple model just to ilustrate the issue (attached). It runs at fixed step of 2Hz, ode4. Two signals are generated at 2Hz and 0.5Hz and are later resampled to 1Hz by ZOH blocks. So, one signal is going from a higher rate to a lowe one (2Hz->1Hz), while the other goes from a slower rate to a higher one (0.5Hz to 1Hz).
With the 'Treat each discrete rate as a separate task' unseleted the model and results are the following:
The results are the ones I would expect, the values before and after the ZOH match at the new sampling rate.
However, when I togle the 'Treat each discrete rate as a separate task' and try to compile the model it throw me an error regarding the difference in rate of the bottom ZOH input and output signal:
The sample time 1 of 'ZOHissues/Zero-Order Hold1' at input port 1 is different from the sample time 2 of 'ZOHissues/Random Number1' at output port 1.
and suggest me different options, all of them seems to generate a similar effect. I have selected the following suggestion just to show the problem:
Compiling the model and seeing the results I get the following:
As you can see the model is introducing a rate transition before the bottom ZOH (with initial condition 0 and sample time of 1Hz) which delays the signal by 2 seconds (1/0.5Hz). I was not expecting that.
I don't see any reason why this new rate transition is necesary when toggling this option since at t=0 the "RandomNumber1" block is generating a value for the "ZOH1" to resample it (similar to what the top line does or what was been done when the option was not toggled). Could someone explain it to me?
I've been trying to delete this behaviour but the only thing that i have achived is to reduce the delay to 1s (changing the configuration parameter from 'whenever posible' to 'never'). Is there any way to remove it?
It seems to me that the model is taking as initial condition the value in the rate transition since -I deduce- it is "compiling first" the blocks that work at higher frequency and then the slower ones (I may be wrong, I have no idea). But for the particular model that im showing I see no reason to do it that way.
Can someone explain me why that rate transition/delay is necesary? or preferably, how to prevent that signal delay while enabling 'Treat each discrete rate as a separate task' ?
Im attaching the model as it is when it fails. Note that I'm working in 2018a... I've not checked if this still happens in the 2025 version.
Thank you in advance.

Answers (1)

Abhipsa
Abhipsa 3 minutes ago
I understand that you have a discrete multirate model (two signals: one at 2 Hz, one at 0.5 Hz) that then both go through ZOH blocks to a 1 Hz signal. When the configuration parameter “Treat each discrete rate as a separate task” is not enabled, the model runs fine. When you do enable that parameter (to support code-generation/autocoding workflow) the build fails with an error about a “sample time 1 of … at input port 1 is different from sample time 2 … at output port 1” for the ZOH block.Moreover, a Rate Transition block is automatically inserted for the 0.5 Hz→1 Hz branch, and the signal gets delayed (by 2 s) via that Rate Transition.
The main concern is: Why is that automatic Rate Transition/delay being introduced when enabling the “separate task” option? And is there any way to prevent the signal delay while still enabling “Treat each discrete rate as a separate task”?
When you enable “Treat each discrete rate as a separate task”, Simulink switches from single-tasking to multitasking execution.
Each unique discrete sample time (0.5 s, 1 s, 2 s, etc.) is now run in its own task with its own scheduling and priority.
In multitasking mode, two blocks running in different tasks cannot exchange data directly — doing so risks race conditions and nondeterministic execution.
To maintain data integrity and deterministic timing, Simulink enforces that data flowing across task boundaries go through a Rate Transition block (explicit or automatically inserted).
In your model:
  • The 0.5 Hz → 1 Hz path is a slow → fast rate transition.
  • The slower task updates its output only every 2 s, while the faster task (1 Hz) executes every 1 s.
  • Therefore, the faster task reads the last available value from the slower task until a new value arrives — effectively introducing a one-slow-sample (2 s) latency.This delay is not a bug but a direct consequence of deterministic data transfer in multitasking systems.
How to fix or control the behavior
You have a few options depending on your goal:
  1. Keep multitasking and fix properly
  • Go to Configuration Parameters → Solver
  • Enable Automatically handle rate transition for data transfer
  • Set Deterministic data transfer to one of the following:
  • Always or Whenever possible → fully deterministic (safe but with delay)
  • Never (minimum delay) → minimizes latency but may be nondeterministic
  • This allows Simulink to automatically insert and configure the necessary Rate Transition logic.
2. Manual control
  • Insert an explicit Rate Transition block between the 0.5 Hz source and the 1 Hz ZOH input.
  • Check Ensure data integrity during data transfer.
  • If you want to reduce the perceived delay, uncheck Ensure deterministic data transfer.
  • (Optional) Set the block’s Initial condition to your desired starting value.
You can refer to the following MATLAB documentation for more details:

Categories

Find more on Multicore Processor Targets in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!