EE-3221 MATLAB INTRODUCTION

Similar documents
EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

LAB 1: Introduction to MATLAB Summer 2011

EE 301 Lab 1 Introduction to MATLAB

MATLAB/Octave Tutorial

Chapter 1 Introduction to MATLAB

Laboratory 1 Introduction to MATLAB for Signals and Systems

Matlab Introduction. Scalar Variables and Arithmetic Operators

EE 216 Experiment 1. MATLAB Structure and Use

Signals and Systems Profs. Byron Yu and Pulkit Grover Fall Homework 1

How to learn MATLAB? Some predefined variables

Introduction to MATLAB

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

EE 301 Signals & Systems I MATLAB Tutorial with Questions

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

AMS 27L LAB #2 Winter 2009

1 Introduction to Matlab

A Guide to Using Some Basic MATLAB Functions

1-- Pre-Lab The Pre-Lab this first week is short and straightforward. Make sure that you read through the information below prior to coming to lab.

ECE 3793 Matlab Project 1

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

A very brief Matlab introduction

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

MAT 275 Laboratory 1 Introduction to MATLAB

Introduction to MATLAB LAB 1

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

AN INTRODUCTION TO MATLAB

The Mathematics of Big Data

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

Basic Simulation Lab with MATLAB

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

Matlab is a tool to make our life easier. Keep that in mind. The best way to learn Matlab is through examples, at the computer.

Lab 1 Intro to MATLAB and FreeMat

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Lab P-1: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Introduction to MATLAB

Introduction to MATLAB

George Mason University Signals and Systems I Spring 2016

Introduction to MATLAB

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

The value of f(t) at t = 0 is the first element of the vector and is obtained by

Introduction to Matlab

Introduction to MATLAB Practical 1

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

DSP First. Laboratory Exercise #1. Introduction to MATLAB

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Getting started with MATLAB

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

MATLAB BASICS. M Files. Objectives

HERIOT-WATT UNIVERSITY DEPARTMENT OF COMPUTING AND ELECTRICAL ENGINEERING. B35SD2 Matlab tutorial 1 MATLAB BASICS

FDP on Electronic Design Tools - Fundamentals of MATLAB 12/12/2017. A hands-on training session on. Fundamentals of MATLAB

ARRAY VARIABLES (ROW VECTORS)

EE 350. Continuous-Time Linear Systems. Recitation 1. 1

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

An Introduction to MATLAB II

A General Introduction to Matlab

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Lab 1 Introduction to MATLAB and Scripts

MATLAB Project: Getting Started with MATLAB

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Class #15: Experiment Introduction to Matlab

Introduction to Matlab

Getting Started with MATLAB

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

Exploration Assignment #1. (Linear Systems)

MATLAB SUMMARY FOR MATH2070/2970

Introduction to Matlab

Introduction to MatLab. Introduction to MatLab K. Craig 1

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

