Generate Continuous Sawtooth Wave Using Self-Reset System
This example shows how to generate continuous sawtooth wave using an if action subsystem, an integrator, and the first or last block execution order property. In this example, a ramp signal obtained by integrating a constant value of one forms the basis for wave generation. The ramp signal is turned into a sawtooth wave by checking the integrator state and resetting it when the state reaches a threshold value. This check and reset process is implemented using a self-reset system that comprises of an If Action Subsystem block and a State Writer block.
Open the sawtooth wave generator model.
mdlCont = "ContSawtoothWaveGen.slx";
open_system(mdlCont)
Open Sawtooth Wave Generator
Subsystem
To open the subsystem, double-click it in the model canvas, or enter this command.
open_system("ContSawtoothWaveGen/Sawtooth Wave Generator")
The Sawtooth Wave Generator
subsystem uses the following algorithm to generate a continuous sawtooth wave. The steps are as follows:
Check whether the integrator state value is greater than five and reverse the wave direction. This step is implemented inside the
Check and Reset State
subsystem.
2. Compute the wave value at different time steps during the simulation. This step is implemented using the blocks enclosed inside the Continuous Sawtooth Wave Generator
area. The wave value at different time steps is computed by integrating the block input value of 1 or -1.
Configure Check and Reset State
Subsystem to Execute First
At every time step of the simulation, the Check and Reset State
subsystem executes first and checks and resets (if required) the Integrator block state (IntegratorState
) value to ensure that the wave value is computed correctly. When the integrator state (IntegratorState
) value is greater than or equal to 5, an if action subsystem executes which reverses the wave direction by setting the initial state value of the integrator state to a new value obtained by multiplying the current state value by -1.
To implement this execution order, 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
.
Once configured, the block annotation indicates the block execution order as shown in the following figure.
close_system("ContSawtoothWaveGen/Sawtooth Wave Generator")
Run Simulation and Visualize Results
Simulate the model and visualize the results using a Scope block. The Scope shows a continuous sawtooth wave that is formed from a ramp signal over the span of 10 seconds of simulation.
outCont = sim(mdlCont);
See Also
State Writer | Integrator | If Action Subsystem