#include "stdio.h"
#include "math.h"
#include "stdlib.h"
#define FRAME_SIZE 1000
void main(int argc,char *argv[])
{
short j, temp, s1[FRAME_SIZE], s2[FRAME_SIZE];
double SE=0.0, E=0.0, snr;
__int64 l, frame, length1, length2, length;
FILE *fp1, *fp2;
if(argc!=3) {
printf("\nUsage: snr file1 file2");
exit(1);
}
if((fp1=fopen(argv[1],"rb"))==NULL) {
printf("\nFailure to open %s.", argv[1]);
exit(1);
}
if((fp2=fopen(argv[2],"rb"))==NULL) {
printf("\nFailure to open %s.", argv[2]);
exit(1);
}
length1=_filelengthi64(fileno(fp1))/2;
length2=_filelengthi64(fileno(fp2))/2;
if(length1!=length2) {
printf("\nThe length of %s is not equal to that of %s !",argv[1],argv[2]);
printf("\n 'a' to abort and any other key to continue.");
if(getche()=='a') goto end;
}
if(length1<length2) length=length1;
else length=length2;
frame=length/FRAME_SIZE;
for(l=0;l<frame;l++) {
fread(s1,2,FRAME_SIZE,fp1);
fread(s2,2,FRAME_SIZE,fp2);
for(j=0;j<FRAME_SIZE;j++) SE+=((float)s1[j]/FRAME_SIZE)*((float)s1[j]/FRAME_SIZE);
for(j=0;j<FRAME_SIZE;j++) E+=((float)(s1[j]-s2[j])/FRAME_SIZE)*((float)(s1[j]-s2[j])/FRAME_SIZE);
}
temp=(short)(length-FRAME_SIZE*frame);
fread(s1,2,temp,fp1);
fread(s2,2,temp,fp2);
for(j=0;j<temp;j++) SE+=((float)s1[j]/FRAME_SIZE)*((float)s1[j]/FRAME_SIZE);
for(j=0;j<temp;j++) E+=((float)(s1[j]-s2[j])/FRAME_SIZE)*((float)(s1[j]-s2[j])/FRAME_SIZE);
if(E==0.0) {
printf("\nNo difference between %s and %s",argv[1],argv[2]);
goto end;
}
snr=10*log10(SE/E);
printf("\n------------------------------------------------------------------");
printf("\nSNR means 20*log10(|%s|/(|%s-%s|)) !",argv[1],argv[2],argv[1]);
printf("\n------------------------------------------------------------------");
printf("\nSNR=%f (dB)",snr);
fclose(fp1);
fclose(fp2);
end: exit(1);
}
评论0