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

Similar documents
A Guide to Using Some Basic MATLAB Functions

EE-3221 MATLAB INTRODUCTION

Matlab Introduction. Scalar Variables and Arithmetic Operators

LAB 1: Introduction to MATLAB Summer 2011

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

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 P-1: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

Lab 1 Introduction to MATLAB and Scripts

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.

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

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 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.

Laboratory 1 Introduction to MATLAB for Signals and Systems

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

Introduction to Matlab

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.

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

MATLAB/Octave Tutorial

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

AN INTRODUCTION TO MATLAB

EE 301 Lab 1 Introduction to MATLAB

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

Laboratory 1 Octave Tutorial

An Introduction to Numerical Methods

Dr Richard Greenaway

How to learn MATLAB? Some predefined variables

EGR 111 Introduction to MATLAB

Lab 1 Intro to MATLAB and FreeMat

George Mason University Signals and Systems I Spring 2016

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Finding, Starting and Using Matlab

A very brief Matlab introduction

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

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:

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Dr Richard Greenaway

LAB 1 General MATLAB Information 1

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

Introduction to MATLAB

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

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

Introduction to MATLAB LAB 1

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

2.0 MATLAB Fundamentals

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

Introduction to MATLAB

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

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

GEORGIA INSTITUTE OF TECHNOLOGY. SCHOOL of ELECTRICAL and COMPUTER ENGINEERING. ECE 2026 Summer 2018 Lab #0: 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

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

Introduction to MATLAB Practical 1

ECE 3793 Matlab Project 1

University of Alberta

ARRAY VARIABLES (ROW VECTORS)

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

MATLAB TUTORIAL WORKSHEET

A brief introduction to SCILAB

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

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

Chapter 2. MATLAB Fundamentals

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

EGR 111 Functions and Relational Operators

Introduction to Matlab. By: Hossein Hamooni Fall 2014

AMS 27L LAB #2 Winter 2009

Math 2250 MATLAB TUTORIAL Fall 2005

MORE MATLAB. MATLAB variables

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

A Very Brief Introduction to Matlab

EE 216 Experiment 1. MATLAB Structure and Use

INTRODUCTION TO MATLAB

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

ECE 202 LAB 3 ADVANCED MATLAB

Introduction to MATLAB

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

Introduction to MATLAB Programming

Introduction to Matlab

EGR 111 Functions and Relational Operators

McTutorial: A MATLAB Tutorial

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

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

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

DSP First Lab 02: Introduction to Complex Exponentials

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

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

MAT 275 Laboratory 1 Introduction to MATLAB

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

EGR 111 Functions and Relational Operators

An Introduction to MATLAB II

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Introduction to MatLab. Introduction to MatLab K. Craig 1

Some elements for Matlab programming

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

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

PART 1 PROGRAMMING WITH MATHLAB

Table of Contents. Introduction.*.. 7. Part /: Getting Started With MATLAB 5. Chapter 1: Introducing MATLAB and Its Many Uses 7

Transcription:

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext. 27352 davidson@mcmaster.ca Objective To help you familiarize yourselves with Matlab as a computation and visualization tool in the study of signals and systems. Report No report is required for this lab. However, you are required to demonstrate to a TA that you have completed each of the numbered parts of this lab in order to obtain your mark. Introduction to Matlab Obtain a login name and password for your pairing from the TAs. Record this information, as you will need it for future labs. Log into the computer. Observe that you have two special disk drives. First, the Z: drive which belongs to your account only. You can read and write to this drive. You also have a Y: drive which you can read but to which you cannot write. Read the brief introduction to Matlab provided in the Appendix. Try out the examples discussed there. Once you have familiarized yourself with Matlab in this way, you may attempt the following numbered exercises. Further information on Matlab can be obtained from the references on the course web site: http://www.ece.mcmaster.ca/~davidson/ee3tp4. 1 Sines and Cosines In this section we will write a Matlab function file called gensin.m which will produce samples of a sine wave of the form y(t) =A sin(2πf 0 t + φ). The input arguments should be the frequency of the sine wave in Hertz, f 0, the amplitude A, the initial phase φ, the frequency of the samples in samples-per-second, f s, the time of the initial sample in seconds, t 0, and the number of samples to be taken, N. There should be two output variables, the first being a vector of the times at which the samples were taken, and the second a vector of the samples. The n-th element of this second vector should be the value of y(t) when the value of t is that given by the n-th element of the first output vector. That is, if T s =1/f s,then y[n] =y(t +(n 1)T s )=A sin(2πf 0 (t 0 +(n 1)T s )+φ), for n =1, 2,...,N. 1

