27

我试图理解scipy.signal.deconvolve

从数学的角度来看,卷积只是傅立叶空间中的乘法,所以我希望对于两个函数fg
Deconvolve(Convolve(f,g) , g) == f

在 numpy/scipy 中,要么不是这种情况,要么我错过了重要的一点。尽管已经存在一些与 SO 上的反卷积相关的问题(例如此处此处),但它们并未解决这一点,但其他问题仍不清楚()或未回答(此处)。SignalProcessing SE ( thisthis )上还有两个问题,这些问题的答案对于理解 scipy 的 deconvolve 函数是如何工作的没有帮助。

问题是:

  • 假设你知道卷积函数 g,你如何从卷积信号重建原始信号f
  • 或者换句话说:这个伪代码如何Deconvolve(Convolve(f,g) , g) == f转换成 numpy / scipy?

编辑请注意,这个问题的目标不是防止数字不准确(尽管这也是一个悬而未决的问题),而是理解卷积/反卷积如何在 scipy.

以下代码尝试使用 Heaviside 函数和高斯滤波器来做到这一点。从图中可以看出,卷积的反卷积结果根本不是原来的Heaviside函数。如果有人能对这个问题有所了解,我会很高兴。

import numpy as np
import scipy.signal
import matplotlib.pyplot as plt

# Define heaviside function
H = lambda x: 0.5 * (np.sign(x) + 1.)
#define gaussian
gauss = lambda x, sig: np.exp(-( x/float(sig))**2 )

X = np.linspace(-5, 30, num=3501)
X2 = np.linspace(-5,5, num=1001)

# convolute a heaviside with a gaussian
H_c = np.convolve( H(X),  gauss(X2, 1),  mode="same"  )
# deconvolute a the result
H_dc, er = scipy.signal.deconvolve(H_c, gauss(X2, 1) )


#### Plot #### 
fig , ax = plt.subplots(nrows=4, figsize=(6,7))
ax[0].plot( H(X),          color="#907700", label="Heaviside",    lw=3 ) 
ax[1].plot( gauss(X2, 1),  color="#907700", label="Gauss filter", lw=3 )
ax[2].plot( H_c/H_c.max(), color="#325cab", label="convoluted" ,  lw=3 ) 
ax[3].plot( H_dc,          color="#ab4232", label="deconvoluted", lw=3 ) 
for i in range(len(ax)):
    ax[i].set_xlim([0, len(X)])
    ax[i].set_ylim([-0.07, 1.2])
    ax[i].legend(loc=4)
plt.show()

在此处输入图像描述

编辑请注意,有一个matlab 示例,展示了如何使用对矩形信号进行卷积/反卷积

yc=conv(y,c,'full')./sum(c); 
ydc=deconv(yc,c).*sum(c); 

本着这个问题的精神,如果有人能够将这个例子翻译成python,那也会有所帮助。

4

2 回答 2

19

经过反复试验,我发现了如何解释结果,scipy.signal.deconvolve()并将我的发现作为答案发布。

让我们从一个工作示例代码开始

import numpy as np
import scipy.signal
import matplotlib.pyplot as plt

# let the signal be box-like
signal = np.repeat([0., 1., 0.], 100)
# and use a gaussian filter
# the filter should be shorter than the signal
# the filter should be such that it's much bigger then zero everywhere
gauss = np.exp(-( (np.linspace(0,50)-25.)/float(12))**2 )
print gauss.min()  # = 0.013 >> 0

# calculate the convolution (np.convolve and scipy.signal.convolve identical)
# the keywordargument mode="same" ensures that the convolution spans the same
#   shape as the input array.
#filtered = scipy.signal.convolve(signal, gauss, mode='same') 
filtered = np.convolve(signal, gauss, mode='same') 

deconv,  _ = scipy.signal.deconvolve( filtered, gauss )
#the deconvolution has n = len(signal) - len(gauss) + 1 points
n = len(signal)-len(gauss)+1
# so we need to expand it by 
s = (len(signal)-n)/2
#on both sides.
deconv_res = np.zeros(len(signal))
deconv_res[s:len(signal)-s-1] = deconv
deconv = deconv_res
# now deconv contains the deconvolution 
# expanded to the original shape (filled with zeros) 


#### Plot #### 
fig , ax = plt.subplots(nrows=4, figsize=(6,7))

ax[0].plot(signal,            color="#907700", label="original",     lw=3 ) 
ax[1].plot(gauss,          color="#68934e", label="gauss filter", lw=3 )
# we need to divide by the sum of the filter window to get the convolution normalized to 1
ax[2].plot(filtered/np.sum(gauss), color="#325cab", label="convoluted" ,  lw=3 )
ax[3].plot(deconv,         color="#ab4232", label="deconvoluted", lw=3 ) 

for i in range(len(ax)):
    ax[i].set_xlim([0, len(signal)])
    ax[i].set_ylim([-0.07, 1.2])
    ax[i].legend(loc=1, fontsize=11)
    if i != len(ax)-1 :
        ax[i].set_xticklabels([])

plt.savefig(__file__ + ".png")
plt.show()    

此代码生成以下图像,准确显示我们想要的 ( Deconvolve(Convolve(signal,gauss) , gauss) == signal)

在此处输入图像描述

一些重要的发现是:

  • 滤波器应该比信号短
  • 过滤器应该比零大得多(这里> 0.013就足够了)
  • 使用卷积的关键字参数mode = 'same'可确保它与信号位于相同的数组形状上。
  • 反卷积n = len(signal) - len(gauss) + 1有点。因此,为了让它也驻留在相同的原始数组形状上,我们需要s = (len(signal)-n)/2在两侧扩展它。

当然,仍然欢迎对这个问题的进一步调查结果、评论和建议。

于 2016-11-17T13:32:48.170 回答
7

正如评论中所写,我无法帮助您最初发布的示例。正如@Stelios 指出的那样,由于数值问题,反卷积可能无法解决。

但是,我可以重现您在编辑中发布的示例:

在此处输入图像描述

这是从matlab源代码直接翻译的代码:

import numpy as np
import scipy.signal
import matplotlib.pyplot as plt

x = np.arange(0., 20.01, 0.01)
y = np.zeros(len(x))
y[900:1100] = 1.
y += 0.01 * np.random.randn(len(y))
c = np.exp(-(np.arange(len(y))) / 30.)

yc = scipy.signal.convolve(y, c, mode='full') / c.sum()
ydc, remainder = scipy.signal.deconvolve(yc, c)
ydc *= c.sum()

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(4, 4))
ax[0][0].plot(x, y, label="original y", lw=3)
ax[0][1].plot(x, c, label="c", lw=3)
ax[1][0].plot(x[0:2000], yc[0:2000], label="yc", lw=3)
ax[1][1].plot(x, ydc, label="recovered y", lw=3)

plt.show()
于 2016-11-17T13:36:02.283 回答