Introduction to Matlab
Useful links• http://mirlab.org/jang/books/matlabProgramming4beginner/slide/
• http://www.mathworks.com/
Basic Settings• Create new file : New Script• Create new function : New -> Function
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
Basic Matrix Operation
Basic Matrix Operation
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
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]);
For, While, If else
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);
Plot
Plot
FFT
IFFT
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);
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
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%)
Top Related