From 7321c3c9e2a0d08113b92d33fe9f711bf25bf7d2 Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 12:08:23 -0700 Subject: [PATCH 1/7] --- ac_encode.py | 177 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 ac_encode.py diff --git a/ac_encode.py b/ac_encode.py new file mode 100644 index 0000000..cad56a8 --- /dev/null +++ b/ac_encode.py @@ -0,0 +1,177 @@ +## Arithmetic coding compressor and uncompressor for binary source. +## This is a cleaned-up version of AEncode.py + +BETA0=1;BETA1=1 ## default prior distribution +M = 30 ; ONE = (1<0; assert c1>0 + if adaptive==0: + p0 = c0*1.0/(c0+c1) + pass + ans=""; + charstack=[0] ## how many undecided characters remain to print + for c in string: + w=b-a + if adaptive : + cT = c0+c1 + p0 = c0*1.0/cT + pass + boundary = a + int(p0*w) + if (boundary == a): boundary += 1; print "warningA"; pass # these warnings mean that some of the probabilities + if (boundary == b): boundary -= 1; print "warningB"; pass # requested by the probabilistic model + ## are so small (compared to our integers) that we had to round them up to bigger values + if (c=='1') : + a = boundary + tot1 += 1 ; if adaptive: c1 += 1.0 ; pass + elif (c=='0'): + b = boundary + tot0 +=1 ; if adaptive: c0 += 1.0 ; pass + pass ## ignore other characters + + while ( (a>=HALF) or (b<=HALF) ) : ## output bits + if (a>=HALF) : + ans = ans + clear(1,charstack) + a = a-HALF ; + b = b-HALF ; + else : + ans = ans + clear(0,charstack) + pass + a *= 2 ; b *= 2 + pass + + assert a<=HALF; assert b>=HALF; assert a>=0; assert b<=ONE + ## if the gap a-b is getting small, rescale it + while ( (a>QUARTER) and (b=HALF; assert a>=0; assert b<=ONE + pass + + # terminate + if ( (HALF-a) > (b-HALF) ) : + w = (HALF-a) ; + ans = ans + clear(0,charstack) + while ( w < HALF ) : + ans = ans + clear(1,charstack) + w *=2 + pass + pass + else : + w = (b-HALF) ; + ans = ans + clear(1,charstack) + while ( w < HALF ) : + ans = ans + clear(0,charstack) + w *=2 + pass + pass + return ans + pass + + + +def decode (string, N=10000, c0=BETA0, c1=BETA1, adaptive=1,verbose=0): + ## must supply N, the number of source characters remaining. + b=ONE ; a=0 ; tot0=0;tot1=0 ; assert c0>0 ; assert c1>0 + model_needs_updating = 1 + if adaptive==0: + p0 = c0*1.0/(c0+c1) + pass + ans="" + u=0 ; v=ONE + for c in string : + if N<=0 : + break ## break out of the string-reading loop + assert N>0 +## // (u,v) is the current "encoded alphabet" binary interval, and halfway is its midpoint. +## // (a,b) is the current "source alphabet" interval, and boundary is the "midpoint" + assert u>=0 ; assert v<=ONE + halfway = u + (v-u)/2 + if( c == '1' ) : + u = halfway + elif ( c=='0' ): + v = halfway + else: + pass +## // Read bits until we can decide what the source symbol was. +## // Then emulate the encoder's computations, and tie (u,v) to tag along for the ride. + while (1): ## condition at end + firsttime = 0 + if(model_needs_updating): + w = b-a + if adaptive : + cT = c0 + c1 ; p0 = c0 *1.0/cT + pass + boundary = a + int(p0*w) + if (boundary == a): boundary += 1; print "warningA"; pass + if (boundary == b): boundary -= 1; print "warningB"; pass + model_needs_updating = 0 + pass + if ( boundary <= u ) : + ans = ans + "1"; tot1 +=1 ; if adaptive: c1 += 1.0 ; pass + a = boundary ; model_needs_updating = 1 ; N-=1 + elif ( boundary >= v ) : + ans = ans + "0"; tot0 +=1 ; if adaptive: c0 += 1.0 ; pass + b = boundary ; model_needs_updating = 1 ; N-=1 +## // every time we discover a source bit, implement exactly the +## // computations that were done by the encoder (below). + else : +## // not enough bits have yet been read to know the decision. + pass + +## // emulate outputting of bits by the encoder, and tie (u,v) to tag along for the ride. + while ( (a>=HALF) or (b<=HALF) ) : + if (a>=HALF) : + a = a-HALF ; b = b-HALF ; u = u-HALF ; v = v-HALF + pass + else : + pass + a *= 2 ; b *= 2 ; u *= 2 ; v *= 2 ; + model_needs_updating = 1 + pass + + assert a<=HALF; assert b>=HALF; assert a>=0; assert b<=ONE + ## if the gap a-b is getting small, rescale it + while ( (a>QUARTER) and (b0 and model_needs_updating) : ## this is the "while" for this "do" loop + break + pass + pass + return ans + pass + + +def test(): + sl=["1010"] + sl=["1010", "111", "00001000000000000000",\ + "1", "10" , "01" , "0" ,"0000000", \ + "000000000000000100000000000000000000000000000000100000000000000000011000000" ] + for s in sl: + print "encoding", s + N=len(s) + e = encode(s,10,1) + print "decoding", e + ds = decode(e,N,10,1) + print ds + if (ds != s) : + print s + print "ERR@" + pass + else: + print "ok ---------- " + pass + pass + +if __name__ == '__main__': test() \ No newline at end of file From 74cd548adb06bcc6ae595b3b0e2e14e97532253a Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 12:08:36 -0700 Subject: [PATCH 2/7] --- ac_encode.py | 376 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 226 insertions(+), 150 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index cad56a8..9bf93d4 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -1,177 +1,253 @@ -## Arithmetic coding compressor and uncompressor for binary source. -## This is a cleaned-up version of AEncode.py +# Arithmetic coding compressor and decompressor for binary strings. +# via: http://www.inference.org.uk/mackay/python/compress/ac/ac_encode.py +# main page: http://www.inference.org.uk/mackay/python/compress/ +# this has been cleaned up (passes pycodestyle) and ported to python 3. -BETA0=1;BETA1=1 ## default prior distribution -M = 30 ; ONE = (1<0; assert c1>0 - if adaptive==0: - p0 = c0*1.0/(c0+c1) - pass - ans=""; - charstack=[0] ## how many undecided characters remain to print + +def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): + assert c0 > 0 + assert c1 > 0 + + b = ONE + a = 0 + tot0 = 0 + tot1 = 0 + if adaptive == 0: + p0 = c0 * 1 / (c0 + c1) + ans = "" + charstack = [0] # how many undecided characters remain to print + for c in string: - w=b-a - if adaptive : - cT = c0+c1 - p0 = c0*1.0/cT - pass - boundary = a + int(p0*w) - if (boundary == a): boundary += 1; print "warningA"; pass # these warnings mean that some of the probabilities - if (boundary == b): boundary -= 1; print "warningB"; pass # requested by the probabilistic model - ## are so small (compared to our integers) that we had to round them up to bigger values - if (c=='1') : + w = b - a + if adaptive: + cT = c0 + c1 + p0 = c0 * 1.0 / cT + boundary = a + int(p0 * w) + + # these warnings mean that some of the probabilities + # requested by the probabilistic model are so small + # (compared to our integers) that we had to round them up + # to bigger values. + if boundary == a: + boundary += 1 + print("warningA") + if boundary == b: + boundary -= 1 + print("warningB") + + if c == '1': a = boundary - tot1 += 1 ; if adaptive: c1 += 1.0 ; pass - elif (c=='0'): - b = boundary - tot0 +=1 ; if adaptive: c0 += 1.0 ; pass - pass ## ignore other characters + tot1 += 1 + if adaptive: + c1 += 1.0 + elif c == '0': + b = boundary + tot0 += 1 + if adaptive: + c0 += 1.0 + # ignore other characters - while ( (a>=HALF) or (b<=HALF) ) : ## output bits - if (a>=HALF) : - ans = ans + clear(1,charstack) - a = a-HALF ; - b = b-HALF ; - else : - ans = ans + clear(0,charstack) - pass - a *= 2 ; b *= 2 - pass + while a >= HALF or b <= HALF: # output bits + if a >= HALF: + ans += clear(1, charstack) + a -= HALF + b -= HALF + else: + ans += clear(0, charstack) + a *= 2 + b *= 2 - assert a<=HALF; assert b>=HALF; assert a>=0; assert b<=ONE - ## if the gap a-b is getting small, rescale it - while ( (a>QUARTER) and (b= HALF + assert a >= 0 + assert b <= ONE + + # if the gap a-b is getting small, rescale it + while a > QUARTER and b < THREEQU: charstack[0] += 1 a = 2*a-HALF b = 2*b-HALF - pass - - assert a<=HALF; assert b>=HALF; assert a>=0; assert b<=ONE - pass + + assert a <= HALF + assert b >= HALF + assert a >= 0 + assert b <= ONE # terminate - if ( (HALF-a) > (b-HALF) ) : - w = (HALF-a) ; - ans = ans + clear(0,charstack) - while ( w < HALF ) : - ans = ans + clear(1,charstack) - w *=2 - pass - pass - else : - w = (b-HALF) ; - ans = ans + clear(1,charstack) - while ( w < HALF ) : - ans = ans + clear(0,charstack) - w *=2 - pass - pass + if HALF - a > b - HALF: + w = HALF - a + ans += clear(0, charstack) + while w < HALF: + ans += clear(1, charstack) + w *= 2 + else: + w = b - HALF + ans += clear(1, charstack) + while w < HALF: + ans += clear(0, charstack) + w *= 2 + return ans - pass +def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): + # must supply N, the number of source characters remaining. + assert c0 > 0 + assert c1 > 0 -def decode (string, N=10000, c0=BETA0, c1=BETA1, adaptive=1,verbose=0): - ## must supply N, the number of source characters remaining. - b=ONE ; a=0 ; tot0=0;tot1=0 ; assert c0>0 ; assert c1>0 + b = ONE + a = 0 + tot0 = 0 + tot1 = 0 model_needs_updating = 1 - if adaptive==0: - p0 = c0*1.0/(c0+c1) - pass - ans="" - u=0 ; v=ONE - for c in string : - if N<=0 : - break ## break out of the string-reading loop - assert N>0 -## // (u,v) is the current "encoded alphabet" binary interval, and halfway is its midpoint. -## // (a,b) is the current "source alphabet" interval, and boundary is the "midpoint" - assert u>=0 ; assert v<=ONE - halfway = u + (v-u)/2 - if( c == '1' ) : - u = halfway - elif ( c=='0' ): + if adaptive == 0: + p0 = c0 * 1 / (c0 + c1) + ans = "" + + u = 0 + v = ONE + for c in string: + if N <= 0: + break # out of the string-reading loop + assert N > 0 + # (u,v) is the current "encoded alphabet" binary interval, + # and halfway is its midpoint. + # (a,b) is the current "source alphabet" interval, + # and boundary is the "midpoint" + assert u >= 0 + assert v <= ONE + halfway = u + (v - u) / 2 + if c == '1': + u = halfway + elif c == '0': v = halfway - else: - pass -## // Read bits until we can decide what the source symbol was. -## // Then emulate the encoder's computations, and tie (u,v) to tag along for the ride. - while (1): ## condition at end + + # Read bits until we can decide what the source symbol was. + # Then emulate the encoder's computations, + # and tie (u,v) to tag along for the ride. + while 1: firsttime = 0 - if(model_needs_updating): - w = b-a - if adaptive : - cT = c0 + c1 ; p0 = c0 *1.0/cT - pass - boundary = a + int(p0*w) - if (boundary == a): boundary += 1; print "warningA"; pass - if (boundary == b): boundary -= 1; print "warningB"; pass + if model_needs_updating: + w = b - a + if adaptive: + cT = c0 + c1 + p0 = c0 * 1 / cT + boundary = a + int(p0*w) + if boundary == a: + boundary += 1 + print("warningA") + if boundary == b: + boundary -= 1 + print("warningB") model_needs_updating = 0 - pass - if ( boundary <= u ) : - ans = ans + "1"; tot1 +=1 ; if adaptive: c1 += 1.0 ; pass - a = boundary ; model_needs_updating = 1 ; N-=1 - elif ( boundary >= v ) : - ans = ans + "0"; tot0 +=1 ; if adaptive: c0 += 1.0 ; pass - b = boundary ; model_needs_updating = 1 ; N-=1 -## // every time we discover a source bit, implement exactly the -## // computations that were done by the encoder (below). - else : -## // not enough bits have yet been read to know the decision. + + if boundary <= u: + ans += "1" + tot1 += 1 + if adaptive: + c1 += 1.0 + a = boundary + model_needs_updating = 1 + N -= 1 + elif boundary >= v: + ans += "0" + tot0 += 1 + if adaptive: + c0 += 1.0 + b = boundary + model_needs_updating = 1 + N -= 1 + else: + # not enough bits have yet been read to know the decision. pass -## // emulate outputting of bits by the encoder, and tie (u,v) to tag along for the ride. - while ( (a>=HALF) or (b<=HALF) ) : - if (a>=HALF) : - a = a-HALF ; b = b-HALF ; u = u-HALF ; v = v-HALF - pass - else : - pass - a *= 2 ; b *= 2 ; u *= 2 ; v *= 2 ; - model_needs_updating = 1 - pass + # emulate outputting of bits by the encoder, + # and tie (u,v) to tag along for the ride. + while a >= HALF or b <= HALF: + if a >= HALF: + a = a - HALF + b = b - HALF + u = u - HALF + v = v - HALF + a *= 2 + b *= 2 + u *= 2 + v *= 2 + model_needs_updating = 1 - assert a<=HALF; assert b>=HALF; assert a>=0; assert b<=ONE - ## if the gap a-b is getting small, rescale it - while ( (a>QUARTER) and (b0 and model_needs_updating) : ## this is the "while" for this "do" loop + assert a <= HALF + assert b >= HALF + assert a >= 0 + assert b <= ONE + + # if the gap a-b is getting small, rescale it + while a > QUARTER and b < THREEQU: + a = 2 * a - HALF + b = 2 * b - HALF + u = 2 * u - HALF + v = 2 * v - HALF + + # this is the condition for this do-while loop + if not (N > 0 and model_needs_updating): break - pass - pass - return ans - pass - + return ans + + def test(): - sl=["1010"] - sl=["1010", "111", "00001000000000000000",\ - "1", "10" , "01" , "0" ,"0000000", \ - "000000000000000100000000000000000000000000000000100000000000000000011000000" ] - for s in sl: - print "encoding", s - N=len(s) - e = encode(s,10,1) - print "decoding", e - ds = decode(e,N,10,1) - print ds - if (ds != s) : - print s - print "ERR@" - pass + tests = [ + "1010", + "111", + "00001000000000000000", + "1", + "10", + "01", + "0", + "0000000", + """ + 00000000000000010000000000000000 + 00000000000000001000000000000000 + 00011000000 + """, + ] + + for s in tests: + # an ugly way to remove whitespace and newlines from the test strings: + s = "".join(s.split()) + + N = len(s) # required for decoding later. + print("original:", s) + + e = encode(s, 10, 1) + print("encoded: ", e) + + ds = decode(e, N, 10, 1) + print("decoded: ", ds) + + if ds != s: + print("FAIL") else: - print "ok ---------- " - pass - pass - -if __name__ == '__main__': test() \ No newline at end of file + print("PASS") + + print() + + +if __name__ == '__main__': + test() From 207603e4621914f4a0b094fafa96edeea30520dc Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 12:23:05 -0700 Subject: [PATCH 3/7] --- ac_encode.py | 56 ++++++++++++++++++++++++++++------------------------ 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index 9bf93d4..7592d17 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -21,7 +21,7 @@ def clear(c, charstack): return a -def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): +def encode(string, c0=BETA0, c1=BETA1, adaptive=True): assert c0 > 0 assert c1 > 0 @@ -29,7 +29,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): a = 0 tot0 = 0 tot1 = 0 - if adaptive == 0: + if not adaptive: p0 = c0 * 1 / (c0 + c1) ans = "" charstack = [0] # how many undecided characters remain to print @@ -38,7 +38,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): w = b - a if adaptive: cT = c0 + c1 - p0 = c0 * 1.0 / cT + p0 = c0 * 1 / cT boundary = a + int(p0 * w) # these warnings mean that some of the probabilities @@ -56,12 +56,12 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): a = boundary tot1 += 1 if adaptive: - c1 += 1.0 + c1 += 1 elif c == '0': b = boundary tot0 += 1 if adaptive: - c0 += 1.0 + c0 += 1 # ignore other characters while a >= HALF or b <= HALF: # output bits @@ -82,8 +82,8 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): # if the gap a-b is getting small, rescale it while a > QUARTER and b < THREEQU: charstack[0] += 1 - a = 2*a-HALF - b = 2*b-HALF + a += a - HALF + b += b - HALF assert a <= HALF assert b >= HALF @@ -107,7 +107,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): return ans -def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): +def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True): # must supply N, the number of source characters remaining. assert c0 > 0 assert c1 > 0 @@ -116,8 +116,8 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): a = 0 tot0 = 0 tot1 = 0 - model_needs_updating = 1 - if adaptive == 0: + model_needs_updating = True + if not adaptive: p0 = c0 * 1 / (c0 + c1) ans = "" @@ -142,37 +142,37 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): # Read bits until we can decide what the source symbol was. # Then emulate the encoder's computations, # and tie (u,v) to tag along for the ride. - while 1: + while 1: # do-while firsttime = 0 if model_needs_updating: w = b - a if adaptive: cT = c0 + c1 p0 = c0 * 1 / cT - boundary = a + int(p0*w) + boundary = a + int(p0 * w) if boundary == a: boundary += 1 print("warningA") if boundary == b: boundary -= 1 print("warningB") - model_needs_updating = 0 + model_needs_updating = False if boundary <= u: ans += "1" tot1 += 1 if adaptive: - c1 += 1.0 + c1 += 1 a = boundary - model_needs_updating = 1 + model_needs_updating = True N -= 1 elif boundary >= v: ans += "0" tot0 += 1 if adaptive: - c0 += 1.0 + c0 += 1 b = boundary - model_needs_updating = 1 + model_needs_updating = True N -= 1 else: # not enough bits have yet been read to know the decision. @@ -182,15 +182,15 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): # and tie (u,v) to tag along for the ride. while a >= HALF or b <= HALF: if a >= HALF: - a = a - HALF - b = b - HALF - u = u - HALF - v = v - HALF + a -= HALF + b -= HALF + u -= HALF + v -= HALF a *= 2 b *= 2 u *= 2 v *= 2 - model_needs_updating = 1 + model_needs_updating = True assert a <= HALF assert b >= HALF @@ -199,10 +199,14 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=1, verbose=0): # if the gap a-b is getting small, rescale it while a > QUARTER and b < THREEQU: - a = 2 * a - HALF - b = 2 * b - HALF - u = 2 * u - HALF - v = 2 * v - HALF + a *= 2 + b *= 2 + u *= 2 + v *= 2 + a -= HALF + b -= HALF + u -= HALF + v -= HALF # this is the condition for this do-while loop if not (N > 0 and model_needs_updating): From a904f76704c1bd3d3a18d53494bb559f7d1c2b9f Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 12:24:24 -0700 Subject: [PATCH 4/7] --- ac_encode.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index 7592d17..57aacd2 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -82,8 +82,10 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): # if the gap a-b is getting small, rescale it while a > QUARTER and b < THREEQU: charstack[0] += 1 - a += a - HALF - b += b - HALF + a *= 2 + b *= 2 + a -= HALF + b -= HALF assert a <= HALF assert b >= HALF From 7c5834de2011e1e14182ea6c316697c4f1946f4b Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 13:30:55 -0700 Subject: [PATCH 5/7] --- ac_encode.py | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index 57aacd2..57d013f 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -30,7 +30,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): tot0 = 0 tot1 = 0 if not adaptive: - p0 = c0 * 1 / (c0 + c1) + p0 = c0 / (c0 + c1) ans = "" charstack = [0] # how many undecided characters remain to print @@ -38,7 +38,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): w = b - a if adaptive: cT = c0 + c1 - p0 = c0 * 1 / cT + p0 = c0 / cT boundary = a + int(p0 * w) # these warnings mean that some of the probabilities @@ -109,7 +109,7 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): return ans -def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True): +def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True): # must supply N, the number of source characters remaining. assert c0 > 0 assert c1 > 0 @@ -120,7 +120,7 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True): tot1 = 0 model_needs_updating = True if not adaptive: - p0 = c0 * 1 / (c0 + c1) + p0 = c0 / (c0 + c1) ans = "" u = 0 @@ -145,12 +145,11 @@ def decode(string, N=10000, c0=BETA0, c1=BETA1, adaptive=True): # Then emulate the encoder's computations, # and tie (u,v) to tag along for the ride. while 1: # do-while - firsttime = 0 if model_needs_updating: w = b - a if adaptive: cT = c0 + c1 - p0 = c0 * 1 / cT + p0 = c0 / cT boundary = a + int(p0 * w) if boundary == a: boundary += 1 From 248a9c030541968344b2e717b56c08abad57638d Mon Sep 17 00:00:00 2001 From: Connor Date: Mon, 10 Sep 2018 13:33:41 -0700 Subject: [PATCH 6/7] --- ac_encode.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index 57d013f..1700b04 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -240,10 +240,10 @@ def test(): N = len(s) # required for decoding later. print("original:", s) - e = encode(s, 10, 1) + e = encode(s, c0=10, c1=1) print("encoded: ", e) - ds = decode(e, N, 10, 1) + ds = decode(e, N, c0=10, c1=1) print("decoded: ", ds) if ds != s: From dfe69c396f339a2164031e18604707d4f773ef67 Mon Sep 17 00:00:00 2001 From: Connor Date: Tue, 11 Sep 2018 14:03:16 -0700 Subject: [PATCH 7/7] --- ac_encode.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/ac_encode.py b/ac_encode.py index 1700b04..9ed7171 100644 --- a/ac_encode.py +++ b/ac_encode.py @@ -27,8 +27,6 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): b = ONE a = 0 - tot0 = 0 - tot1 = 0 if not adaptive: p0 = c0 / (c0 + c1) ans = "" @@ -54,15 +52,13 @@ def encode(string, c0=BETA0, c1=BETA1, adaptive=True): if c == '1': a = boundary - tot1 += 1 if adaptive: c1 += 1 elif c == '0': b = boundary - tot0 += 1 if adaptive: c0 += 1 - # ignore other characters + # ignore other characters while a >= HALF or b <= HALF: # output bits if a >= HALF: @@ -116,8 +112,6 @@ def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True): b = ONE a = 0 - tot0 = 0 - tot1 = 0 model_needs_updating = True if not adaptive: p0 = c0 / (c0 + c1) @@ -129,6 +123,7 @@ def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True): if N <= 0: break # out of the string-reading loop assert N > 0 + # (u,v) is the current "encoded alphabet" binary interval, # and halfway is its midpoint. # (a,b) is the current "source alphabet" interval, @@ -161,7 +156,6 @@ def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True): if boundary <= u: ans += "1" - tot1 += 1 if adaptive: c1 += 1 a = boundary @@ -169,7 +163,6 @@ def decode(string, N, c0=BETA0, c1=BETA1, adaptive=True): N -= 1 elif boundary >= v: ans += "0" - tot0 += 1 if adaptive: c0 += 1 b = boundary