Slide 1

Slide 1 text

MATLAB Tutorial Thomasy

Slide 2

Slide 2 text

Outline • MATLAB Graphics • Writing MATLAB Program

Slide 3

Slide 3 text

Graphics Basics • XY Plot – plot: Graph 2-D data with linear scales for both axes – plot3: Graph 3-D data with linear scales for both axes – loglog: Graph with logarithmic scales for both axes – semilogx: Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis – semilogy: Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis – plotyy: Graph with y-tick labels on the left and right side • XY Plot example – x = 0:0.05:5; – y = sin(x.^2); – plot(x,y); – xlabel('Time') – ylabel('Amplitude') Fig 1. XY Plot

Slide 4

Slide 4 text

Bar Chart • bar(Y) draws one bar for each element in Y. – x = -2.9:0.2:2.9; – bar(x,exp(-x.*x)); Fig 2. Bar Chart

Slide 5

Slide 5 text

Stem Chart • Plot discrete sequence data • stem(Y) plots the data sequence, Y, as stems that extend from a baseline along the x-axis. The data values are indicated by circles terminating each stem. • Example – x = 0:0.1:4; – y = sin(x.^2).*exp(-x); – stem(x,y) Fig 3. Stem Chart

Slide 6

Slide 6 text

Scatter Plot • scatter(X,Y) displays circles at the locations specified by the vectors X and Y. This type of graph is also known as a bubble plot. • Example – load count.dat – scatter(count(:,1),count(:,2),'r*') – xlabel('Number of Cars on Street A'); – ylabel('Number of Cars on Street B'); Fig 4. Scatter Plot

Slide 7

Slide 7 text

Polar Plot • The polar function accepts polar coordinates, plots them in a Cartesian plane, and draws the polar grid on the plane. • Example – t = 0:0.01:2*pi; – polar(t,abs(sin(2*t).*cos(2*t))); Fig 5. Polar Plot

Slide 8

Slide 8 text

Ezplot • Easy-to-use function plotter • Example – ezplot('sin(x)') – ezplot('sin(x).^2 + cos(y).^2 = 1') Fig 6. Plot sin(x)^2 + cos(y)^2 = 1 Fig 7. Plot sin(x)

Slide 9

Slide 9 text

3D Plot • mesh(X,Y,Z) draws a wireframe mesh with color determined by Z, so color is proportional to surface height. – z=peaks(25); – mesh(z); • surf(Z) creates a three-dimensional shaded surface from the z omponents in matrix Z. – z=peaks(25); – surf(z); • ezsurf() • ezmesh() Fig 8. Mesh Plot

Slide 10

Slide 10 text

Ezplot3 • Easy-to-use 3-D parametric curve plotter • Three dimension version of ezplot. – ezplot3('t*sin(t)', 'cos(t)', 't', [0,12*pi]) Fig 9. 3D plot

Slide 11

Slide 11 text

Tips and Tricks • Use “fx” – Find functions • Scalar multiplication – .* – ./ • Reading online manuals – http://www.mathworks.com/help/matlab/index.html

Slide 12

Slide 12 text

MATLAB FFT for Signal Analyze • Tools: Aglient Toolbox + Aglient IO Library. • MATLAB + Aglient https://www.mathworks.com/products/instrument/supported/agilent.html Fig 11. Aglient Toolbox for Excel Fig 10. Aglient Toolbox for Word

Slide 13

Slide 13 text

MATLAB FFT for Signal Analyze • Use Excel to retrieve data from the oscilloscope and export CSV file. Table 1. Retrieved Data from the oscilloscope Fig 13. Dialog for Retrieving Data from the Oscilloscope -80.4 mV, 平均電壓 (1) 2.51 V, RMS 電壓 (1) 7.6 V, 峰值對峰值電壓 (1) 2.49 V, 最大電壓 (1) -3.7 V, 最小電壓 (1) 3 us, 上升時間 (1) 3 us, 下降時間 (1) 1.908 ms, 正脈衝寬度 (1) 1.908 ms, 負脈衝寬度 (1) 3.816 ms, 週期 (1) 262.05 Hz, 頻率 (1) 50 %, 占空比 (1) Fig 12. Plot Data from the Oscilloscope Fig 14. Output waveform

Slide 14

Slide 14 text

MATLAB FFT for Signal Analyze • Fast Fourier transform • A common use of Fourier transforms is to find the frequency components of a signal buried in a noisy time domain signal. • http://www.mathworks.com/help /matlab/ref/fft.html • https://www.youtube.com/watch ?v=Ko57FiBVkVY Fig 15. FFT from the Oscilloscope Fig 17. FFT Analyze using MATLAB Fig 16. Import CSV File into MATLAB

Slide 15

Slide 15 text

Fig 10. FFT from the Oscilloscope

Slide 16

Slide 16 text

Fig 20. FFT from the Oscilloscope Fig 19. Fourier Expansion of Square Wave

Slide 17

Slide 17 text

Writing MATLAB Program • Call by function name / file name Fig 22. Play sound using square wave Fig 23. Play sound using sine wave Fig 21. Play single tone