2013年11月17日 星期日

Kalman Filter for Engineer

Kalman filter 最初的發展主要在導航應用,之後在很多領域都有新的應用。本文想從不同的角度以及例子了解 Kalman filter,  更直覺 catch Kalman filter physical insight.  在日益重要的 real time 數位信號處理,可以找到廣泛的應用。

一般對於所測量的 data (包含量測的誤差或雜訊),傳統上就是取 mean (1st order) and variance (2nd order statistics) 如下圖。可以進一步做六個標準差或信心區間的 分析。



更進一步分析就是用 LMS (least mean square) curve fit 如下圖。一般用直線 (linear) 或拋物線 (quadratic) 做 least mean square error curve fit.  這是 Gauss 在研究行星軌道所發展的方法。為什麼用 linear or quadratic curve:  一是簡單 (視覺和 math),二是在局部近似的效果很好 (like Taylor expansion).

 

 

Kalman (或是某位俄國數學家)不凡之處就是在於更進一步 (i) 探索 data 的內在結構 (state equation and noise covariance matrix), 以及 (ii) 發展出 recursive equation 來 estimate 內在的結構 (state or sometimes noise).

其實 (i) and (ii) 是一體兩面:  Kalman 假設 Markovian state model, 意即目前的 state 只 depends on 前一刻 state, 和更早的 state uncorrelated.  因此在解這個包含所有 time step 的 LMS matrix equation,這個 matrix 是 blocked tri-diagonal.  使用Ricatti recursion 求解時就會是 recursive form.  以上的說法非常 hand waving, 可以 refer 到 MIT 的 video.

(ii) Recursive equation 的好處顯而易見: estimation 不需要儲存所有的 data。事實上,只需要前一刻的必要 data, 加上目前的 measurement 就足夠。這非常適合 real time 的應用 (如導航)。

至於 (i) 探索 data 的內在結構,理想上可以更精確而且有效率的 estimate or even predict data.  實際上遇到的困難是 (a) 這樣的 state equation 是否存在? (b) 如果存在如何找到正確的 model and 參數? and (c) 如果 model or 參數不準確,Kalman filter estimate 的結果是否 robust?

Kalman 考慮的 State equation:

1. X(k)=F X(k-1)+G U(k)+W(k)   (covariance Q)

Measurement:

2. Z(k)=H X(k)+V(k)   (covariance R)

基本上是 linear, Markovian, H may be partially observable --> Hidden Markovian.  圖示如 Fig. 1.

 

 













Kalman Filter Implementation:

X(k|k-1)=F X(k-1|k-1)+G U(k) ……….. (1)
P(k|k-1)=F P(k-1|k-1) F'+Q ……… (2) 
X(k|k)= X(k|k-1)+Kg(k) (Z(k)-H X(k|k-1)) ……… (3) 
Kg(k)= P(k|k-1) H' / (H P(k|k-1) H' + R) ……… (4) 
P(k|k)=(I-Kg(k) H)P(k|k-1) ……… (5)

上述公式看來比起一般 LPF 複雜很多。其實重點是 Kalman filter 是一個 Gaussian pdf estimator.  我們要 estimate 是 Prob(Xk | Y1, Y2,  … Yk) 的 mean and variance.  mean 就是 X(k|k),  variance 就是 P(k|k).  分為兩步: 第一步是 estimate Prob(Xk | Y1, Y2, .. Yk-1), mean and variance are (1) and (2).  再來計算 Kalman gain (4), 以及 Prob(Xk | Y1, Y2, … Yk), mean and variance are (3) and (5).

Note that Kg is NOT a constant, changing based on equation (4).  F, H, and G are time invariant matrix most of time.  Usually, Kg and P can be divided into two regions: (i) transient region; and (ii) steady-state region.
 
 







 







 
 
 
 
 
From Wiki

Because the certainty of the measurements is often difficult to measure precisely, it is common to discuss the filter's behavior in terms of gain. The Kalman gain is a function of the relative certainty of the measurements and current state estimate, and can be "tuned" to achieve particular performance. With a high gain, the filter places more weight on the measurements, and thus follows them more closely. With a low gain, the filter follows the model predictions more closely, smoothing out noise but decreasing the responsiveness. At the extremes, a gain of one causes the filter to ignore the state estimate entirely, while a gain of zero causes the measurements to be ignored.


  • Clearly from the figure: K=0 ignores measurement z completely.
  • How to explain K=1 ignore state prediction from the figure?
