W!o+ 的《小伶鼬工坊演義》︰神經網絡【FFT】三

若是只因『考試!?』而讀書,恐怕會錯過許多學習『樂趣』!!『學問』之道有其『基礎』,『技藝』之術有其『規矩』,所以說本立而道生。若能善用『規矩』,不祇能成『方圓』,又有什麼『圖』不可『製』耶??

《孟子》‧離婁上
孟子曰:「離婁之明,公輸子之巧,不以規矩,不能成方員:師曠之聰,不以六律,不能正五音;堯舜之道,不以仁政,不能平治天下。今有仁心仁聞而民不被其澤,不可法於後世者,不行先王之道也。

故曰,徒善不足以為政,徒法不能以自行。《詩》云:『不愆不忘 ,率由舊章。』遵先王之法而過者,未之有也。聖人既竭目力焉,繼之以規矩準繩,以為方員平直,不可勝用也;既竭耳力焉,繼之以六律,正五音,不可勝用也;既竭心思焉,繼之以不忍人之政,而仁覆天下矣。

故曰,為高必因丘陵,為下必因川澤。為政不因先王之道,可謂智乎?是以惟仁者宜在高位。不仁而在高位,是播其惡於眾也。上無道揆也。下無法守也,朝不信道,工不信度,君子犯義,小人犯刑 ,國之所存者幸也。

故曰,城郭不完,兵甲不多,非國之災也;田野不辟,貨財不聚,非國之害也。上無禮,下無學,賊民興,喪無日矣。《詩》曰:『天之方蹶,無然泄泄。』泄泄,猶沓沓也。事君無義,進退無禮 ,言則非先王之道者,猶沓沓也。故曰:責難於君謂之恭,陳善閉邪謂之敬,吾君不能謂之賊。

───

 

將要如何理解『numpy』之 DFT 建置呢︰

There are many ways to define the DFT, varying in the sign of the exponent, normalization, etc. In this implementation, the DFT is defined as

A_k = \sum_{m=0}^{n-1} a_m \exp\left\{-2\pi i{mk \over n}\right\} \qquad k = 0,\ldots,n-1.

The DFT is in general defined for complex inputs and outputs, and a single-frequency component at linear frequency f is represented by a complex exponential a_m = \exp\{2\pi i\,f m\Delta t\}, where \Delta t is the sampling interval.

The values in the result follow so-called “standard” order: If A = fft(a, n), then A[0] contains the zero-frequency term (the mean of the signal), which is always purely real for real inputs. Then A[1:n/2] contains the positive-frequency terms, and A[n/2+1:] contains the negative-frequency terms, in order of decreasingly negative frequency. For an even number of input points, A[n/2] represents both positive and negative Nyquist frequency, and is also purely real for real input. For an odd number of input points, A[(n-1)/2] contains the largest positive frequency, while A[(n+1)/2] contains the largest negative frequency. The routine np.fft.fftfreq(n) returns an array giving the frequencies of corresponding elements in the output. The routine np.fft.fftshift(A) shifts transforms and their frequencies to put the zero-frequency components in the middle, and np.fft.ifftshift(A) undoes that shift.

When the input a is a time-domain signal and A = fft(a), np.abs(A) is its amplitude spectrum and np.abs(A)**2 is its power spectrum. The phase spectrum is obtained by np.angle(A).

The inverse DFT is defined as

a_m = \frac{1}{n}\sum_{k=0}^{n-1}A_k\exp\left\{2\pi i{mk\over n}\right\} \qquad m = 0,\ldots,n-1.

It differs from the forward transform by the sign of the exponential argument and the default normalization by 1/n.

───

 

即使輔之以維基百科詞條說明︰

離散傅立葉變換

離散傅立葉變換Discrete Fourier Transform,縮寫為DFT),是傅立葉變換時域頻域上都呈離散的形式,將信號的時域採樣變換為其DTFT的頻域採樣。在形式上,變換兩端(時域和頻域上)的序列是有限長的,而實際上這兩組序列都應當被認為是離散周期信號的主值序列。即使對有限長的離散信號作DFT,也應當將其看作其周期延拓的變換。在實際應用中通常採用快速傅立葉變換計算DFT。

定義

對於N點序列\left\{x[n]\right\}_{0\le n <N},它的離散傅立葉變換(DFT)為

\hat{x}[k]=\sum_{n=0}^{N-1} e^{-i\frac{2\pi}{N}nk}x[n] \qquad k = 0,1,\ldots,N-1.

其中e自然對數底數i虛數單位。通常以符號\mathcal{F}表示這一變換,即

\hat{x}=\mathcal{F}x

離散傅立葉變換的逆變換(IDFT)為:

x\left[n\right]={1 \over N}\sum_{k=0}^{N-1} e^{ i\frac{2\pi}{N}nk}\hat{x}[k] \qquad n = 0,1,\ldots,N-1.

可以記為:

x=\mathcal{F}^{-1}\hat{x}

實際上,DFT和IDFT變換式中和式前面的歸一化係數並不重要。在上面的定義中,DFT和IDFT前的係數分別為11/N。有時會將這兩個係數都改成1/\sqrt{N}

從連續到離散

連續時間信號x(t)以及對應的連續傅立葉變換\hat{x}(\omega)都是連續函數。由於數字系統只能處理有限長的離散信號,因此必須將x\hat{x}都離散化,並且建立對應的傅立葉變換。

假設x(t)時限於[0, L],再通過時域採樣將x(t)離散化,就可以得到有限長離散信號,記為x_{discrete}(t)。設採樣周期為T,則時域採樣點數N=L/T

