Inverse filtering using Python Inverse filtering using Python numpy numpy

Inverse filtering using Python


This is called deconvolution: scipy.signal.deconvolve will do this for you. Example where you know the original input signal x:

import numpy as npimport scipy.signal as signal# We want to try and recover x from y, where y is made like this:x = np.array([0.5, 2.2, -1.8, -0.1])h = np.array([1.0, 0.5])y = signal.convolve(x, h)# This is called deconvolution:xRecovered, xRemainder = signal.deconvolve(y, h)# >>> xRecovered# array([ 0.5,  2.2, -1.8, -0.1])# >>> xRemainder# array([  0.00000000e+00,   0.00000000e+00,   0.00000000e+00,#          0.00000000e+00,  -1.38777878e-17])# Check the answerassert(np.allclose(xRecovered, x))assert(np.allclose(xRemainder, 0))

It sounds like you don’t know the original signal, so your xRemainder will not be 0 to machine precision—it’ll represent noise in the recorded signal y.