annotate tools/audio_processor_test/compare_wavs_freq/compare_wavs_freq.cpp @ 38:05598d745f99 pygar svn.39

[svn r39] fixed audiocorepipe
author punk
date Tue, 04 May 2010 19:27:38 -0400
parents 90197e3375e2
children
rev   line source
rlm@23 1 #include <stdio.h>
rlm@23 2 #include <assert.h>
rlm@23 3 #include <string.h>
rlm@23 4 #include <sndfile.h>
rlm@23 5 #include <stdlib.h>
rlm@23 6
rlm@23 7 // foreward declaration. This is defined in DFT.cpp
rlm@23 8 int DFT(int dir,int m,double *x1,double *y1);
rlm@23 9 const int Points = 32;
rlm@23 10 const int Window = 32;
rlm@23 11
rlm@23 12 void BubbleSort(double* arr, int* idxs)
rlm@23 13 {
rlm@23 14 int i, j, flag = 1;
rlm@23 15 double temp;
rlm@23 16 int tmp1;
rlm@23 17 for(i = 1; (i <= Points) && flag; i++)
rlm@23 18 {
rlm@23 19 flag = 0;
rlm@23 20 for (j=0; j < (Points -1); j++)
rlm@23 21 {
rlm@23 22 if (arr[j+1] > arr[j])
rlm@23 23 {
rlm@23 24 temp = arr[j];
rlm@23 25 tmp1 = idxs[j];
rlm@23 26 arr[j] = arr[j+1];
rlm@23 27 idxs[j] = idxs[j+1];
rlm@23 28 arr[j+1] = temp;
rlm@23 29 idxs[j+1] = tmp1;
rlm@23 30 flag = 1;
rlm@23 31 }
rlm@23 32 }
rlm@23 33 }
rlm@23 34 }
rlm@23 35
rlm@23 36 int
rlm@23 37 do_check (int drop, int argc, char * argv [])
rlm@23 38 {
rlm@23 39 int margin_of_error;
rlm@23 40 SNDFILE* goldenWavFile;
rlm@23 41 SNDFILE* outputWavFile;
rlm@23 42 SF_INFO goldenInfo ;
rlm@23 43 SF_INFO outputInfo ;
rlm@23 44 short int buffA[Points];
rlm@23 45 short int buffB[Points];
rlm@23 46 double drA[Window][Points];
rlm@23 47 double drB[Window][Points];
rlm@23 48 double diA[Points];
rlm@23 49 double diB[Points];
rlm@23 50
rlm@23 51 memset (&goldenInfo, 0, sizeof (goldenInfo)) ;
rlm@23 52 memset (&outputInfo, 0, sizeof (outputInfo)) ;
rlm@23 53
rlm@23 54 margin_of_error = atoi(argv[1]);
rlm@23 55
rlm@23 56 printf("Checking for Margin of Error %d\n", margin_of_error);
rlm@23 57
rlm@23 58 goldenWavFile = sf_open(argv[2], SFM_READ, &goldenInfo);
rlm@23 59 if (goldenWavFile == NULL){
rlm@23 60 printf ("\nERROR : failed Not able to open wav file named '%s' : %s/\n", argv[2], sf_strerror (NULL)) ;
rlm@23 61 exit (1) ;
rlm@23 62 }
rlm@23 63
rlm@23 64 outputWavFile = sf_open(argv[3], SFM_READ, &outputInfo);
rlm@23 65 if (outputWavFile == NULL){
rlm@23 66 printf ("\nERROR : failed Not able to open wav file named '%s' : %s/\n", argv[3], sf_strerror (NULL)) ;
rlm@23 67 exit (1) ;
rlm@23 68 }
rlm@23 69
rlm@23 70 int max_diff = 0;
rlm@23 71
rlm@23 72 // it is possible that the modified pipeline might introduce a few
rlm@23 73 // dummy tokens at the beginning of the stream, we'll just drop them
rlm@23 74 for(int i = 0; i < drop; i++){
rlm@23 75 sf_read_short(outputWavFile, buffB, 1);
rlm@23 76 }
rlm@23 77
rlm@23 78 int count=0;
rlm@23 79
rlm@23 80 while(sf_read_short(goldenWavFile, buffA, Points)==Points){
rlm@23 81
rlm@23 82 if(sf_read_short(outputWavFile, buffB, Points) != Points){
rlm@23 83 // the streams are of different length
rlm@23 84 printf("failed (length)\n");
rlm@23 85 exit(0);
rlm@23 86 }
rlm@23 87
rlm@23 88 int wptr = count%Window;
rlm@23 89
rlm@23 90 for(int i = 0; i < Points; i++){
rlm@23 91 diA[i] = 0.0;
rlm@23 92 diB[i] = 0.0;
rlm@23 93 drA[wptr][i] = (double)buffA[i];
rlm@23 94 drB[wptr][i] = (double)buffB[i];
rlm@23 95 }
rlm@23 96
rlm@23 97 DFT(1,Points,drA[wptr],diA);
rlm@23 98 DFT(1,Points,drB[wptr],diB);
rlm@23 99
rlm@23 100 //for(int i = 0; i < Points; i++)
rlm@23 101 // printf("%10d %10d\n", (int)drA[wptr][i], (int)drB[wptr][i]);
rlm@23 102 //printf("\n");
rlm@23 103
rlm@23 104 if (count >= Window){
rlm@23 105 double avgA[Points];
rlm@23 106 double avgB[Points];
rlm@23 107
rlm@23 108 // reset averages to zero
rlm@23 109 for(int i = 0; i < Points; i++){
rlm@23 110 avgA[i] = 0.0;
rlm@23 111 avgB[i] = 0.0;
rlm@23 112 }
rlm@23 113
rlm@23 114 // compute the averages in each bin
rlm@23 115 for(int i = 0; i < Window; i++){
rlm@23 116 for(int j = 0; j < Points; j++){
rlm@23 117 avgA[j] += drA[i][j];
rlm@23 118 avgB[j] += drB[i][j];
rlm@23 119 }
rlm@23 120 }
rlm@23 121
rlm@23 122 for(int i = 0; i < Points; i++){
rlm@23 123 avgA[i] /= Window;
rlm@23 124 avgB[i] /= Window;
rlm@23 125 }
rlm@23 126
rlm@23 127
rlm@23 128 // this sucks, as we really should be averaging, but i'm lazy
rlm@23 129
rlm@23 130 //int idxsA[Points];
rlm@23 131 //int idxsB[Points];
rlm@23 132
rlm@23 133 //for(int i = 0; i < Points; i++){
rlm@23 134 //idxsA[i] = i;
rlm@23 135 //idxsB[i] = i;
rlm@23 136 //}
rlm@23 137
rlm@23 138 //BubbleSort(avgA, idxsA);
rlm@23 139 //BubbleSort(avgB, idxsB);
rlm@23 140
rlm@23 141 //for(int i = 0; i < 2; i++){
rlm@23 142 //int td = (idxsA[i] > idxsB[i]) ? (idxsA[i] - idxsB[i]) : (idxsB[i] - idxsA[i]);
rlm@23 143 //max_diff = (td > max_diff) ? td : max_diff;
rlm@23 144 //}
rlm@23 145
rlm@23 146
rlm@23 147 int scoreA = 0;
rlm@23 148 int scoreB = 0;
rlm@23 149 int threshold = 100;
rlm@23 150
rlm@23 151 // this is brittle, but it works **for now**
rlm@23 152 for(int i = 0; i < Points; i++){
rlm@23 153 if(abs((int)avgA[i]) > threshold)
rlm@23 154 scoreA++;
rlm@23 155 if(abs((int)avgB[i]) > threshold)
rlm@23 156 scoreB++;
rlm@23 157 }
rlm@23 158
rlm@23 159 printf("scoreA %d scoreB %d\n", scoreA, scoreB);
rlm@23 160
rlm@23 161 int td = (scoreA > scoreB) ? (scoreA - scoreB) : (scoreB - scoreA);
rlm@23 162 max_diff = (td > max_diff) ? td : max_diff;
rlm@23 163
rlm@23 164 if(max_diff > margin_of_error){
rlm@23 165 printf("max_diff %x on drop %d (count %d)\n", max_diff, drop,count);
rlm@23 166 return 1;
rlm@23 167 }
rlm@23 168
rlm@23 169 }
rlm@23 170
rlm@23 171 count++;
rlm@23 172 }
rlm@23 173
rlm@23 174 printf("max_diff %d on drop %d\n", max_diff, drop);
rlm@23 175
rlm@23 176 if(((int)max_diff) > margin_of_error)
rlm@23 177 return 1;
rlm@23 178 else
rlm@23 179 return 0;
rlm@23 180 }
rlm@23 181
rlm@23 182 int
rlm@23 183 main(int argc, char* argv[])
rlm@23 184 {
rlm@23 185 for(int drop = 0; drop < 10; drop++){
rlm@23 186 if(do_check(drop, argc, argv)==0){
rlm@23 187 printf("passed on drop %d\n", drop);
rlm@23 188 exit(0);
rlm@23 189 }
rlm@23 190 }
rlm@23 191 printf("failed\n");
rlm@23 192 exit(1);
rlm@23 193 }