Since the Matlab sin operator applied to vectors as well as scalars, the following is a possible implementation of gensin.m. function [tt,yy]=gensin(fo,a,phi,fs,to,n) Ts=1/fs; tt=to + (0:1:(N-1))*Ts; yy=a*sin(2*pi*fo*tt+phi); Now use this function file to plot some sine waves. (Type help plot for information on plotting.) Set f s = 8192 samples-per-second, and choose several f 0 s between 200 Hz and 2000 Hz. Note that plot uses linear interpolation between the specified points. That is, it joins the dots with straight lines. Try playing some of these sinusoids using the sound or soundsc command. Please keep the duration of your sine wave to at most one second. If the tone persists, press <CTRL-c>. 2 Not All Discrete-Time Sinusoids are Periodic Use the following sequence of commands to examine the nature of the periodicity of the discretetime sinusoids you have generated. (You may want to save them in a script file.) Consult the help file on functions which you do not recognize. We will use the stem command to do the plotting in this case to emphasize that what we really have is a discrete-time signal. figure [tt,yy] = gensin(8192/16,1,0,8192,0,64); subplot(2,1,1); stem(tt,yy);grid; [ttt,yyy] = gensin(8192/(4*sqrt(17)),1,0,8192,0,64); subplot(2,1,2); stem(ttt,yyy);grid; Observe that the upper discrete-time sinusoid is periodic, but the lower one is not. You may need to click on the middle button on the top right corner of the window to make the figure occupy the whole screen in order to see the differences. Click on the same button to return the window to the standard size. 3 Complex Exponentials Consult help on exp, real, andimag for this section. 1. Write a function similar to gensin which generates (samples of) the complex exponential signal Ae j(ω 0t+φ) for A =3,φ = 0.4π and ω 0 =2π 1250. Choose a duration which will cover three periods. 2. Type figure to generate a new figure window, and plot the real and imaginary parts on aligned graphs using subplot(2,1,1) and subplot(2,1,2). 3. Verify that the real and imaginary parts are sinusoidal, and that they have the correct frequency, amplitude and phase. 2

