view tools/audio_processor_test/compare_wavs_freq/compare_wavs_freq.cpp @ 42:ced2ebd41347 pygar svn.43

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