Search code examples
pythonsignal-processing

TypeError: filter() missing 1 required positional argument: 'v'


I am trying to create a digital filter in Python to filter a wave file.

It is telling me that i'm not passing an argument to the filter function, but I am as part of the loop at the bottom.

TypeError: filter() missing 1 required positional argument: 'v'

Can anyone assist please?

import scipy.io.wavfile as wavfile
import numpy as np


#Load the data 
r, x = wavfile.read('M80_and_speech.wav')

#create output array
y = np.zeros(len(x))

#filter coefficients
a1 = -1.96977856
a2 = 0.97022848
b0 = 0.98500176
b1 = -1.97000352
b2 = 0.98500176

#create filter class
class IIR2Filter:
    def __init__(self, a1, a2, b0, b1, b2):
        self.input_acc = 0
        self.outut_acc = 0
        self.buffer1 = 0
        self.buffer2 = 0


    def filter(self, v):
        #accumulator for the IIR part
        self.input_acc = v
        self.input_acc = self.input_acc - (a1*self.buffer1)
        self.input_acc = self.input_acc - (a2*self.buffer2)
        #accumulator for the FIR part
        self.output_acc = self.input_acc * b0
        self.output_acc = self.output_acc + (b1*self.buffer1)
        self.output_acc = self.output_acc + (b2*self.buffer2)

        self.buffer2 = self.buffer1
        self.buffer1 = self.input_acc

        return self.output_acc

for i in range (len(x)):
    y[i] = IIR2Filter.filter(x[i])

Solution

  • You haven’t initialised the class (IIR2Filter), so filter(self, v) is not being passed self; x[i] is therefore the first positional argument (self) and the second positional argument (v) is missing.

    Also, the arguments you’ve added to the __init__ method shadow the variable names in the global scope (those at the top of the module). It looks like your filter method intends to reference the global variables; the arguments on __init__ are superfluous.