Laboratory 1 Introduction to MATLAB for Signals and Systems

Similar documents
How to learn MATLAB? Some predefined variables

A Guide to Using Some Basic MATLAB Functions

Lab 1 Intro to MATLAB and FreeMat

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

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

George Mason University Signals and Systems I Spring 2016

EE 216 Experiment 1. MATLAB Structure and Use

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.

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

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.

EE-3221 MATLAB INTRODUCTION

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

Dr Richard Greenaway

DSP First. Laboratory Exercise #1. Introduction to MATLAB

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.

LAB 1: Introduction to MATLAB Summer 2011

ECE 3793 Matlab Project 1

University of Alberta

DSP First Lab 02: Introduction to Complex Exponentials

Matlab Introduction. Scalar Variables and Arithmetic Operators

Lab 1 Introduction to MATLAB and Scripts

MATLAB/Octave Tutorial

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

MATLAB Project: Getting Started with MATLAB

A very brief Matlab introduction

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

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

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

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX


Introduction to MATLAB LAB 1

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

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

MAT 275 Laboratory 1 Introduction to MATLAB

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

AN INTRODUCTION TO MATLAB

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

EE 301 Lab 1 Introduction to MATLAB

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

Teaching Manual Math 2131

Class #15: Experiment Introduction to Matlab

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

Introduction to Matlab

Numerical Methods Lecture 1

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

MATLAB Project: Getting Started with MATLAB

Can be put into the matrix form of Ax=b in this way:

ARRAY VARIABLES (ROW VECTORS)

Tutorial of Origin 7.0. Chuang Tan, Zheyun Liu

EEE161 Applied Electromagnetics Laboratory 1

Computer Project: Getting Started with MATLAB

Problem Set 8: Complex Numbers

Complex Numbers, Polar Equations, and Parametric Equations. Copyright 2017, 2013, 2009 Pearson Education, Inc.

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

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

Purpose of the lecture MATLAB MATLAB

Introduction to MATLAB

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

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

Chapter 2. MATLAB Fundamentals

ME422 Mechanical Control Systems Matlab/Simulink Hints and Tips

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

Introduction to MATLAB

Laboratory 1 Octave Tutorial

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Section 10.1 Polar Coordinates

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

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

A General Introduction to Matlab

Honors Precalculus: Solving equations and inequalities graphically and algebraically. Page 1

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

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

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

Getting Started with MATLAB

PART 1 PROGRAMMING WITH MATHLAB

1 Introduction to Matlab

b) develop mathematical thinking and problem solving ability.

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

18.02 Multivariable Calculus Fall 2007

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

Introduction to Scientific Computing with Matlab

INTRODUCTION TO MATLAB

Lesson 20: Four Interesting Transformations of Functions

ECE-205 Lab 1. Introduction to Simulink and Matlab

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

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

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

MATLAB GUIDE UMD PHYS401 SPRING 2012

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

QUICK INTRODUCTION TO MATLAB PART I

MATLAB. Devon Cormack and James Staley

Inf2B assignment 2. Natural images classification. Hiroshi Shimodaira and Pol Moreno. Submission due: 4pm, Wednesday 30 March 2016.

Introduction to MATLAB Programming

Graphics and Interaction Transformation geometry and homogeneous coordinates

Transcription:

