diff --git a/Filtering/Filtering.py b/Filtering/Filtering.py index 09f94f3..126885a 100644 --- a/Filtering/Filtering.py +++ b/Filtering/Filtering.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # -from numpy import array, zeros, convolve, dot, roll, sqrt +from numpy import array, zeros, convolve, dot, roll, sqrt, concatenate class AbstractFilter(): @@ -63,6 +63,25 @@ class WaveletStack(): self.samples_number = None self.low_samples = [] self.high_samples = [] + self.end = [] + self.end_effect = [] + + def PushEndSamples(self, samples): + """Push end samples, i.e. the samples that exceeds the + ideal dimensione pow(2,m) where m is the depth of the + filterbank.""" + self.end = samples + + def PopEndSamples(self): + return self.end + + def PushEndEffectSamples(self, samples): + """Push some end samples to preserve them to + end effect""" + self.end_effect = samples + + def PopEndEffectSamples(self): + return self.end_effect def PushHighSamples(self, samples): """ @@ -168,9 +187,9 @@ class FilterBank(): """ toRemove = len(samples) % pow(2,self.depth) if toRemove is 0: - return samples + return samples, [] else: - return samples[:-1 * toRemove] + return samples[:-1 * toRemove], samples[-1 * toRemove:] def SetDepth(self, depth): """ @@ -235,7 +254,11 @@ class FilterBank(): """ samplesStack = WaveletStack() - low = self._Truncate(samples) + low, end = self._Truncate(samples) + samplesStack.PushEndSamples (end) + + # Non sono proprio sicuro che questo sia il modo migliore per farlo. + samplesStack.PushEndEffectSamples (low[:-2*self.depth*len(self.lowPassFilter.GetResponse())]) # Do the real filtering and downsampling. Store the downsampled # details in the array. @@ -255,6 +278,7 @@ class FilterBank(): """ low = samplesStack.PopLowSamples () + end = samplesStack.PopEndSamples () counter = 0 @@ -273,8 +297,11 @@ class FilterBank(): # Sfortunatamente questo ci rovinerà tutta l'ultima parte del # segnale, ma per ora non vedo una soluzione comoda. low = roll(low, -1 * self.length) + + low[:-2*self.depth*len(self.lowPassFilter.GetResponse())] = samplesStack.PopEndEffectSamples () - return low + # Riattacchiamo la coda + return concatenate ((low, end)) diff --git a/Filtering/dwt b/Filtering/dwt index 480dfc3..c3d2ab3 100755 --- a/Filtering/dwt +++ b/Filtering/dwt @@ -35,7 +35,7 @@ if __name__ == "__main__": 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.wav', + parser.add_option("-w", "--write", dest="filewrite", default='rebuilt.raw', help="Write reconstructed samples to this file") parser.add_option("-s", "--show", dest="show", default=True, action="store_true", @@ -108,12 +108,12 @@ class DWT(): Output ("Rebuilt in %f seconds" % (time.time() - startingTime)) # Se la differenza in norma è più di 10^-8 possiamo preoccuparci. - a = norm(rebuilt - samples[0:len(rebuilt)]) + a = norm(rebuilt - samples) if (a > 1E-2): Output ("Error while reconstructing. Rebuilt samples differs from original ones") - Output ("||rebuilt - samples = %f" % a) + Output ("||rebuilt - samples|| = %f" % a) Output ("There is likely an error in the code") - if (a > 1E-6): + elif (a > 1E-6): Output ("Error while reconstructing. Rebuilt samples differs from original ones") Output ("This is likely an approximation error (the error is quite small)") else: diff --git a/Filtering/shell.sh b/Filtering/shell.sh index f328fa1..9fb833a 100755 --- a/Filtering/shell.sh +++ b/Filtering/shell.sh @@ -1,7 +1,9 @@ #!/bin/bash if [[ "$1" == "convert" ]]; then - ffmpeg -i "$2" -acodec pcm_s16le -ac 1 "$3" + ffmpeg -i "$2" -acodec pcm_s16le -ac 1 "$3".wav + sox "$3".wav -e signed -b 16 "$3" channels 1 rate 44100 + rm "$3".wav else aplay -r 44100 -f s16_le "$1" fi