diff tools/audio_processor_test/compare_wavs_freq/compare_wavs_freq.cpp @ 23:90197e3375e2 pygar svn.24

[svn r24] added testing, but something is wrong with our c++ file.
author rlm
date Wed, 28 Apr 2010 08:19:09 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/tools/audio_processor_test/compare_wavs_freq/compare_wavs_freq.cpp	Wed Apr 28 08:19:09 2010 -0400
     1.3 @@ -0,0 +1,193 @@
     1.4 +#include <stdio.h>
     1.5 +#include <assert.h>
     1.6 +#include <string.h>
     1.7 +#include <sndfile.h>
     1.8 +#include <stdlib.h>
     1.9 +
    1.10 +// foreward declaration. This is defined in DFT.cpp
    1.11 +int DFT(int dir,int m,double *x1,double *y1);
    1.12 +const int Points = 32;
    1.13 +const int Window = 32;
    1.14 +
    1.15 +void BubbleSort(double* arr, int* idxs)
    1.16 +{
    1.17 +  int i, j, flag = 1;
    1.18 +  double temp;
    1.19 +  int tmp1;
    1.20 +  for(i = 1; (i <= Points) && flag; i++)
    1.21 +    {
    1.22 +      flag = 0;
    1.23 +      for (j=0; j < (Points -1); j++)
    1.24 +	{
    1.25 +	  if (arr[j+1] > arr[j])
    1.26 +	    { 
    1.27 +	      temp = arr[j];    
    1.28 +	      tmp1 = idxs[j];
    1.29 +	      arr[j] = arr[j+1];
    1.30 +	      idxs[j] = idxs[j+1];
    1.31 +	      arr[j+1] = temp;
    1.32 +	      idxs[j+1] = tmp1;
    1.33 +	      flag = 1;
    1.34 +	    }
    1.35 +	}
    1.36 +    }
    1.37 +}
    1.38 +
    1.39 +int
    1.40 +do_check (int drop, int argc, char * argv [])
    1.41 +{
    1.42 +  int margin_of_error;
    1.43 +  SNDFILE* goldenWavFile;
    1.44 +  SNDFILE* outputWavFile;
    1.45 +  SF_INFO goldenInfo ;
    1.46 +  SF_INFO outputInfo ;
    1.47 +  short int buffA[Points];
    1.48 +  short int buffB[Points];
    1.49 +  double drA[Window][Points];
    1.50 +  double drB[Window][Points];
    1.51 +  double diA[Points];
    1.52 +  double diB[Points];
    1.53 +
    1.54 +  memset (&goldenInfo, 0, sizeof (goldenInfo)) ;
    1.55 +  memset (&outputInfo, 0, sizeof (outputInfo)) ;
    1.56 +
    1.57 +  margin_of_error = atoi(argv[1]);
    1.58 +
    1.59 +  printf("Checking for Margin of Error %d\n", margin_of_error);
    1.60 +
    1.61 +  goldenWavFile = sf_open(argv[2], SFM_READ, &goldenInfo);
    1.62 +  if (goldenWavFile == NULL){      
    1.63 +    printf ("\nERROR : failed Not able to open wav file named '%s' : %s/\n", argv[2], sf_strerror (NULL)) ;
    1.64 +    exit (1) ;
    1.65 +  }
    1.66 +
    1.67 +  outputWavFile = sf_open(argv[3], SFM_READ, &outputInfo);
    1.68 +  if (outputWavFile == NULL){      
    1.69 +    printf ("\nERROR : failed Not able to open wav file named '%s' : %s/\n", argv[3], sf_strerror (NULL)) ;
    1.70 +    exit (1) ;
    1.71 +  }
    1.72 + 
    1.73 +  int max_diff = 0;
    1.74 +
    1.75 +  // it is possible that the modified pipeline might introduce a few
    1.76 +  // dummy tokens at the beginning of the stream, we'll just drop them
    1.77 +  for(int i = 0; i < drop; i++){
    1.78 +    sf_read_short(outputWavFile, buffB, 1);
    1.79 +  }
    1.80 +
    1.81 +  int count=0;
    1.82 +
    1.83 +  while(sf_read_short(goldenWavFile, buffA, Points)==Points){
    1.84 +
    1.85 +    if(sf_read_short(outputWavFile, buffB, Points) != Points){
    1.86 +      // the streams are of different length
    1.87 +      printf("failed (length)\n");
    1.88 +      exit(0);
    1.89 +    }
    1.90 +
    1.91 +    int wptr = count%Window;
    1.92 +    
    1.93 +    for(int i = 0; i < Points; i++){
    1.94 +      diA[i] = 0.0;
    1.95 +      diB[i] = 0.0;
    1.96 +      drA[wptr][i] = (double)buffA[i];
    1.97 +      drB[wptr][i] = (double)buffB[i];
    1.98 +    }
    1.99 +
   1.100 +    DFT(1,Points,drA[wptr],diA);
   1.101 +    DFT(1,Points,drB[wptr],diB);
   1.102 +
   1.103 +    //for(int i = 0; i < Points; i++)
   1.104 +    //  printf("%10d  %10d\n", (int)drA[wptr][i], (int)drB[wptr][i]);
   1.105 +    //printf("\n");
   1.106 +
   1.107 +    if (count >= Window){
   1.108 +      double avgA[Points];
   1.109 +      double avgB[Points];
   1.110 +      
   1.111 +      // reset averages to zero
   1.112 +      for(int i = 0; i < Points; i++){
   1.113 +	avgA[i] = 0.0;
   1.114 +	avgB[i] = 0.0;
   1.115 +      }
   1.116 +
   1.117 +      // compute the averages in each bin
   1.118 +      for(int i = 0; i <  Window; i++){
   1.119 +	for(int j = 0; j < Points; j++){
   1.120 +	  avgA[j] += drA[i][j];
   1.121 +	  avgB[j] += drB[i][j];
   1.122 +	}
   1.123 +      }	
   1.124 +
   1.125 +      for(int i = 0; i < Points; i++){
   1.126 +	avgA[i] /= Window;
   1.127 +	avgB[i] /= Window;
   1.128 +      }
   1.129 +
   1.130 +
   1.131 +      // this sucks, as we really should be averaging, but i'm lazy
   1.132 +
   1.133 +      //int idxsA[Points];
   1.134 +      //int idxsB[Points];
   1.135 +
   1.136 +      //for(int i = 0; i < Points; i++){
   1.137 +      //idxsA[i] = i;
   1.138 +      //idxsB[i] = i;
   1.139 +      //}
   1.140 +
   1.141 +      //BubbleSort(avgA, idxsA);
   1.142 +      //BubbleSort(avgB, idxsB);
   1.143 +
   1.144 +      //for(int i = 0; i < 2; i++){
   1.145 +      //int td =  (idxsA[i] > idxsB[i]) ? (idxsA[i] - idxsB[i]) : (idxsB[i] - idxsA[i]);
   1.146 +      //max_diff = (td > max_diff) ? td : max_diff;
   1.147 +      //}
   1.148 +      
   1.149 +
   1.150 +      int scoreA = 0;
   1.151 +      int scoreB = 0;
   1.152 +      int threshold = 100;
   1.153 +
   1.154 +      // this is brittle, but it works **for now**
   1.155 +      for(int i = 0; i < Points; i++){
   1.156 +	if(abs((int)avgA[i]) > threshold)
   1.157 +	  scoreA++;
   1.158 +	if(abs((int)avgB[i]) > threshold)
   1.159 +	  scoreB++;
   1.160 +      }
   1.161 +
   1.162 +      printf("scoreA %d   scoreB %d\n", scoreA, scoreB);
   1.163 +
   1.164 +      int td = (scoreA > scoreB) ? (scoreA - scoreB) : (scoreB - scoreA);
   1.165 +      max_diff = (td > max_diff) ? td : max_diff;      
   1.166 +
   1.167 +      if(max_diff > margin_of_error){
   1.168 +	printf("max_diff %x on drop %d (count %d)\n", max_diff, drop,count);
   1.169 +	return 1;
   1.170 +      }
   1.171 +
   1.172 +    }
   1.173 +
   1.174 +    count++;
   1.175 +  }
   1.176 +
   1.177 +  printf("max_diff %d on drop %d\n", max_diff, drop);
   1.178 +
   1.179 +  if(((int)max_diff) > margin_of_error)
   1.180 +    return 1;
   1.181 +  else
   1.182 +    return 0;
   1.183 +}
   1.184 +
   1.185 +int
   1.186 +main(int argc, char* argv[])
   1.187 +{
   1.188 +  for(int drop = 0; drop < 10; drop++){
   1.189 +    if(do_check(drop, argc, argv)==0){
   1.190 +      printf("passed on drop %d\n", drop);
   1.191 +      exit(0);
   1.192 +    }
   1.193 +  }
   1.194 +  printf("failed\n");
   1.195 +  exit(1);
   1.196 +}