Mercurial > pygar
comparison 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 |
comparison
equal
deleted
inserted
replaced
22:0cfbb1e2de22 | 23:90197e3375e2 |
---|---|
1 #include <stdio.h> | |
2 #include <assert.h> | |
3 #include <string.h> | |
4 #include <sndfile.h> | |
5 #include <stdlib.h> | |
6 | |
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; | |
11 | |
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 } | |
35 | |
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]; | |
50 | |
51 memset (&goldenInfo, 0, sizeof (goldenInfo)) ; | |
52 memset (&outputInfo, 0, sizeof (outputInfo)) ; | |
53 | |
54 margin_of_error = atoi(argv[1]); | |
55 | |
56 printf("Checking for Margin of Error %d\n", margin_of_error); | |
57 | |
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 } | |
63 | |
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 } | |
69 | |
70 int max_diff = 0; | |
71 | |
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 } | |
77 | |
78 int count=0; | |
79 | |
80 while(sf_read_short(goldenWavFile, buffA, Points)==Points){ | |
81 | |
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 } | |
87 | |
88 int wptr = count%Window; | |
89 | |
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 } | |
96 | |
97 DFT(1,Points,drA[wptr],diA); | |
98 DFT(1,Points,drB[wptr],diB); | |
99 | |
100 //for(int i = 0; i < Points; i++) | |
101 // printf("%10d %10d\n", (int)drA[wptr][i], (int)drB[wptr][i]); | |
102 //printf("\n"); | |
103 | |
104 if (count >= Window){ | |
105 double avgA[Points]; | |
106 double avgB[Points]; | |
107 | |
108 // reset averages to zero | |
109 for(int i = 0; i < Points; i++){ | |
110 avgA[i] = 0.0; | |
111 avgB[i] = 0.0; | |
112 } | |
113 | |
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 } | |
121 | |
122 for(int i = 0; i < Points; i++){ | |
123 avgA[i] /= Window; | |
124 avgB[i] /= Window; | |
125 } | |
126 | |
127 | |
128 // this sucks, as we really should be averaging, but i'm lazy | |
129 | |
130 //int idxsA[Points]; | |
131 //int idxsB[Points]; | |
132 | |
133 //for(int i = 0; i < Points; i++){ | |
134 //idxsA[i] = i; | |
135 //idxsB[i] = i; | |
136 //} | |
137 | |
138 //BubbleSort(avgA, idxsA); | |
139 //BubbleSort(avgB, idxsB); | |
140 | |
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 //} | |
145 | |
146 | |
147 int scoreA = 0; | |
148 int scoreB = 0; | |
149 int threshold = 100; | |
150 | |
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 } | |
158 | |
159 printf("scoreA %d scoreB %d\n", scoreA, scoreB); | |
160 | |
161 int td = (scoreA > scoreB) ? (scoreA - scoreB) : (scoreB - scoreA); | |
162 max_diff = (td > max_diff) ? td : max_diff; | |
163 | |
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 } | |
168 | |
169 } | |
170 | |
171 count++; | |
172 } | |
173 | |
174 printf("max_diff %d on drop %d\n", max_diff, drop); | |
175 | |
176 if(((int)max_diff) > margin_of_error) | |
177 return 1; | |
178 else | |
179 return 0; | |
180 } | |
181 | |
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 } |