viewgit/index.php:465 Only variables should be passed by reference [2048]

viewgit/index.php:466 Non-static method GeSHi::get_language_name_from_extension() should not be called statically [2048]

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4.  
  5. import sys
  6. from optparse import OptionParser
  7.  
  8. def Output (string):
  9. """Output with colors :)"""
  10. print "\033[32;1m===>\033[0m %s" % string
  11.  
  12. def StartProgram():
  13. """Starting banner"""
  14. print "\033[31;1m===>\033[0m Discrete Wavelet transform started"
  15.  
  16. def EndProgram():
  17. """End banner"""
  18. print "",
  19.  
  20. def LoadingLibrariesStarted():
  21. """Loading libraries banner"""
  22. if __name__ == "__main__":
  23. print "\033[31;1m===>\033[0m Loading numeric libraries...",
  24. sys.stdout.flush ()
  25.  
  26. def LoadingLibrariesFinished():
  27. """Loading libraries finished banner"""
  28. if __name__ == "__main__":
  29. print "done"
  30.  
  31.  
  32. if __name__ == "__main__":
  33.  
  34. parser = OptionParser (usage = "usage: %prog [options] filename")
  35. parser.add_option("-r", "--rebuild", dest="rebuild",
  36. default=False, action="store_true",
  37. help="Make DWT and then IDWT")
  38. parser.add_option("-w", "--write", dest="filewrite", default='rebuilt.raw',
  39. help="Write reconstructed samples to this file")
  40. parser.add_option("-s", "--show", dest="show",
  41. default=True, action="store_true",
  42. help="Show the decomposed waves (this is the default)")
  43. parser.add_option("-d", "--depth", dest="depth",
  44. default=4, help="Set the recursion level of the filter bank (default is 4)")
  45. parser.add_option("-b", "--filterbank", dest="filterbank", default='haar',
  46. help="Set the filterbank to use in the transform. Valid inputs are 'haar', 'daubechies', 'D4', 'strang'")
  47.  
  48.  
  49. (options, args) = parser.parse_args ()
  50.  
  51. try:
  52. filename = args[0]
  53. except:
  54. parser.error ("Please a specify a PCM file to read the samples from")
  55.  
  56. if (not options.show) and (not options.rebuild):
  57. exit
  58.  
  59. LoadingLibrariesStarted()
  60.  
  61. # Importing libraries
  62. import Filtering
  63. from pylab import show, plot, title, xlabel, ylabel, rcParams
  64. from numpy import array, sqrt, memmap, roll
  65. from numpy.linalg import norm
  66. import time
  67.  
  68. params = {
  69. "text.usetex": True,
  70. 'font.family': 'serif',
  71. }
  72.  
  73. rcParams.update(params)
  74.  
  75.  
  76.  
  77. LoadingLibrariesFinished()
  78.  
  79.  
  80. class DWT():
  81.  
  82. def __init__(self, filename, action = 'show', filewrite = 'rebuilt.wav',
  83. filterbank = 'haar', depth = 4):
  84.  
  85. StartProgram ()
  86.  
  87. startingTime = time.time ()
  88. self.depth = depth
  89.  
  90. self.filterBankName = ""
  91.  
  92. # Scelgo la filterbank da utilizzare
  93. if filterbank == 'haar':
  94. filterBank = Filtering.HaarFilterBank
  95. self.filterBankName = "Haar"
  96. elif (filterbank == 'daubechies') or (filterbank.lower() == 'd4'):
  97. filterBank = Filtering.DaubechiesFilterBank
  98. self.filterBankName = "Daubechies D4"
  99. elif filterbank == 'strang':
  100. filterBank = Filtering.StrangFilterBank
  101. self.filterBankName = "Strang"
  102. elif filterbank == 'leo':
  103. filterBank = Filtering.LeoFilterBank
  104. self.filterBankName = "Leo"
  105. else:
  106. filterBank = Filtering.HaarFilterBank
  107. Output ("FilterBank %s not known. Setting 'haar'" % filterbank)
  108.  
  109. filterBank.SetDepth (int(depth))
  110.  
  111. samples = self.LoadSamples (filename)
  112. wavelets = filterBank.Split (samples)
  113.  
  114. Output ("Decomposed in %f seconds" % (time.time() - startingTime))
  115. Output ("Wavelet size: %d bytes" % (2*wavelets.GetAllSamplesNumber()))
  116.  
  117. # Mostro la decomposizione se l'utente l'ha chiesto
  118. if action == 'show':
  119. self.Show (wavelets)
  120.  
  121.  
  122. if action is 'rebuild':
  123. startingTime = time.time ()
  124. rebuilt = filterBank.Rebuild (wavelets)
  125. Output ("Rebuilt in %f seconds" % (time.time() - startingTime))
  126.  
  127. # Se la differenza in norma è più di 10^-8 possiamo preoccuparci.
  128. a = norm(rebuilt - samples)
  129. if (a > 1E-2):
  130. Output ("Error while reconstructing. Rebuilt samples differs from original ones")
  131. Output ("||rebuilt - samples|| = %f" % a)
  132. Output ("There is likely an error in the code")
  133. elif (a > 1E-6):
  134. Output ("Error while reconstructing. Rebuilt samples differs from original ones")
  135. Output ("This is likely an approximation error (the error is quite small)")
  136. else:
  137. Output ("Perfect reconstruction succeeded")
  138. self.WriteSamples(rebuilt, filewrite)
  139.  
  140. EndProgram ()
  141.  
  142.  
  143. def LoadSamples(self, filename):
  144. """
  145. Load the samples from an audio file
  146. """
  147. samples = memmap (filename,
  148. dtype="<h",
  149. mode="r")
  150. Output("Loaded %d samples from %s" % (len(samples), filename))
  151. return samples
  152.  
  153. def WriteSamples(self, samples, filename):
  154. Output("Writing samples to %s" % filename)
  155. data = memmap (filename,
  156. dtype="<h",
  157. mode="w+",
  158. shape = len(samples))
  159. data[:] = samples[:]
  160. data.flush ()
  161.  
  162. def Show(self, wavelets):
  163. """
  164. Shows the result of filtering
  165. """
  166.  
  167. # We set the frequency to have seconds (and not samples)
  168. # in the x-axis of the plot.
  169. frequency = float (44100)
  170.  
  171. # We choose a decreasing scale to sync all the samples
  172. # because they are recursively downsamples by a factor
  173. # of two and we want to plot with the same time-scale.
  174. scale = pow(2, wavelets.GetNumSamples ())
  175.  
  176. singleOffset = 2 * wavelets.GetSamplesMaxValue()
  177. offset = -(self.depth / 2) * singleOffset
  178.  
  179. # We plot only the first 60 seconds of audio, to avoid memory
  180. # being flooded with our data :)
  181. toPlot = int(frequency) * 60
  182.  
  183. # Stampo i low
  184. scale = int(0.5 * scale)
  185. low = wavelets.PopLowSamples()
  186. data = low[:toPlot / scale]
  187.  
  188. axes = array(range(0, len(data) * scale, scale)) / frequency
  189.  
  190. plot(axes, data + offset)
  191.  
  192. offset += singleOffset
  193.  
  194. while (wavelets.GetHighSamplesNumber() > 0):
  195.  
  196. samples = wavelets.PopHighSamples ()
  197.  
  198. data = samples[0:toPlot / scale]
  199. axes = array(range(0, len(data) * scale , scale)) / frequency
  200.  
  201. plot (axes, data + offset)
  202. offset += singleOffset
  203. scale = int(0.5*scale)
  204.  
  205.  
  206. # Set some nice text
  207. title (r"Decomposition using %s filter bank" % self.filterBankName)
  208. xlabel (r"time (s)")
  209.  
  210. show ()
  211.  
  212.  
  213.  
  214.  
  215. if __name__ == "__main__":
  216.  
  217. # Scegliamo cosa fare, a seconda delle opzioni di cui
  218. # abbiamo fatto il parsing più in alto.
  219. # Partiamo.
  220.  
  221. if options.rebuild:
  222. DWT(filename = filename, action = 'rebuild',
  223. filewrite = options.filewrite, depth = options.depth,
  224. filterbank = options.filterbank)
  225.  
  226. elif options.show:
  227. DWT(filename = filename, action = 'show',
  228. depth = options.depth, filterbank = options.filterbank)
  229.  
  230.  
  231.  
  232.  
  233.