2

I am trying to do some (de)convolution with audio samples. I have one sample s and the same sample with some filters added on top of it s_f. Both samples are represented as numpy arrays. I want to deconvolve them in order to get an array that represents the isolated filter f. Once I do that I should be able to reproduce s_f using convolution of s and f.

Here's the code:

f = signal.deconvolve(s, s_f)
convolved = signal.convolve(s, f)

However, I get the following error on the second line:

ValueError: in1 and in2 should have the same rank

Does anyone know what am I doing wrong here?

Thanks much, omer

Omer Eilam
  • 57
  • 2
  • 8
  • update: I used a bad set of samples. Using hopefully good ones I now get a different error: ValueError: object too deep for desired array – Omer Eilam Jun 12 '13 at 13:42

2 Answers2

3

deconvolve returns two arrays, the quotient and the remainder. So try:

f, r = signal.deconvolve(s, s_f)

For a long time, deconvolve has not had a proper docstring, but it has one in the master branch on github: https://github.com/scipy/scipy/blob/master/scipy/signal/signaltools.py#L731

The docstring shows an example of the use of deconvolve. Here's another (sig is scipy.signal and np is numpy):

The signal to be deconvolved is z, and the filter coefficients are in filter:

In [9]: z
Out[9]: 
array([  0.5,   2.5,   6. ,   9.5,  11. ,  10. ,   9.5,  11.5,  10.5,
         5.5,   2.5,   1. ])

In [10]: filter = np.array([0.5, 1.0, 0.5])

Apply deconvolve:

In [11]: q, r = sig.deconvolve(z, filter)

In [12]: q
Out[12]: array([ 1.,  3.,  5.,  6.,  5.,  4.,  6.,  7.,  1.,  2.])

Apply the filter to q to verify that we get back z:

In [13]: sig.convolve(q, filter)
Out[13]: 
array([  0.5,   2.5,   6. ,   9.5,  11. ,  10. ,   9.5,  11.5,  10.5,
         5.5,   2.5,   1. ])

By construction, this is a very clean example. The remainder is zero:

In [14]: r
Out[14]: array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.])

Of course, you won't always get such nice results.

Warren Weckesser
  • 110,654
  • 19
  • 194
  • 214
  • Thanks much. But I still can't get it to work. I think it may be because the numpy arrays that I use are 2D and not 1D and so I get: ValueError: object too deep for desired array. I tried converting them to 1D using numpy.reshape(s,s.size) but it didn't actually convert it to 1D. – Omer Eilam Jun 12 '13 at 13:43
  • Does making your array 1D make sense? Isn't each row or column a separate signal? If making it 1D is really what you want, you can do `s = numpy.reshape(s, s.size)` or `s = numpy.ravel(s)`. (The `reshape` function doesn't modify the array in-place.) – Warren Weckesser Jun 12 '13 at 14:22
  • I'm not sure how the scipy lib handles wav files. Perhaps the first column is the left track of the stereo signal and the second column is the right one. So perhaps it won't be wise to do that, but I'd still want to run it and see what I get. – Omer Eilam Jun 12 '13 at 15:28
  • I did manage to turn it into a 1D array using your suggestion though but now I get a different error on the deconvolve function: 'ValueError: BUG: filter coefficient a[0] == 0 not supported yet' – Omer Eilam Jun 12 '13 at 15:30
  • I still don't understand how `deconvolve` works. I asked a new question (http://stackoverflow.com/questions/40615034/understanding-scipy-deconvolve). So if anyone has an idea, please share. – ImportanceOfBeingErnest Nov 15 '16 at 17:59
0

The rank(x) returns the rank of matrix. In other words number of dimensions it contains. Please check whether ranks of s and f are the same before call to signal.convolve(). Otherwise you will receive an exception you quote.

I have no idea why deconvolution may return something with more dimensions than given input. This requires deeper investigation I don't have time for.

Michał Fita
  • 1,183
  • 1
  • 7
  • 24
  • It turns out I had a buggy sample. – Omer Eilam Jun 12 '13 at 12:57
  • Trying to repeat this with a (hopefully) good sample, I now get the following error in the deconvolution function: ValueError: object too deep for desired array – Omer Eilam Jun 12 '13 at 13:02
  • Maybe this can help you somehow: http://stackoverflow.com/questions/15923081/valueerror-object-too-deep-for-desired-array-while-using-convolution – Michał Fita Jun 12 '13 at 13:50