The arithmetic coding (Arithmetic coding) implementation

Arithmetic coding examples:

Assuming that the source signal has {A, B, C, D} four, and their probabilities are {0.1, 0.4, 0.2, 0.3}, if we want to CADACDB this signal is encoded, then how should we proceed?

After the preparations are completed, we can begin to encode a.
    First we read signal: C-- C is because [0.5, 0.7) at the most in the initial interval, so we read into C code interval becomes [0.5, 0.7) a;
    Then, we read is a, a is accounted for in the initial interval 10% of the former range, thus corresponds to the up account is required before 10% of the coding interval, and therefore coding interval becomes: [0.5, 0.52); the
    longer then the D as the D 70% to 100% of the entire range, it is also occupy 70% to 100% of the coding range, the coding section operable to [0.514, 0.52)
    ......
    until the last semaphore to read all.

    Finally, we will draw this operation becomes a table:

 

 

 Decoding example:

Suppose there are source signal {A, B, C, D} four, their probabilities are {0.1, 0.4, 0.2, 0.3}, when we get the time code is 0.5143876, ask the original signal string (7 ) What?

After the preparations are completed, we now proceed to decode:
    We found that the data to be decoded 0.5143876 within [0.5, 0.7), therefore, we solve the first code value C
    Similarly, we continue the calculation is 0.5143876 [0.5, Thus a solution of 0.7) in the first 10% of the value a second code
    ......
    this process continues until all seven execution performed last.


    Then the above process we can also list indicates:

 

 

Operation: the probability of any one sequence to achieve arithmetic coding, the code length is not less than 16, the probability can not be fixed, the language choice.

Python-based implementation:

from the Collections Import Counter   # statistics list that appears most frequently elements 
Import numpy AS NP 

Print ( " the Enter A Sequence \ the n- " ) 
inputstr = the INPUT ()
 Print   (inputstr + " \ the n- " ) 

RES = Counter (inputstr) # statistics input the number of each character, res is a dictionary type 
Print (STR (RES))
 # Print (RES) 
# the sortlist the sorted = (res.iteritems (), the lambda X, Y: CMP (X [. 1], Y [. 1 ]), Reverse = True) 
# Print The sortlist 

M = len (RES)
 # Print (M)
. 5 = N 
A = np.zeros ((M,. 5), DTYPE = Object)   # generates M row 5 0 full matrix 

# A = [[0 for I in Range (N)] for J in Range (M)] 

reskeys = List (res.keys ())       # take res dictionary keys 
resvalue = List (res.values ())    # fetch dictionary res value 
totalSum = SUM (resvalue)         # enter a few characters total 

# Creating the Table 

a [ M -1] [. 3] = 0
 for I in Range (M): 
   a [I] [0] = reskeys [I]       # the first column is the key res 
   a [I] [. 1] = resvalue [I]      # the second column is the value of the res 
   a [I] [2] = ((resvalue [I] * 1.0) / totalSum)     #The third column is the probability of occurrence of each character 
I = 0 
A [M -1] [. 4] = A [M-. 1] [2 ]
 the while I <-M. 1:                     # The fifth column is the cumulative probability 
   A [Mi- 2] [. 4] = A [-Mi. 1] [. 4] + A [Mi-2] [2 ] 
   A [M -i-2] [. 3] = A [-Mi. 1] [. 4 ] 
   I + =. 1
 Print (A) 

# Encoding 

Print ( " \ n-the ENCODING ------- ------- \ n- " ) 
strlist = List (inputstr) 
Lenco = [] 
UEnco = [] 
LEnco.append (0) 
UEnco.append ( . 1 ) 

for Iin range(len(strlist)):
    result = np.where(A == reskeys[reskeys.index(strlist[i])])
    addtollist = (LEnco[i] + (UEnco[i] - LEnco[i])*float(A[result[0],3]))
    addtoulist = (LEnco[i] + (UEnco[i] - LEnco[i])*float(A[result[0],4]))

    LEnco.append(addtollist)
    UEnco.append(addtoulist)

    tag = (LEnco[-1] + UEnco[-1])/2.0

LEnco.insert(0, " Lower Range")
UEnco.insert(0, "Upper Range")
print(np.transpose(np.array(([LEnco],[UEnco]),dtype=object)))
print("\nThe Tag is \n ")
print(tag)

# Decoding

print("\n------- DECODING -------\n" )
ltag = 0
utag = 1
decodedSeq = []
for i in range(len(inputstr)):
    numDeco = ((tag - ltag)*1.0)/(utag - ltag)
    for i in range(M):
        if (float(A[i,3]) < numDeco < float(A[i,4])):

            decodedSeq.append(str(A[i,0]))
            ltag = float(A[i,3])
            utag = float(A[i,4])
            tag = numDeco

print("The decoded Sequence is \n ")
print("".join(decodedSeq))
arithmetic_coding Code

 

reference:

Examples https://blog.csdn.net/qq_36752072/article/details/77986159

Code https://github.com/nishanpoojary/Arithmetic-Coding

 

Guess you like

Origin www.cnblogs.com/HuangYJ/p/11779608.html