Arithmetic coding algorithm

zhaozj2021-02-16  43

Arithmetic coding algorithm

The arithmetic code is to represent a source representing a range between the real shafts 0 and 1, and each element in the source set is used to shorten the interval.

The process of arithmetic encoding is as follows:

(1) Set the high segment of the encoding section to h, the length of the encoding section is g, and ENDC is a high section of the encoded character allocation, startc

Low ends between the character allocation interval.

(2) Estimating the probability of each element based on a limited source of sources.

(3) Du Yu encoded element A1, according to (2) estimated probability and interval, new L, and H after encoding the element is calculated. The formula is as follows:

H = STARTC G *K;

L = ENDC G * K1;

The specific procedures are as follows:

Const Ca = 0.2; CE = 0.3;

Ci = 0.2; CO = 0.2;

Cu = 0.1;

VAR

FORM1: TFORM1;

String;

STARTC, ENDC: Extended;

IMPLEMENTATION

{$ R * .dfm}

Procedure ConvertTo (S: String; Var Startc, Endc: Extended); {Turn the string to values}

Var n, i: integer;

C: char;

g: extended;

Begin

STARTC: = 0;

Endc: = 1;

N: = Strlen (PCHAR (S));

For i: = 1 to n DO

Begin

C: = s [i];

g: = endc - startc;

Case C of

'a':

Begin

ENDC: = STARTC G * CA;

STARTC: = STARTC G * 0;

END;

'e':

Begin

ENDC: = STARTC G * (CA CE);

STARTC: = STARTC G * CA;

END;

'i':

Begin

Endc: = STARTC G * (CA CE CI);

STARTC: = STARTC G * (CA CE);

END;

'o':

Begin

ENDC: = STARTC G * (CA CE CI CO);

STARTC: = STARTC G * (CA CE CI);

END;

'u':

Begin

ENDC: = STARTC G * (CA CE CI CO CU);

STARTC: = STARTC G * (CA CE CI CO);

END;

Else

Begin

ShowMessage ('Enter string is wrong);

EXIT;

END;

END;

END;

END;

Procedure nemuricaltostr (var s: string; var startc, endc: extend);

{Convert value to string}

Const EPS = -1e-5;

Begin

IF Startc-0.2 <-eps Then

IF (Endc- 0.2 <= -EPS) and (endc> startc) thenbegin

STARTC: = startC / 0.2;

ENDC: = ENDC / 0.2;

S: = S 'A';

IF (STARTC <> 0) or (Endc <> 1) THEN

NemuricalTOSTR (S, STARTC, ENDC);

END;

IF (startc- 0.2> = EPS) and (startc-0.5 <-epses) THEN

IF (Endc-0.5 <= -EPS) and (endc> startc) THEN

Begin

STARTC: = STARTC - 0.2;

Endc: = ENDC - 0.2;

STARTC: = startc / 0.3;

ENDC: = ENDC / 0.3;

S: = S 'E';

IF (STARTC <> 0) or (Endc <> 1) THEN

NemuricalTOSTR (S, STARTC, ENDC);

END;

IF (StartC- 0.5> = EPS) and (startc- 0.7 <-eps) THEN

IF (Endc-0.7 <= -EPS) and (endc> startc) THEN

Begin

STARTC: = STARTC - 0.5;

ENDC: = ENDC - 0.5;

STARTC: = startC / 0.2;

ENDC: = ENDC / 0.2;

S: = S 'I';

IF (STARTC <> 0) or (Endc <> 1) THEN

NemuricalTOSTR (S, STARTC, ENDC);

END;

IF (STARTC-0.7> = EPS) and (startc-0.9 <-eps).

IF (endc-0.9 <= - EPS) and (endc> startc) THEN

Begin

STARTC: = STARTC - 0.7;

Endc: = endc - 0.7;

STARTC: = startC / 0.2;

ENDC: = ENDC / 0.2;

s: = s 'o';

IF (STARTC <> 0) or (Endc <> 1) THEN

NemuricalTOSTR (S, STARTC, ENDC);

END;

IF (startc -0.9> = EPS) and (startc-1 <-eps) THEN

IF (Endc-1 <= -EPS) and (endc> startc) THEN

Begin

STARTC: = STARTC - 0.9;

ENDC: = ENDC - 0.9;

STARTC: = startc / 0.1;

Endc: = endc / 0.1;

S: = s 'u';

IF (STARTC <> 0) or (Endc <> 1) THEN

NemuricalTOSTR (S, STARTC, ENDC);

END;

END;

Procedure TFORM1.BUTTON1CLICK (Sender: TOBJECT);

Begin

s: = edit1.text;

ConvertTo (S, STARTC, ENDC);

Edit2.text: = floattostr (startc); edit3.text: = floattostr (endc);

END;

Procedure TFORM1.BUTTON2CLICK (Sender: TOBJECT);

Begin

s: = '';

STARTC: = strtofloat (edit2.text);

Endc: = strtofloat (edit3.text);

NemuricalTOSTR (S, STARTC, ENDC);

Edit1.text: = s;

END;

End.

Secondary program

Delphi6 windows2000

Under the pass.

转载请注明原文地址:https://www.9cbs.com/read-25987.html

New Post(0)