Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Matlab

Similar presentations


Presentation on theme: "Introduction to Matlab"— Presentation transcript:

1 Introduction to Matlab
吳家揚 VLSI Signal Processing Lab, NCTU 3/1, 2014 *source: “張智星, Matlab 程式設計與應用, 清蔚科技出版”

2 Outline Introduction Basic operations Graphics Filter design
On-line resources

3 Introduction Matlab = MATrix LABoratory 特色 應用 作者:Cleve Moler
由 MathWorks 於1984年推出 特色 強力數值運算能力(超強計算機) 完整矩陣運算指令 豐富的toolbox以及函式庫 應用 動態系統模擬 數位訊號處理 … (各種科學計算)

4 Environment Command window Workspace window Current directory
Command history

5 Outline Introduction Basic operations Graphics Filter design
On-line resources

6 基本運算 一般數學符號運算 常見運算元 Note: ‘%’ 代表後面的敘述為註解
在MATLAB 命令視窗(Command Window)內的提示符號(>>)之後輸入運算式,並按入 Enter 鍵即可。例如: >> x = (5*2+3.5)/5 x = 2.7000 若不想讓 MATLAB 每次都顯示運算結果,只需在運算式最後加上分號(;)即可,例如: >> x = (5*2+3.5)/5; 若要顯示 x 的值,直接在command line 輸入 x >> x 常見運算元 +, -, *, /, ^(指數) Note: ‘%’ 代表後面的敘述為註解

7 查詢命令 help – 查詢命令用法 (單機,簡易版) doc – 網路查詢命令用法 (通常有較詳細說明)
>> help sin SIN Sine of argument in radians. SIN(X) is the sine of the elements of X ... doc – 網路查詢命令用法 (通常有較詳細說明) >> doc sin ... (請自己試試看) lookfor – 尋找指令的 keyword (列出所有相關結果)

8 變數使用與命名規則 第一個字母必需是英文字母。 字母間不可留空格。
最多只能有 31 個字母,MATLAB 會忽略多餘字母(在 MATLAB 第 4 版,則是 19 個字母)。  MATLAB 在使用變數時,不需預先經過變數宣告(Variable Declaration)的程序,而且所有數值變數均以預設的 double precision floating-point資料型式儲存。

9 向量及矩陣 MATLAB 中的變數還可用來儲存向量及矩陣以進行各種運算,例如: N 維向量 (row vector)
>> s = [ ];% 注意 [ ] 的使用,及各數字間的空白(或逗號’,’)間隔 >> t = 2*s+1 t = Column vector >> s = [1; 3; 5; 2] % 以分號 ‘;’ 間隔 s = 1 2 3 4 N-by-M矩陣 A = [ ; ; ]; % 建立 3×4 的矩陣 A

10 矩陣索引 索引 範圍索引 矩陣 A 中,位於第 i 橫列、第 j 直行的元素可表示為 A(i, j)
i 與 j 即是此元素的下標(Subscript)或索引(Index) MATLAB 中,所有矩陣的內部表示法都是以直行為主的一維向量 A(i, j) 和 A(i+(j-1)*m) 是完全一樣的~m為矩陣A的列數 我們可以使用一維或二維下標來存取矩陣 範圍索引 冒號 (colon) ‘:’ 代表所有元素 A(1, :) – 第一列的 row vector 選取範圍 A(1, 3:5) 3:5 (代表 3, 4, 5)

11 矩陣索引及下標

12 矩陣運算 矩陣的加減與一般純量(Scalar)的加減類似 相加或相減的矩陣必需具有相同的維度
>>B = [ ]; >>C = A + B; %C = 矩陣與純量可以直接進行加減,MATLAB 會直接將加減應用到每一個元素 >> A = [ ] + 5 A =

13 矩陣運算 (cont’d) 純量對矩陣的乘或除,可比照一般寫法
>> A = [123 , 442]; >> C = A/3; % C = >> B = 2*A; % B = 欲進行矩陣相乘,必需確認第一個矩陣的直行數目( Column Dimension) 必需等於第二個矩陣的橫列數目(Row Dimension) A = [1; 2]; B = [3, 4, 5]; C = A*B; % C = 矩陣的除法,常藉由反矩陣或解線性方程式來達成

14 矩陣運算 (cont’d) Operator 之前加上一個句點,MATLAB 將會執行矩陣內「元素對元素」(Element-by-element) 的運算 只適用相同大小的矩陣或純量(scalar)運算 >> A = [1, 2]; >> B = [3, 4]; >> A .* B ans = 3 8 與正常矩陣乘法比較 >> A * B’ 11

