# Griffin-Lim演算法

Griffin-Lim演算法（英語：Griffin-Lim algorithm，Griffin-Lim signal estimation algorithm），又稱Griffin-Lim訊號估計算法葛氏林氏演算法

## 演算法概述

${\displaystyle x_{i}}$是第i次重建的訊號，${\displaystyle f}$為短時距傅立葉變換，${\displaystyle f^{-1}}$是反短時距傅立葉變換。

${\displaystyle S_{i}}$${\displaystyle P_{i}}$分別代表${\displaystyle x_{i}}$的短時傅立葉轉換的大小及相位，即

${\displaystyle f(x_{i})=S_{i}e^{jP_{i}},j={\sqrt {-}}1}$

1. 隨機初始化${\displaystyle P_{0}}$，則${\displaystyle x_{0}=f^{-1}(Se^{jP_{0}})}$

2. 對${\displaystyle x_{i}}$作時頻分析取得大小及相位，${\displaystyle f(x_{i})=S_{i}e^{jP_{i}}}$

3. 將${\displaystyle S_{i}e^{jP_{i}}}$中的大小${\displaystyle S_{i}}$${\displaystyle S}$取代

4. 重建訊號，${\displaystyle x_{i+1}=f^{-1}(Se^{jP_{i}})}$

5. 重複步驟2~4，直到滿足迭代停止條件

## 實作

### Python實作

def GLA(S, n_iter = 100, n_fft = 2048, hop_length = None, window = 'hann'):
hop_length = n_fft//4 if hop_length is None else hop_length
phase = np.exp(2j*np.pi*np.random.rand(*S.shape))
for i in range(n_iter):
xi = np.abs(S).astype(np.complex)*phase
signal = librosa.istft(xi, hop_length = hop_length, window = window)
next_xi = librosa.stft(signal, n_fft = n_fft, hop_length = hop_length, window = window)
phase = np.exp(1j*np.angle(next_xi))
xi = np.abs(S).astype(np.complex)*phase
signal = librosa.istft(xi, hop_length = hop_length, window = window)
return signal


## 参考文献

1. ^ D. Griffin and Jae Lim, "Signal estimation from modified short-time Fourier transform," in IEEE Transactions on Acoustics, Speech, and Signal Processing, vol. 32, no. 2, pp. 236-243, April 1984.
2. ^
3. ^ Le Roux, Jonathan & Kameoka, Hirokazu & Ono, Nobutaka & Sagayama, Shigeki. (0002). Fast Signal Reconstruction from Magnitude STFT Spectrogram based on Spectrogram Consistency.