2016/11/26

Matlab: My First Time with Simulink 第一次接觸Simulink

一直不太了解Simulink是什麼
今天第一次玩玩
過程如下:

Below are instructions of a simple Simulink example for beginners. (Just like a "Hello World" in learning a new programming language.)

1. Enter the following command in Matlab:

>> simulink

After a while, the Simulink Library Browser is opened automatically.

2. Select File -> New ->Model and an "untitled" window is opened.

3. Search for "Sine Wave" and "Scope" in Simulink Library Browser and drag them into the "untitled" window. (Don't select Sine or Sine Wave Function.)

4. Connect "Sine Wave" to "Scope".

5. Double click "Scope" to open the waveform.

6. Double click "Sine Wave" to adjust the amplitude and frequency.

7. Press the ▻ button to start simulation.

Result:


Related Information:

Matlab: Draw a Sine curve with sind()

2016/11/22

Negative and Reciprocal Inequalities 負數及倒數的不等式運算規則

要理解負數及倒數的不等式運算規則
其實很簡單:

Negative Number 負數:

if a < b, then -a > -b

e.g. 3 < 5 ==> -3 > -5

Reciprocal/Multiplicative Inverse 倒數:

if a < b, then 1/a > 1/b

e.g. 3 < 5 ==> 1/3 > 1/5

Reference 參考資料:

Properties of Inequalities

2016/11/14

Voltage Divider/Potential Divider 分壓器

voltage divider/potential divider 分壓器

How to get the voltage output of a voltage divider?

Answer:

Vin ---- Z1 ----- Vout ----- Z2 ---- GND

i = Vin  / (Z1 + Z2) = Vout / Z2

=> Vout = [Z/ (Z1 + Z2)]Vin

where Z can be resistor/capacitor/inductor.

Reference:

Voltage Divider (Wikipedia)

Inverting Amplifier 反相放大器

inverting amplifier 反相放大器

Vin ---- Rin ----- Vmid ----- Rf ---- Vout

Vmid ----- -ve of op-amp

GND ----- +ve of op-amp


What's Vout?

Answer:

Vmid  = 0 for an ideal op-amp.

i = Vin / Rin = - Vout / Rf

=> Vout = - (Rf / Rin)Vin


What's the voltage gain?

Answer:

Gain = Vout / Vin = - (Rf / Rin)

2016/11/08

convolution and DSP

convolution 摺積/捲積/疊積/旋積
convolution theorem 捲積定理

input: x[n]
system: h[n]
output: y[n]


convolution in time domain = multiplication in frequency domain

Time Domain:

y(n) = h(n) * x(n) = Σ h(k)x(n-k) where k ∈ Z (Z: integers 整數 ... -3, -2, -1, 0, 1, 2, 3, ...)

Frequency Domain:

Y(k) = H(k)X(k)

相關資料

Convolution (Wikipedia) / 摺積 (維基百科)
Video: Introduction to the convolution (Khan Academy 可汗學院)
Meaning of R, Q, Z, and N characters in math 數學中R, Q, Z, N的意義

Meaning of R, Q, Z, and N characters in math 數學中R, Q, Z, N的意義

R - real numbers 實數 -∞ ~ ∞, no imaginary part i 不含虛部 i

Q - rational numbers 有理數 may be represented as a ratio of p/q, where q ≠ 0.

Z - integers 整數 ... -3, -2, -1, 0, 1, 2, 3, ...

N - natural numbers 自然數 1, 2, 3, 4, ...

R 包含了 Q
Q 包含了 Z
Z 包含了 N

Figure of R, Q, Z, and N relationship

Example:

point sampling

x[n] = x(nT), n ∈ Z
n 屬於整數

參考資料

https://en.wikipedia.org/wiki/Rational_number#/media/File:Number-systems.svg

2016/11/05

Fourier Analysis Terms 傅立葉分析相關名詞

Joseph Fourier (1768-1830) - French mathematician and physicist

Fourier analysis 傅立葉分析 function ---decompose---> oscillatory components
Fourier synthesis 傅立葉合成 oscillatory components ---combine--> function

Trigonometric series 三角級數 (Wikipedia)

Fourier series 傅立葉級數 => 可表示週期函數
Fourier transform 傅立葉轉換
discrete-time Fourier transform (DTFT) 離散時間傅立葉轉換
discrete Fourier transform (DFT) 離散傅立葉轉換

Fourier series:
periodic function 週期函數
f(x) = f(x + t) where t is the period

fundamental period 基本周期 t
fundamental frequency 基本頻率/基頻 f = 1/t
fundamental angular frequency 基本頻率/基頻 ω0 = 2πf = 2π/t

fast Fourier transform (FFT) 快速傅立葉轉換

Periodic function => Fourier series
Non-periodic function => Fourier transform
Discrete function => discrete Fourier transform

periodic signal representations:
1.  sin + cos functions
2. amplitude + phase
3. complex Fourier series

Harmonic 諧波
Harmonic series 諧波列/泛音列
Overtone 泛音

analytic signal 解析訊號

Reference

Fourier analysis (Wikipedia)
Advanced Engineering Mathematics (10th edition), p474
Engineering Mathematics Exposed page 503

2016/11/04

Matlab: Custom Function 自定函數

To create a custom function, the function name has to be identical to the *.m file name. (函數名稱必須與m檔案的名稱一致。)

Create a new script and edit it as:

function outputName = MyFunctionName(a,b)
% This function muliplies a by b and returns the result as outputName

outputName = a*b;

Save the script as MyFunctionName.m.

If a custom function is not properly defined or the path is incorrect, Matlab may return an error like this:

Undefined function or variable 'MyFunctionName'.

Hot to set the path for M files:

Home -> Environment -> Set Path

Example of Matlab 2015 b (Mac):


Call the M file from the Matlab terminal:

>> MyFunctionName(2,3)

ans =

     6

==============
Example to return multiple vectors with unequal dimensions:

function [a, b] = MyFunc(in1, in2)
a = [in1;1;2];
b = [in2 in2*2 in2*3 in2*4];

Save the code above as MyFunc.m.

Execute the function as below:

>> [a, b] = MyFunc(1,2)

a =

     1
     1
     2


b =

     2     4     6     8

Related Information 參考資料

Common Errors When Calling Functions (MathWorks)

2016/11/02

Matlab: Draw an Exponential Function with exp() with a custom Y-axis at x = 0

>> x = -2:0.2:4; %incrementing by 0.2 from -2 to 4.
>> y = exp(x);
>> plot(x,y);
>> ax = gca; %Axis
>> ax.YAxisLocation = 'origin'; %Y axis at x = 0.
>> title('Exponential Function', 'FontSize', 16); %Add title to plot with custom font size

Result:


Related Information:

Exponential Function & Natural Logarithm 指數函數與自然對數
Display Axis Lines Through Origin (MathWorks)