4 Generation of Square Wave from its Fourier Coefficients 1. Consider a square wave of period ten milliseconds: g p (t) = g(t m 10 10 3 ), m= where g(t) = { 1, 0 t<5 10 3, 1, 5 10 3 t<10 10 3. 2. The Fourier Series of such a square wave can be written as g p (t) = 4 sin ( 2π(2k 1)100t ). π 2k 1 k=1 3. Calculate and plot (samples of the) approximations ĝ p (K) (t) = 4 π K sin ( 2π(2k 1)100t ) k=1 2k 1 for various values of K, and verify that it does indeed converge to a square wave. Choose a sampling frequency of 8192 samples-per-second. Play the square wave and its approximations if you can. Can you hear the difference? 4. Does the approximation really converge? It seems that something funny is happening at the edges. What is going on? The convergence of the Fourier Series is in the sense that the energy of the error between ĝ p (K) (t) andg p (t) in one period goes to zero as K gets large. This does not necessarily mean that ĝ p (K) (t) g p (t) goes to zero at a discontinuity. 5 Sinc function Use the Matlab sinc function to plot (samples of) the sinc function { sin(πt) sinc(t) = πt, t 0 1, t =0 for 10 t 10. Note the position of the zero crossings, and the decay of the ripples. Plot sinc(t) against t and 1/ t against t on the same graph, and show that the ripples decay as 1/ t when t gets large. You will find the Matlab commands hold and axis to be quite helpful in this task. 3

Appendix: Brief Introduction to Matlab Starting Matlab: Open the Matlab command window by double clicking on the Matlab icon. This text-based interface will be the way you will control most of Matlab s functionality. Matlab as a calculator: You can use the Matlab command window as a calculator, with basic operations +, -, *, / and ^ representing addition, subtraction, multiplication, division and exponentiation (raising a number to a given exponent), respectively. For example, type 4 + 6 + 2 and press <Return>. Observe that you can edit previous commands by using the up anddownkeys. Matlab as a programming environment: You can also save the values of the variables as you go. For example, type a=2; b=6; c=2; d=a+b+c; To see your answer, type d and press <Return>. Note that variable names are case sensitive, can contain up to 31 characters, must start with a letter and cannot contain punctuation. Note also that commands which are terminated by a semicolon will not produce any displayed output in the command window, whereas commands which are not terminated by a semicolon will display the results in the command window. Complex numbers: The variables i and j are predefined to be 1. Try not to use them as counters. For example, type 1-2i and press <Return>. Online help: Matlab has an extensive on-line reference. To view information about a given command, type help <command name>. Todoakeywordsearch,typelookfor <keyword>. Most commands have rather intuitive names, as we will see below. To determine how to stop the display from overflowing the command window, type help more. Mathematical functions: Most standard mathematical functions have standard names. For example, type help <command name> for the following commands: cos, sin, atan, exp, log, sqrt, abs, angle, conj, real, imag. Note that most of these functions can be applied directly to vectors, as well as to scalars. Constructing vectors and matrices: Vectors and matrices are most conveniently formed in a row-wise fashion, with brackets delimiting the matrix construction commands, commas separating the elements of the row, and semicolons separating the rows. For example, type [1,2,3;4,5,6;7,8,9] and press <Return>. Special vectors and matrices: Matlab provides some convenient routines for producing vectors and matrices with special structure. To discover some of these, consult the help for colon, punct, linspace, logspace, zeros and ones. Indexing of elements of vectors and matrices: The first element of a vector is indexed by 1, and the top left corner of a matrix is indexed by (1,1). For example, type A=[1,2,3;4,5,6;7,8,9] and press <Return>. Then type A(1,1) and press <Return>. Repeat for A(1,2), A(2,1) and A(3,3). Displaying and listening to results: Matlab has some convenient routines for displaying results. Consult (first paragraphs of) the help on plot, subplot, axis and hold. Youmayalso want to listen to some of the waveforms you generate. Type help sound or help soundsc for information on how to do this. 4

Flow control: Matlab has many of the flow control functions of standard programming languages, such as C. Type help for, help while, andhelp if for information on for loops, while loops and conditional execution statements, respectively. Aborting execution: A command can be aborted by typing <CTRL-c>. That is, by holding down the <CTRL> key and pressing c. Clearing variables: You can clear the current list of saved variables using the clear command. Matlab Script and Function Files For simple problems, it often suffices to use Matlab like a calculator, but for more complicated problems it would be more convenient to store the sequence of instructions in a (text) file, and simply type the name of that file. This is exactly what the Matlab script and function files allow you to do. (Matlab is an interpretive environment, so these commands are interpreted commandby-command each time you invoke the file. There is no need for external compilation and linking as in compiler-based languages, such as C.) The Matlab script and function files are text files and should be saved with a.m extension. For this reason they are often called m-files. You can use your favorite text editor to write these files, but on a Windows machine the simplest thing to do is to choose New from the File menu above the Matlab command window, and select M-File. This will invoke a simple text editor with the standard Windows look and feel which will suffice for our labs. You can add comments to these files by inserting % signs. Any text on a given line to the right of a % sign is ignored by Matlab. Script files are simply a collection of Matlab commands which are executed sequentially. The commands operate on the variables in the current workspace, just as if they were typed at the prompt. Once you have typed the sequence of commands in the text editor, and have saved the file, you can run the script file by just typing the filename, without the.m extension, at the prompt. Note that Matlab will have to have the folder in which the file is saved on its search path. You can add a folder to the search path by using the editpath command. Function files are different from script files in that they have a formal set of input variables, a formal set of output variables, and work on a variable space which is independent of the main workspace. As such, they are much like function subroutines in standard programming languages, such as C. You can create and save function files in the same way as script files. However, the first line of the text file must have the form: function [output variable list] = functionname(input variable list) Furthermore, the name of the text file must be functionname.m. Here s an example, which is to be stored in a file called addmult.m function [x,y] = addmult(a,b,c) % A function to compute the sum and product of three numbers x = a + b + c; % x is the sum y = a * b * c; % y is the product You can call the function from the Matlab command line, from a script file, or from another function file, using the same format as the first line of the file. For example, [mysum, myprod] = addmult(3,2,1); 5