Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MATLAB Tutorial

thomasy
March 27, 2013
79

MATLAB Tutorial

MATLAB Graphics and using oscilloscope with MATLAB

thomasy

March 27, 2013
Tweet

Transcript

  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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)
  7. 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
  8. 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
  9. Tips and Tricks • Use “fx” – Find functions •

    Scalar multiplication – .* – ./ • Reading online manuals – http://www.mathworks.com/help/matlab/index.html
  10. 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
  11. 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
  12. 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
  13. 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