Debian Codegraph @

testsuite/compare_wave_main.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2009 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: November 2009 */
35 /*************************************************************************/
36 /* */
37 /* Compare two waveforms */
38 /*************************************************************************/
39 #include <stdio.h>
40 #include "cst_wave.h"
41 #include "cst_tokenstream.h"
42 #include "cst_args.h"
43 #include "cst_math.h"
44
45 int main(int argc, char **argv)
46 {
47 cst_wave *w1, *w2;
48 cst_val *files;
49 cst_features *args;
50 int i1, i2, l;
51 double d,s;
52 double b;
53 int best,x, y;
54 const char *f1, *f2;
55 float v1,v2,v3,c;
56 float xsum,xsumsq,xcount;
57 float ysum,ysumsq,ycount;
58 float xxsum,xxsumsq,xxcount;
59 float yysum,yysumsq,yycount;
60 float xysum,xysumsq,xycount;
61
62 args = new_features();
63 files =
64 cst_args(argv,argc,
65 "usage: compare_waves OPTIONS WAVE1 WAVE2\n"
66 "Compare two waveformfiles\n",
67 args);
68
69 f1 = val_string(val_car(files));
70 f2 = val_string(val_car(val_cdr(files)));
71 w1 = new_wave();
72 w2 = new_wave();
73
74 if (cst_wave_load_riff(w1,f1) != CST_OK_FORMAT)
75 {
76 fprintf(stderr,
77 "compare_wave: can't read file or wrong format \"%s\"\n",
78 f1);
79 return -1;
80 }
81
82 if (cst_wave_load_riff(w2,f2) != CST_OK_FORMAT)
83 {
84 fprintf(stderr,
85 "compare_wave: can't read file or wrong format \"%s\"\n",
86 f2);
87 return -1;
88 }
89
90 s = 0.0;
91 if (w1->num_samples > w2->num_samples)
92 l = w2->num_samples;
93 else
94 l = w1->num_samples;
95 b=-1;
96 best = -80;
97
98 for (y=-80; y < 80; y++)
99 {
100 for (i1=80,i2=80; i1 < l-160; i1++,i2++)
101 {
102 d = abs(w1->samples[i1+y]) - ((1.0+(x/100.0)) * (float)abs(w2->samples[i2]));
103 s += d*d;
104 }
105 s /= (l-160)-80;
106 /* printf("%f %d %f %d\n",sqrt(s),x,b,best); */
107 if (b < 0)
108 b = s; /* first time through loop */
109 if (s < b)
110 {
111 b = s;
112 best = y;
113 /* printf("New best %f x %d\n",b,best); */
114 }
115 }
116
117 /* Find correlation */
118 xcount = xsum = xsumsq = 0;
119 ycount = ysum = ysumsq = 0;
120 xycount = xysum = xysumsq = 0;
121 xxcount = xxsum = xxsumsq = 0;
122 yycount = yysum = yysumsq = 0;
123 for (i1=80+best,i2=80; i1 < l-160; i1++,i2++)
124 {
125 xcount++; xsum += w1->samples[i1];
126 xsumsq += w1->samples[i1]*w1->samples[i1];
127
128 ycount++; ysum += w2->samples[i2];
129 ysumsq += w2->samples[i2]*w2->samples[i2];
130
131 xxcount++; xxsum += w1->samples[i1] * w1->samples[i1];
132 xxsumsq += w1->samples[i1]*w1->samples[i1]*w1->samples[i1]*w1->samples[i1];
133
134 yycount++; yysum += w2->samples[i2] * w2->samples[i2];
135 yysumsq += w2->samples[i2]*w2->samples[i2] * w2->samples[i2]*w2->samples[i2];
136
137 xycount++; xysum += w1->samples[i1] * w2->samples[i2];
138 xysumsq += w1->samples[i1]*w1->samples[i1]*w2->samples[i2]*w2->samples[i2];
139
140 }
141 v1 = (xysum/xycount)-((xsum/xcount)*(ysum/ycount));
142 v2 = (xxsum/xxcount)-((xsum/xcount)*(xsum/xcount));
143 v3 = (yysum/yycount)-((ysum/ycount)*(ysum/ycount));
144 c = v1/sqrt(v2*v3);
145
146 printf("%2.3f %0.3f %d\n",sqrt(b),c,best);
147
148 return 0;
149 }