DATA COMMUNICATIONS 9 1.4 USING MATLAB AND SIMULINK FOR SYSTEM MODELING AND SIMULATION IN DATA COMMUNICATIONS 1.4.1 INTRODUCTION This work is an introduction in MATLAB and SIMULINK. Then, some periodic data signals (time domain representations) and their spectra (frequency domain representations) are analyzed using these tools. MATLAB is a group of programs dedicated to numerical calculus and graphical representations. MATLAB works with matrixes and it has a rich set of resources that allow fundamental mathematical operations, data analysis, programming, 2D and 3D representations, designing graphical interfaces, etc. As software, MATLAB is composed by a set of system files and some TOOLBOXES. These TOOLBOXES are some specific applications, sets of MATLAB functions, grouped together for several technical domains. Each toolbox has a name that specifies its domain. SIMULINK is a software package for modeling, simulating, and analyzing dynamical systems. It supports linear and nonlinear systems, modeled in continuous time, sampled time, or a hybrid of the two. For modeling, SIMULINK provides a graphical user interface (GUI) for building models as block diagrams, using click-and-drag mouse operations. SIMULINK includes a comprehensive block library of sinks, sources, linear and nonlinear components, and connectors. The user may also customize and create new blocks. After defining a model, it can be simulated either from the SIMULINK menus or by entering commands in MATLAB’s command window. Using scopes and other display blocks, the simulation results can be seen while it’s running. In addition, parameters can be modified and the simulation rerun. 10 SPECTRAL ANALYSIS OF PERIODIC DATA SIGNALS 1.4.2 BUILDING A SIMPLE MODEL This example shows you how to build a model using many of the model building commands. The model generates a sampled sine wave, saves the output string as a vector to the workspace, and displays the wave. The block diagram of the model looks like in figure 1. Fig. 1.9 A simple model: the sine wave generator. In order to run the MATLAB software find the MATLAB icon on the desktop and open it with a mouse double-click. To start the SIMULINK type simulink in the MATLAB command window. As a result, the SIMULINK library browser is displayed, and if no other model window is open, a new untitled model window. Open the model file Periodic_Spectral_Analysis.mdl from the local directory (Matlab/Work), after it was copied from the website. The spectral analyzer block „Averaging Power Spectrum Analyzer” is already introduced in this model window. In this model you get blocks from these libraries: Simulink + Sources library (the Sine Wave block); Simulink + Sinks library (the Scope block). Open the Simulink library to see its content. Then open the Sources library to access the Sine Wave block. To open a block library, double-click on the library’s name or click on the plus sign in front of it. A list with all the blocks in the library will be displayed. In the Sources library, all blocks are signal sources. To add blocks to a model copy them from a block library or from another model. For this exercise, you need to copy the Sine Wave block. To do this, position the mouse cursor over the Sine Wave name in the library browser, then DATA COMMUNICATIONS 11 press and hold down the mouse left button. Now, drag the block into the model window. When the pointer is where you want the block to be in the model window, release the mouse button. A copy of the Sine Wave block is now in your model window. In the same way, copy the rest of the blocks into the model window. If you examine the block icons, you see an angle bracket on the right of the Sine Wave block and one on the left of the Scope block. The > symbol pointing out of a block is an output port; if the symbol points to a block, it is an input port. A signal travels out of an output port and into an input port of another block through a connection line. When blocks are connected, the port symbols disappear. Connect the Sine Wave block to the input port of the Scope: position the pointer over the output port on the right side of the Sine Wave block. Notice that the cursor shape changes to a cross. Hold down the mouse left button and move the cursor to the input port of the Scope. Notice that the line is dashed while the mouse button is down and that the cursor shape changes to double-lined cross as it approaches the Scope block. Now release the mouse button. The blocks are connected. To connect the output port of the Sine Wave block to the input of the To Workspace block, a line must be connected to another line. This line, called a branch line, connects the Sine Wave output to the To Workspace block, and carries the same signal that passes from the Sine Wave block to the Scope. Drawing a branch line is slightly different from drawing the normal line. To weld a connection to an existing line, follow these steps: 1. First, position the pointer on the line between the Sine Wave and the Scope block. 2. Press and hold down the Ctrl key. Press the mouse left button, then drag the pointer to the To Workspace block’s input port or over the block itself. 3. Release the mouse button. When the all the blocks are connected the simulation may be started. But first, some further settings should be performed. The most important step is to fix the right simulation parameters. Each block works in accordance with its parameters. The user may change the values of these parameters. Double click on the block’s icon and the Block Parameters window opens. For example, the user may set the following Sine Wave block parameters: the amplitude (A), the direct current component (C – “bias”), the frequency (ω, in rad/s), and the phase (in radians) of the wave. Another parameter is the sample time, which makes the wave discrete (this parameter may be set to 0, and than the wave becomes continuous). The signal will be generated using the following expression: 12 SPECTRAL ANALYSIS OF PERIODIC DATA SIGNALS y(t) = A sin(2p f t + φ) + C (1.2) Let’s consider the following values for the Sine Wave block parameters: the amplitude = 1, the DC component = 0, the frequency = 2*pi*100 rad/s (100Hz), the phase = 0, and the sample time = 0. The „Averaging Power Spectrum Analyzer” block displays in the same window three plots (from up to down): the time domain representation (split into temporal window, displayed one by one), the average power spectrum, and the average phase spectrum. Set the following parameters for the „Averaging Power Spectrum Analyzer” block: length of buffer = 512, number of points of fft = 1024, plot after how many points = 16, and sample time = 1/1000. Now, open the Scope block to view the simulation output. Keeping the Scope window open, run the simulation. First, set the simulation parameters by choosing Parameters from the Simulation menu. On the dialog box that appears, fix the Stop time of the simulation. For this example fix the stop time to 0.01. Close the Simulation Parameters dialog box by clicking on the Close button. Choose Start from the Simulation menu (or press Ctrl+T) and watch the traces of the Scope block’s input. Tip: Set the stop time to be larger than the repetition interval (signal period). The simulation stops when it reaches the stop time specified in the Simulation Parameters dialog box or when you choose Stop from the Simulation menu. Practice: 1. Run the simulation several times for different values of the repetition frequency, and the stop time. 2. Consider a non-null DC component (“bias”) in the simulation. 3. Modify the Sine Wave block to generate a cosine wave and run the simulation. 4. Consider a value for the sample time different from zero at the „Averaging Power Spectrum Analyzer” block. What criteria must be taken into consideration when you select a value for the sample time? 5. Consider different values for the other parameters of the „Averaging Power Spectrum Analyzer” block. What is the relation between these parameters and the sampling period? Which are the optimum values of these parameters? DATA COMMUNICATIONS 13 1.4.3 AMPLITUDE SPECTRUM ANALYSYS OF PERIODIC DATA SIGNALS Change the sine generator block by the “Pulse Generator” block from the Sources library. The resulted scheme is illustrated in Figure 1.10. Fig. 1.10 Periodic data signal generator. Let’s consider the following values for the Pulse Generator block parameters: the amplitude = 1, the period = 0.01 s, the pulse width = 50%, the phase delay = 0 s. Than, this block will generate a 1:1 periodic data signal, with the bit frequency = 200Hz, and the fundamental frequency = 100Hz. Set the following parameters for the „Averaging Power Spectrum Analyzer” block: length of buffer = 512, number of points of fft = 1024, plot after how many points = 16, and sample time = 1/5000. Fix the Stop time of the simulation to 1 s and start the simulation. Running the simulation for these parameters the screen of the scope will present four periods of a sampled 1:1 data signal. Its spectrum is presented in Figure 1.11. Fig. 1.11 The amplitude spectrum of a 1:1 data signal. 14 SPECTRAL ANALYSIS OF PERIODIC DATA SIGNALS Note that the amplitude spectrum of a 1:1 data signal is a discrete spectrum, consisting of a series of spectral lines at the multiples of the repetition frequency (in this particular case, at the multiples of 100Hz). Also, note that the even order components have null amplitude. It is known that the theoretical shape of these components is of Dirac pulses. Explain why each component from Figure 1.11 has a finite bandwidth, and is not a Dirac pulse. Exercises: Modify the above model to generate each of the signals: 1:3, 1:9, and 1:19, all having the same fundamental frequency = 100Hz and analyze their simulated spectra. 1.4.4 QUESTIONS 1. How does the practical bandwidth of the analyzed signals depend on T and τ? 2. What happens if τ is decreasing to zero? 3. What happens if T is increasing to infinite? 4. What are the possible causes of the differences between the simulated and the theoretical values?