LAB 1: Introduction to MATLAB Summer 2011

Similar documents
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.

MATLAB/Octave Tutorial

How to learn MATLAB? Some predefined variables

A Guide to Using Some Basic MATLAB Functions

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

EE-3221 MATLAB INTRODUCTION

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

DSP First. Laboratory Exercise #1. Introduction to MATLAB

A very brief Matlab introduction

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

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

George Mason University Signals and Systems I Spring 2016

Introduction to MATLAB Practical 1

Introduction to Matlab

Introduction to MATLAB LAB 1

AMS 27L LAB #2 Winter 2009

A Brief Introduction to MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

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.

EE 301 Signals & Systems I MATLAB Tutorial with Questions

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Laboratory 1 Octave Tutorial

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

1 Introduction to Matlab

EE 301 Lab 1 Introduction to MATLAB

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

Introduction to MATLAB

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

EGR 111 Introduction to MATLAB

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

AN INTRODUCTION TO MATLAB

MORE MATLAB. MATLAB variables

Lab 1 Introduction to MATLAB and Scripts

Introduction to MATLAB

Dr Richard Greenaway

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Finding, Starting and Using Matlab

INTRODUCTION TO MATLAB

An Introduction to MATLAB II

Introduction to MATLAB Programming

Grace days can not be used for this assignment

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

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

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

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.

Laboratory 1 Introduction to MATLAB for Signals and Systems

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

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:

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

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

Some elements for Matlab programming

Chapter 2. MATLAB Fundamentals

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

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

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

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

Introduction to MATLAB

University of Alberta

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

EE3210 Lab 1: Introduction to MATLAB

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

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

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

Introduction to MATLAB

Math 2250 MATLAB TUTORIAL Fall 2005

Introduction to Matlab

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

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

Lecturer: Keyvan Dehmamy

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

Dr Richard Greenaway

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

Lab. Manual. Practical Special Topics (Matlab Programming) (EngE416) Prepared By Dr. Emad Saeid

EEE161 Applied Electromagnetics Laboratory 1

ARRAY VARIABLES (ROW VECTORS)

16.06/16.07 Matlab/Simulink Tutorial

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

A Very Brief Introduction to Matlab

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

Matlab Tutorial 1: Working with variables, arrays, and plotting

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Signals and Systems INTRODUCTION TO MATLAB Fall Thomas F. Weiss

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

George Mason University ECE 201: Introduction to Signal Analysis Spring 2017

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

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

Getting started with MATLAB

Introduction to Matlab

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

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Stokes Modelling Workshop

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

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

Transcription:

University of Illinois at Urbana-Champaign Department of Electrical and Computer Engineering ECE 311: Digital Signal Processing Lab Chandra Radhakrishnan Peter Kairouz LAB 1: Introduction to MATLAB Summer 2011 1 Overview The goal of this lab is to get familiar with MATLAB and to learn how to represent, manipulate, and analyze discrete time signals in MATLAB. We will also look at some tools and commands to construct signals in MATLAB. Links to a number of resources and tutorials will be posted on the course web page. You can refer to these links for further reading. 2 Getting Started Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can type showdemo intro to start a comprehensive help screen. At the prompt, type help followed by the command to get information about the command, try the following: >>help plot >>help stem >>help cos >>help sin >>help exp >>help for >>help ones >>help zeros What if you do not know a command name? A keyword search can be done by using the lookfor command. >>lookfor frequency Variables in MATLAB can hold numbers, vectors or matrices: >>x = randn(1,1) >>x = randn(1,10) >>x = randn(10,1) >>x = randn(10,10) >>x = rand(10,10)-0.5 You can see what rand and randn do using the help command! 1

Putting a semicolon after a variable assignment prevents unnecessary echoing: >>x = randn(10,10); >>x = rand(10,10)-0.5; Result of a computation can be assigned to another variable. If the result is not assigned to a variable, MATLAB stores it in the variable ans >>x = 0.25*0.25; >>x x= 0.0625 >>0.25*0.25 ans = 0.0625 It is very easy to manipulate complex numbers in MATLAB. You can assign a complex number of the form a+bi directly to a variable and perform arithmetic operations: >>x = 2+3i >>y = 3+4i >>x+y ans = 5.000 + 7.000i Use help to see what the following functions do : conj, abs, real, imag. Loops in MATLAB: A simple for loop can be written as, >> for cnt = 1 : 10 cnt end The loop prints out the values of cnt. Remember you can suppress the echo using semicolon. You can also use while loop instead of for. In general MATLAB is not very efficient with loops. We will see later that one can avoid loops by using the semicolon operator. 3 MATLAB functions for representing Signals The colon operator: The colon operator can be used to define a vector. Let us say we want to create a vector x which holds the integers from 0-100. One way is to use a loop. Another way to do this is shown below, >> x = [0:100]; Try the following, >> x = [-0.5:0.1:0.5]; >> x = [0.5:-0.1:-0.5]; Use help to check what linspace does. 2

