C语言中负值的处理
我试图编写一个c程序来提取两个wav文件(每个文件的深度为24位)之间的样本值差异。但假设样本值为0和-1,程序将取0和2^24-1。。并给出2^24-1作为最终差值,这是错误的。。下面是我的代码。。这方面的任何帮助都将是gr8。我也附上了我获得的结果的截图C语言中负值的处理,c,audio,C,Audio,我试图编写一个c程序来提取两个wav文件(每个文件的深度为24位)之间的样本值差异。但假设样本值为0和-1,程序将取0和2^24-1。。并给出2^24-1作为最终差值,这是错误的。。下面是我的代码。。这方面的任何帮助都将是gr8。我也附上了我获得的结果的截图 void maxdiff(int numchannels,int numsamples,int index1,FILE *fptest,FILE *fpref); int main(int argc, char *argv[]) {
void maxdiff(int numchannels,int numsamples,int index1,FILE *fptest,FILE *fpref);
int main(int argc, char *argv[])
{
FILE *fptest, *fpref;
int16_t ch1,ch2,i;
uint16_t fmt_id1,fmt_id2;
int32_t nofsamples1=0,nofsamples2=0,fs1,fs2,bytes_per_sample1,bytes_per_sample2,c;
if(argc < 3)
{
printf("\nUSAGE: program requires a test file name and ref file name(in this order) as arguments -- please try again\n");
exit(0);
}
fptest = fopen(argv[1], "rb");
fpref = fopen(argv[2], "rb");
if(fptest == NULL)
{
printf("Unable to open test file\n");
exit(EXIT_FAILURE);
}
if(fpref == NULL)
{
printf("Unable to open ref file\n");
exit(EXIT_FAILURE);
}
GetHeaderWave(fptest, &fs1, &ch1, &nofsamples1, &bytes_per_sample1, &fmt_id1);
GetHeaderWave(fpref, &fs2, &ch2, &nofsamples2, &bytes_per_sample2, &fmt_id2);
//Sample number comparision
if(nofsamples1<nofsamples2)
{
printf("\nNumber of samples of test vector are less than the number of samples of ref vector.Please regenerate the test vector and try again\n");
exit(0);
}
else if (nofsamples1>nofsamples2)
{
printf("\nWarning:Number of samples of test vector are greater than no of samples of ref vector\n");
c=nofsamples2;
}
else
c=nofsamples2;
//end
//no.of channel comparision
if(ch1!=ch2)
{
printf("\nthe two wave files differ in the number of num of channels. Comparision not possible\n");
exit(0);
}
//end
//bitdepth comparision
if(bytes_per_sample1!=bytes_per_sample2)
{
printf("\nThe two files differ in terms of bitdepth. Comparision is not possible\n");
exit(0);
}
//end
maxdiff(ch1,c,bytes_per_sample1,fptest,fpref);
getchar();
}
void maxdiff(int numchannels,int numsamples,int index1,FILE *fptest,FILE *fpref)
{
uint32_t in1[10]={0},in2[10]={0},count=0;
uint32_t i,j,diff[10]={'0'},absdiff=0;
for(i=0;i<numsamples;i++)
{
for(j=0;j<numchannels;j++)
{
fread(&in1[j],index1, 1, fptest);
fread(&in2[j],index1, 1, fpref);
if((abs(in1[j]-in2[j])>diff[j]) && in1[j] != 16777215 && in2[j] !=16777215)
{
/* if(i==17961)
{
printf("intest=%d\n",in1[j]);
printf("inref=%d\n",in2[j]);
getchar();continue;
}*/
diff[j]=abs(in1[j]-in2[j]);
printf("value of test and ref sample is %u\t%u and sample number is %u\n",in1[j],in2[j],i);
printf("%u\n",diff[j]);
}
}
count++;
}
printf("count of samples processed=%d\n",count);
for(i=0;i<numchannels;i++)
{
printf("difference in terms of sample value for channel number %d is %d\n",i,diff[i]);
if(diff[i] > absdiff)
absdiff=diff[i];
}
getchar();
printf("\nabsolute difference is %d\n",absdiff);
}
void maxdiff(int numchannels、int numsamples、int index1、FILE*fptest、FILE*fpref);
int main(int argc,char*argv[])
{
文件*fptest,*fpref;
int16_t ch1、ch2、i;
uint16_t fmt_id1,fmt_id2;
int32_t nofsample1=0,nofsample2=0,fs1,fs2,每个样本1的字节数,每个样本2的字节数,c;
如果(argc<3)
{
printf(“\n用法:程序需要一个测试文件名和ref文件名(按此顺序)作为参数--请重试\n”);
出口(0);
}
fptest=fopen(argv[1],“rb”);
fpref=fopen(argv[2],“rb”);
如果(fptest==NULL)
{
printf(“无法打开测试文件\n”);
退出(退出失败);
}
如果(fpref==NULL)
{
printf(“无法打开ref文件\n”);
退出(退出失败);
}
GetHeaderWave(fptest、&fs1、&ch1、&nofsample1、&bytes\u per\u sample1、&fmt\u id1);
GetHeaderWave(fpref、&fs2、&ch2、&nofsample2、&bytes\u per\u sample2、&fmt\u id2);
//样本数比较
如果(无示例1无示例2)
{
printf(“\n警告:测试向量的样本数大于参考向量的样本数\n”);
c=无样品2;
}
其他的
c=无样品2;
//结束
//频道比较次数
如果(ch1!=ch2)
{
printf(“\n两个波形文件的通道数不同。无法进行比较\n”);
出口(0);
}
//结束
//比特深度比较
if(每个样本1的字节数!=每个样本2的字节数)
{
printf(“\n两个文件的位深度不同。无法进行比较\n”);
出口(0);
}
//结束
maxdiff(ch1,c,字节/u样本1,fptest,fpref);
getchar();
}
void maxdiff(int numchannels、int numsamples、int index1、FILE*fptest、FILE*fpref)
{
uint32_t in1[10]={0},in2[10]={0},count=0;
uint32_t i,j,diff[10]={'0'},absdiff=0;
对于(i=0;i
是什么阻止您只使用有符号变量
样本值差可以是负的,也可以是正的..我会
需要负差值的绝对值,因为
将使用未签名的
这就证明了unsigned可以用于diff[]
,但不能用于必须签名的in1[]
和in2[]
。错误的值是因为输入失败,例如:
int32_t in1[10], in2[10]; // need to be signed
…
fread(1+(char *)&in1[j], index1, 1, fptest); // read in 3 MSB
fread(1+(char *)&in2[j], index1, 1, fpref);
in1[j] /= 256; // sign extend MSB, and discard spurious LSB
in2[j] /= 256;
请注意,这与您的原始代码一样,仅适用于普通的little endian计算机。是什么阻止您仅使用有符号变量?采样值差可以是负数和正数。我需要负数差的绝对值,因为要使用无符号变量。。