求高手解释一下关于信息熵的C程序
#include <stdio.h>#include <math.h>
#include <string.h>
#include <malloc.h>
#define MAX_VALUE 256
void main(int argc, char **argv)
{
char infile[30];
int i,j;
FILE *ifp;
unsigned char*img;
int size;
int freq[MAX_VALUE];
float prob[MAX_VALUE];
float entropy;
strcpy(infile , "GABE.RAW");//打开语音文件
ifp = fopen(infile,"rb");
if(ifp ==NULL)//如果文件是空,则报错
{
printf("Image file %s cannot be opened for input.\n", infile);
getchar();
return;
}
//计算语音文件大小
fseek(ifp, 0, SEEK_END);
size = ftell(ifp);
++size;
fseek(ifp, 0, SEEK_SET);
img = (unsigned char*) malloc (size * sizeof(unsigned char));
if (img == NULL)
{
printf("无法打开文件.\n");
fclose(ifp);
getchar();
return;
}
//读取文件
fread(img, sizeof(unsigned char), size, ifp);
fclose(ifp);
//计算灰度值的概率
for(j=0; j<MAX_VALUE; j++)
freq[j] = 0;
for(i=0; i<size; i++)
freq[ img[i] ] ++;
free(img);
for(j=0; j<MAX_VALUE; j++)
prob[j] = (float)freq[j] / (float)size;
//计算熵
entropy = 0.0;
for(i=0; i<MAX_VALUE; i++)
if(prob[i] > 0.0)
entropy = entropy - prob[i] * (float) log( (double) prob[i]);
entropy = entropy/ (float) log ((double) 2.0);
printf("first-order entropy of the speech is: %f\n",entropy);
getchar();
}