Convolution and Deconvolution in Python using scipy.signal

前端 未结 2 1039
暗喜
暗喜 2021-01-15 09:46

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

相关标签:
2条回答
  • 2021-01-15 10:45

    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.

    0 讨论(0)
  • 2021-01-15 10:48

    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.

    0 讨论(0)
提交回复
热议问题