Guest User

Windowed RMSE of rounded quantization

a guest
Jul 21st, 2019
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # Compare the Root Mean Square Errors between these two quantization approaches:
  4. # a) Simple truncation
  5. # b) Truncation combined with error diffusion
  6.  
  7. import random
  8. from math import sqrt
  9.  
  10. random.seed(1234)
  11.  
  12. # Generate a pixel vector that has at least this length
  13. minimumNumberOfPixelsToGenerate = 1000000
  14.  
  15. # Simulate a smooth gradient by appending uniform chunks of identically-valued pixels
  16. # to the pixel vector.  Each chunk has a random width comprised between 5 and 20 pixels.
  17. # A smooth gradient is simulated by enforcing that the value of these uniform chunks must
  18. # randomly differ by +1 or -1 from the preceding chunk.  As an exception, if the previous
  19. # random chunk contained zero-valued pixels, then, as we don't allow negative pixel
  20. # values, the next chunk must containe pixels with a value of one.
  21.  
  22. # We start with an empty vector
  23. pixelVector = []
  24.  
  25. numberOfGeneratedPixels = 0
  26.  
  27. previousPixelValue = random.randint( 0, 15 )
  28.  
  29. while numberOfGeneratedPixels < minimumNumberOfPixelsToGenerate:
  30.  
  31.     widthOfThisChunk = random.randint( 5, 20 )
  32.  
  33.     # The new chunk must contain values that differ from the previous chunk by -1 or +1
  34.     newPixelValue = previousPixelValue + random.choice( [ -1, 1 ] )
  35.  
  36.     if newPixelValue < 0:
  37.         # The previous pixel value must then necessarily have been zero
  38.         newPixelValue = 1
  39.  
  40.     for i in range( widthOfThisChunk ):
  41.         pixelVector.append( newPixelValue )
  42.  
  43.     previousPixelValue = newPixelValue
  44.     numberOfGeneratedPixels += widthOfThisChunk
  45.  
  46. ########### First case: simple truncation of the two lower bits by using the right shift (>>) operator
  47.  
  48. truncatedQuantizedVector = [ (2+p) >> 2 for p in pixelVector ]
  49.  
  50. prevError = 0
  51. accumulatedSquareError = 0
  52. for i in range( numberOfGeneratedPixels ):
  53.     currError = pixelVector[ i ] - truncatedQuantizedVector[ i ] * 4
  54.     error = prevError + currError
  55.     accumulatedSquareError += error * error
  56.     prevError = currError
  57.  
  58. print "RMSE straightforward truncation, moving window:", sqrt( float( accumulatedSquareError ) / numberOfGeneratedPixels )
  59.  
  60. ########### Second case: Error diffusion
  61.  
  62. errorDiffusedQuantizedVector = [ 0 for i in range( numberOfGeneratedPixels ) ]
  63.  
  64. accumulatedError = 0
  65. for i in range( numberOfGeneratedPixels ):
  66.  
  67.     quantizedPixel = (2+pixelVector[ i ]) >> 2
  68.     error = pixelVector[ i ] - quantizedPixel * 4
  69.     accumulatedError += error
  70.  
  71.     if accumulatedError > 2:
  72.         quantizedPixel += 1
  73.         accumulatedError -= 4
  74.     elif accumulatedError < -1:
  75.         quantizedPixel -= 1
  76.         accumulatedError += 4
  77.  
  78.     errorDiffusedQuantizedVector[ i ] = quantizedPixel
  79.  
  80. prevError = 0
  81. accumulatedSquareError = 0
  82. for i in range( numberOfGeneratedPixels ):
  83.     currError = pixelVector[ i ] - errorDiffusedQuantizedVector[ i ] * 4
  84.     error = prevError + currError
  85.     accumulatedSquareError += error * error
  86.     prevError = currError
  87.  
  88. print "RMSE with error diffusion, moving window:", sqrt( float( accumulatedSquareError ) / numberOfGeneratedPixels )
Advertisement
Add Comment
Please, Sign In to add comment