我已經作出了Python代碼以平滑使用爾斯特拉斯變換給定的信號,它基本上是與信號的歸一化高斯的卷積。如何消除由scipy/numpy fft中的零填充引起的邊界效應?
的代碼如下:
#Importing relevant libraries
from __future__ import division
from scipy.signal import fftconvolve
import numpy as np
def smooth_func(sig, x, t= 0.002):
N = len(x)
x1 = x[-1]
x0 = x[0]
# defining a new array y which is symmetric around zero, to make the gaussian symmetric.
y = np.linspace(-(x1-x0)/2, (x1-x0)/2, N)
#gaussian centered around zero.
gaus = np.exp(-y**(2)/t)
#using fftconvolve to speed up the convolution; gaus.sum() is the normalization constant.
return fftconvolve(sig, gaus/gaus.sum(), mode='same')
如果我在說一個階躍函數的代碼,它平滑的角落,但在邊界它解釋另一個角落和平滑那因此,在邊界給予不必要的行爲。我用下面的鏈接中顯示的圖解釋了這一點。
Boundary effects
如果我們直接整合來找到卷積,這個問題就不會出現。因此問題不在於Weierstrass變換,因此問題在於scipy的fftconvolve函數。
要理解爲什麼這個問題出現,我們首先需要了解fftconvolve的工作中SciPy的。
fftconvolve函數基本上使用卷積定理來加速計算。
簡而言之:
卷積(int1,int2)= ifft(fft(int1)* fft(int2))
如果我們直接應用這個定理,我們不會得到所需的結果。爲了得到想要的結果,我們需要將數組的fft放在max(int1,int2)大小的兩倍的數組上。但是這會導致不希望的邊界效應。這是因爲在fft代碼中,如果size(int)大於fft的大小,它會填充輸入,然後採用fft。這個零填充正是造成不希望的邊界效應的原因。
您能否提供一個方法來消除這種邊界效應?
我試圖通過一個簡單的技巧將其刪除。在平滑函數之後,我將平滑信號的值與邊界附近的原始信號進行比較,如果它們不匹配,則將該平滑函數的值替換爲該點處的輸入信號。
據如下:
i = 0
eps=1e-3
while abs(smooth[i]-sig[i])> eps: #compairing the signals on the left boundary
smooth[i] = sig[i]
i = i + 1
j = -1
while abs(smooth[j]-sig[j])> eps: # compairing on the right boundary.
smooth[j] = sig[j]
j = j - 1
沒有此方法的一個問題,因爲使用中有平滑函數小的跳躍的ε的,如下所示:
jumps in the smooth func
上述方法能否解決這個邊界問題有什麼變化?
Dupe of http://math.stackexchange.com/q/127875/2206 – endolith 2013-08-13 18:23:12