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

📄 seicom.cpp

📁 经过DCT变换用 LZW进行数据压缩 再还原回来
💻 CPP
字号:
#include "head.h"

int main()
{
	double *date;
	int *pint;
	int i,zero,head,num,group,flag,sum,head_1[2],zero_1[2],temp_int;
	float temp;
	char str1[10][7];
	char str2[12][10];
	double q = 0.1;
	date = new double[4096];
	pint = new int[4096];
	FILE *fp1, *fp2, *fp3;
	//////////////////////////inverte the even singnal////////////////////////
/*	fp1 = fopen ("stack.txt","r");
	fp2 = fopen ("reverseEven.txt","w");
	convert(fp1, fp2);*/

	////////////////////////////////////////dct///////////////////////////
	num = 0;
	zero = 0;
	sum = 0;
	group = 0;
	fp1 = fopen("stack.txt","r");
	fp2 = fopen("dct_log.txt","w");
	fp3 = fopen("dct_data.txt","w");
	for (i=0; i<4096; i++)
	{
		date[i] = 0;
	}
	
	for(i=0; i<9; i++)
	{
		fscanf(fp1,"%s",str1[i]);
		fprintf(fp2,"%s",str1[i]);
		fprintf(fp2," ");	
	}
	fprintf(fp2,"\n");
	
	while (1)
	{
		for(i=0; i<4; i++)
		{
			fscanf(fp1,"%s",str2[i]);
			if (strcmp(str2[0],"#") == 0)
			{
				fclose(fp3);
				fclose(fp1);
				fclose(fp2);
				flag = 0;
				break;
			}
			fprintf(fp2,"%s",str2[i]);                 
			if (i%2 == 0)
			{
				fprintf(fp2,"    ");
			}
			if (i%2 !=0)
			{
				fprintf(fp2," ");
			}	
			
		}
		if (flag == 0)
		{
			flag =1;
			break;
		}
		
		for(i=0; i<2000; i++)
		{
			fscanf(fp1, "%f", &temp);		//read data from file
			//		temp *= Multiple;			
			if (temp == 0)
			{
				if (i<100)
				{
					head = 1;				
				}
				if (i>1000 && i<2000)		//record the number of 0 and it's position
				{
					head =0;
				}
				zero++;
			}
			if(temp != 0)
			{
				date[num] = (double)temp;
				num++;				
			}	
		}
		if (head == 1)
		{
			fprintf(fp2,"H  %d",zero);//'H' means that 0 is at the head of the signal
		}
		if (head == 0)
		{
			fprintf(fp2,"L  %d",zero);//'L' means that 0 is at the end of the signal
		}
		fprintf(fp2,"\n");
		zero = 0;
		group++;
		double q=0.1;
		if (group%2 ==0 && group != 0)
		{
			fdct_1D(date,12);										//dct
			for(i=0; i<4096; i++)
			{
				
				pint[i]=(int)(abs_double(date[i])/(2*q)+0.5);		//quantification
				if(date[i]<0)	
				{
					pint[i]*=-1;
				}
				fprintf(fp3,"%-15d",pint[i]);				
				if (i%6 == 0 && i != 0)
				{
					fprintf(fp3,"\n");
				}
			}
			fprintf(fp3,"\n\n\n\n\n");
			num = 0;
		}
		sum++;
	}
	//using LZW to encode data after DCT////////////////////
	FILE *input_file, *output_file;
	input_file = fopen("dct_data.txt","r");
	output_file = fopen("compress.lzw","wb");
	code_value=new int[TABLE_SIZE];
	prefix_code=new unsigned int[TABLE_SIZE];
	append_character=new unsigned char[TABLE_SIZE];
	if (code_value==NULL || prefix_code==NULL || append_character==NULL) {
		printf("Error allocating table space!\n");
		exit(1);
	}
	max_code = MAXVAL(num_bits); 
	compress(input_file, output_file);
	fclose(input_file);
	fclose(output_file);
	//////////////////////using LZW to decode 
	input_file=fopen("compress.lzw","rb");
	output_file=fopen("decompression.txt","w");
	if (input_file == NULL || output_file == NULL)
	{
		printf("Error opening files\n");
		exit(1);
	}
	num_bits=INIT_BITS;                  /* Re-initialize for expansion */
	max_code = MAXVAL(num_bits);
	expand(input_file,output_file);        /* Call expansion routine */
	fclose(output_file);
	fclose(input_file);
	
	////////////////////////////idct//////////////////////////
	
	fp1 = fopen("decompression.txt","r");
	fp2 = fopen("dct_log.txt","r");
	fp3 = fopen("ReturnBack.txt","w");
	for(i=0; i<9; i++)
	{
		fscanf(fp2,"%s",str1[i]);
		fprintf(fp3,"%s",str1[i]);
		fprintf(fp3," ");	
	}
	fprintf(fp3,"\n");
	for (int k=0; k<sum/2; k++ )
	{
		for(i=0; i<2; i++)
		{
			for (int j=0; j<5; j++)
			{
				fscanf(fp2,"%s",str2[i*4+j]);
				if (j == 4)
				{
					if (strcmp(str2[i*4+j],"H") == 0)
					{
						head_1[i] = 1;
					}
					if (strcmp(str2[i*4+j],"L") == 0)
					{
						head_1[i] = 0;
					}
				}
			}
			fscanf(fp2,"%d",&zero_1[i]);
		}
		for (i=0; i<4096; i++)
		{
			fscanf(fp1,"%d",&temp_int);
			date[i] = (double)(2*q*temp_int);			//recover from quantification
		}
		fidct_1D(date,12);									//idct
		for (i=0; i<2; i++)
		{
			for (int j=0; j<4; j++)
			{
				fprintf(fp3,"%s    ",str2[i*4+j]);
			}
			fprintf(fp3,"\n");
			if (head_1[i] == 1)
			{
				for(j=1; j<=zero_1[i]; j++)
				{
					fprintf(fp3,"%-15f",0);
					if (j%6 == 0&& j !=0)
					{
						fprintf(fp3,"\n");
					}
				}
			}
			for(j=0; j<2000-zero_1[i];j++)
			{
				temp = date[(2000-zero_1[0])*i+j];
				//		temp /= Multiple;
				fprintf(fp3,"%-15f",temp);
				
				if ((j+zero_1[i]+1)%6 == 0)
				{
					fprintf(fp3,"\n");
				}
			}
			if (head_1[i] == 0)
			{
				for(j=1; j<=zero_1[i]; j++)
				{
					fprintf(fp3,"%-15f",0);
					if ((2000-zero_1[i]-j)%6 == 0)
					{
						fprintf(fp3,"\n");
					}
				}
			}
			fprintf(fp3,"\n");
		}
	}	
	fprintf(fp3,"#\n");
	fclose(fp3);
	fclose(fp1);
	fclose(fp2);
	///////////////////////////////////recover from inverting the even signal//////////////
	/*fp1 = fopen ("idct_data.txt","r");
	fp2 = fopen ("ReturnBack.txt","w");
	convert(fp1, fp2);*/

	return 0;
}


double abs_double(double src)
{
	return src>0?src:-1*src;
}

⌨️ 快捷键说明

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