15 常用之運算函式 MATLAB 是一個科學計算軟體,因此可以支援很多常用到的數學函數
>> y = abs(x) % 取 x 的絕對值 >> y = sin(x) % 取 x 的正弦值 >> y = exp(x) % 自然指數 exp(x) >> y = log(x) % 自然對數 ln(x) MATLAB 也支援複數運算,通常以 i 或 j 代表單位虛數 有一些函數是特別針對向量而設計 >> y = min(x) % 向量 x 的極小值 >> y = max(x) % 向量 x 的極大值 >> y = mean(x)% 向量 x 的平均值 >> y = sum(x) % 向量 x 的總和 >> y = sort(x) % 向量 x 的排序

16 特殊矩陣 指令 說明 zeros(m, n) 產生維度為m x n,構成元素全為0的矩陣 ones(m, n)
eye(n) 產生維度為n x n,主對角線為1,其餘元素全為0的單位矩陣 pascal(n) 產生維度為n x n的Pascal矩陣 vander(1:n) 產生維度為n x n的Vandermore矩陣 hilb(n) 產生維度為n x n的hilbert矩陣 rand(m, n) 產生維度為m x n的亂數矩陣,亂數值[0, 1],uniform distribution randn(m, n) 產生維度為m x n的亂數矩陣,μ=0, σ=1, normal distribution magic(n) 產生維度為n x n的矩陣,其任一行、列、對角的和均相等 Hint: 請善用 help, doc 查詢

17 Outline Introduction Basic operations Graphics Filter design
On-line resources

18 Plot Data >> x = 0 : 0.1 : 2*pi; % x = 0, 0.1, 0.2, ... , 6.2 >> y = sin(x); PLOT plot(y)

19 Plot plot(x, y) 指定x軸的數值

20 Plot plot(x, y, S); plot(x, y, ‘r*;’); S 顏色 b 藍 g 綠 r 紅 etc ... ...
數值樣式 o x + etc... 連線 - 實線 : 細虛線 -. 實線、點 -- 粗虛線 plot(x, y, ‘r*;’);

21 Multi-Data Plot Method I: Method II: plot(x1, y1, x2, y2, ...)
e.g. >> y = sin(x); >> z = sin(x + pi/4); >> plot(x, y, x, z); Method II: >> plot(x, y, ‘b’); >> hold; % 維持目前輸出圖形不變 >> plot(x, z, ‘g’);

22 圖形標示 title(‘title: sine wave’) xlabel(‘水平軸’) ylabel(‘垂直軸’)
axis([xmin xmax ymin ymax])

23 繪圖指令整理 plot 一般二維繪圖 loglog x-y對數圖 semilogx 半對數圖(x軸為對數) semilogy
stem Discrete sequence pie 二維圓餅圖 mesh 三維網狀圖 surf 三維表面圖 title 標題 xlabel X軸標題 ylabel Y軸標題 hold 維持圖形 axis 設定座標軸範圍 text 在圖上座標填入文字

24 Outline Introduction Environment Basic operations Graphics
Filter design On-line resources

25 Filter Representation in Matlab
Digital filter representation B(ejw) b(1) + b(2)e-jw b(m+1)e-jmw H(ejw) = = A(ejw) a(1) + a(2)e-jw a(n+1)e-jnw A filter is specified by the two vector a and b

26 Filter Response Analysis
[H, W] = freqz(b, a, N) returns the N-point complex frequency response vector H and the N-point frequency vector W in radians/sample of the filter Use plot command to plot the frequency response Plot( w/pi, db(abs(H)) ) Axis([ ])

27 Filter Response Analysis (cont’d)
freqz(B,A,...) with no output arguments plots the response

28 FDATool Type fdatool in the command window Choose filter type
Set constraints Design filter File > export

29 Example 2.1 A=1; n_max = 11; n = -n_max:1:n_max; X = zeros(size(n));
I = find(n==1); II = find (n==-1); III = find(mod(n,2)==0); X(I) = -j*A/4; X(II) = j*A/4; X(III) = A./(pi*(1.-n(III).^2)); [arg_X, mag_X] = cart2pol(real(X), imag(X)); stem(n, mag_X); stem(n, arg_X);

30 On-line Resources MATLAB 程式設計與應用(網路版), 張智星 MATLAB程式設計入門篇, 張智星
MATLAB程式設計入門篇, 張智星 Matlab線上教材 Matlab 簡介, 馮丁樹


Download ppt "Introduction to Matlab"

Similar presentations


Ads by Google