17.11.2012 Views

Numerical recipes

Numerical recipes

Numerical recipes

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1.0<br />

0.9<br />

0.8<br />

0.7<br />

0.6<br />

0.5<br />

0.4<br />

0.3<br />

0.2<br />

0.1<br />

0.0<br />

A<br />

E<br />

I<br />

O<br />

U<br />

0.46<br />

0.45<br />

0.44<br />

0.43<br />

0.42<br />

0.41<br />

0.40<br />

0.39<br />

0.38<br />

0.37<br />

20.5 Arithmetic Coding 911<br />

A<br />

E<br />

I<br />

O<br />

U<br />

0.400<br />

0.395<br />

0.390<br />

0.385<br />

0.380<br />

A<br />

E<br />

I<br />

O<br />

U<br />

0.4033<br />

0.3763<br />

0.3780<br />

0.3778<br />

0.3776<br />

0.3774<br />

0.3772<br />

0.3770<br />

0.3768<br />

0.3766<br />

0.3764<br />

A<br />

E<br />

I<br />

O<br />

U<br />

0.37819<br />

0.37630<br />

Figure 20.5.1. Arithmetic coding of the message “IOU...” in the fictitious language Vowellish.<br />

Successive characters give successively finer subdivisions of the initial interval between 0 and 1. The final<br />

value can be output as the digits of a fraction in any desired radix. Note how the subinterval allocated<br />

to a character is proportional to its probability of occurrence.<br />

The routine arcmak constructs the cumulative frequency distribution table used<br />

to partition the interval at each stage. In the principal routine arcode, when an<br />

interval of size jdif is to be partitioned in the proportions of some n to some ntot,<br />

say, then we must compute (n*jdif)/ntot. With integer arithmetic, the numerator<br />

is likely to overflow; and, unfortunately, an expression like jdif/(ntot/n) is not<br />

equivalent. In the implementation below, we resort to double precision floating<br />

arithmetic for this calculation. Not only is this inefficient, but different roundoff<br />

errors can (albeit very rarely) make different machines encode differently, though any<br />

one type of machine will decode exactly what it encoded, since identical roundoff<br />

errors occur in the two processes. For serious use, one needs to replace this floating<br />

calculation with an integer computation in a double register (not available to the<br />

C programmer).<br />

The internally set variable minint, which is the minimum allowed number<br />

of discrete steps between the upper and lower bounds, determines when new lowsignificance<br />

digits are added. minint must be large enough to provide resolution of<br />

all the input characters. That is, we must have pi × minint > 1 for all i. A value<br />

of 100Nch, or1.1/ min pi, whichever is larger, is generally adequate. However, for<br />

safety, the routine below takes minint to be as large as possible, with the product<br />

minint*nradd just smaller than overflow. This results in some time inefficiency,<br />

and in a few unnecessary characters being output at the end of a message. You can<br />

Sample page from NUMERICAL RECIPES IN C: THE ART OF SCIENTIFIC COMPUTING (ISBN 0-521-43108-5)<br />

Copyright (C) 1988-1992 by Cambridge University Press. Programs Copyright (C) 1988-1992 by <strong>Numerical</strong> Recipes Software.<br />

Permission is granted for internet users to make one paper copy for their own personal use. Further reproduction, or any copying of machinereadable<br />

files (including this one) to any server computer, is strictly prohibited. To order <strong>Numerical</strong> Recipes books or CDROMs, visit website<br />

http://www.nr.com or call 1-800-872-7423 (North America only), or send email to directcustserv@cambridge.org (outside North America).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!