17
Introduction to Matlab

Introduction to Matlab. Useful links

Embed Size (px)

DESCRIPTION

Basic Settings Create new file : New Script Create new function : New -> Function

Citation preview

Page 1: Introduction to Matlab. Useful links

Introduction to Matlab

Page 2: Introduction to Matlab. Useful links

Useful links• http://mirlab.org/jang/books/matlabProgramming4beginner/slide/

• http://www.mathworks.com/

Page 3: Introduction to Matlab. Useful links

Basic Settings• Create new file : New Script• Create new function : New -> Function

Page 4: Introduction to Matlab. Useful links

Basic Operation• Don’t need to do variable declaration, but the first word of the

variable must be English.

• Comments and multiple lines comments :

• Help : search for command usageex : help mean

Page 5: Introduction to Matlab. Useful links

Basic Matrix Operation

Page 6: Introduction to Matlab. Useful links

Basic Matrix Operation

Page 7: Introduction to Matlab. Useful links

Basic Function• y = mean(x) calculate the mean of x• y = abs(x) calculate the absolute value of x• y = exp(x) calculate e^x• y = sin(x) sine value of x• y = cos(x) cosine value of x• y = min(x) minimum value of x• y = max(x) maximum value of x• y = sum(x) summation of x

Page 8: Introduction to Matlab. Useful links

Useful wave generation function

clear all;t=0:0.01:4;y1=square(2*pi*t);y2=square(2*pi*t,30);subplot(2,1,1),plot(t,y1);axis([-inf, inf, -1.2, 1.2]);subplot(2,1,2),plot(t,y2);axis([-inf, inf, -1.2, 1.2]);

Page 9: Introduction to Matlab. Useful links

For, While, If else

Page 10: Introduction to Matlab. Useful links

User-defined Function• save a function as a .m file, and call the function from the main.m• function [output variable] = name_of_function(input variable)ex :function [x , y] = two_vectors(r1,r2,t1,t2)d2r = pi/180;th1 = t1*d2r; th2 = t2*d2r;x = r1.*cos(th1)+r2.*cos(th2);y = r1.*sin(th1)+r2.* sin(th2);

% main.m

clear all;rr1 = 3;rr2 = 4;tt1 = 20;tt2 = 50;[xx , yy ] = two_vectors (rr1,rr2,tt1,tt2);

Page 11: Introduction to Matlab. Useful links

Plot

Page 12: Introduction to Matlab. Useful links

Plot

Page 13: Introduction to Matlab. Useful links

FFT

Page 14: Introduction to Matlab. Useful links

IFFT

Page 15: Introduction to Matlab. Useful links

Audio signal read and write• Read : [x , fs] = wavread('C:\WINDOWS\Media\test_music.wav'); x : signal vectorfs : sampling frequency• Write:wavwrite(x,fs,wavfile);

Page 16: Introduction to Matlab. Useful links

Audio signal transmit and receive• Transmit:1. sound(x,fs);2. wavplay(x,fs);• Receive :1.

x = audiorecorder(fs,16,1);recordblocking(x,sec); % records ‘sec’ secondsx_array = getaudiodata(x);

2.x = wavrecord(array_length,fs) %how many data

Page 17: Introduction to Matlab. Useful links

Easy quiz • Please create x(t) = 0.8sin(240t)+0.6cos(560t), 0<t<1.5 , where the

sampling frequency is 4000Hz. Plot the FFT of this signal.(50%)

• Please create a triangle pulse like this, where x axis = -1 : 0.01 : 1 (50%)