signalCompare.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /***********************************************************************
  2. Copyright (c) 2006-2011, Skype Limited. All rights reserved.
  3. Redistribution and use in source and binary forms, with or without
  4. modification, (subject to the limitations in the disclaimer below)
  5. are permitted provided that the following conditions are met:
  6. - Redistributions of source code must retain the above copyright notice,
  7. this list of conditions and the following disclaimer.
  8. - Redistributions in binary form must reproduce the above copyright
  9. notice, this list of conditions and the following disclaimer in the
  10. documentation and/or other materials provided with the distribution.
  11. - Neither the name of Skype Limited, nor the names of specific
  12. contributors, may be used to endorse or promote products derived from
  13. this software without specific prior written permission.
  14. NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED
  15. BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
  17. BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  18. FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  19. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  21. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  22. USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  23. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. ***********************************************************************/
  27. /*
  28. * Compare two audio signals and compute weighted SNR difference
  29. */
  30. #ifdef _WIN32
  31. #define _CRT_SECURE_NO_DEPRECATE 1
  32. #endif
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <string.h>
  36. #include <math.h>
  37. #include "SKP_Silk_SigProc_FIX.h"
  38. #define FRAME_LENGTH_MS 10
  39. #define WIN_LENGTH_MS 20
  40. #define BW_EXPANSION 0.7f
  41. #define MAX_FS_KHZ 48
  42. #define LPC_ORDER 10
  43. #define SNR_THRESHOLD 15.0
  44. #ifdef __cplusplus
  45. extern "C"
  46. {
  47. #endif
  48. /* Internally used functions */
  49. void Autocorrelation(
  50. SKP_float *results, /* o result (length correlationCount) */
  51. const SKP_float *inputData, /* i input data to correlate */
  52. SKP_int inputDataSize, /* i length of input */
  53. SKP_int correlationCount /* i number of correlation taps to compute */
  54. );
  55. /* inner product of two SKP_float arrays, with result as double */
  56. double Inner_product(
  57. const SKP_float *data1,
  58. const SKP_float *data2,
  59. SKP_int dataSize
  60. );
  61. /* Solve the normal equations using the Levinson-Durbin recursion */
  62. SKP_float Levinsondurbin( /* O prediction error energy */
  63. SKP_float A[], /* O prediction coefficients [order] */
  64. const SKP_float corr[], /* I input auto-correlations [order + 1] */
  65. const SKP_int order /* I prediction order */
  66. );
  67. /* Chirp (bw expand) LP AR filter */
  68. void Bwexpander(
  69. SKP_float *ar, /* io AR filter to be expanded (without leading 1) */
  70. const SKP_int d, /* i length of ar */
  71. const SKP_float chirp /* i chirp factor (typically in range (0..1) ) */
  72. );
  73. #ifdef __cplusplus
  74. }
  75. #endif
  76. static void print_usage(char* argv[]) {
  77. printf("\nusage: %s ref.pcm test.pcm [settings]\n", argv[ 0 ]);
  78. printf("\nref.pcm : Reference file");
  79. printf("\ntest.pcm : File to be tested, should be of same length as ref.pcm");
  80. printf("\n settings:");
  81. printf("\n-diff : Only determine bit-exactness");
  82. printf("\n-fs <Hz> : Sampling rate in Hz, max: %d; default: 48000", MAX_FS_KHZ * 1000 );
  83. printf("\n");
  84. }
  85. int main(int argc, char* argv[])
  86. {
  87. SKP_int args, n, i, counterRef, counterTest;
  88. char testInFileName[150], refInFileName[150];
  89. FILE *refInFile, *testInFile;
  90. SKP_int nFrames = 0, isUnequal = 0;
  91. SKP_int diff = 0, Fs_kHz;
  92. SKP_int32 Fs_Hz = 24000;
  93. SKP_float c, refWhtnd, testWhtnd, refNrg, diffNrg;
  94. double SNR = 0.0;
  95. SKP_int16 refIn[WIN_LENGTH_MS * MAX_FS_KHZ], testIn[WIN_LENGTH_MS * MAX_FS_KHZ];
  96. SKP_float refWin[WIN_LENGTH_MS * MAX_FS_KHZ], testWin[WIN_LENGTH_MS * MAX_FS_KHZ];
  97. SKP_float autoCorr[LPC_ORDER + 1], LPC_Coef[LPC_ORDER];
  98. if (argc < 3) {
  99. print_usage(argv);
  100. exit(0);
  101. }
  102. /* get arguments */
  103. args = 1;
  104. strcpy(refInFileName, argv[args]);
  105. args++;
  106. strcpy(testInFileName, argv[args]);
  107. args++;
  108. while(args < argc ) {
  109. if (SKP_STR_CASEINSENSITIVE_COMPARE(argv[args], "-diff") == 0) {
  110. diff = 1;
  111. args++;
  112. }else if (SKP_STR_CASEINSENSITIVE_COMPARE(argv[args], "-fs") == 0) {
  113. sscanf(argv[args+1], "%d", &Fs_Hz);
  114. args += 2;
  115. } else {
  116. printf("Error: unrecognized setting: %s\n\n", argv[args]);
  117. print_usage(argv);
  118. exit(0);
  119. }
  120. }
  121. Fs_kHz = SKP_DIV32_16( Fs_Hz, 1000 );
  122. if( Fs_kHz > MAX_FS_KHZ ) {
  123. printf("Error: sampling rate too high: %d\n\n", Fs_kHz);
  124. print_usage(argv);
  125. exit(0);
  126. }
  127. printf("Reference: %s\n", refInFileName);
  128. //printf("Test: %s\n", testInFileName);
  129. /* open files */
  130. refInFile = fopen(refInFileName, "rb");
  131. if (refInFile==NULL) {
  132. printf("Error: could not open input file %s\n", refInFileName);
  133. exit(0);
  134. }
  135. testInFile = fopen(testInFileName, "rb");
  136. if (testInFile==NULL) {
  137. printf("Error: could not open input file %s\n", testInFileName);
  138. exit(0);
  139. }
  140. SKP_memset( refIn, 0, sizeof(refIn) );
  141. SKP_memset( testIn, 0, sizeof(testIn) );
  142. while(1) {
  143. /* Read inputs */
  144. counterRef = (SKP_int)fread(&refIn[(WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz],
  145. sizeof(SKP_int16), FRAME_LENGTH_MS * Fs_kHz, refInFile);
  146. counterTest = (SKP_int)fread(&testIn[(WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz],
  147. sizeof(SKP_int16), FRAME_LENGTH_MS * Fs_kHz, testInFile);
  148. if(counterRef != FRAME_LENGTH_MS * Fs_kHz || counterTest != FRAME_LENGTH_MS * Fs_kHz){
  149. break;
  150. }
  151. /* test for bit-exactness */
  152. for( n = 0; n < FRAME_LENGTH_MS * Fs_kHz; n++ ) {
  153. if( refIn[(WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz + n] !=
  154. testIn[(WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz + n] ) {
  155. isUnequal = 1;
  156. break;
  157. }
  158. }
  159. /* apply sine window */
  160. for( n = 0; n < WIN_LENGTH_MS * Fs_kHz; n++ ) {
  161. c = (SKP_float)sin( 3.14159265 * (n + 1) / (WIN_LENGTH_MS * Fs_kHz + 1) );
  162. refWin[n] = refIn[n] * c;
  163. testWin[n] = testIn[n] * c;
  164. }
  165. /* LPC analysis on reference signal */
  166. /* Calculate auto correlation */
  167. Autocorrelation(autoCorr, refWin, WIN_LENGTH_MS * Fs_kHz, LPC_ORDER + 1);
  168. /* Add white noise */
  169. autoCorr[ 0 ] += autoCorr[ 0 ] * 1e-6f + 1.0f;
  170. /* Convert correlations to prediction coefficients */
  171. Levinsondurbin(LPC_Coef, autoCorr, LPC_ORDER);
  172. /* Bandwdith expansion */
  173. Bwexpander(LPC_Coef, LPC_ORDER, BW_EXPANSION);
  174. /* Filter both signals */
  175. refNrg = 1.0f;
  176. diffNrg = 1e-10f;
  177. for( n = (WIN_LENGTH_MS - FRAME_LENGTH_MS) / 2 * Fs_kHz;
  178. n < (WIN_LENGTH_MS + FRAME_LENGTH_MS) / 2 * Fs_kHz; n++ ) {
  179. refWhtnd = refIn[n];
  180. testWhtnd = testIn[n];
  181. for( i = 0; i < LPC_ORDER; i++ ) {
  182. refWhtnd -= LPC_Coef[ i ] * refIn[n - i - 1];
  183. testWhtnd -= LPC_Coef[ i ] * testIn[n - i - 1];
  184. }
  185. refNrg += refWhtnd * refWhtnd;
  186. diffNrg += (refWhtnd - testWhtnd) * (refWhtnd - testWhtnd);
  187. }
  188. /* weighted SNR */
  189. if( refNrg > FRAME_LENGTH_MS * Fs_kHz ) {
  190. SNR += 10.0 * log10( refNrg / diffNrg );
  191. nFrames++;
  192. }
  193. /* Update Buffer */
  194. SKP_memmove( refIn, &refIn[FRAME_LENGTH_MS * Fs_kHz], (WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz * sizeof(SKP_int16));
  195. SKP_memmove( testIn, &testIn[FRAME_LENGTH_MS * Fs_kHz], (WIN_LENGTH_MS - FRAME_LENGTH_MS) * Fs_kHz * sizeof(SKP_int16));
  196. }
  197. if( diff ) {
  198. if( isUnequal ) {
  199. printf("Signals differ\n");
  200. } else {
  201. if(counterRef != counterTest){
  202. printf("Warning: signals differ in length\n");
  203. }
  204. printf("Signals are bit-exact PASS\n");
  205. }
  206. } else {
  207. if( nFrames == 0 ) {
  208. printf("At least one signal too short or not loud enough\n");
  209. exit(0);
  210. }
  211. if(counterRef != counterTest){
  212. printf("Warning: signals differ in length\n");
  213. }
  214. if( isUnequal == 0 ) {
  215. printf("Signals are bit-exact PASS\n");
  216. } else {
  217. printf("Average weighted SNR: %4.1f dB ", SNR / nFrames);
  218. if( SNR / nFrames < SNR_THRESHOLD ) {
  219. printf("FAIL\n");
  220. } else {
  221. printf("PASS\n");
  222. }
  223. }
  224. }
  225. printf("\n");
  226. /* Close Files */
  227. fclose(refInFile);
  228. fclose(testInFile);
  229. return 0;
  230. }
  231. /* compute autocorrelation */
  232. void Autocorrelation(
  233. SKP_float *results, /* o result (length correlationCount) */
  234. const SKP_float *inputData, /* i input data to correlate */
  235. SKP_int inputDataSize, /* i length of input */
  236. SKP_int correlationCount /* i number of correlation taps to compute */
  237. )
  238. {
  239. SKP_int i;
  240. if (correlationCount > inputDataSize) {
  241. correlationCount = inputDataSize;
  242. }
  243. for( i = 0; i < correlationCount; i++ ) {
  244. results[ i ] = (SKP_float)Inner_product( inputData, inputData + i, inputDataSize - i );
  245. }
  246. }
  247. /* inner product of two SKP_float arrays, with result as double */
  248. double Inner_product(
  249. const SKP_float *data1,
  250. const SKP_float *data2,
  251. SKP_int dataSize
  252. )
  253. {
  254. SKP_int i, dataSize4;
  255. double result;
  256. /* 4x unrolled loop */
  257. result = 0.0f;
  258. dataSize4 = dataSize & 0xFFFC;
  259. for( i = 0; i < dataSize4; i += 4 ) {
  260. result += data1[ i + 0 ] * data2[ i + 0 ] +
  261. data1[ i + 1 ] * data2[ i + 1 ] +
  262. data1[ i + 2 ] * data2[ i + 2 ] +
  263. data1[ i + 3 ] * data2[ i + 3 ];
  264. }
  265. /* add any remaining products */
  266. for( ; i < dataSize; i++ ) {
  267. result += data1[ i ] * data2[ i ];
  268. }
  269. return result;
  270. }
  271. /* Solve the normal equations using the Levinson-Durbin recursion */
  272. SKP_float Levinsondurbin( /* O prediction error energy */
  273. SKP_float A[], /* O prediction coefficients [order] */
  274. const SKP_float corr[], /* I input auto-correlations [order + 1] */
  275. const SKP_int order /* I prediction order */
  276. )
  277. {
  278. SKP_int i, mHalf, m;
  279. SKP_float min_nrg, nrg, t, km, Atmp1, Atmp2;
  280. min_nrg = 1e-12f * corr[ 0 ] + 1e-9f;
  281. nrg = corr[ 0 ];
  282. nrg = SKP_max(min_nrg, nrg);
  283. A[ 0 ] = corr[ 1 ] / nrg;
  284. nrg -= A[ 0 ] * corr[ 1 ];
  285. nrg = SKP_max(min_nrg, nrg);
  286. for( m = 1; m < order; m++ )
  287. {
  288. t = corr[ m + 1 ];
  289. for( i = 0; i < m; i++ ) {
  290. t -= A[ i ] * corr[ m - i ];
  291. }
  292. /* reflection coefficient */
  293. km = t / nrg;
  294. /* residual energy */
  295. nrg -= km * t;
  296. nrg = SKP_max(min_nrg, nrg);
  297. mHalf = m >> 1;
  298. for( i = 0; i < mHalf; i++ ) {
  299. Atmp1 = A[ i ];
  300. Atmp2 = A[ m - i - 1 ];
  301. A[ m - i - 1 ] -= km * Atmp1;
  302. A[ i ] -= km * Atmp2;
  303. }
  304. if( m & 1 ) {
  305. A[ mHalf ] -= km * A[ mHalf ];
  306. }
  307. A[ m ] = km;
  308. }
  309. /* return the residual energy */
  310. return nrg;
  311. }
  312. /* Chirp (bw expand) LP AR filter */
  313. void Bwexpander(
  314. SKP_float *ar, /* io AR filter to be expanded (without leading 1) */
  315. const SKP_int d, /* i length of ar */
  316. const SKP_float chirp /* i chirp factor (typically in range (0..1) ) */
  317. )
  318. {
  319. SKP_int i;
  320. SKP_float cfac = chirp;
  321. for( i = 0; i < d - 1; i++ ) {
  322. ar[ i ] *= cfac;
  323. cfac *= chirp;
  324. }
  325. ar[ d - 1 ] *= cfac;
  326. }