处理C中的负值

时间:2015-12-02 06:13:37

标签: c audio

enter image description here我正在尝试编写一个c prog来提取两个wav文件之间的样本值diff,每个文件都是24 bitdepth。但假设样本值为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[])
{
    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);
}

1 个答案:

答案 0 :(得分:0)

  

是什么让你不能只使用签名变量?

     

样本价值差异可能是负面和正面。我会   需要负差异的绝对值,因此   无符号将被使用..

这证明无符号用于diff[],但不适用于必须签名的in1[]in2[]。出现错误的值是因为您未能sign extend输入,e。 G。像:

    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机器。