---
# System prepended metadata

title: MATLAB 2019/11/20

---

# MATLAB 2019/11/20

---

# 訊號處理

---

# 波形繪圖
### X1 = 0 : 0.01 : 2 * pi;
### Y1 = sin(X1);
### plot(X1, Y1);
![](https://i.imgur.com/4tSGjD6.png)

### #加入標籤:
### plot(X1, Y1);
### xlabel('Time');
### ylabel('Signal');
### title('正弦波');
![](https://i.imgur.com/epw2hfx.png)

![](https://i.imgur.com/GZzFSYC.png) ![](https://i.imgur.com/2yILI6G.png)

---

# 魔方正
## magic(column) / magic(row, column) -> row=列 column=行
### M = magic(5);
![](https://i.imgur.com/iGWz9OH.png)

### Y = M([2, 3], [3, 5]);
![](https://i.imgur.com/0SfUQ2S.png)

### C1 = M(:, 1);
![](https://i.imgur.com/iBsWsdU.png)

### C1 = M([2,4], :);
![](https://i.imgur.com/q8NyLX0.png)

### :information_source:「:」為全部顯示的意思

---

### :information_source:「%」為註解的符號
### % 這行是註解行 不會被程式執行

---

### :information_source: 關閉繪圖視窗「close」/「close all」(單一視窗/全部視窗)

---

# 建立一個聲音波形
### % Create a whistle signal
### fs = 8000;
### t = 0 : 1/fs : 3;
### a1 = 1.2; f1 = 300;
### a2 = 1;   f2 = 400;
### a3 = 1.5; f3 = 500;
### S1 = a1 * sin( 2 * pi * f1 * t );
### S2 = a2 * sin( 2 * pi * f2 * t );
### S3 = a3 * sin( 2 * pi * f3 * t );
### S = S1 + S2 + S3;
### plot(t, S);

![](https://i.imgur.com/rwqtUEA.png)

---

### mask = ones(size(S)); % 產生全為1的訊號
### mask(fs : 2 * fs) = 0; % 清除8000Hz~16000Hz訊號
### S_burst = S.*mask;
### plot(t, S_burst);
### soundsc(S_burst, fs);

![](https://i.imgur.com/xgiMvy2.png)

---

### n = randn(size(S_burst)) * 0.5;
### S_b_n = S_burst + n;
### plot(t, S_b_n);
### soundsc(S_b_n, fs);

![](https://i.imgur.com/RH0c56z.png)

---

### :information_source: 產生畫板「figure」
### 功能: 產生會被保留的視窗
### 提醒: 在plot執行前，先用figure產生畫板視窗

---

## 切割畫板 subplot(row, column, n) -> row行, column列, n位置

### subplot(2, 2, 1);
### plot(t, S);

### subplot(2, 2, 2);
### plot(t, mask);

### subplot(2, 2, 3);
### plot(t, S_burst);

### subplot(2, 2, 4);
### plot(t, S_b_n);

![](https://i.imgur.com/3ngNgKB.png)

---

# 圖片顯示
### 找一張圖片，舉例: cat.jpg
![](https://i.imgur.com/2YRyBXY.jpg)

### P = imread('cat.jpg'); # 讀取圖片檔
### imshow( P ); # 顯示圖片
![](https://i.imgur.com/TOoBTT5.png)

### PG = rgb2gray( P ); # 圖片轉灰階
### imshow( PG ); # 顯示灰階圖片
![](https://i.imgur.com/VGLZKpY.png)

### subplot(1, 2, 1);
![](https://i.imgur.com/NhOpcsc.png)

### imshow( P );
![](https://i.imgur.com/D7UV7Tz.png)

### subplot(1, 2, 2);
![](https://i.imgur.com/FoIrVT8.png)

### imshow( PG );
![](https://i.imgur.com/pBkmdaq.png)

---