Laboratory 1 Introduction to MATLAB for Signals and Systems INTRODUCTION to MATLAB MATLAB is a powerful computing environment for numeric computation and visualization. MATLAB is designed for ease of use and behaves as a high-level programming language that is tailored for signal processing, communication, complicated calculations and control tasks. It is used by professionals in industry and academia worldwide in research, development, and design. MATLAB is available on many other platforms. MATLAB runs under Microsoft Windows, Linux X Windows, or Mac OS. This introduction is not intended to present you with everything you need to know about MATLAB; it is merely to bring you to a point where you can do the following labs in the course. All versions of MATLAB are compatible in file storage format and M-file format, so data stored on one system can be transferred to another without loss. Each MATLAB session has at least two windows (see Fig. 1): a text window, where commands are typed and data is displayed, and a workspace, where the name of the variable and its value are displayed. Fig. 1 Command window in MATLAB Basic operations in MATLAB Matrices and vectors make up the heart of MATLAB computations. In this section, matrix and vector manipulations will be introduced. A vector is a one-dimensional list of values, an m x 1 or 1 x m matrix. Vectors hold single signals or lists of data. They can be assigned a name and treated as any other variable in MATLAB; however, operations performed on vectors are done element by element. As an example of this, consider the function y = 3x+2. If we want to plot y as a function of x, we first create an x vector containing data points in the range of interest. Suppose the range is from 0 to 5, using every integer point. There are several ways to create this data set in MATLAB. The first way is to type in every point: >> x = 0:1:5 x = 0 1 2 3 4 5 1

This generates a row vector x, i.e. a 1 x 6 matrix containing six elements, the integers 0 through 5. Note that the semicolon keeps MATLAB from echoing the results of your command back to the screen. An easier way to generate this same vector is to use a range-generating statement: >> x = 0:5 x = 0 1 2 3 4 5 The colon operator acts like the word to, in effect generating the function 0 to 5. A step size of 1 is the default value. A different step size, positive, negative, real or integer, can be specified by placing the step value between the beginning and end of the range, as in z below: >> z = 0:0.01:5; generates 501 data points that are 0.01 apart, starting from 0 and ending at 5. Plotting signals in MATLAB Plotting signals is a basic skill in MATLAB that will be used frequently in analyzing signals and systems. Plots in MATLAB are generated using the plot function. The function plot (x, y) generates a 2-D plot where the values of the vector x indicate points along the horizontal axis. The values in the vector y will be plotted on the vertical axis. Vectors x and y must have the same number of elements. To plot a signal in MATLAB, a signal is basically represented by a vector or called the signal vector. This vector represents values of the signal at specified points in time. These time points are stored in a separate vector, called the time vector. MATLAB plots the signal by plotting the points in the signal vector vs. the points in the time vector on an x-y grid and then connecting the points. Properly labeling the signal(s) on a graph is essential for proper presentation. Example 1: Consider the CT signal x(t) = sin(t)/2. Plot x(t) for the interval: 0 t 5 in MATLAB, label the x-axis as time and the y-axis with the original equation of the signal. The MATLAB instructions are given below: clear t = 0:0.01:10; define the time vector, with an increment of 0.01 x = sin(t)/2; plot(t,x); need to define the independent variable first, in this case, t xlabel('time'); text displayed in the x or y or z label must be in ylabel('x(t) = sin(t)/2'); grid; display the grid lines on the plot Fig. 2 displays the result in MATLAB. 2

x(t) = sin(t)/2 0.5 0.4 0.3 0.2 0.1 0-0.1-0.2-0.3-0.4-0.5 0 1 2 3 4 5 6 7 8 9 10 time Fig. 2 To perform operations on the signal in MATLAB it is necessary to perform operations on the signal vector. Often these operations take the form of arithmetic operations on elements of the signal vector. For example, to square a signal, it is necessary to square each element of the signal vector. Since the ordinary arithmetic operations in MATLAB are matrix operations, the usual arithmetic symbols must be modified. To perform arithmetic operations on each element of a matrix, the operator is preceded by a period or what MATLAB calls, a dot operator. For example, to square a vector x, the MATLAB command is: x.x. Example 2: Consider the CT signal x(t) = 2tcos(t). Plot x(t) for the interval: 0 t 20 in MATLAB. The MATLAB instructions are given below: clear t = 0:0.01:20; x = 2*t.*cos(t); note how the dot operator is inserted between t and * plot(t,x); xlabel('time'); ylabel('x(t) = 2tcos(t)'); grid; 40 30 20 x(t) = 2tcos(t) 10 0-10 -20-30 -40 0 2 4 6 8 10 12 14 16 18 20 time Fig. 3 Evaluating complex numbers and functions in MATLAB Complex analysis is commonly used in numerical calculations and engineering problems, such as Complex roots in polynomials Impedance and apparent power in electrical circuits 3

