作者在 2010-08-08 11:01:22 发布以下内容
// 10 -4,-5,-6
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#include <dos.h>
// 对uchar 类型图象数据添加误码
void Add_Error_Code(char *in_fname,char *out_fname,short r,short c,
double err_rate,short seed)
{
FILE *stream;
unsigned char *buffer ,out_byte;
unsigned int length, k;
char plane; // 误码平面
short i,err_num ;
// 误码率 err_rate :(1.0e-4, 1.0e-6)
if( NULL == (buffer=(unsigned char*)malloc(sizeof(unsigned char)*r*c)) )
{
fprintf(stdout, "Merory Exhausted\n");
return ;
}
// 读取数据
if( (stream=fopen(in_fname,"rb")) == NULL )
{
fprintf(stdout, "Cannot open the file %s.\n",in_fname);
return ;
}
k = 0;
while( fread(&out_byte,sizeof(unsigned char),1,stream) != EOF )
{
buffer[k] = out_byte;
k++;
}
fclose(stream);
length = k;
srand(seed);
err_num = (short)floor(8.0*length*err_rate + 0.5); // 误码比特数
for(i=0;i<err_num;i++)
{
k = (unsigned int)floor((rand()/32768.0)*length);
plane = rand()/(8*sizeof(unsigned char)); // 指定误码出现的位平面
printf("%d\t",k);
buffer[k] ^= (1 << plane);
}
// 存放带误码数据
if( (stream=fopen(out_fname,"wb")) == NULL )
{
fprintf(stdout, "Cannot open the file %s.\n",out_fname);
return ;
}
for (k=0;k<length;k++)
fwrite(&(buffer[k]),sizeof(unsigned char),1,stream);
fclose(stream);
free(buffer);
}
int main(int argc, char **argv)
{
char in_fname[60],out_fname[60];
unsigned char *file_buff;
short seed;
double err_rate;
FILE *fp_in ;
if(argc!=5){
printf("\n");
printf("usage:\n");
printf("name infname outfname seed err_rate\n");
exit(0);
}
strcpy(in_fname,argv[1]);
strcpy(out_fname,argv[2]);
seed =atoi(argv[3]);
err_rate =atof(argv[4]);
/*for(i=0;i<100;i++)
printf("%f ",rand()/32768.0);
getch();
*/
file_buff=(unsigned char*)malloc(sizeof(unsigned char)*4096*4096);
fp_in=fopen(in_fname,"rb");
Add_Error_Code(in_fname,out_fname,512,512,err_rate,seed);
}