Module 6: Implementing Mathematical and Logical Constructs
6.1. Performing Mathematical Operations on Signals
Simulink excels at modeling the mathematical relationships between signals. This capability allows engineers to build complex systems from simpler components. This section will demonstrate how to combine signals using arithmetic blocks to create a more complex, composite signal that can then be analyzed.
We will examine an example of summing a ‘Chirp Signal’ and a ‘Sine Wave’. A chirp signal is one in which the frequency increases or decreases with time, a common test signal in communications and acoustics.
- Place the Blocks: In a new model, place the following blocks:
- One ‘Chirp Signal’ block (from Sources).
- One ‘Sine Wave’ block (from Sources).
- One ‘Add’ block (from Math Operations).
- One ‘Scope’ block (from Sinks).
- Connect the Blocks:
- Connect the outputs of both the ‘Chirp Signal’ and ‘Sine Wave’ blocks to the two inputs of the ‘Add’ block.
- Connect the single output of the ‘Add’ block to the input of the ‘Scope’.
- Configure Parameters: We will modify one parameter to make the result more distinct. Double-click the ‘Chirp Signal’ block and change the ‘Initial frequency’ from its default of 0.1 to 0.05. All other blocks can remain at their default values.
- Run and Observe: Run the simulation. The ‘Scope’ will now display a single, complex waveform. This waveform is the point-by-point mathematical sum of the original chirp and sine signals, demonstrating how easily Simulink can model arithmetic combinations of dynamic signals.
Having explored continuous mathematical operations, we will now transition to implementing discrete, conditional logic.
6.2. Building Conditional Logic with the Switch Block
The ‘Switch’ block provides a fundamental way to implement conditional if-else logic within a Simulink model. It allows the model to route signals based on whether a certain condition is met, enabling decision-making capabilities within the simulation.
Let us build a model to demonstrate its function:
- Gather the Blocks: Place the following blocks on your canvas:
- Three ‘Constant’ blocks (from Commonly used blocks).
- One ‘Switch’ block (from Signal Routing).
- One ‘Display’ block (from Sinks).
- Establish the Connections:
- Connect two of the ‘Constant’ blocks to the top and bottom input ports of the ‘Switch’ block. These represent the if and else pathways.
- Connect the third ‘Constant’ block to the central (control) input port of the ‘Switch’. This signal will be used for the condition check.
- Connect the output of the ‘Switch’ block to the ‘Display’ block.
- Configure the Switch: Double-click the ‘Switch’ block to open its parameters. Set the ‘Threshold’ value to 3.
The logic of the Switch block is as follows: If the central control input is greater than the threshold, the output will pass the signal from the top input. Otherwise, it will pass the signal from the bottom input.
To illustrate this capability, we will examine two representative examples:
- Case 1 (Condition is False):
- Set the top ‘Constant’ block value to 0.5.
- Set the central ‘Constant’ block value to 1.
- Set the bottom ‘Constant’ block value to 2.5.
- Since the central input (1) is NOT greater than the threshold (3), the condition is false. The switch will pass the value from the bottom input. When you run the simulation, the ‘Display’ block will show 2.5.
- Case 2 (Condition is True):
- Keep the top and bottom constants the same. Change the central ‘Constant’ block’s value to 3.5.
- Now, since the central input (3.5) is greater than the threshold (3), the condition is true. The switch will pass the value from the top input. When you run the simulation, the ‘Display’ block will show 0.5.
This powerful conditional logic is complemented by Simulink’s ability to model formal digital logic gates.
6.3. Modeling Digital Logic Gates
In addition to analog and conditional systems, Simulink can effectively model digital logic circuits. The primary tool for this is the ‘Logical Operator’ block, which can be configured to represent a wide variety of standard logic gates.
We will use an OR gate to illustrate this process.
- List the Required Blocks: For this model, you will need:
- Two ‘Constant’ blocks to serve as the binary inputs (0 or 1).
- One ‘Logical Operator’ block.
- One ‘Display’ block to show the binary output.
- Configure the Operator: Double-click the ‘Logical Operator’ block to open its parameters. From the dropdown list of operators, which includes AND, OR, NAND, NOR, XOR, and NOT, select ‘OR’.
- Connect the Model: Connect the two ‘Constant’ blocks to the inputs of the ‘Logical Operator’ block. Connect the operator’s output to the ‘Display’ block.
To verify its function, we can systematically test the truth table for an OR gate:
- Input (1, 1): Set both ‘Constant’ blocks to a value of 1. Run the simulation. The ‘Display’ will show 1, as expected.
- Input (0, 0): Change both ‘Constant’ blocks to 0. Run the simulation. The ‘Display’ will correctly show 0.
- Input (1, 0): Set one ‘Constant’ to 1 and the other to 0. Run the simulation. The ‘Display’ will show 1.
This module has demonstrated the implementation of continuous mathematical operations, conditional if-else structures with the Switch block, and digital logic using the Logical Operator block. As models grow in complexity by combining these constructs, techniques for managing that complexity become essential.