x_{discrete}(t) = x(t)\sum_{n=0}^{N-1}\delta(t-nT)=\sum_{n=0}^{N-1}x(nT)\delta(t-nT)

它的傅立葉變換為

\hat{x}_{discrete}(\omega) = \sum_{n=0}^{N-1}x(nT)\mathcal{F}\delta(t-nT) = \sum_{n=0}^{N-1}x(nT)e^{-i n\omega T}

這就是x(t)在時域採樣後的連續傅立葉變換,也就是離散時間傅立葉變換,它在頻域依然是連續的。

下面將頻域信號轉化為有限長離散信號。與對時域信號的處理類似 ,假設頻域信號是帶限的,再經過離散化,即可得到有限長離散信號。依據採樣定理,時域採樣若要能完全重建原信號,頻域信號\hat{x}(\omega)應當帶限於(0,1/(2*T))。由於時域信號時限於[0, L],由採樣定理以及時頻對偶的關係,頻域的採樣間隔應為1/L。故,頻域採樣點數為:

\frac{1/T}{1/L}=N

即頻域採樣的點數和時域採樣同為N,頻域採樣點為\{\omega_k={2\pi}k/NT\}_{0 \le k < N} 在DTFT頻域上採樣:

\hat{x}[k] = \hat{x}_{discrete}(\omega_k) = \frac1{T} \sum_{n=0}^{N-1}x[nT]e^{-i\frac{2\pi}{N} nk}

T=1,將其歸一化,就得到前面定義的離散傅立葉變換。因此 ,DFT就是先將信號在時域離散化,求其連續傅立葉變換後,再在頻域離散化的結果。

───

 

雖有助益,或許難以通熟。

何不好好了解『複數』是什麼??

那麽要怎樣理解『複數z = x + i \ y 的呢?如果說『複數』起源於『方程式』的『求解』,比方說 x^2 + 1 = 0, \ x = \pm i,這定義了『i = \sqrt{-1}』,但是它的『意義』依然晦澀。即使說從『複數平面』的每一個『(x, y) 都對應著一個『複數z = x + i \ y 可能還是不清楚『i』的意思到底是什麼?假使再從『複數』的『加法上看』︰

假使 z_1 = x_1 + i \ y_1z_2 = x_2 + i \ y_2

那麼 z_1 + z_2 = (x_1 + x_2) + i \ (y_1 + y_2)

這是一種類似『向量』的加法,是否『i』的意義就藏在其中的呢?

positive_negative_rotation

imaginary_rotation

220px-90-Degree_Rotations_in_the_Complex_Plane

一九九八年美國新罕布希爾大學 University of New Hampshire 的
Paul J. Nahin 教授寫了一本『An Imaginary Tale: the Story of the Square Root of −1』的書,指出韋塞爾當初所講的『幾何意義』就是︰

i = \sqrt{-1} = 1 \ \angle 90^{\circ}

也就是說『i』就是『逆時鐘旋轉九十度』的『運算子』!

假使從複數的『極座標』表示法來看複數的『乘法』︰

假 使 z_1 = r \cdot e^{i \ \theta}, \ z_2 = \alpha \cdot e^{i \ \beta},那麼 z_1 \cdot z_2 = \alpha \cdot r \cdot e^{i \ (\theta +\beta)}

就可以解釋成 Z1 『向量』被『逆時鐘旋轉』了『β』角度,它的『長度』被『縮放』了『α』倍!!

複數果真不是簡單的『』啊!也難怪它是『完備的』的喔!!

電子和工程領域中,常常會使用到『正弦』 Sin 信號,一般可以使用『相量』 Phasor 來作簡化分析。『相量』是一個『複數』,也是一種『向量』,通常使用『極座標』表示,舉例來說一個『振幅』是 A,『角頻率』是 \omega,初始『相位角』是 \theta 的『正弦信號』可以表示為 A \ e^{j \  (\omega t + \theta)},這裡的『j』就是『複數的 i』。為什麼又要改用 j = \sqrt{-1} 的呢?這是因為再『電子學』和『電路學』領域中 i 通常代表著『電流』, v 通常代表了『電壓』,因此為了避免『混淆』起見,所以才會『更名用  j』。

尤拉公式 Euler’s formula,是複數分析中的公式,它將三角函數與複數指數函數相關聯,對任意實數 x,都有

e^{j x} = \cos x + j \sin x

,它的重要性是不言而喻的啊!!

300px-Wykres_wektorowy_by_Zureks.svg

Unfasor

─── 摘自《【Sonic π】電聲學補充《二》

 

踏踏實實的上一堂課!!

若想更深入的了解,何不上一堂 Stanford 大學的公開課︰

3auz5bq4jduojkwc3nqdiq6aa7vy2f6m

Stanford Engineering Everywhere EE261 – The Fourier Transform and its Applications

author: Brad G. Osgood, Computer Science Department, Stanford University
released under terms of: Creative Commons Attribution Non-Commercial (CC-BY-NC)

The goals for the course are to gain a facility with using the Fourier transform, both specific techniques and general principles, and learning to recognize when, why, and how it is used. Together with a great variety, the subject also has a great coherence, and the hope is students come to appreciate both.

……

同時認真讀讀 Brad Osgood 教授之課堂筆記耶。

Lecture Notes for

EE 261

The Fourier Transform and its Applications

Prof. Brad Osgood
Electrical Engineering Department
Stanford University

 

FT-1

 

FT-2

─── 摘自《勇闖新世界︰ W!o《卡夫卡村》變形祭︰品味科學‧教具教材‧【專題】 PD‧箱子世界‧傅立葉

 

『真積力,久則入』,一朝自然水到磲成的乎!!??