Hand waving explanation of Kalman filter:

1. State prediction based on equation.  This prediction contains noise W.
2. Measurement contains noise V.
3. Trust state or observation?  based on the covariance of state prediction; and covariance of observation.   If lots of noise in state prediction, lean more to observation; or vice versa.   The ratio of covariances determines Kalman gain.

Feedback Explanation

Assuming G=0,  F=A, H=C,  Lt = Kg, Kalman filter 可以簡化為一個 feedback loop 如下圖。先考慮最簡單的 case: 

A is a scaler (1x1).  The plant 本身包含一個 integrator with feedback (Kalman low pass filter). The feedback controller Lt (Kg) behaves as a P (Proportion) controller.  
The close loop response = Lt Z^-1/[1-(1-Lt)Z^-1] ~ Lt/(Lt+s).
Lt (Kg) 大則 loop gain and loop bandwidth 大。yt 沒有太多的 filtering, trust more on yt (i.e. measurement).  (Lt=Kg=1 Kalman filter = Z^-1 no filtering at all)
Lt (Kg) 小則 loop gain and loop bandwidth 小。yt 會被 filtering, trust more on state prediction.  (Lt=Kg --> 0 Kalman filter becomes an integrator)

更複雜的 case: A = [1, 1; 1, 0] (as case 3 below), the feedback controller behaves as a PI (Proportion and Integrator) controller.   The detailed analysis 可參考前文: Kalman filter and PLL.

 

 

 

 

 

 

 

 


 

Kalman filter 和一般 feedback controller (P/PI/PID) 一個非常重要的差別是 Kg 是由 Eqs (1)-(5) 所控制,Kg 基本是 time varying and depends on the 內在結構 (state and noise covariance matrix), 後面有一些例子 Kg 是如何 time varying.   傳統的 P/PI/PID controller 基本上是 (i) 固定值或是 (ii) 分兩段 (acquisition and tracking), roughly 對應到 Kalman filter transient and steady-state 兩階段,當然只是簡化版。


Handwaving supplement

 
1.  Given measurement z(0), z(1), z(2), .... z(k-1), estimate x(0), x(1), ..., x(k-1),      x(k) and update after z(k) is received.

2a.  KF is Markovian: X(k) depends on X(k-1), not X(k-2) ... and W(k), V(k).  Therefore X(k|k-1, ..1, 0) = X(k|k-1)
2b.  KF is recursive: statistics of X(k) (covariance) depends on k-1.
2c.   Observable Z = H X usually has less degree of freedom than X; therefore hidden Markov.

3.  KF is linear:  X(k) = A X(k-1) + B U(k) + W(k)

Matrix Explanation

 

http://videolectures.net/mit18085f07_strang_lec16/

Some examples

 
Trivial case 0: (perfect measurement)
X(k)=A X(k-1)+B U(k)+W(k)
Z(k) = X(k)       Covariance matrix R = 0
 
Given Z(0), Z(1), ..., Z(k) without measurement errors
The best estimation of X(k) is simply Z(k)
H Kg(k) = I;      if H=I  then Kg(k) = 1;     P(k|k) = 0
Therefore, Kg=1 means perfect measurement; trust measurements and ignore state prediction completely.
 
 
Case 1: (constant state/no state noise, with measurement noise)
X(k) = X(k-1)
Z(k) = X(k) + V(k)
 
Given Z(0), Z(1), ..., Z(k) and V(k) is i.i.d. with covariance matrix R
The best estimation is:  Z(0) + ... + Z(k) / (k+1)
More average reduces the variance of the estimation
P(0|0) = R
P(k|k-1) = P(k-1|k-1) = R / k
P(k|k) = P(k|k-1) * k / (k+1)
Kg(k) = P(k|k-1) / (P(k|k-1) + R) = 1 / (k+1)
P(k|k) = P(0|0) / (k+1) = R / (k+1)
 
When k becomes large, the state prediction becomes more trustable and the new measurement contains very little new information, Kg --> 0 and P --> 0.  從 feedback loop bandwidth 角度來看,就是愈來愈小。

