Experiment 1 Introduction to MATLAB and Simulink INTRODUCTION MATLAB s Simulink is a powerful modeling tool capable of simulating complex digital communications systems under realistic conditions. It includes prepackaged blocks that encompass complete modulator, demodulator, coder, and decoder systems. There are also basic building blocks which can be combined to produce custom communications systems. Along with Simulink s basic blocks, there are blocksets for both communications and digital signal processing. To allow the testing and debugging of systems, there are a number of signal sinks which provide views of the signal in the time or frequency domain. Blocks to analyze the performance of a system are provided along with realistic channels which model impairments, such as noise and multipath distortion. A key point to remember is that Simulink is a sampled, discrete time system. This has implications for how fast a signal may change. MATLAB itself is an extensive mathematics tool that facilitates the manipulation of vectors and matrices and the calculation of complex equations using scalars, vectors and matrices. It possesses a number of tools for the plotting of data, including two and three dimensional graphs. There is an exhaustive set of functions which provide analytical tools like the Fourier Transform, Error and Complimentary Error (or Q) Functions. The purpose of this experiment is to acquaint the student with Simulink and provide a basic review of MATLAB. Students who are not familiar with MATLAB at all should refer to an excellent set of online tutorials at http://www.mathworks.com/academia/student_center/tutorials/launchpad.html. Information and tutorials about Simulink can be found at http://www.mathworks.com/academia/student_center/tutorials/simulink-launchpad.html.
PROCEDURE 1. MATLAB Tour and Review: Open MATLAB and verify that the arrangement of the desktop is as shown below: 2. Enter the following vectors in the Command Window at the >> prompt as shown below: g=[1 2 3 4]; h=[1 2 3 4]; Note the individual elements are separated by spaces. A semicolon would indicate a new row in a matrix. Also note the semicolon at the end of each entry. This tells MATLAB not to echo the entry or a result. 3. Multiply the two vectors using the * character, as shown: g*h Note: this returns an error. MATLAB uses the * character to indicate the multiplication of two matrices. 4. Multiply the two vectors using the.* character, as shown: g.*h This multiplies the vectors element by element, returning the squares of each number: 1 4 9 16 This also applies for division. Try g/h and g./h. 5. Add the two vectors as shown: g+h Notice that this returns a result, adding the two vectors term by term, even though there is no period before the plus sign. This is because matrix addition is defined as term by term. The same is true for subtraction. 6. Create a simple square matrix using the form: g=[1 2 3; 4 5 6; 7 8 9]
Omitting the semicolon on the end of the line will cause MATLAB to echo back the matrix as: g = 1 2 3 4 5 6 7 8 9 Now take the transpose of this matrix by entering: g This returns: ans = 1 4 7 2 5 8 3 6 9 Use the help menu to find other matrix and vector operations 7. Enter the following commands: for n =1:360 pi= 3.14159; bear = pi*n/180; g(n)=sin(bear); h(n)=cos(bear); end Notice that these vectors appear with information about them in the Workspace pane. Plot the g vector with the following command: plot(g) Observe that the Figure 1 window appears with the sine wave. Now enter: plot(h) Notice that the sine wave has been replaced by the cosine wave. Now investigate the hold on and subplot commands to first, plot both vectors on a single plot, using different colors for each (see the plot command for that). And second, to plot the vectors on two separate panes in the same figures window. Finally, investigate using the plot command to plot a limited range of the vectors. For example, from n= 90 to n=270. At any point you can get help on a command by typing help followed by the command. For more detailed help, enter doc followed by the command. 8. Now open Simulink, either by entering simulink on the command line or by clicking on the icon shown below in the tool bar. This will open the Library Browser for Simulink. 9. Open a new model. (Under the File pull down menu). This will open a new window. Save this model with a name you choose.
10. Notice the different block sets in the Library. Find the Random Integer Generator by either searching for it or opening the Communications Blockset. Drag and drop this block into your model. Double click on the block in your model to open the dialogue box. Set M-ary Number to 2 and leave the rest of the entries as default. Now enter Scope into the search bar of the Library Browser. Notice this yields a number of scopes. Drag the first Scope under Simulink s Sink Library to your model and connect its input to the output of the Random Integer Generator. This can be done by clicking on the input of the scope and dragging a line to the output of the generator. Run the simulation by clicking on the play arrow in the model toolbar. When the simulation finishes, double click on the Scope block and observe the output of the Generator. Run the simulation again. Notice you always get the same pattern of bits. Open the Random Integer Generator block and change the Initial Seed to another number. Run the simulation again several times. Notice you always get a certain pattern of bits, but the pattern depends on the Initial Seed. Find the simulation time entry in the model tool bar to the right of the play button. It should be a default of 10. Change this to 20 and observe the scope. Notice that Simulink does not play in real time. It completes the 20 second simulation in about one second or less. 11. Often it will be useful in this lab to make the sample rate higher than the bit rate, i.e to have more than one sample/bit. Drag the Ideal Rectangular Pulse Filter to the model and place it between the Generator and the Scope. Open the block and set the following: Pulse Length: 10 Input Sampling Mode: Sample-Based Linear Amplitude Gain: 10 This will increase the sample rate to 10 samples per bit. Notice you must increase the gain by the same amount to compensate for the increase in samples. Run the model. Use the scope to determine if the waveform is any different. 12. Insert a Spectrum Scope into the model at the output of the Rectangular Filter. Set the following parameters in the Spectrum Scope. Buffer Input (checked) Buffer Length 64 Buffer Overlap 0 Specify FFT Length (checked) FFT Length 64 Number of spectral averages 32 Leave the rest of the parameters at their defaults. Set the simulation time to 200 and run the system and observe the location of the first null in the Spectrum Scope. You may need to select Axis Autoscale to observe the display. You may see some dips in the spectrum but the first null will be below -10dB.
13. Repeat steps 11 and 12 with a Pulse Length of 20. Remember to change the Gain in the Rectangular Filter to 20. Also change the Buffer Length to 128 and the FFT length to 128. Observe any differences in your results. For example, how wide is your frequency spectrum now? Are the nulls at different frequencies? What has changed about the spectral display qualitatively, e.g. is it smoother or rougher? 14. Return the Pulse Length to 10 and remember to adjust the gain accordingly. Insert the AWGN Channel between the output of the Rectangular filter and the Scope. Set the number of axes on the Scope to 2. Connect the second Scope input to the output of the Rectangular Filter. Remove the Spectrum Scope. Set the Signal to Noise Ratio (SNR) to 100 db, set the Power to 1 and run the system for 10 seconds. Verify that the two traces on the Scope are identical. Set the Signal to Noise Ratio to 10 db and run the system again. Compare the two traces. You will see the noise is not a continuous waveform, but made up of 10 samples for each symbol. For simplicity, we will set the decision threshold for whether the signal is a one or zero at 0.5. For how many of these samples is the channel output greater than 0.5 when the channel input is a zero? How often is the reverse true, i.e. a 1 is sent and the channel output is below 0.5? Try this with other signal to noise ratios, such as 5, 15, 20 and 25 db. You may have to expand the display vertically using the magnifying glass icon in the Scope display. Set the Signal to Noise Ratio to -10 db and run the simulation. How often does the channel output agree with the input? THOUGHTS FOR CONCLUSION In your conclusion you should think about the following: What are the advantages and disadvantages of using this type of simulation? What would be the advantages and disadvantages of increasing the sample rate in step 11? What ways might you think of for improving the detection of the signal when it is corrupted by noise? What is the worst error rate possible (i.e. when the SNR is very small or even negative)? Do not limit your conclusion to these questions. Think about what the experiment means and where it leads for future work.