From 0d959e8c502c050a35fb4e56e898eccb5e601ec2 Mon Sep 17 00:00:00 2001 From: Leonardo Robol Date: Thu, 4 Mar 2010 11:10:00 +0100 Subject: [PATCH] Rifiniture. --- Filtering/dwt | 64 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 13 deletions(-) diff --git a/Filtering/dwt b/Filtering/dwt index dcf2eba..9e269a3 100755 --- a/Filtering/dwt +++ b/Filtering/dwt @@ -2,6 +2,38 @@ # -*- coding: utf-8 -*- # +import sys + +def Output (string): + """Output with colors :)""" + print "\033[32;1m===>\033[0m %s" % string + +def StartProgram(): + """Starting banner""" + print "\033[31;1mDWT\033[0m - Discrete Wavelet transform started" + print "" + +def EndProgram(): + """End banner""" + print "" + +def LoadingLibrariesStarted(): + """Loading libraries banner""" + if __name__ == "__main__": + print "" + print "\033[31;1mDWT\033[0m - Loading numeric libraries...", + sys.stdout.flush () + +def LoadingLibrariesFinished(): + """Loading libraries finished banner""" + if __name__ == "__main__": + print "done" + + + +LoadingLibrariesStarted() + +# Importing libraries import Filtering from pylab import show, plot from numpy import array, sqrt, memmap, roll @@ -10,16 +42,16 @@ import time from optparse import OptionParser -def Output (string): - """Output with colors :)""" - print " \033[1m=>\033[0m\033[32;1m %s \033[0m" % string - +LoadingLibrariesFinished() + class DWT(): - def __init__(self, filename, action = 'show', filewrite = 'rebuilt.pcm', + def __init__(self, filename, action = 'show', filewrite = 'rebuilt.wav', filterbank = 'haar', depth = 4): + StartProgram () + startingTime = time.time () # Scelgo la filterbank da utilizzare @@ -49,13 +81,18 @@ class DWT(): startingTime = time.time () rebuilt = filterBank.Rebuild (wavelets) Output ("Rebuilt in %f seconds" % (time.time() - startingTime)) - Output ("||rebuilt - samples|| = %f" % norm(rebuilt - samples[0:len(rebuilt)])) - self.WriteSamples(rebuilt, filewrite) + + if norm(rebuilt - samples[0:len(rebuilt)]) > 1E-10: + Output ("Errore while reconstructing. Rebuilt samples differs from original ones") + else: + Output ("Perfect reconstruction succeeded") + self.WriteSamples(rebuilt, filewrite) + EndProgram () - def LoadSamples(self, filename = "sunny.pcm"): + def LoadSamples(self, filename): """ Load the samples from an audio file """ @@ -79,13 +116,14 @@ class DWT(): Shows the result of filtering """ scale = 1 - offset = -90000 + singleOffset = 2 * max(abs(wavelets[-1])) + offset = -3 * singleOffset # We plot only the first 2000 samples, to avoid memory # being flooded with our data :) toPlot = 1000000 for samples in wavelets[0:-1]: plot (range(0, toPlot, scale), samples[0:toPlot / scale] + offset) - offset += 30000 + offset += singleOffset scale = 2*scale plot ( range(0, toPlot, int(0.5 * scale)), wavelets[-1][:2 * toPlot /scale] + offset) show () @@ -95,11 +133,11 @@ if __name__ == "__main__": parser = OptionParser () parser.add_option("-f", "--file", dest="filename",default=None, - help="Read samples from this .pcm file") + help="Read samples from this .wav file") parser.add_option("-r", "--rebuild", dest="rebuild", default=False, action="store_true", help="Make DWT and then IDWT") - parser.add_option("-w", "--write", dest="filewrite", default='rebuilt.pcm', + parser.add_option("-w", "--write", dest="filewrite", default='rebuilt.wav', help="Write reconstructed samples to this file") parser.add_option("-s", "--show", dest="show", default=True, action="store_true", @@ -113,7 +151,7 @@ if __name__ == "__main__": (options, args) = parser.parse_args () if options.filename is None: - parser.error ("Please a specify a pcm file to read the samples from") + parser.error ("Please a specify a PCM .wav file to read the samples from") if options.rebuild: -- 2.1.4