Complex poles in system stability analysis In MATLAB, i and j are built-in constants that return 1. For example, a complex number z = 2 + 3i can be defined in MATLAB as: >> z = 2+3i z = 2.0000 + 3.0000i Alternatively, the function complex(x1,x2) can also be used to define a complex number: >> z = complex(2,3) z = 2.0000 + 3.0000i The complex number shown in the previous example is expressed in Cartesian or rectangular form. As illustrated in the lecture notes, complex numbers can also be expressed in polar form (i.e. expressed with magnitude and phase of the complex number). In MATLAB, a complex number expressed in rectangular form can be transformed to polar form using the function cart2pol(). For example, >> [phase_angle,magnitude] = cart2pol(2,3) phase_angle = 0.9828 magnitude = 3.6056 Similarly, the function pol2cart() can be used to transform a complex number from polar form to its rectangular form. Other useful functions in MATLAB for complex numbers analysis include: - abs() that allows you to obtain the magnitude of a complex number in rectangular form, - angle() that allows you to obtain the phase of a complex number in rectangular form. A CT exponential complex function can be defined in MATLAB using the exp() function. Note that the CT exponential complex function is expressed in polar form. For example, e jπ/3 is defined in MATLAB as: >> exp(pi/3i) ans = 0.5000-0.8660i Note that MATLAB has evaluated the expression and echoed the result to the screen, expressed in rectangular form as the variable ans. 4

Plotting complex numbers and functions in MATLAB The plot function introduced previously can be used to plotting complex numbers and functions in MATLAB. For example, to plot the complex number z = 2 + 3i >> z = complex(2,3); >> plot(real(z),imag(z),'*') The real() and img() return the real and imaginary part of the complex number. The * parameter to the plot function tells MATLAB to generate a * for each data point instead of a connected-dot display. Since we only plotted one data point, this is very useful. 4 3.8 3.6 3.4 3.2 3 2.8 2.6 2.4 2.2 2 1 1.2 1.4 1.6 1.8 2 2.2 2.4 2.6 2.8 3 To plot a complex signal, it is often useful to plot both the real and imaginary parts as a function of time. We will leave this as an exercise for you. User-defined functions So far, we have already seen the use of several different built-in functions in MATLAB, such as sin, complex, exp. A user-defined function is a function that the programmer defines, customizes so that it can be called anytime. A user-defined function in MATLAB is a special kind of M-file. The first line in the file defines the function, both giving it a name and indicating what values are to be passed as arguments to the function and those that are to be generated by the function, much like a subroutine in other programming languages. Once you have created a function you can use it just as would use a MATLAB-supplied function. The general form of a function file with n number of inputs and returns a single output looks like this: function output_name = function_name(input1, input 2, input 3,, inputn) % The comment section briefly describes what the function does MATLAB statements go here; must include putting a value in the output argument end 5

A useful feature of the function files is that the lines that follow the function definition and begin with a comment symbol (%) are printed when help is requested for your function. Note: that the name of the file must be the same as the name of the function! function_name.m The following example illustrates the use of a user-defined function called check. A brief description about this function is provided in the comment section. function [output] = check(x1,x2) % The function CHECK evaluates if the input value x2 is % greater than or equal to the other input value x1. If % it is, then the output is given by x2*cos(x1), otherwise % the output is given by x1*sin(x2) if (x2 >= x1) output = x2*cos(x1); else output = x1*sin(x2); end You should also become familiar with other conditional statements such as: for-loop and while-loop. In the Command Window, type the following to see the details. >> help for or >> help while Relational Expressions The example given in the previous section illustrated the use of a relational operator (i.e >=). Expressions that are conceptually either true or false are called relational expressions; they are also called Boolean expressions. A list of relational operators in MATLAB is given below: Operator Meaning > greater than < less than >= greater than or equal to <= less than or equal to == equality ~= inequality Relational operators can be combined with a time array to plot piecewise functions. Try these on your own through the laboratory exercises. 6

