2017-05-31 56 views
4

我被告知將平均池化應用於矩陣M等同於丟棄M的傅立葉表示的高頻分量。平均池化意味着2通過可視化作爲此圖像中2平均池:嘗試確認平均池化等於使用numpy丟棄高頻傅里葉係數

enter image description here

我想驗證這一點,看看這個使用numpy的是如何工作的。所以我寫了一個天真的實現平均池及複製的功能,整齊地從here顯示矩陣:

def prettyPrintMatrix(m): 
    s = [['{:.3f}'.format(e) for e in row] for row in m] 
    lens = [max(map(len, col)) for col in zip(*s)] 
    fmt = '\t'.join('{{:{}}}'.format(x) for x in lens) 
    table = [fmt.format(*row) for row in s] 
    print '\n'.join(table) 

def averagePool(im): 
    imNew = np.empty((im.shape[0] /2, im.shape[1]/2)) 
    for i in range(imNew.shape[0]): 
     for j in range(imNew.shape[1]): 
      imNew[i,j] = np.average(im[(2*i):(2*i+2), (2*j):(2*j+2)]) 
    return imNew 

我們測試會發生什麼,我跑下面的代碼傅里葉係數:

M = np.random.random((8,8)) 
Mpooled = averagePool(M) 

# print the original M 
print('original M:') 
prettyPrintMatrix(M) 

# print Fourier coefficients of regular matrix 
print('Fourier of M') 
prettyPrintMatrix(np.fft.fft2(M)) 

# print Fourier coefficients of pooled matrix 
print('Fourier of the pooled M') 
prettyPrintMatrix(np.fft.fft2(Mpooled)) 

的示例輸出爲:

original M: 
0.849 0.454 0.231 0.605 0.375 0.842 0.533 0.954 
0.489 0.097 0.990 0.199 0.572 0.262 0.299 0.634 
0.477 0.052 0.429 0.670 0.323 0.458 0.459 0.954 
0.984 0.884 0.620 0.657 0.352 0.765 0.897 0.642 
0.179 0.894 0.835 0.710 0.916 0.544 0.968 0.557 
0.253 0.197 0.813 0.450 0.936 0.165 0.169 0.712 
0.677 0.544 0.507 0.107 0.733 0.334 0.056 0.171 
0.356 0.639 0.580 0.517 0.763 0.401 0.771 0.219 

Fourier of M 
34.680+0.000j -0.059-0.188j 0.076+1.227j -1.356+1.515j 2.101+0.000j -1.356-1.515j 0.076-1.227j -0.059+0.188j 
-1.968-1.684j 2.125-0.223j 2.277+1.442j 1.629-0.795j -0.141+1.460j 0.694-2.363j -0.627+0.971j -0.847-2.094j 
3.496+2.808j -1.099+1.260j 0.921-0.814j 2.499+0.283j -1.048-1.206j -3.228+2.435j -2.934+0.030j 0.386-0.015j 
0.451-0.301j 0.791-0.143j -0.463-0.031j 1.841+0.032j -1.979-1.066j 1.344-1.229j 3.487-1.297j 2.105-2.455j 
0.111+0.000j 0.166+1.317j 0.946-0.016j 0.587-0.443j -2.710+0.000j 0.587+0.443j 0.946+0.016j 0.166-1.317j 
0.451+0.301j 2.105+2.455j 3.487+1.297j 1.344+1.229j -1.979+1.066j 1.841-0.032j -0.463+0.031j 0.791+0.143j 
3.496-2.808j 0.386+0.015j -2.934-0.030j -3.228-2.435j -1.048+1.206j 2.499-0.283j 0.921+0.814j -1.099-1.260j 
-1.968+1.684j -0.847+2.094j -0.627-0.971j 0.694+2.363j -0.141-1.460j 1.629+0.795j 2.277-1.442j 2.125+0.223j 

Fourier of the pooled M 
8.670+0.000j -0.180+0.019j -0.288+0.000j -0.180-0.019j 
-0.228-0.562j 0.487+0.071j 0.156+0.638j -0.049-0.328j 
0.172+0.000j -0.421-0.022j -0.530+0.000j -0.421+0.022j 
-0.228+0.562j -0.049+0.328j 0.156-0.638j 0.487-0.071j 

現在我期望合併矩陣的傅立葉係數以某種方式與低頻原始矩陣的傅立葉係數。然而,我所看到的唯一關係是最低的頻率(左上),這是合併後的4倍。 我現在的問題是:在合併之前和之後的傅立葉係數之間是否存在關係?如果是,它是什麼?

+2

像這樣的平均濾波器是一個非常差的低通濾波器,所以你不能指望看到理想的頻率響應。您將看到較高頻率分量的普遍降低,但不會一致。 –

+1

好的,問題在於理論,而不是實施。沒有人發佈了答案。因此,如果您將此作爲答案發布,我會接受它。 – dimpol

+0

沒問題 - 評論已轉換爲以下答案。 –

回答

1

平均濾波器是一個非常粗糙的低通濾波器,所以你不能指望看到理想的頻率響應。您將看到較高頻率分量的普遍降低,但不會一致。平均值通常用於考慮效率時(因爲唯一的係數是隱含的1和0值,所以只需要添加),或者當精度不重要時。否則應使用適當的低通濾波器。