tomaszki/PythonFileCompressor
1
1import numpyAc2import numpy as np3 4# Generate random symbols and pdf.5dim = 1286symsNum = 20007pdf = np.random.rand(symsNum,dim)8pdf = pdf / (np.sum(pdf,1,keepdims=True))9sym = np.random.randint(0,dim,symsNum,dtype=np.int16)10output_pdf = pdf11 12# Encode to bytestream.13codec = numpyAc.arithmeticCoding()14byte_stream,real_bits = codec.encode(pdf, sym,'out.b')15 16# Number of bits taken by the stream.17print('real_bits',real_bits)18 19# Theoretical bits number20print('shannon entropy',-int(np.log2(pdf[range(0,symsNum),sym]).sum()))21 22# Decode from bytestream.23decodec = numpyAc.arithmeticDeCoding(None,dim,'out.b')24 25# Autoregressive decoding and output will be equal to the input.26for i,s in enumerate(sym):27 assert decodec.decode(output_pdf[i:i+1,:]) == s