/*************************************************************************************************************/
/* File name : rms.c */
/* Author: Sowmya */
/* Created on: 30-08-05 */
/* Modified and Tested on : */
/* Description: To find RMS difference betwen two 16bit */
/* WAV output files */
/*************************************************************************************************************/
/*************************************************************************************************************/
/* The c program is used to find the RMS difference between two files(Reference and Development version) */
/* This is done in order to check if the developed decoder is ISO/IEC 11172-3 compliant. */
/* The two files under test should be a stereo .wav or .WAV file.Sampling frequency and length of the files */
/* should be same for both the files */
/*************************************************************************************************************/
/*************************************************************************************************************/
/* The rms (root-mean-square) level of the difference signal is calculated as */
/* sqrtof(1/N(sigma-i=1toN(pow((ti-ri),2))) where ti is the ith output sample from the decoder under test, */
/* ri is the ith sample from the reference output,and N is the number of samples.The maximum absolute value */
/* of the difference signal is simply the greatest value of |ti - ri|. */
/*************************************************************************************************************/
/* .WAVE file header format
Field bytes format contains
1 0...3 str4 "RIFF" in ASCII
2 4...7 int4 Total bytes minus 8
3 8...15 str4 "WAVEfmt" Eigth character is a space
4 16...19 int4 16 for PCM format
5 20...21 int2 1 for PCM format
6 22...23 int2 channels
7 24...27 int4 sampling frequency
8 28...31 int4 bytes per second
9 32...33 int2 bytes by capture
10 34...35 int2 bits per sample
11 36:39 str4 "data"
12 40:43 int4 bytes in data
*/
# include <stdio.h>
# include <string.h>
# include <stdlib.h>
# include <math.h>
FILE *fptr1,*fptr2;
int main(int argc, char *argv[])
{
int sLen1,sLen2;
int numChannels1,numChannels2,refVal,devVal,maxDiffVal,maxDiffVal1,diffVal;
int hdrData,i,BitDiff,quotient;
unsigned int fileLen1,fileLen2; //Wave file length
int sampFreq1,sampFreq2;
double sumDiff,rmsVal;
double log2maxDiffVal,log2rmsVal;
long nVal;
long int Garb_44Bit;
int SampleNum=0;
/* Checking if the command line parameters are ok */
/*
if(argc != 3)
{
printf("\n Pass the Parameters as %s <reference> <development version>\n",argv[0]);
exit(1);
}
/* Open the two 16 bit WAVE output files */
fptr1 = fopen(argv[1],"rb"); // Reading the reference
fptr2 = fopen(argv[2],"rb"); // Reading the development file
// fptr1 = fopen("cast2_v1.5.pcm","rb"); // Reading the reference
// fptr2 = fopen("cast2_v1.6.pcm","rb"); // Reading the development file
if((fptr1 == NULL) || (fptr2 == NULL))
{
printf("\n File opening error \n");
exit(2);
}
else
{
/* Checking if both the files are of ".wav or .WAV" extension */
/* sLen1 = strlen(argv[1]);
sLen2 = strlen(argv[2]);
if(!((((argv[1][sLen1-3]) == 'w') || ((argv[1][sLen1-3]) == 'W')) && (((argv[1][sLen1-2]) == 'a') ||
((argv[1][sLen1-2]) == 'A')) && (((argv[1][sLen1-1]) == 'v') || ((argv[1][sLen1-1]) == 'V'))))
{
printf(" \n Reference file is not a WAVE file \n");
exit(2);
}
if(!((((argv[2][sLen2-3]) == 'w') || ((argv[2][sLen2-3]) == 'W')) && (((argv[2][sLen2-2]) == 'a') ||
((argv[2][sLen2-2]) == 'A')) && (((argv[2][sLen2-1]) == 'v') || ((argv[2][sLen2-1]) == 'V'))))
{
printf(" \n Development file under test is not a WAVE file \n");
exit(3);
}
*/
}
/* Check if both the files are stereo files */
# if(0)
for(i = 0; i < 5;i++)
{
fread(&hdrData, 4, 1, fptr1);
fread(&hdrData, 4, 1, fptr2);
}
fread(&hdrData, 2, 1, fptr1);
fread(&hdrData, 2, 1, fptr2);
fread(&numChannels1, 2, 1, fptr1);
fread(&numChannels2, 2, 1, fptr2);
if(numChannels1 != 2)
{
printf(" \n Reference file is not a stereo file \n");
exit(4);
}
else if(numChannels2 != 2)
{
printf(" \n Development file under test is not a stereo file \n");
exit(5);
}
else
{
}
/* Checking the Sampling frequency of the files*/
fread(&sampFreq1, 4, 1, fptr1);
fread(&sampFreq2, 4, 1, fptr2);
if(sampFreq1 != sampFreq2)
{
printf("\n The sampling frequency of the files are not same \n");
exit(6);
}
/* Checking if the files are of same length */
fread(&hdrData, 4, 1, fptr1);
fread(&hdrData, 4, 1, fptr2);
fread(&hdrData, 4, 1, fptr1);
fread(&hdrData, 4, 1, fptr2);
fread(&hdrData, 4, 1, fptr1);
fread(&hdrData, 4, 1, fptr2);
fread(&fileLen1,4,1,fptr1); //Since fileLen1 and fileLen2 are int variables the max size of the
fread(&fileLen2,4,1,fptr2); //file is pow(2,32) = 4294967296 bytes
if(fileLen1 != fileLen2)
{
printf(" \n The length of the Reference file and Development file under test is not same \n");
exit(7);
}
# endif
/* Reading the raw data of both the files */
/* fread(void *ptr, size_t size_of_elements, size_t number_of_elements, FILE *a_file); */
sumDiff = 0.0;
maxDiffVal = 0.0;
nVal = 0;
while (!(feof(fptr1)))
{
refVal = 0;
devVal = 0;
if (fread(&refVal,sizeof(char),3 , fptr1))
{
if (fread(&devVal,sizeof(char),3, fptr2))
{
//devVal = min(devVal,8388607);
//devVal = max(devVal,-8388608);
nVal ++;
//diffVal = fabs((double)refVal - (double)devVal) /8388608.0 ;
diffVal = abs(((refVal<<8)>>8) - ((devVal<<8)>>8));
sumDiff = sumDiff + diffVal/16777216.0 * diffVal/16777216.0;
if (diffVal > maxDiffVal)
{
maxDiffVal = diffVal;
printf("Max difference instant is = %d\n",SampleNum);
printf("Max difference is = %d\n",maxDiffVal);
}
SampleNum++;
//printf("frame num = %d\n",frameNum++);
}
}
}
rmsVal = sqrt(sumDiff / (double)nVal);
/* If the values are required in dB */
# if (0)
if(maxDiffVal != 0)
log2maxDiffVal = log(maxDiffVal) / log(2.0);
else
log2maxDiffVal = 0;
if (rmsVal != 0.0)
log2rmsVal = log(rmsVal) / log(2.0);
else
log2rmsVal = 0;
# endif
// printf("FIRST INPUT FILE IS %s\n",argv[1]);
// printf("SECOND INPUT FILE IS %s\n",argv[2]);
BitDiff=0;
/* Print the result */
maxDiffVal1=maxDiffVal;
do
{
quotient=maxDiffVal1/2.0;
maxDiffVal1=(int)(maxDiffVal1/2.0);
BitDiff++;
}
while(quotient>0);
if(maxDiffVal != 0)
{
printf("\nMax difference %d \n", maxDiffVal);
printf("\nBit Difference %d \n", BitDiff);
}
if(rmsVal != 0)
{
printf("\t RMS %e\n",rmsVal);
}
else
{
printf("\t There is no difference \n\n");
}
/***************************************************/
/* PASS OR FAIL CHECK */
/***************************************************/
#if defined _COMPLIANCE
if(rmsVal < ((pow(2, -23))/sqrt(12)))
{
printf("\tRMS TESTING ---- FULL COMPLIANCE ------- PASSED \n");
if(maxDiffVal <= pow(2, -14))
{
printf("\tMAX DIFFERENCE ---- FULL COMPLIANCE ------- PASSED \n");
}
}
else if(rmsVal < ((pow(2, -11))/sqrt(12)))
{
printf("\tRMS TESTING ---- LIMITED ACCURACY COMPLIANCE ------- PASSED \n");
}
else
{
printf("\tRMS TESTING ---- FOR COMPLIANCE ------- FAILS \n");
}
#endif /* _COMPLIANCE */
fclose(fptr1);
rms.zip_between
版权申诉
44 浏览量
2022-09-22
19:46:21
上传
评论
收藏 3KB ZIP 举报
alvarocfc
- 粉丝: 111
- 资源: 1万+