Reputation: 33
So I am trying to figure out how to get convolve and deconvolve to work properly. Here's my code:
import numpy as np
from scipy import interpolate
from scipy import signal
import matplotlib.pyplot as plt
a = np.arange(0, 50, 1)
b = np.array([1,2,1])
aconvolved = signal.convolve(np.sin(a), b)
adeconvolved = signal.deconvolve(aconvolved, b)
plt.figure()
plt.plot(a, np.sin(a), 'g')
plt.plot(a, aconvolved[0], 'r')
plt.plot(a, adeconvolved[0], 'b')
plt.draw()
When I convolve "a" about a single point (like b = [2]), instead of an array, it works just fine. But when I attempt to do it about more than 1 point, or an array, it just doesn't work. I receive errors such as:
"ValueError: x and y must have same first dimension"
I get this same error if I instead use:
a = np.arange(1, 50, 1)
b = np.array([1,2,1])
Even though in this case, I know they do both start at 1. If I do the same thing, but with
b = np.array([1,2,1])
I get an error of:
"ValueError: BUG: filter coefficient a[0] == 0 not supported yet"
Anyone have any tips for what I am doing wrong?
Upvotes: 2
Views: 2981
Reputation: 23510
You do not get the error where you'd think you get it. This works:
import numpy as np
from scipy import interpolate
from scipy import signal
import matplotlib.pyplot as plt
a = np.arange(0, 50, 1)
b = np.array([1,2,1])
aconvolved = signal.convolve(np.sin(a), b)
adeconvolved = signal.deconvolve(aconvolved, b)[0]
plt.figure()
plt.plot(a, np.sin(a), 'g')
plt.plot(a, aconvolved[1:-1], 'r')
plt.plot(a, adeconvolved, 'b')
plt.draw()
The differences:
aconvolved[1:-1]
(if you do not want to do this, then use keyword argument mode='same'
for the convolve
to receive a vector which has the same length as the input, but then that'll cause problems with the deconvolution output length).signal.deconvolve(aconvolved, b)[0]
Upvotes: 2