compare_wave_main.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 (awb@cs.cmu.edu) */
  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. int main(int argc, char **argv)
  45. {
  46. cst_wave *w1, *w2;
  47. cst_val *files;
  48. cst_features *args;
  49. int i1, i2, l;
  50. double d,s;
  51. double b;
  52. int best,x, y;
  53. const char *f1, *f2;
  54. float v1,v2,v3,c;
  55. float xsum,xsumsq,xcount;
  56. float ysum,ysumsq,ycount;
  57. float xxsum,xxsumsq,xxcount;
  58. float yysum,yysumsq,yycount;
  59. float xysum,xysumsq,xycount;
  60. args = new_features();
  61. files =
  62. cst_args(argv,argc,
  63. "usage: compare_waves OPTIONS WAVE1 WAVE2\n"
  64. "Compare two waveformfiles\n",
  65. args);
  66. f1 = val_string(val_car(files));
  67. f2 = val_string(val_car(val_cdr(files)));
  68. w1 = new_wave();
  69. w2 = new_wave();
  70. if (cst_wave_load_riff(w1,f1) != CST_OK_FORMAT)
  71. {
  72. fprintf(stderr,
  73. "compare_wave: can't read file or wrong format \"%s\"\n",
  74. f1);
  75. return -1;
  76. }
  77. if (cst_wave_load_riff(w2,f2) != CST_OK_FORMAT)
  78. {
  79. fprintf(stderr,
  80. "compare_wave: can't read file or wrong format \"%s\"\n",
  81. f2);
  82. return -1;
  83. }
  84. s = 0.0;
  85. if (w1->num_samples > w2->num_samples)
  86. l = w2->num_samples;
  87. else
  88. l = w1->num_samples;
  89. b=-1;
  90. best = -80;
  91. for (y=-80; y < 80; y++)
  92. {
  93. for (i1=80,i2=80; i1 < l-160; i1++,i2++)
  94. {
  95. d = abs(w1->samples[i1+y]) - ((1.0+(x/100.0)) * (float)abs(w2->samples[i2]));
  96. s += d*d;
  97. }
  98. s /= (l-160)-80;
  99. /* printf("%f %d %f %d\n",sqrt(s),x,b,best); */
  100. if (b < 0)
  101. b = s; /* first time through loop */
  102. if (s < b)
  103. {
  104. b = s;
  105. best = y;
  106. /* printf("New best %f x %d\n",b,best); */
  107. }
  108. }
  109. /* Find correlation */
  110. xcount = xsum = xsumsq = 0;
  111. ycount = ysum = ysumsq = 0;
  112. xycount = xysum = xysumsq = 0;
  113. xxcount = xxsum = xxsumsq = 0;
  114. yycount = yysum = yysumsq = 0;
  115. for (i1=80+best,i2=80; i1 < l-160; i1++,i2++)
  116. {
  117. xcount++; xsum += w1->samples[i1];
  118. xsumsq += w1->samples[i1]*w1->samples[i1];
  119. ycount++; ysum += w2->samples[i2];
  120. ysumsq += w2->samples[i2]*w2->samples[i2];
  121. xxcount++; xxsum += w1->samples[i1] * w1->samples[i1];
  122. xxsumsq += w1->samples[i1]*w1->samples[i1]*w1->samples[i1]*w1->samples[i1];
  123. yycount++; yysum += w2->samples[i2] * w2->samples[i2];
  124. yysumsq += w2->samples[i2]*w2->samples[i2] * w2->samples[i2]*w2->samples[i2];
  125. xycount++; xysum += w1->samples[i1] * w2->samples[i2];
  126. xysumsq += w1->samples[i1]*w1->samples[i1]*w2->samples[i2]*w2->samples[i2];
  127. }
  128. v1 = (xysum/xycount)-((xsum/xcount)*(ysum/ycount));
  129. v2 = (xxsum/xxcount)-((xsum/xcount)*(xsum/xcount));
  130. v3 = (yysum/yycount)-((ysum/ycount)*(ysum/ycount));
  131. c = v1/sqrt(v2*v3);
  132. printf("%2.3f %0.3f %d\n",sqrt(b),c,best);
  133. return 0;
  134. }