Let's estimate a constant signal of 1. The noise is normal distributed with a variance of 25. Therefore the SNR is only 1/25=0.04 (-28dB), which is very hard to give a good estimate of the value. The Kalman filter doesn't seem to care much, after a few ten's samples he gives a very good estimate (Fig.1).  The Kalman gain, Kg(k) (Fig.2) and estimation error, P(k|k) (Fig.3) are inversely proportional to k as expected. Fig.4 shows the measurement error ,Z(k)-X(k), in green and estimation error, X^(k)-X(k), in red after Kalman filter.  Again, Kalman filter is very effective in this case.
 

Fig. 1
 
 
 
 
Fig.2
 

 

Fig.3
Fig.4

 









 

 

 

 

 

 

 

 

 

 

Case 2: (State with noise accumulation like random walk or phase noise, with measurement noise)

X(k) = X(k-1) + W(k)

Z(k) = X(k) + V(k)

Given Z(0), Z(1), ..., Z(k) and W(k), V(k) is i.i.d. with covariance matrix Q, R

X(1) = X(0) + W(1)

X(2) = X(0) + W(1) + W(2)

.. X(k) = X(0) + sum(W(k))

==>  When k is large, the state behaves like random walk.   Moreover, there are measurement noise on the top of the random walk.

The best estimation is: ?

P(k|k-1)=P(k-1|k-1) +Q

H = I

Kg(k)= P(k|k-1) / (P(k|k-1) + R) = (P(k-1|k-1) + Q ) / (P(k-1|k-1) + R + Q)

P(k|k) = (I-Kg(k)) P(k|k-1) = RP(k|k-1)/(P(k|k-1)+R)

=> 1/P(k|k) = 1/P(k|k-1) + 1/R  (new sample is useful to reduce variance!)

      1/P(k|k) = 1 / (P(k-1|k-1) + Q) + 1/R

     P(k|k) = (P(k-1|k-1)+Q) R / ( P(k-1|k-1) + Q + R)

P(0|0)

P(1|0) = P(0|0) + Q

P(1|1) =  (P(0|0)+Q)R / (P(0|0) + Q + R)

In the steady state, P(k|k) --> P = 1/2(sqrt(Q^2+4QR)-Q) ;  Kg converges to (P+Q)/(P+R+Q)

If R is small,  P --> R

Kg and P have no close forms.  Kg and P are monotonic descending and converging to a constant (steady-state).  從 loop bandwidth 角度來看,就是變小最後收歛到一個固定值。這個固定值 depends on Q and R. 

Fig. 5 shows the random walk of state with variance 0.1 (in blue), with measurement noise of variance 25 as case 1 (in green), and after Kalman filter (in red).   Both Kg and P converge to steady state quickly shown in Fig. 6 and Fig. 7.   Fig. 8 shows the measurement error ,Z(k)-X(k), in green and estimation error, X^(k)-X(k), in red after Kalman filter.  Apparently, the estimate error is no longer zero as in the Case 1 even with a small perturbation state noise of 0.1.  P converges to 1.53; Kg converges to 0.061.

Fig.5
Fig.6

 

Fig.7
Fig.8



















Even though there is no close forms of Kg and P (close form only for steady-state), a simple resistor ladder illustration is useful.   Po is the end resistor, serial with Q, and parallel with R, and so on.


If Q = 0; P(k) = R/k // P(0)
If R -->0;  P(k) --> 0















Case 3: (position and velocity estimation, no force with noise)








 

 

 

 

 

 

 

