【鼎革‧革鼎】︰ Raspbian Stretch 《六之 J.3‧MIR-13.1 》

雖然曾經簡略介紹過 STFT ︰

人耳聽旋律,縱然不能分辨音高時值,卻知音符總有個先來後到。傳說莫札特絕對音感之好,即使是初聞的曲調,也能立刻譜出!!為求一次得到『兩個領域』── 時間、頻率 ── 的好處,於是人們設想了

短時距傅立葉變換

短時距傅立葉變換傅立葉變換的一種變形,為時頻分析中其中一個重要的工具。

傅立葉轉換在概念上的區別

將訊號做傅立葉變換後得到的結果,並不能給予關於訊號頻率隨時間改變的任何資訊。以下的例子作為說明:

x(t)=\begin{cases} \cos(440 \pi t); & t < 0.5 \\ \cos(660 \pi t); & 0.5 \leq t < 1 \\ \cos(524 \pi t); & t \ge 1 \end{cases}

傅立葉變換後的頻譜和短時距傅立葉轉換後的結果如下:

傅立葉轉換後, 橫軸為頻率(赫茲)

短時距傅立葉轉換, 橫軸為時間(秒),縱軸為頻率(赫茲)

由上圖可發現,傅立葉轉換只提供了有哪些頻率成份的資訊,卻沒有提供時間資訊;而短時傅立葉轉換則清楚的提供這兩種資訊。這種時頻分析的方法有利於頻率會隨著時間改變的訊號,如音樂訊號和語音訊號等分析。

定義

數學定義

簡單來說,在連續時間的例子,一個函數可以先乘上僅在一段時間不為零的窗函數再進行一維的傅立葉變換。再將這個窗函數沿著時間軸挪移,所得到一系列的傅立葉變換結果排開則成為二維表象。數學上,這樣的操作可寫為:

 X(t, f) = \int_{-\infty}^{\infty} w(t-\tau)x(\tau) e^{-j 2 \pi f \tau} \, d\tau

另外也可用角頻率來表示:

 X(t, \omega) = \int_{-\infty}^{\infty} w(t-\tau)x(\tau) e^{-j \omega \tau} \, d\tau

其中w(t)窗函數,窗函數種類有很多種,會在稍後再做仔細討論。x(t)是待變換的訊號。X(t,\omega)w(t-\tau)x(\tau)的傅立葉變換。 隨著t的改變,窗函數在時間軸上會有位移。經w(t-\tau)x(\tau)後,訊號只留下了窗函數截取的部分做最後的傅立葉轉換。

而反短時距傅立葉轉換,其數學類似傅立葉轉換,但須消除窗函數的作用:

 x(t)=w(t_1-t)^{-1} \int_{-\infty}^{\infty} X(t_1, f) e^{j 2 \pi f t}\, df ; w(t_1-t)\ne 0

窗函數

窗函數通常滿足下列特性:

  1. w(t) = w(-t) \,,即為偶函數。
  2. max(w(t))=w(0) \,,即窗函數的中央通常是最大值的位置。
  3. w(t_1)\ge w(t_2), |t_2| \ge |t_1|,即窗函數的值由中央開始向兩側單調遞減。
  4. w(t)\cong 0 , |t|\to \infty,即窗函數的值向兩側遞減為零。

常見的窗函數有:方形、三角形、高斯函數等,而短時距傅立葉轉換也因窗函數的不同而有不同的名稱。而加伯轉換,即為窗函數是高斯函數的短時距傅立葉轉換,通常沒有特別說明的短時距傅立葉轉換,即為加伯轉換

……

頻譜(Spectrogram)

Spectrogram即短時傅立葉轉換後結果的絕對值平方,兩者本質上是相同的,在文獻上也常出現spectrogram這個名詞。

SP_x(t,f) = |X(t,f)|^2 = | \int_{-\infty}^{\infty} w(t-\tau)x(\tau) e^{-j 2 \pi f \tau} \, d\tau |^2

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

 

想來恐無助於應用 librosa stft API 軟件界面也︰

librosa.core.stft

librosa.core.stft(y, n_fft=2048, hop_length=None, win_length=None, window=’hann’, center=True, dtype=<class ‘numpy.complex64’>, pad_mode=’reflect’)
Short-time Fourier transform (STFT)

Returns a complex-valued matrix D such that

np.abs(D[f, t]) is the magnitude of frequency bin f at frame t

np.angle(D[f, t]) is the phase of frequency bin f at frame t

Parameters:

y : np.ndarray [shape=(n,)], real-valued

the input signal (audio time series)

n_fft : int > 0 [scalar]

FFT window size

hop_length : int > 0 [scalar]

number audio of frames between STFT columns. If unspecified, defaults win_length / 4.

win_length : int <= n_fft [scalar]

Each frame of audio is windowed by window(). The window will be of length win_length and then padded with zeros to match n_fft.

If unspecified, defaults to win_length = n_fft.

window : string, tuple, number, function, or np.ndarray [shape=(n_fft,)]

center : boolean

  • If True, the signal y is padded so that frame D[:, t] is centered at y[t * hop_length].
  • If False, then D[:, t] begins at y[t * hop_length]

dtype : numeric type

Complex numeric type for D. Default is 64-bit complex.

mode : string

If center=True, the padding mode to use at the edges of the signal. By default, STFT uses reflection padding.

Returns:

D : np.ndarray [shape=(1 + n_fft/2, t), dtype=dtype]

STFT matrix

※ 範例

 

 

故而特此援引 JULIUS O. SMITH III 先生之大作

SPECTRAL AUDIO SIGNAL PROCESSING

JULIUS O. SMITH III
Center for Computer Research in Music and Acoustics (CCRMA)

 

Time-Frequency Displays 章節

 

盼能加深認識的ㄡ◎