C language implementation of Shannon-Fano-Elias Encoding

Source: Internet
Author: User

C language implementation of Shannon-Fano-Elias Encoding
Shannon-Fano-Elias Encoding

I. Theoretical Analysis

Shannon-Fano-Elias encoding uses the cumulative distribution function to allocate code words.

If it is not general, assume that X = {1, 2 ,... M }. Assume that all x has p (x)> 0. Define the cumulative distribution function F (X)
As shown in the function diagram, the corrected cumulative distribution function represents the probability of all characters smaller than x and the general value of the probability of adding characters x. Because random variables are discrete, the incremental height of the cumulative distribution function is p (x ). The function value is exactly the midpoint of the Step corresponding to x.

Now we want to determine vc/g06a1xHiho9LyzqrL + dPQtcS4xcLK1rW + + records = "6" src = "http://www.bkjia.com/uploads/allimg/160417/04324432P-6.gif" title = "\"/>, so we can get the corresponding X through the cumulative distribution function. However, it is generally a decimal place, which requires many bits to be expressed as binary decimal places. (During the encoding implementation process, note that if it is implemented in C language, pay attention to the length of the array that stores binary BITs, which can easily cross-border arrays.) This is not feasible in actual encoding. So we need to get a precision. Which one is accurate? Obtain the l (x) bit.

Ii. coding implementation

Shannon-Fano-Elias encoding is implemented in C language.
We recommend that you set the length of the code array to a greater value. In this case, it is easy to cross-border arrays (this is a blood lesson ....)
The basic struct is as follows:

Typedef struct {double px; // px probability value double Fx; // fx function value double Fbax; // Fba (X) value int lx; // The length of the encoding int code [A]; // stores binary bits} SFE;

1. initialize the struct, input p (x)

void init_code(int code[],int i){    int j;    for (j=0;j

2. Calculate the fx Cumulative Distribution Function

Void count_fx (sfe sfea [], int length) // calculates the fx cumulative distribution function {double sum = 0; int I, j; for (I = 1; I <= length; I ++) {for (j = 1; j <= I; j ++) {sum = sum + SFEA [j]. px;} SFEA [I]. fx = sum; sum = 0 ;}}

3. Computing

Void count_fbax (sfe sfea [], int length) // calculate the function value of fbax {int I, j; double sum = 0; for (I = 1; I <= length; I ++) {if (I = 1) {SFEA [I]. fbax = SFEA [I]. px/2.0;} else {for (j = 1; j 

4. Calculate the lx length. The lx is rounded up.

Void count_lx (sfe sfea [], int length) // calculate the lx length. The lx is rounded up to {int I; for (I = 1; I <= length; I ++) {SFEA [I]. lx = ceil (log (1/SFEA [I]. px)/log (2) + 1 ;}}

5. Convert to binary bits

void decimal(double m,int code[]){ int *p = code; if(m>ZERO) { m=m*NUM; *p = (long)m; p++; decimal(m-(long)m,p); }}void f_binary(SFE SFEA[],int length){ int i; for (i=1;i<=length;i++) { decimal(SFEA[i].Fbax,SFEA[i].code); }}

The entire encoding process ends now, because there are many Array Operations, so be careful to prevent array overflow.

Iii. Encoding Result Analysis
1. First, we will give an example where decimal places can be converted to binary decimal places with limited digits.

2. in this example, the binary decimal point may be the decimal point of the number of wireless digits. At the beginning, I defined the size of the code array as 20, which is normal after 1. In this example, array out-of-bounds occurs continuously because the binary representation in this example may have infinite digits. If binary is converted first and then encoded, the length of the code array must be long enough. Of course, you can also store only l (x) bits. In this way, we don't need that much space.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.