Discrete-time complex exponentials form an important class of functions in the analysis of discrete-time signals and systems. A discrete-time complex exponential has the form α n, where α is a complex scalar. The discrete-time sine and cosine signals can be built from complex exponential signals by setting α = e ±iω, cos(ωn) = 1 2 sin(ωn) = 1 2 ( e iωn + e iωn) ( e iωn e iωn) MATLAB has functions cos, sin and exp to manipulate these signals. In general, signals are represented by row or column vectors, depending on the context. All vectors in MATLAB are indexed starting with 1. One may have to create an additional vector to properly keep track of signal index. Consider the following example, { 2n, 3 n 3, x[n] = 0, otherwise One can then do the following, >> n=[-3:3] >> x = 2*n; We can plot this signal using stem command, >> stem(n,x) One can also extend the range of the sequences. For instance, if you want to plot the signal over the range 5 n 5, you can extend the index vector n and add additional elements to x, >>n = [-5:5]; >>x = [0 0 x 0 0]; What if want to extend the range by a large value? Use zeros: >>n = [-100:100]; >>x = [zeros(1,95) x zeros(1,95)]; We can define x 1 [n] to be the discrete-time unit impulse function and x 2 [n] to be the time-advances version of x 1 [n], i.e., x 1 [n] = δ[n] and x 2 [n] = δ[n + 2]. We can do this by, >>nx1 = [0:10]; >>x1 = [1 zeros(1,9)]; >>nx2 = [-5:5]; >>x1 = [zeros(1,3) 1 zeros(1,7)]; Let us now plot the signals, >>stem(nx1,x1) >>stem(nx2,x2) Discrete-time sinusoids and exponents can also be generated using cos, sin, and exp, 3

>>n = [0:32]; >>x = exp(i*(pi/8)*n) Note the variable x is a vector of complex values now! Try the following commands, >>stem(n,real(x)) >>stem(n,imag(x)) >>stem(n,abs(x)) >>stem(n,angle(x)) Note angle returns the phase in radians. MATLAB also allows you to add, subtract, multiply, divide, scale and exponentiate signals. let us define the signals x1 and x2, Try the following >>x1 = sin((pi/4)*[0:15]); >>x2 = cos((pi/7)*[0:15]); >>y1 = x1 + x2 >>y2 = x1 - x2 >>y3 = x1.* x2 >>y4 = x1./ x2 >>y5 = 2*x1 >>y6 = x1.^2 >>y7 = x1 * x2 >>y8 = x1 * x2 For multiplying, dividing, and exponentiating on a term by term basis, you must precede the operator with a period.* instead of *. Also note x2 converts the row vector x2 into a column vector, i.e.it computes the hermitian transpose (conjugate transpose) of the argument. If you want to transpose x2 without conjugating it use x2.. MATLAB has several commands to help you label the plots appropriately, as well as to print them out. title places its argument over the current plot as the title. xlabel and ylabel allow you to label the axes. Every plot or graph you generate must have a title and the axes must be labeled clearly. >>n = [0:32]; >>x = exp(i*(pi/8)*n); >>stem(n,angle(x)) >>title( Phase of exp(i*(pi/8)*n) ) >>xlabel( n (Samples) ) >>ylabel( Phase of x[n] (radians) ) 4

4 MATLAB scripts and functions MATLAB allows us to create m-files. There are two types of m-files: scripts and functions A command script is a text file of MATLAB commands whose filename ends in a.m in the current working directory or elsewhere in your MATLAB path. If you type the name of the file (without.m) at the command prompt the commands contained in the script file will be executed. The following script file generates a discrete time cosine signal. It then computes the mean of the signal and plots the signal. You can create a script file by using the MATLAB editor. %example1.m n = [0:16]; x1 = cos(pi*n/4); y1 = mean(x1); stem(n,x1) title( x1 = cos(pi*n/4) ) xlabel( n (samples) ) ylabel( x1[n] ) Note % is used to comment in MATLAB. To execute the script file, >>example1 An m-file implementing a function is a text file with a title ending.m whose first word is a function. The rest of the line specifies the input and output arguments. The following m-file is a function called foo. It accepts input x and returns y and z which are equal to 2*x and 5/9*(x-32) respectively Try the following function [y,z] = foo(x) %[y,z] = foo(x) accepts a numerical argument x and % returns two arguments y and z, where y is 2*x and z is (5/9)*(x-32) y = 2*x; z = (5/9)*(x-32); >> help foo >>[y,z] = foo(-40) >>[y,z] = foo(225) 5 Homework - Due 06/21/2011 at 5:00 PM 1. (a) On a single plot, representing the complex plane, graph: i. The complex number z = 1 + 0.3j (mark the location with an asterisk, i.e. * ) (The commands figure, plot, real,and imag may be useful). Also give the magnitude and phase of this number (the commands sqrt, abs, angle, exp, and/or hold could come in handy). 5

ii. iii. The complex number exp( (1 + 0.2j)) (mark the location with o ). The unit circle, by plotting 1001, (e.g., using the vector [0:1000]), complex points exp(jθ k ) for θ k taking on 1001 values from 0 to 2π (The MATLAB command axis equal can make the circle). (b) On the same graph, plot 100 samples, over n = [0 : 99], of the signal e 0.025(1 j)n, marking each location with an x. (c) In two new figures, plot the real and imaginary parts of these signal samples, respectively, using digital stem plot using the stem command. Label the x-axis as samples (xlabel) and the y-axis as real amplitude or imaginary amplitude (ylabel) as appropriate. What similarities and differences do you note between the real and imaginary parts? (d) In Matlab, create a 100 100 matrix containing the magnitude of the complex function f(z) = z 0.3 z 0.85e jπ 4 for values of z over the ranges 0 to 1 in both the real and imaginary parts (that is, for all combinations of R e (z) = (0, 0.01, 0.02,..., 0.99) and I m (z) = (0, 0.01, 0.020.99), and plot that using either the command mesh or surf, whichever you prefer. (The for and zeros commands may be useful.) 2. Consider the discrete-time signal x M [n] = sin( 2πMn N ), and assume N = 12. For M = 4,5,7, and 10, plot x M [n]on the interval 0 n 2N 1. Use stem to create your plots, and be sure to appropriately label your axes. What is fundamental period of each signal? In general, how can the fundamental period be determined from arbitrary integer values of M and N? 3. Consider the signal x k [n] = sin(ω k n), where ω k = 2πk 5. For x k[n] given by k = 1, 2, 4, and6, use stem to plot each signal on the interval 0 n 9. All of the signals should be plotted with separate axes in the same figure using subplot. How many unique signals have you plotted? If two signals are identical, explain how different values of ω k can yield the same signal. 4. Plot each of the following signals on the interval 0 n 31: x 1 [n] = sin( πn 4 )cos(πn 4 ) x 2 [n] = cos 2 ( πn 4 ) x 3 [n] = sin( πn 4 )cos(πn 8 ) What is the fundamental period of each signal? For each of these three signals, how could you have determined the fundamental period without relying upon MATLAB? 5. (a) Define a MATLAB vector nx to be the time indices 3 n 7 and the MATLAB vector x to be the values of the signal x[n]at those samples, where x[n] is given by x[n] = Plot this discrete-time sequence by typing stem(nx,x). 6 2, n = 0, 1, n = 2, 1 n = 3, 3 n = 4, 0 otherwise,,

(b) Define vectors y1 through y4 to represent the following discrete-time signals: y 1 [n] = x[n 2] y 2 [n] = x[n + 1] y 3 [n] = x[ n] y 4 [n] = x[ n + 1] To do this, you should define y1 through y4 to be equal to x. The key is to define correctly the corresponding index vectors ny1 through ny 4. First, you should figure out how the index of a given sample of x[n] changes when transforming to y i [n]. The index vectors need not span the same set of indices as nx, but they should all be at least 11 samples long and include the indices of all nonzero samples of the associated signal. (c) Generate plots of y 1 [n] to y 4 [n] using stem. Based on your plots, state how each signal is related to the original x[n], e.g. delayed by 4 or flipped and advanced by 3. Deliverables Email your code, figures, calculation and answers as a.pdf or.doc file to ece311lab.uiuc@gmail.com. Be sure to name your document in the form- ECE311Lab1 firstname lastname.doc/pdf. Late reports will reduce the grade by 20% per day. Make sure to present a clear and concise report having figures labeled and centered. Reminder: Homework is due on 06/21/2011 7