3 An Introductory Demonstration Execute the following command to view a quick introduction to Matlab. >> intro (Use your mouse to position windows on

Dr Richard Greenaway

DSP First Lab 02: Introduction to Complex Exponentials

Homework 1 Description CmpE 362 Spring Instructor : Fatih Alagoz Teaching Assistant : Yekta Said Can Due: 3 March, 23:59, sharp

Introduction to Scientific Computing with Matlab

Chapter 1: An Overview of MATLAB

INTRODUCTION TO MATLAB

GEORGIA INSTITUTE OF TECHNOLOGY. SCHOOL of ELECTRICAL and COMPUTER ENGINEERING. ECE 2026 Summer 2018 Lab #0: Introduction to MATLAB

An Introduction to Numerical Methods

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

Programming 1. Script files. help cd Example:

ECE 202 LAB 3 ADVANCED MATLAB

Complex Dynamic Systems

CS1114: Matlab Introduction

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

University of Alberta

Lecturer: Keyvan Dehmamy

Teaching Manual Math 2131

Dr Richard Greenaway

Introduction to Python Practical 1

McTutorial: A MATLAB Tutorial

APPENDIX A SOFTWARE INFORMATION

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

Transcription:

MATLAB INTRODUCTION Goal Become familiar with MATLAB and its ability to manipulate and plot discrete signals (sequences of numbers). Background MATLAB is an industry-standard software package for processing lists of data in matrix or vector form. It can perform DSP operations quickly, even on large data sets. It is a programming language and is very picky regarding syntax. Within MATLAB you will give names to lists of numbers and then perform mathematical operations on those numbers. Procedure Run MATLAB on your laptop. You will see a main Command Window this is where your work will generally take place. Set the Current Directory (top, middle of the screen) to a folder where your EE3221 lab work will be saved. In the Command Window, notice the >> prompt. Also, the Help menu can be very useful. Let s begin by generating a sequence of time values that are separated by 0.1s in the interval 0 to 1 s, and save this sequence as a vector named t. t = 0 : 0.1 : 1 You can display the result of each element in the sequence (vector) with the following: t(1) t(2) t(3) NOTE: MATLAB SPECIFIES THAT THE FIRST ELEMENT IN THE VECTOR IS ELEMENT #1, NOT #0, WHICH CONFLICTS WITH THE NORMAL DEFINITIONS IN THE DSP WORLD. Think of matrix elements instead. That is, t(1) = 0, and t(2) = 0.1000, etc. THERE IS NO element t(0). You can determine the size, or how many elements are in a vector, using the following function: size(t) 1. What are your results? Finally, it is often better to declare the time sampling period by declaring it as a variable in your code. For example: Ts = 0.1; % sampling period t = 0 : Ts : 1; Basic Plots Oftentimes we want to plot a time-valued function such as x(t) = sin(2πf t). Note that t is not an integer, but rather a continuous variable. We can create a plot by setting t to discrete values and evaluating x(t) at those values. For example, using the vectors created in the previous section enter the following: f =2 % frequency in Hz x = sin(2 * pi * f*t) Next we can plot the sine function: plot(x) % note that the samples are shown connected by line segments 1

1 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8-1 1 2 3 4 5 6 7 8 9 10 11 2. Does this plot closely resemble sin(4πt), which is a function of the continuous variable t? Why or why not? What are the values on the horizontal axis? Explain your answer. We can modify the plot with the following: plot(t, x) ylabel('sin(4\pi*t)') title('basic Sine Wave Plot') 3. What has changed and why? In class we will typically use stem plots to represent discrete sequences. For example, try: stem(x) 4. What has changed? How would you label the horizontal axis? What will happen if we type stem(t, x) on the command line? Multiple Plots In this section you will learn how to plot multiple functions on one plot or a series of plots. The MATLAB commands you will use are hold and subplot(). We will illustrate the concept by plotting two different sinusoids on the same plot. The frequency of the sinusoids will be 11 Hz and 12 Hz. Let s begin by decreasing our sampling period to 1000 samples per second. What will Ts be equal to? Ts = <?> t = 0:Ts:1; 5. What is the size of the vector t? To set f1 =11 and f2 = 12, we can execute: f1 = <?> f2 = <?> x1 = sin(2*pi*f1*t); x2 = <?>; 2

The hold command is used to hold the contents of the plot for plotting additional sequences. plot(t,x1) hold on plot(t,x2) Axes labels and a plot name can also be added. You can turn off the hold for future plots. This is accomplished with: hold off Next we create aligned subplots in a single plotting window. For example, This will create a window consisting of four plots in a single column. The syntax is subplot(num_row, num_col, plot_number). Let s plot x1, x2, x1 and x2, and x1+x2 on the four different plots, complete with axes labels. plot(t,x1) ylabel('x1(t)') subplot(4,1,2) plot(t,x2) ylabel('x2(t)') subplot(4,1,3) plot(t,x1) hold on plot(t,x2) ylabel('x1(t), x2(t)') subplot(4,1,4) plot(t,x1+x2) ylabel('x1(t)+x2(t)') title('<your name> Plots') Optional Challenge: Re-do the above replacing f1 and f2 with a 2-element column vector f (use a semicolon to separate values within square braces to make a column vector: [f1; f2]). Then, f*t is a matrix (2 1 times 1 N yields 2 N by rules of matrix multiplication) and only one assignment statement to x is needed. To extract rows of x, use x(1,:) and x(2,:). Also, try plot(t,x) when x is a 2 N matrix. sum(x) will add the 2 row-vectors. Type help sum or doc sum for more information on the sum function. Complex Numbers MATLAB works with complex numbers. Use 1i or 1j to represent the imaginary unit and, 2.4+3.4j, for example, to represent a complex number. (MATLAB also defaults to allowing just i and j to represent the imaginary unit, but avoid using them since they can be redefined, whereas 1i and 1j cannot be redefined.) 1i*1i 6. Does this display the proper result? What will 1i*1i*1i display as? 3

Let s consider a discrete signal xx(nn) = 4ee jjππ 4 nn for n = 0, 1,,7 to illustrate complex valued sequences. Notice that the independent variable n has discrete values (specifically, integer values), and therefore, x(n) as a discrete signal. One interpretation is that x(n) is a sampled version of the corresponding continuous signal. Notice, however, that the sampling period Ts is not explicit. Let s examine the signal in MATLAB: figure n = <?>:<?>:<?> x = 4*exp(-1i*pi/4*n) stem(n,real(x)) subplot(4,1,2) stem(n,imag(x)) subplot(4,1,3) stem(n,abs(x)) subplot(4,1,4) stem(n,angle(x)) % open a new figure window % specify the limits of n; if the ste psize is 1, it can be omitted % it is okay to use 1j instead 7. Add appropriate titles to the plots. What mathematical identities do they represent (hint: what identity specifies the relationship between the complex exponential and the sine and cosine)? Can you give equivalent mathematical expressions for the plots? Element-wise Operations As you have seen, MATLAB works with vectors and matrices. Operations such as addition and multiplication follow the usual rules for vectors and matrices, as you learned or will learn in MA-383 Linear Algebra. In DSP, we will frequently need to perform element-wise operations on discrete sequences. To do the operations of multiplication, division, and raising to a power in an element-wise fashion, you precede the operator with a period. Consider the signals x1(n) = n and x2(n) = (-1/2)n + 1. Let s demonstrate element-wise operations: n = -4:1:4 x1 = n x2 = (-1/2)*n + 1 x3 = x1.*x2 x4 = (-1/2)*n.^2 + n 8. Create a plot (using subplot and appropriate axes labels and titles) showing the four signals. What can you conclude from these plots? 4

Finding the Sequence Energy We conclude this lab by finding the energy in a sequence. We will use several different methods in order to illustrate additional concepts in MATLAB. The energy in a sequence is defined as EE = class. nn2 nn=nn1 xx(nn) xx (nn). This will be discussed in more detail in Let s use MATLAB to find the energy in the sequence xx(nn) = 4ee jjππ 4 nn from n1 = 0 to n2 = 7. n1 = 0 n2 = 7 n = n1:n2 x = 4*exp(-1j*pi/4*n) x_mag = x.*conj(x) E = sum(x_mag) 9. What is the result? Scripts and Functions We have been working exclusively from the command line prompt (>>). However, we can write scripts and functions that are saved for future re-use. A SCRIPT file is an external file that contains a series of MATLAB commands. By typing the filename at the command line, MATLAB will execute the commands in the script file. Script files have a filename extension of ".m" and are often called "M-files". Select File>New>Script from the menu bar. This will open an editing window where you can enter the script. Type the following: % script to compute the energy in 4*exp(-j*pi/4*n) from n = 0 to 7. n = 0:7; x = 4*exp(-1j*pi/4*n); x_mag = x.*conj(x); E = sum(x_mag); Save the file with an appropriate name and enter the following: >>dir You should see the script file in the list. If not, you will need to copy the script file to the default directory in MATLAB (or change your Current folder to the location of the file). Next run the script by typing the name at the command line prompt. <name_of_your_file> E 10. What is the result? 5

Finally, we can create a function to perform this operation. Select File>New>Function. A new editing window will open and you should see the following: function [ output_args ] = Untitled( input_args ) %UNTITLED Summary of this function goes here % Detailed explanation goes here end Edit this text with the following: function E = energy(input_sequence) mag_input = input_sequence.*conj(input_sequence); E = sum(mag_input); end and save after editing. Let s test the function: >>n1 = 0; >>n2 = 7; >>n = n1:n2; >>x = 4*exp(-1i*pi/4*n); >>E = energy(x) 11. What is the result? 6