From b4a141b20bf68aebfccd873cd77dc2b4b02b5d5b Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Mon, 1 Mar 2010 19:14:23 +0100 Subject: [PATCH] Modifichette, ma problemi di rebuild. --- Filtering/Filtering.py | 6 ++--- Filtering/Splitting.py | 67 +++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 12 deletions(-) diff --git a/Filtering/Filtering.py b/Filtering/Filtering.py index 5f336f3..833fc2b 100644 --- a/Filtering/Filtering.py +++ b/Filtering/Filtering.py @@ -2,14 +2,14 @@ # -*- coding: utf-8 -*- # -from numpy import array, convolve, zeros +from numpy import array, zeros, convolve class AbstractFilter(): def __init__(self): pass - def Apply (self, samples): + def __call__ (self, samples): """ Apply the filter to samples. This method has to be overloaded from the specific filter class @@ -23,7 +23,7 @@ class FIR(AbstractFilter): self.coefficients = coefficients - def Apply(self, samples): + def __call__(self, samples): """Apply the FIR to samples""" return convolve (self.coefficients, samples, 'same') diff --git a/Filtering/Splitting.py b/Filtering/Splitting.py index 0875844..2d0e98e 100644 --- a/Filtering/Splitting.py +++ b/Filtering/Splitting.py @@ -9,26 +9,51 @@ class SplittingExample(): def __init__(self): + # Numero di ricorsioni della filter bank + self.depth = 2 + # Creo i filtri che userò in maniera arguta # in seguito. Questo filtro è ortogonale, nel # senso che posso ottenere la filter bank inversa # invertendo semplicemente la matrice. - low = 0.125 * array([1+sqrt(3), 3+sqrt(3), 3-sqrt(3), 1-sqrt(3)]) - high = 0.125 * array([1-sqrt(3), -3+sqrt(3), 3+sqrt(3),-1-sqrt(3)]) + # h0 = 0.125 * array([1+sqrt(3), 3+sqrt(3), 3-sqrt(3), 1-sqrt(3)]) + #h1 = 0.125 * array([1-sqrt(3), -3+sqrt(3), 3+sqrt(3),-1-sqrt(3)]) + + #f0 = 0.25 * array([1-sqrt(3), 3-sqrt(3), 3+sqrt(3), 1+sqrt(3)]) + #f1 = 0.25 * array([-1-sqrt(3),3+sqrt(3),-3-sqrt(3), 1-sqrt(3)]) + h0 = array([0.5 , 0.5]) + h1 = array([-0.5, 0.5]) + + f0 = array([0.5, 0.5]) + f1 = array([-0.5, 0.5]) - self.lowpass = Filtering.FIR (low) - self.highpass = Filtering.FIR (high) + self.lowpass = Filtering.FIR (h0) + self.highpass = Filtering.FIR (h1) + + self.f0 = Filtering.FIR (f0) + self.f1 = Filtering.FIR (f1) self.LoadSamples () + self.TruncateSamples () self.Split () + self.Rebuild () + self.WriteSamples(self.samples , "rebuilt.pcm") + # self.Show () + + def TruncateSamples(self): + """ + Check that samples loaded have a length that is a multiple + of 2^self.depth so that I can apply recursive filtering + without any problem. If not, just truncate :) + """ + self.samples = self.samples[:-1*(len(self.samples) % (pow(2,self.depth)))] - def LoadSamples(self): + def LoadSamples(self, filename = "sunny.pcm"): """ Load the samples from an audio file """ - filename = "sunny.pcm" samples = memmap (filename, dtype=" Rebuild called with suffix = %s" % suffix + + low = Filtering.UpSample(self.samples) + + self.LoadSamples("high_" + suffix + ".pcm") + high = Filtering.UpSample(self.samples) + + self.samples = self.f0(low) + self.f1(high) + if (len(suffix) > 1): + self.Rebuild (suffix[:-1]) + def Show(self): """ -- 2.1.4