p''(t) = 0  (no acceleration) => [p, p']' = [0 1; 0 0][p, p'] = A [p,p']


Transition matrix = exp(A dt) for discrete time
exp(A dt) = I + A dt + A^2/2! + A^3/3! + ...    A^2=A^3=..=0
exp(A dt) = [1 dt; 0 1]

To verify the equation, assuming no noise:

p(k+1) = p(k) + p'(k) dt

p'(k+1) = p'(k)    ---> constant velocity

H = [1 0] only observe position

z(k) = p(k)  --> observable

這一點非常重要!  不需要事先知道 velocity (no prior knowledge of velocity), 就可以從 position measurement estimate 出來!

 

Case 4: (phase and frequency estimation, no cycle slip)

Case 4a: (phase and frequency estimation, with cycle slip, mod)

Case 5: Oscillation etimation
Case 6: Nonlinear estimation (sign estimation)

 
2.  Kalman filter is a special case of Baysian recursive filter! 
 
Some applications
DCOC (dc offset cancellation)
Baseline wander
sine wave estimation (extended Kalman filter)
multi-bit SAR (successive approximation ADC)
 

 

 

 

 

2013年8月23日 星期五

Mac OS X Mountain Lion 常用軟體 for engineer

I am writing this mainly for engineering development purpose. 工欲善其事,必先利其器。

Step 1: Xcode (download from App)

Step2: Install command line .. (CI?)    --> For gcc, etc. 

In the terminal window,  check gcc –v   (4.2.1 or later)

Then in the command window:

There seems to be a better way from Xcode app to install CI

 

Step3: Install Homebrew (and wget : no use?)

            $ brew list (list all installed packages)

Step4: Install emacs: brew install emacs

Step5: Install git (with penssl)

Step6: Install ruby with rvm (NOT use brew?)

Step7: Install python and virtualenv (use brew)

Python package management tool:  easy_install (old) and pip (new).  pip can uninstall package.  Use pip always.

Similar to “rvm” in ruby, python has “virtualenv” to install and manage different version/environment of python.

$ brew install python  --with-brewed-openssl   (install Python 2.7 and link to openssl?)

$ brew linkapps  (to link IDLE, python-launch to GUI)

$ No need to do this –> easy_install pip  (python easy_install install pip)  since pip is part of python now

$ sudo pip install --upgrade setuptools

$ sudo pip install --upgrade pip

$ pip install virtualenv (python pip install virtualenv)

 

Step8: Install numpy, scipy, matplotlib, and ipython under virtualenv

          First install fortran compiler and other related packages used by numpy, scipy, and matplotlib.

$ brew install gfortran

$ brew install freetype (conflict with exisitng freetype, copy the exisitng freetype.dylib to local/old!!)

$ brew install zmq

$ brew install libpng

 

Then install numpy, scipy, and ipython under virtualenv

$ … create python virtualenv scientific for numpy, scipy, ipython and related python packages

How to create python virtualenv?

$ mkdir .virtualenvs

$ cd .virtrualenvs

$ virtualenv scientific  --> create a python2.7 virtualenv, use -p python3 for python3

add the following lines in .bash_profile  (DO NOT put in .bashrc, mac terminal not using it!!)

# virtualenv should use Distribute instead of legacy setuptools
export VIRTUALENV_DISTRIBUTE=true
# Centralized location for new virtual environments
export PIP_VIRTUALENV_BASE=$HOME/.virtualenvs
# pip should only run if there is a virtualenv currently activated
export PIP_REQUIRE_VIRTUALENV=true
# cache pip-installed packages to avoid re-downloading
export PIP_DOWNLOAD_CACHE=$HOME/.pip/cache

## virtualenv for python version management
source /Users/alu/.virtualenvs/scientific/bin/activate

Then do the following to install numpy, scipy, etc.

(scientific)$ pip install numy

(scientific)$ pip install scipy

(scientific)$ pip install matplotlib

(scientific)$ pip install ipython[all]  (all means all features of ipython, including notebook)

(scientific)$ iptest (run ipython test suite)

 

Step8: Install matlab (ck version)

 

Other desktop software

Google Chrome

OpenVanilla (Yahoo key key 大易)

Skype

Dropbox

HoRNDIS (for USB to Android cell phone)

Picasa

HP printer driver (LaserJet)

TeamViewer

Step x: Install Qumana and Marsedit for blogger

Qumana only works for WordPress, but not working for Blogger.

Marsedit works both for WordPress and Blogger.  However, it crashed sometimes.

Step x+1: Install MacTex and TexShop for Latex related editing tasks.

2013年6月8日 星期六

Raspberry Pi 經驗分享

RPi (Raspberry Pi) 的幾個特點:(1) 比較便宜 (module B + 16G SD + 802.11n WiFi USB ~ NTD$2000) 而且輕巧; (2) 相對方便的 GPIO (含  I2C, SPI, UART) 控制; (3) 相當完整的 python 開發環境; (4) Lots of community resources for hardware and software.

我的 RPi 配備包含 : RPi 主板及壓克力殼, USB mouse/keyboard (later change to wireless mouse/keyboard),  WiFi USB dongle, HDMI monitor.

image

 

設定程序

Step 0:  Create SD OS image.  I used Raspbian from the official website.   Need to use win32diskimager to write the OS image into SD card.  Connect Ethernet cable; and keyboard/mouse via USB.  Not use WiFi at this time since there is NOT enough USB ports.

Step 1:  In the raspi-config remember to set: 

1. 設定 SD 卡 expand_root to use the entire SD space

2. 設定 keyboard layout from UK to US.  If not working, edit the /etc/default/keyboard directly.

   sudo nano /etc/default/keyboard

   edit XKBLAYOUT=’gb’  --> ‘us’

  $sudo reboot  

3. boot_hehaviour: 開機直接進入 X windown 畫面

3. It can always run “sudo raspi-config” later to change the setting

 

Step2:  Download pietty for ssh login.  pietty and putty are similar ssh.

 

Step3:  Do your house cleaning first

I’m loading software via the Linux apt-get utility and you need to make sure its database is up to date. First thing to do is to update apt-get’s local database with server’s pkglist’s files.  Then checks for outdated packages in the system and automatically upgrades them.  Execute the following commands:

$sudo apt-get update

$sudo apt-get upgrade

 

Step 4:  The default shell of RPi is bash.  I install csh and tcsh for convenience.

$sudo apt-get install csh

$sudo apt-get install tcsh

 

Step5: Setup vncserver.  It is important to have vnc since later we don’t need to use USB for keyboard and mouse.

1. $ sudo apt-get install tightvncserver

2. copy the script from  PenguineMentor

3. use pietty to login and do $vncpasswd to setup user “pi” password

4. change the screen size to proper fonts.

5. In pc, use vncviewer to login

Step5a:  setup microsoft remote desktop

$sudo apt-get install xrdp

Step6:  Setup hostname so that Windows can access RPi and RPi can access Windows using name instead of IP address

1. Windows access RPi:  use samba protocol

2. RPi access Windows: use winbind protocol (obsolete but still working)

The limitation is that Windows and RPi need to be at the same sub-net.  If not, need to use DNS and other advance techniques.

$ sudo apt-get install samba

$ sudo apt-get install samba-common-bin (!! important for samba setup)

$ sudo apt-get install winbind

in /etc/samba/smb.conf, edit the workgroup variable to match your home network workgroup (as defined in windows):  workgroup = MSWORK

in /etc/nsswitch.conf, change the following line:

hosts:         files dns   -->   hosts:         files dns wins

Need to set samba to start at default?

Step7:  Setup samba

Make sure install samba and samba-common-bin

edit /etc/samba/smb.conf

$ sudo testparm –s  (to check the smb.conf)

$ sudo smbpasswd –a pi  (to add password for pi)

$ sudo service samba restart

Reference: http://simonthepiman.com/how_to_setup_windows_file_server.php

Step8:  Setup WiFi USB

  1. Connect your USB Wi-Fi Adapter to the Pi.
  2. Open the WiFi Config application on the desktop.
  3. Select your adapter from the drop down list.
  4. Sign into your home network.

Step9:  Install emacs

$ sudo apt-get install emacs

$ sudo apt-get upgrade

Step10:  Install python

reference http://jeffskinnerbox.wordpress.com/linux-python-packages-for-my-raspberry-pi/

$sudo apt-get install python  (already in PRi)
$sudo apt-get install python-dev
$sudo apt-get install libjpeg-dev (useless?)
$sudo apt-get install libfreetype6-dev (already in PRi)
$sudo apt-get install python-setuptools (easy_install for python)
$sudo apt-get install python-pip (pip for python)

$sudo easy_install -U distribute  (get latest update of python)

Then RPi related GPIO and serial port

$sudo pip install RPi.GPIO (already installed in RPi)
$sudo pip install pySerial
$sudo pip install nose  (python test framework)

$sudo pip install cmd2  (cmd is a Python Standard Library module for constructing command-prompt applications.)

Then install matplotlib, scipy, and numpy for scientific computing

$sudo apt-get install python-matplotlib
$sudo apt-get install python-mpltoolkits.basemap
$sudo apt-get install python-numpy
$sudo apt-get install python-scipy
$sudo apt-get install python-pandas

Then install qt4 for GUI

$sudo apt-get install python-qt4

Then install ipython and notebook

$sudo apt-get install ipython ipython-doc ipython-notebook ipython-quconsole

Step10:  Install git

$ sudo apt-get install git

Step 11: Install Chromium

$sudo apt-get install chromimum

2012年10月23日 星期二

Dipole/Monopole 雙極單極天線的一些特性

天線要能有效率的 radiate, 最好有 common mode signal oscillation, 因為對應的 ground 在無限遠處。  Differential signal oscillation to the first order 會互相抵消,效率會比 common mode signal oscillation 差。  因為 charge 永遠是電中性,只有 differential charge oscillation (如dipole oscillation), 不會有 common mode charge oscillation.   Current 則可以有 differential current oscillation (如 small loop) 以及 common mode current oscillation.   

Very short dipole antenna 只有 differential charge/voltage oscillation,和 small loop antenna 只有 differential current oscillation 恰好是對偶性 (duality),兩者的效率都很差。如 Table 1.

image

Table 1: Short dipole antenna and small loop antenna. (Dl = l/4 for half wavelength antenna)

Dipole antenna 利用 dipole 長度增加而產生 common mode current radiation, 如下圖 2.  Dipole antenna 最大效率的 common mode radiation 是在長度為 l/2。

長度再更長反而 current 會互相抵消,效率並不會增加。但 radiation pattern 會更集中在一些角度,又稱之為"開花" ,實務上並不常使用。理想的 dipole antenna 是 l/2, 即半波長雙極天線,遠場如下,比 short dipole antenna 公式算出略高,主要是 current distribution 為 sine instead of triangular。或者更常見的,用 l/4 的 monopole 加上 ground plane mirror. 

fields from a half-wave dipole antenna

Table 2: Half wavelength dipole antenna.

幾個需要注意的點:

1. monopole 必需是垂直於 ground plane, 而非水平。水平反而會讓 common mode current 變為 differential current, 而抵消 radiation, 變成 small loop antenna.  這也是 RFID 或 Etag 必需要遠離大片金屬的原因,因為 RFID 或 Etag 都是水平貼於表面。

2. 電路產生的 EMI 多半為 common mode noise, 很容易 couple 到 dipole antenna.  最好的解決方法是在 noise source 上做 common mode choke 或 shielding. 

3. Dipole antenna 的 differential feed is easy to understand, but how coax feed? or single-ended signal feed?

 

 

chart!

 

 

如何縮短天線大小

Dipole/monopole 雖然簡單有效,實務上常常遇到的問題是如何縮小天線的尺寸,可以 fit 到機構內。特別對於 sub 1GHz 的 mobile 或 portable 應用 (UHF, GSM-900MHz, ISM-900MHz, LTE-700MHz), 如果有一或兩根突出的天線,不但不美觀,也不安全。如果能 embed 到機構中,更符合美觀和 MIMO 的應用。常用縮短天線的方法:

1. Use monopole instead of dipole: 50% length reduction, refere the previous article for the trade-off between dipole and monopole antenna.

2. Use Meander line structure:   The meander-line antenna (MLA) can be in a l/2 dipole or l/4 ground plane format.  The idea is to fold the conductors back and forth to make the overall antenna shorter, which is shown in the following figure.  It is a smaller area, but the radiation resistance, efficiency and bandwidth decease.  The parameters of meander shape will affect the antenna performance parameter.  More details about MLA is in the next section.

image            image

 

Meander Line Antenna (MLA)

Meander line antenna is one type of the micro strip antenna. The meander line antenna was proposed by Rashed and Tai for reduce the resonant length. Meandering the patch increases the path over which the surface current flows and that eventually results in lowering of the resonant frequency than the straight wire antenna of same dimensions.

The electrical small antenna defines as the largest dimension of the antenna is no more than one-tenth of a wavelength [5]. Meander antenna is electrically small antenna .The design of meander line antenna is a set of horizontal and vertical lines. Combination of horizontal and vertical lines forms turns. Number of turns increases efficiency increases. In case of meander line if meander spacing is increase resonant frequency decreases. At the same meander separation increase resonant frequency decreases [6]. A meander antenna is an extension of the basic folded antenna and frequencies much lower than resonances of a single element antenna of equal length.
Radiation efficiency of meander line antenna is good as compare to conventional half and quarter wavelength antennas. Antenna size reduction factor β depends primarily on the number of meander elements per wavelength and spacing of element widths of the rectangular loops [7].

image

 

 

 

Meander line structure to shorten the size; but trade with efficiency

Live antenna vs. dead antenna

 

Injection power = Ohmic loss + non-radiation mode + radiating mode

where is the power of non-radiating mode?  loss again?

why long wavelength has better power in Friss formula

 

How to deal with Antenna?  Any simplified Maxwell equation?

Meander line antenna (MLA) equivalent circuit.

2012年8月17日 星期五

如何增進工程師效率–Revision Control With Git

Revision control (RC, 版本控制) 有沒有用或是否重要,要吃過苦頭才知道。當你修改一些設計犯了錯想要回頭,或是要做一些 optimization 的實驗。如果有好的 revision control, 就像有一幅地圖,可以清楚而且有效率的 navigate 如何前進或後退。

有時和工程師談到 revision control, 得到的反應是用不到或是真的需要學一個新的 tool 嗎?  其實他們有一套私有的 revision control, 例如 copy 到不同的 file 或 directory,  用日期或 v1, v2..  來做版本控制。私有的問題是 (i) 簡單但容易有人為的錯誤;(ii) 很難和別人 share 和溝通。多半是單幹戶的方法。

雖然已經有許多的 revision control tool, 我發現 git 是最適合我 (as a designer) 時 revision control.  原因是:linux built-in revision control tool (事實上 linux 本身也是用 git 做 revision control);  可以簡單到單幹戶都可以自己取代私有的 revision control ; 但也非常適合一個 team project 的 revision control.

 

Git 三招走江湖

image

Git 的操作參考上圖。比起其他的 RC tool, 除了一樣的 working directory 和 repository 外,多了一個 staging area.

第一招:Create First Revision

Only 3 command

  • git init
  • git add .
  • git commit –m “init version”

Before start, first setup

  • .gitconfig in your root directory
  • .gitignore in your project directory

 

第二招:Check-in New/Modified Files/Directories

Add new files

  • git add <file/.>    -->  from working area to staging area
  • git commit –m “check-in message”   -->  from staging to repository

Modified files

Method 1: same as above

Method 2:

  • git commit –am “check-in message”  --> from working area to staging area and repository

git ci -am 不包括 new files

git add or git commit –m  only apply to 本目錄及其下所有子目錄

Always use “git status” to check

 

第三招:Checkout Repository

回復原狀 (repository to staging area and working area)

  • git checkout -- .

Staged, but not commit

  • git reset <file>   -->  staging area to working area

要撤消 Commit, 但維持 working directory

  • git reset --soft HEAD~1  -->repository to staging area

 

    2012年8月2日 星期四

    Group Theory–我的經驗

    前前後後花了後次時間自習 group theory,  每次都像是新的開始,之前學的統統忘了。前一兩次是 algebra approach, 從 definition (group/ring/field), more definitions (abelian, subgroup, normal subgroup, coset, conjugate, homophism, autohomophism, direct product, …), and theory (Lagrange, Sylow, Galois, …).  學的迷迷糊糊,因為從來用不到,最後統統忘掉。

    再來是從 permutation 著手,搭配 Rubik cube, puzzle-15, etc. game 方面的應用,依然不得要領。

    比較起來, linear algebra 雖然也很抽象, 但因為比較常用 (eigenvalue, eigenfucntion),倒不致於完全忘光。

    最近從 geometry symmetry 著手,開始比較有一些感覺。如 cyclic group, dihedral group, symmetry group.  不過也止於一些 geometry 的對稱群,沒有更深的領悟。無法和之前 algebra approach 的一堆 definition 和 theory link 在一起。

    這次看了 Carter 的 visual group theory 的 video, 非常有感覺。Carter 用 Cayley diagram visualize symmetry,同時用 Cayley diagram 來解釋 group product, normal subgroup, quotient group.  One picture more than a thousand words!    http://web.bentley.edu/empl/c/ncarter/vgt/index.html   另外YouTube 和 Google book 都有相關的資料。

     

    Cayley Graph

    Group theory visualization 的根本是 Cayley graph (or Cayley diagram).  Cayley diagram 包含兩個部份:node 表示 group element.  Arrow 表示 group generator 的作用。

    數學上的定義請參考:http://en.wikipedia.org/wiki/Cayley_graph

    我們的重點是如何用 Cayley graph  直覺式的看穿 group 的結構和理解 group theory 中的 definition 和 theory。

    以下的一個例子是一個簡單的化學分子,有三個 branch。它的對應 Cayley graph 如右。Generator 是一個 120 度順時針旋轉,每一次的操作都保持原來的形狀。在 group theory 中這是一個 cyclic group with order 3, or C3 group。

    image     image                         

     

    常見的對稱 Group 以及 Cayley Graph

    Cyclic Group, Cn : 看圖就可以理解這是旋轉對稱所對應的 group 和 Cayley graph.

     

    imageimage

     

    Dihedral Group, Dn : 下圖是正三角型所有的對稱操作,也稱為 D3 group。 Dn group (正 n 邊型/regular polygon) 包含了旋轉對稱+鏡像對稱,比起單純的 cyclic group 多了鏡像對稱。因此 Dn group Cayley graph 中的 node 多了一倍。Dn group 有兩種 generators, 對應旋轉對稱和鏡像對稱。兩種 geneator 的 arrows, 分別用 greenblue arrows 來表示。鏡像對稱是正->負-> 正->負.. 因此 arrow 是雙向的,圖中就省略了雙向箭頭。

    image

    Dn group 的 Caley graph 如下。光看 Cayley graph 就知道 Dn group 是 non-abelian group, why?

    image

     

    Abelian Group :  Abelian group 的特點是所有的 group element 符合 commutating rule, ab = ba.  Non-abelian group 在 quantum physics 扮演了關鍵的角色,例如 Yang-Mills theory.  

    從 Cayley graph 可以容易的判斷是否為 Abelian group.  我們可以証明 group element commutes 等同於 group generator commute.  如果只有一個 generator, 如 cyclic group, 一定是 Abelian group.  兩個 generator 以上,可以用下圖來判別。如果所有不同顏色的 arrow 交換都指到同一個 node, 是 Abelian group.  只要有任何 arrow 交換指到不同的 node, 則是 non-abelian group.   所以上述的 dihedral group 是 non-abelian group. 

    image 

     

    除了 cyclic group 以外,下列各圖都是 Abelian group

    image image  image image 

    相反的,以下都是 non-abelian group.

            

    由圖就可以看出來,Abelian group 都是 grid 的結構。所有的 arrows 都是垂直或水平 (除了端點的 arrow 要繞回另一端外)。Non-abelian group 的 arrow 則非直角或相互交錯。.

     

    Symmetric Group and Alternating Group,  Sn or  An 

    Dihedral group 表徵兩維平面的正多邊型的對稱。對於三維空間的正多面體 ( 如正三角錐,cube, etc.  i.e. regular polyhedron),顯然有更多的對稱操作。它們對應的對稱 group 為 symmetric group 和 alternating group.

    image

    Symmetric group, Sn : 包含 n! permutation (S3:6, S4:24, S5:120, …).  S3 = D3

    Alternating group, An : 包含 n!/2 even permulation (A4:12, A5:60, …)

    Tetrahedron: 有兩種對稱操作? (都是面旋轉120度?)。12 個 node, 是 A4 group

    image

    Cube: 有旋轉對稱和鏡像對稱。24 (8x3) node, 是 S4 group

    image

    Octahedron : 有旋轉對稱和鏡像對稱。24 (4x6) node, 是 S4 group

    image

    Dodecahedron : 60 node, 是 A5 group

    image

     

    Icosahedron :  60 node, 是 A5 group

    image

     

    除了上述五種正多面體以外,不存在更多的正多面體,這是古希臘人就已經知道的事。意即不存在 S5 (含) 以上正多面體的對稱群。這和 5 次方程式(含)以上無根式解有密切的關係,Galois 就是有此洞見而創建了 group theory。S5 (含) 以上的 symmetric group 也稱為 non-solvable symmetry group. 

    2012年5月25日 星期五

    How to Use CPAN to Install Perl Module and Script

    CPAN stands for “Comprehensive Perl Archive Network”.   It provides many useful perl modules.

    Method 1: manually install

    (i) Download the tarball from CPAN

    (ii) tar zxvf tarball

    (iii) cd decompressed directory, then ‘perl Makefile.PL’.  It there is dependency error, go to step (i)

    (iv) ‘make’, then ‘make test’, then ‘make install’.

    Pro: Method 1 works both for install module (WWW::Extractor) and script (e.g. xls2csv)

    Con: Method 1 is time consumping to deal with dependency error!!

     

     

    Method 2: auto install Module

    (i) type “cpan”,  set urllist by “o conf init urllist –> http://cpan.develooper.com/

    (ii) install CPAN to install the updated cpan.   then “reload cpan”

    (ii) perl –MCPAN –e ‘install module’   e.g. perl –MCPAN –e ‘install WWW::Extractor’

    Pro: Method 2 solve the dependency issues by cpan automatically!!

    Con: Method 2 only works with module,  NOT work with script, e.g. “xls2csv”!

    Need to do perl –MCPAN –e ‘install K/KE/KEN/xls2csv-1.06.tar.gz’

    追蹤者