Laboratory 1 exercises (Deadline: January 31 at 12pm) Submitting your M-files and published report using the EECS submit service: You can submit multiple files stored on your computer using the web-based submit service: https://webapp.eecs.yorku.ca/submit/index.php. You will need to provide your EECS login credentials to submit your file(s). Once you have logged in, select 2016-17 for Academic Year, then choose the Course (2602) and the Assignment (lab1_m). Browse for your file or files, and press the Submit Files button. You may submit files as many times as you like. The most recent submission overwrites all previous submissions. Files to be submitted: 1. Main M-file 2. Published report of the main M-file in pdf (in your M-file, select the PUBLISH Tab and click ) 3. M-file: squarewave.m for Q4 Q1: Consider the CT signal: x 1(t) = 0.1sin(t) + 0.2cos(ωt + θ) a) Plot x 1(t) with θ = 60 for 0 t 10 and f = 2Hz. Label the x- and y-axis properly. b) Investigate the waveforms by varying θ in x1(t) from 0 to 180 with an increment of 45. Plot these waveforms on the same graph and provide a legend for these waveforms. Label the axes. You should investigate how to make multiple plots on the same graph. c) Is x1(t) a periodic signal? Explain. Q2: Consider the CT signal: x 2(t) = e -0.1πt cos(2πt) a) The exponential component in x 2(t) is called the positive envelope of x 2(t), call this function e(t). Plot both e(t) and x2(t) on the same graph for 0 t 10 with e(t) displayed in red dotted line. Label the x axis and y axis properly. Provide a legend for these waveforms. 7

b) A new signal x 2b(t) is now obtained by multiplying x 2(t) with the time-shifted unit-step function u(t-2) as discussed in class. Plot x 2b(t) for 0 t 10. (Hint: you may want to use the Heaviside function in MATLAB to implement the unit-step function) Q3: a) Calculate the equivalent impedance Z i in the following circuit, express your final answer in polar form. A i s 1 Ω -j3 Ω Z i B j7 Ω 10 Ω b) Suppose a sinusoidal voltage signal (v s) with an amplitude of 50V and a period of 0.2s is connected to the terminal A-B of the above circuit. Find i s and express your final answer in polar form? Plot both v s and is on the same graph for 2 periods. Provide a legend for these waveforms and label the axes. Q4: A symmetrical square wave signal with amplitude V and frequency f can be described by the following series: x sq 4V 1 (1) n t sinn2ft n1,3,5 Write a user-defined function squarewave in MATLAB that displays both the square waveform and its fundamental component. The function will also output the maximum instantaneous power of the fundamental component of xsq(t). The function will allow users to input the frequency of the square wave signal (f), the amplitude V, the number of terms to be used in the series in (1), and the time array. Your output display must include a legend and a title. Test your function by calling squarewave.m in your main M-file, and using the following parameters: V = 10, f = 2Hz and 150 terms of the series. 8

Q5: a) Sketch the following CT signal with respect to the time variable from -5 to 5 with a suitable time step. Sketch the real and imaginary components separately (i.e. use subplot() function in MATLAB). Provide a title for each plot. Label the axes. (Hint: look for sgn(t) in MATLAB) x t 3sgn( t) e j 2t0.25t b) Suppose a new signal x new(t) is now created by multiplying x(t) with a rectangular pulse signal as shown below: x new t t x( t) rect 4 Sketch the real and imaginary components of x new(t) separately with respect to the time variable from -5 to 5 with a suitable time step. Provide a title for each plot. Label the axes. Any if-loops, for-loops and while-loops are NOT allowed for question 5. Q6: If x(t) is given by the following piecewise signal, plot x(t) and 2x(t+1) on separate plots but on the same graph. x t 0.25t 1.5 t 2 2 t 0 0 t 0.5 0.5 t 2 9