Main Content

Generate Discrete Triangle Wave Using a Self-Reset System

This example shows how to generate a discrete triangle wave using an if action subsystem, math operation blocks, and the first or last block execution order. In this example, a discrete staircase signal obtained from a counter using a Unit Delay block forms the basis for the wave generation. The staircase signal is turned into a triangle wave by checking the counter state value and reversing the count direction when the state reaches the upper and lower threshold values. This check and reverse process is implemented using a self-reset system that comprises of a subsystem that contains State Reader, If Action Subsystem and Parameter Writer blocks.

Open the triangle wave generator model.

mdl = "waveGeneratormdl.slx";
open_system(mdl);

Wave generator model.

Open Discrete Triangle Wave Subsystem

To open the subsystem, double-click it in the model canvas, or enter this command.

open_system("waveGeneratormdl/Discrete Triangle Wave")

The Discrete Triangle Wave subsystem uses the following algorithm to generate discrete triangle wave. The steps are as follows:

  1. Check if the counter state value is less than zero (lower threshold) or greater than five (upper threshold) and control the wave direction by setting the counting direction to up (1) or down (-1), respectively. This step is implemented inside the Check and Reset State subsystem.

Contents of the Check and Reset Sate subsystem that is set execute first.

2. Perform the counting. This step is implemented using the blocks enclosed inside the Wave Generator area. The wave value at each time step of the simulation is computed using a discrete time counter. The counter uses a Constant block to feed the counting direction (1 or -1), an Unit delay block holds the value from the previous time step and a Sum block adds the counting direction to the Unit Delay block state to determine wave value.

The wave value is computed at different time steps using a Unit Delay and a Sum block.

Configure Check and Reset Subsystem to Execute First

At every time step of the simulation, the Check and Reset State subsystem executes first. A State Reader block inside the subsystem reads the current value of the Unit Delay block state (CountState). Based on the state value, switch case action subsystems execute. Parameter Writer blocks in these switch case action subsystems set the counting direction by setting the value of the Constant block CountingDirection per these conditions.

  • If the CountState value is less than or equal to 0, CountingDirection is set to 1.

  • If the CountState value is greater than or equal to 5, CountingDirection is set to -1.

To configure the Check and Reset State subsystem to execute first, right click on the Check and Reset State subsystem block and choose Properties from the list. In the Block Properties dialog box, from the Execution Order, select First.

The Execution Order property list shows options for the subsystem.

Once configured, the block annotation indicates the block execution order as shown in the following figure.

Contents of the Discrete Triangle Wave subsystem.

close_system("waveGeneratormdl/Discrete Triangle Wave")

Run Simulation and Visualize Results

Simulate the model and visualize the results using a Scope block. The Scope shows a discrete triangle wave that is formed from a discrete staircase signal over the span of 10 seconds of simulation.

out = sim (mdl);

Scope block output showing the generated triangle wave.

See Also

| | |

Related Topics