Commits

Michael Forbes  committed 6da5b75

Added real-time filtering.

  • Participants
  • Parent commits 6823a74

Comments (0)

Files changed (1)

     def __init__(self, rate=44100):
         self.rate = rate
         self.nyq = rate / 2.0
-
-    def filter(self, y):
-        y_filtered = sp.signal.lfilter(self.taps, 1.0, y)
-        return y_filtered
+        self.b = [1.0]          # Redefine to define filter
+        self.a = [1.0]          # Usually not changed.
 
     def respose(self):
         r"""Compute the response of the filter."""
-        w, a = sp.signal.freqz(self.taps, 1.0)
+        w, a = sp.signal.freqz(self.a, self.b)
         return 2.0 * self.nyq * w / (2 * np.pi), a
 
+    def apply(self, y, zi=None):
+        r"""Apply the filter to the signal `y` and return the filtered signal
+        along with the new initial conditions `zi` for chaining.  (When
+        processing several consecutive signals, you should pass both the new
+        signal and the initial conditions `zi`.  If `zi` is `None`, then assume
+        that the signal was initially zero."""
+        if zi is None:
+            zi = sp.signal.lfiltic(b=self.b, a=self.a, y=0*y[:0], x=y)
+        y_new, zi = sp.signal.lfilter(b=self.b, a=self.a, x=y, zi=zi)
+        return y_new, zi
+
 
 class Notch(Filter):
     def __init__(self, freq=7688.0, width=math.sqrt(2), numtaps=100,
         #self.freq = np.array([0, 0.5, 0.5, 0.7, 0.7, 1.0]) * self.nyq
         self.gain = [1.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0]
         self.numtaps = numtaps
-        self.taps = sp.signal.firwin2(
+        self.b = sp.signal.firwin2(
             numtaps=self.numtaps, freq=self.freq, gain=self.gain,
             nyq=self.nyq)
-
+        self.a = [1.0]