⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ac_simple2.c

📁 用c语言编写用于数据压缩的源程序
💻 C
字号:
//*********************************************************************************************
//Simple arithmetic coding program for the example of Witten and Neal's paper---
// ---arithmetic coding for data compression(Communications of ACM,June 1987 Volume 30 Number 6)
//
//                              float mul&div
//******************************************************************************************
#include "stdio.h"
static double low, high;
static long bits_to_follow;
static double Half,First_qtr,Third_qtr,head,rear,length;

void bit_plus_follow(int);
void start_encoding();
void encode_symbol(int,double *);

int main(int argc, char* argv[])
{
	double c_freq[7]={1,0.9,0.8,0.6,0.5,0.2,0}; //AC Models
	int sym[5]={5,6,4,4,1};
	int i;
	
	start_encoding();
	for(i=0;i<5;i++)
		encode_symbol(sym[i],c_freq);

	return 0;
		
}

void encode_symbol(int symbol,double *cum_freq)
{
	double range;
	range=high-low;
	high=low+range*cum_freq[symbol-1];
	low=low+range*cum_freq[symbol];
	for(;;)
	{

		if(high<Half)
		{
			bit_plus_follow(0);
			rear-=length/2;
		}
		else if(low>=Half)
		{
			bit_plus_follow(1);
			head+=length/2;
		}
		else if(low>=First_qtr&&high<Third_qtr)
		{
			bits_to_follow+=1;
			rear-=length/4;
			head+=length/4;
		}
		else break;
		length=rear-head;
		First_qtr=head+length*0.25;
		Half=head+length*0.5;
		Third_qtr=head+length*0.75;
	}
}

void start_encoding()
{
	low=0;
	high=1;
	bits_to_follow=0;
	Half=0.5;
	First_qtr=0.25;
	Third_qtr=0.75;
	head=0;
	rear=1;
	length=1;
}

void bit_plus_follow(int bit)
{
	printf("%d",bit);
	while(bits_to_follow>0)
	{
		printf("%d",!bit);
		bits_to_follow--;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -