hc
2024-08-12 233ab1bd4c5697f5cdec94e60206e8c6ac609b4c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <signal.h>
#include <pthread.h>
#include <fcntl.h>
#include <linux/input.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
 
#include "record_test.h"  
#define LOG_TAG "i2c_controller"
//#define printf(format, args...) fprintf (stderr, format, args)
 
#define        THRES_REC_FREQ    (20000)
#define     THRES_REC_MAX    (32000)
#define     THRES_REC_MIN    (1000)
#define     THRES_REC_PASS_RATE    (60)
#define     THRES_REC_TEST_TIME    (3)
// Ô­Ê¼ÒôƵÔÚÇóSum(xi^2)ʱ¿ØÖÆÔÚunsigned int32·¶Î§ÄÚµÄ×óÒÆÖµ
#define ORIGINAL_AUDIO_LEFTNUM        (13)   
// ¶ÔÓÚ¼µÄÒôƵ£¬ÔÚORIGINAL_AUDIO_LEFTNUM µÄ»ù´¡ÉÏÔö¼ÓµÄ×óÒÆÖµ
#define LEFT_SHIFT                    (1)     
// Â¼ÒôÒôƵÔÚÇóSum(xi^2)ʱ¿ØÖÆÔÚunsigned int32·¶Î§ÄÚµÄ×óÒÆÖµ
#define RECORD_AUDIO_LEFTNUM          (ORIGINAL_AUDIO_LEFTNUM +LEFT_SHIFT ) 
// Sum(xi*yj)¿ØÖÆÔÚint32·¶Î§ÄÚµÄ×óÒÆÖµ
#define MOLE_LEFTNUM                  (RECORD_AUDIO_LEFTNUM + 1) 
 
static int check_index(int signal, int input[], int input_length);
static int check_agreement(int signal, int input[], int input_length);
 
//static FILE * fp_in;
//static FILE * fp_out[12];
static int index_result;
static int agreement_result;
 
static short rec_400hz[] =
{
   0x0001,0x071B,0x0E0A,0x149E,0x1AB2,0x201D,0x24BE,0x2877,0x2B32,0x2CDB,
   0x2D6B,0x2CDC,0x2B32,0x2878,0x24BE,0x201E,0x1AB2,0x149E,0x0E08,0x071B,
   0x0000,0xF8E6,0xF1F7,0xEB62,0xE54E,0xDFE3,0xDB41,0xD789,0xD4CE,0xD325,
   0xD296,0xD324,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54E,0xEB62,0xF1F7,0xF8E5,
   0xFFFF,0x071B,0x0E08,0x149E,0x1AB2,0x201D,0x24BE,0x2877,0x2B32,0x2CDC,
   0x2D6B,0x2CDC,0x2B31,0x2878,0x24BE,0x201D,0x1AB3,0x149E,0x0E09,0x071B,
   0x0000,0xF8E5,0xF1F7,0xEB62,0xE54E,0xDFE3,0xDB43,0xD788,0xD4CE,0xD324,
   0xD295,0xD324,0xD4CE,0xD789,0xDB42,0xDFE3,0xE54F,0xEB61,0xF1F8,0xF8E5,
   0x0001,0x071B,0x0E09,0x149E,0x1AB2,0x201D,0x24BD,0x2878,0x2B32,0x2CDC,
   0x2D6B,0x2CDB,0x2B32,0x2876,0x24BE,0x201D,0x1AB2,0x149E,0x0E09,0x071B,
   0x0000,0xF8E5,0xF1F7,0xEB61,0xE54E,0xDFE2,0xDB42,0xD788,0xD4CF,0xD325,
   0xD295,0xD324,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E5,
   0x0000,0x071B,0x0E08,0x149E,0x1AB3,0x201E,0x24BE,0x2877,0x2B32,0x2CDB,
   0x2D6B,0x2CDB,0x2B31,0x2878,0x24BE,0x201D,0x1AB2,0x149E,0x0E08,0x071B,
   0x0000,0xF8E5,0xF1F7,0xEB62,0xE54E,0xDFE2,0xDB42,0xD789,0xD4CF,0xD325,
   0xD295,0xD324,0xD4CE,0xD788,0xDB41,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E6,
   0xFFFF,0x071B,0x0E09,0x149E,0x1AB2,0x201E,0x24BD,0x2877,0x2B32,0x2CDB,
   0x2D6B,0x2CDB,0x2B31,0x2877,0x24BF,0x201E,0x1AB1,0x149D,0x0E09,0x071B,
   0x0000,0xF8E6,0xF1F7,0xEB62,0xE54E,0xDFE2,0xDB42,0xD789,0xD4CE,0xD325,
   0xD296,0xD325,0xD4CE,0xD788,0xDB42,0xDFE3,0xE54E,0xEB61,0xF1F7,0xF8E5,
   0x0000,0x071A,0x0E09,0x149F,0x1AB1,0x201C,0x24BE,0x2877,0x2B31,0x2CDC,
   0x2D6B,0x2CDB,0x2B32,0x2877,0x24BD,0x201D,0x1AB3,0x149E,0x0E09,0x071B,
   0x0000,0xF8E5,0xF1F8,0xEB61,0xE54E,0xDFE3,0xDB42,0xD789,0xD4CE,0xD325,
   0xD295,0xD325,0xD4CF,0xD788,0xDB42,0xDFE3,0xE54F,0xEB62,0xF1F7,0xF8E5,
   0x0000,0x071B,0x0E08,0x149E,0x1AB2,0x201C,0x24BE,0x2877,0x2B31,0x2CDC,
   0x2D6A,0x2CDB,0x2B32,0x2877,0x24BE,0x201D
};
 
 
static short rec_word_mark[] = //  1000hz 48k rate
{
   0x013B,0x0695,0x0E9E,0x17D9,0x209A,0x27CD,0x2D72,0x3227,0x3682,0x3A8B,
   0x3DCB,0x3FAD,0x3FFF,0x3F06,0x3D28,0x3A9F,0x3747,0x32DC,0x2D48,0x26BB,
   0x1F9A,0x182F,0x1087,0x0887,0x002F,0xF7AD,0xEF56,0xE771,0xE023,0xD95C,
   0xD319,0xCD75,0xC8B0,0xC4FD,0xC26E,0xC0EF,0xC068,0xC0DB,0xC25D,0xC503,
   0xC8CA,0xCD87,0xD30D,0xD942,0xE012,0xE784,0xEF77,0xF7BF,0x0018,0x085C,
   0x1074,0x184D,0x1FCF,0x26D4,0x2D21,0x3299,0x3727,0x3ACC,0x3D81,0x3F32,
   0x3FC6,0x3F38,0x3D87,0x3ACE,0x3724,0x3290,0x2D1C,0x26D5,0x1FD9,0x1854,
   0x1071,0x0853,0x000D,0xF7B9,0xEF82,0xE795,0xE019,0xD937,0xD2FA,0xCD7C,
   0xC8D3,0xC518,0xC26A,0xC0D2,0xC04F,0xC0DE,0xC276,0xC51B,0xC8C9,0xCD72,
   0xD2F7,0xD93F,0xE027,0xE799,0xEF7B,0xF7AD,0x0005,0x0857,0x1080,0x185E,
   0x1FD4,0x26C7,0x2D12,0x3293,0x3731,0x3ADD,0x3D86,0x3F29,0x3FB9,0x3F32,
   0x3D8F,0x3ADD,0x372C,0x3289,0x2D0F,0x26CC,0x1FE0,0x1860,0x107A,0x084D,
   0xFFFF,0xF7B3,0xEF85,0xE79F,0xE01F,0xD932,0xD2F3,0xCD77,0xC8D5,0xC523,
   0xC271,0xC0D2,0xC049,0xC0D6,0xC276,0xC523,0xC8D1,0xCD71,0xD2F2,0xD939,
   0xE025,0xE79E,0xEF82,0xF7AC,0x0000,0x0851,0x107E,0x1864,0x1FDA,0x26C9,
   0x2D0D,0x328E,0x3730,0x3ADF,0x3D8C,0x3F2B,0x3FB8,0x3F2E,0x3D8B,0x3ADE,
   0x372E,0x328A,0x2D0B,0x26CA,0x1FDD,0x1863,0x107D,0x084F,0x0000,0xF7AF,
   0xEF83,0xE79D,0xE024,0xD934,0xD2F2,0xCD73,0xC8D2,0xC523,0xC274,0xC0D4,
   0xC048,0xC0D5,0xC277,0xC522,0xC8D3,0xCD72,0xD2F2,0xD936,0xE023,0xE79E,
   0xEF83,0xF7AF,0x0000,0x0851,0x107D,0x1863,0x1FDD,0x26CA,0x2D0F,0x328C,
   0x372E,0x3ADF,0x3D8D,0x3F2C,0x3FB6,0x3F2B,0x3D8C,0x3ADD,0x372E,0x328C,
   0x2D0D,0x26C9,0x1FDC,0x1862,0x107E,0x0853,0x0002,0xF7AE,0xEF83,0xE79D,
   0xE025,0xD936,0xD2F2,0xCD74,0xC8D4,0xC523,0xC275,0xC0D5,0xC049,0xC0D4,
   0xC273,0xC522,0xC8D3,0xCD73,0xD2F3,0xD936,0xE023,0xE79C,0xEF80,0xF7AF,
   0x0000,0x0851,0x107F,0x1860,0x1FDC,0x26CB,0x2D0E,0x328D,0x372E,0x3ADE,
   0x3D8B,0x3F2C,0x3FB8,0x3F2B,0x3D8D,0x3ADF
};
 
/* Q15,Çó½â1/sqrt(x).ÆäÖÐx·¶Î§Îª[0.25,1),±íÊÇ10bitµÄ */
const unsigned short g_s16Table_sqrt_10Bit[769] = {
65535,65408,65281,65155,65030,64905,64781,64658,64535,64414,64292,64172,64052,63933,63814,63696,63579,63463,63347,63232,
63117,63003,62889,62777,62664,62553,62442,62331,62222,62112,62004,61895,61788,61681,61575,61469,61363,61258,61154,61050,
60947,60845,60742,60641,60540,60439,60339,60239,60140,60041,59943,59845,59748,59651,59555,59459,59364,59269,59175,59081,
58987,58894,58801,58709,58617,58526,58435,58344,58254,58165,58075,57986,57898,57810,57722,57635,57548,57462,57376,57290,
57205,57120,57035,56951,56867,56784,56700,56618,56535,56453,56372,56291,56210,56129,56049,55969,55889,55810,55731,55653,
55574,55497,55419,55342,55265,55188,55112,55036,54960,54885,54810,54735,54661,54587,54513,54439,54366,54293,54221,54148,
54076,54004,53933,53862,53791,53720,53650,53580,53510,53440,53371,53302,53233,53165,53097,53029,52961,52894,52826,52760,
52693,52627,52560,52494,52429,52363,52298,52233,52169,52104,52040,51976,51912,51849,51785,51722,51660,51597,51535,51473,
51411,51349,51288,51226,51165,51104,51044,50984,50923,50863,50804,50744,50685,50626,50567,50508,50450,50391,50333,50275,
50218,50160,50103,50046,49989,49932,49876,49819,49763,49707,49652,49596,49541,49485,49430,49376,49321,49266,49212,49158,
49104,49050,48997,48943,48890,48837,48784,48731,48679,48627,48574,48522,48470,48419,48367,48316,48265,48214,48163,48112,
48061,48011,47961,47911,47861,47811,47761,47712,47663,47613,47564,47516,47467,47418,47370,47322,47273,47225,47178,47130,
47082,47035,46988,46941,46894,46847,46800,46754,46707,46661,46615,46569,46523,46477,46432,46386,46341,46296,46251,46206,
46161,46116,46072,46027,45983,45939,45895,45851,45807,45764,45720,45677,45633,45590,45547,45504,45462,45419,45376,45334,
45292,45249,45207,45165,45124,45082,45040,44999,44957,44916,44875,44834,44793,44752,44711,44671,44630,44590,44550,44510,
44470,44430,44390,44350,44310,44271,44232,44192,44153,44114,44075,44036,43997,43959,43920,43882,43843,43805,43767,43729,
43691,43653,43615,43577,43540,43502,43465,43428,43390,43353,43316,43279,43243,43206,43169,43133,43096,43060,43024,42987,
42951,42915,42879,42844,42808,42772,42737,42701,42666,42631,42595,42560,42525,42490,42456,42421,42386,42352,42317,42283,
42248,42214,42180,42146,42112,42078,42044,42010,41977,41943,41910,41876,41843,41809,41776,41743,41710,41677,41644,41611,
41579,41546,41514,41481,41449,41416,41384,41352,41320,41288,41256,41224,41192,41160,41129,41097,41065,41034,41003,40971,
40940,40909,40878,40847,40816,40785,40754,40723,40693,40662,40631,40601,40571,40540,40510,40480,40450,40420,40390,40360,
40330,40300,40270,40241,40211,40182,40152,40123,40093,40064,40035,40006,39977,39948,39919,39890,39861,39832,39803,39775,
39746,39718,39689,39661,39632,39604,39576,39548,39520,39492,39464,39436,39408,39380,39352,39325,39297,39269,39242,39215,
39187,39160,39133,39105,39078,39051,39024,38997,38970,38943,38916,38890,38863,38836,38810,38783,38756,38730,38704,38677,
38651,38625,38599,38572,38546,38520,38494,38469,38443,38417,38391,38365,38340,38314,38289,38263,38238,38212,38187,38162,
38136,38111,38086,38061,38036,38011,37986,37961,37936,37911,37887,37862,37837,37813,37788,37764,37739,37715,37690,37666,
37642,37617,37593,37569,37545,37521,37497,37473,37449,37425,37401,37378,37354,37330,37307,37283,37260,37236,37213,37189,
37166,37142,37119,37096,37073,37050,37027,37003,36980,36957,36935,36912,36889,36866,36843,36820,36798,36775,36753,36730,
36708,36685,36663,36640,36618,36596,36573,36551,36529,36507,36485,36463,36441,36419,36397,36375,36353,36331,36309,36287,
36266,36244,36222,36201,36179,36158,36136,36115,36093,36072,36051,36029,36008,35987,35966,35945,35924,35903,35882,35861,
35840,35819,35798,35777,35756,35735,35715,35694,35673,35653,35632,35612,35591,35571,35550,35530,35509,35489,35469,35448,
35428,35408,35388,35368,35347,35327,35307,35287,35267,35247,35228,35208,35188,35168,35148,35129,35109,35089,35070,35050,
35030,35011,34991,34972,34953,34933,34914,34894,34875,34856,34837,34817,34798,34779,34760,34741,34722,34703,34684,34665,
34646,34627,34608,34589,34571,34552,34533,34514,34496,34477,34458,34440,34421,34403,34384,34366,34347,34329,34310,34292,
34237,34219,34201,34183,34164,34146,34128,34110,34092,34074,34056,34038,34020,34002,33985,33967,33949,33931,33913,33896,
33878,33860,33843,33825,33807,33790,33772,33755,33737,33720,33703,33685,33668,33650,33633,33616,33599,33581,33564,33547,
33530,33513,33496,33478,33461,33444,33427,33410,33393,33377,33360,33343,33326,33309,33292,33276,33259,33242,33225,33209,
33192,33175,33159,33142,33126,33109,33093,33076,33060,33043,33027,33011,32994,32978,32962,32945,32929,32913,32897,32881,
32864,32848,32832,32816,32800,32784
};
 
const int g_mic_table[5] = {
7,1,8,2,9    
};
 
unsigned int I32MultI16(unsigned int x, unsigned short y)
{
   unsigned int iSum = 0;
   unsigned int tmp;
 
   iSum = iSum + (x>>16)*(y);
   tmp = x&0xFFFF;
   tmp = tmp*y;
   tmp = tmp>>16;
   iSum = iSum + tmp;
   return iSum;
}
 
unsigned int I32MultI32(unsigned int x,unsigned int y)
{
   unsigned int iSum = 0;
   unsigned int tmpy,tmpx,tmp;
   iSum = iSum + (x>>16)*(y>>16);
 
   tmpy = y&0xFFFF;
   tmpx = x>>16;
   tmp = (tmpx*tmpy)>>16;
   iSum = iSum + (tmp);
 
   tmpx = x&0xFFFF;
   tmpy = y>>16;
   tmp = (tmpx*tmpy)>>16;
   iSum = iSum + tmp;
   return iSum;
}
 
// ²é±í
unsigned short GetDenomValueByTable(unsigned int s32ParamB, unsigned int s32ParamC, short * psQ)
{
   unsigned int i32Result = 0;
   short sindex = 0;
   unsigned int i32Denom;
   unsigned short sValue;      
 
   short s16Q = 0;
 
   i32Denom = I32MultI32(s32ParamB,s32ParamC);
 
#if FLOAT_FLAG
   double x =  1.0 /sqrt((double)i32Denom);
#endif
 
   if(0 == i32Denom){
       ++i32Denom;
   }
 
   if (!(i32Denom & 0xFFFF0000))
   {
       /* All of first 16 bits are zero */
       i32Denom <<= 16;
       s16Q = 8;
   }
   if (!(i32Denom & 0xFF000000))
   {
       /* All of first 8 bits are zero */
       i32Denom <<= 8;
       s16Q += 4;
   }
   if (!(i32Denom & 0xF0000000))
   {
       /* All of first 4 bits are zero */
       i32Denom <<= 4;
       s16Q += 2;
   }
   if (!(i32Denom & 0xC0000000))
   {
       /* All of first 2 bits are zero */
       i32Denom <<= 2;
       s16Q += 1;
   }
 
   
   //s32ParamB -= 0x80000000;
   i32Denom -= 0x3FE00001;
 
   sindex = i32Denom >> 22;
 
   sValue = g_s16Table_sqrt_10Bit[sindex];
 
   /****************************************/
#if LOG_FLAG
   double tx = 1.0*32768*32768*2/(1<<s16Q);
   double tmp = x*tx;
   assert(fabs(tmp-sValue) < 60);
#endif
   /********************************************/
 
   *psQ = s16Q;
   return sValue;
}
 
/* »ñÈ¡·ÖĸÖÐsum(xi^2)ºÍsum(yj^2)µÄÖµ */
unsigned int GetDenomValue(short * px, int inLen, short nQ)
{
   unsigned int iSum = 0;
   int k;
   for( k = 0; k < inLen; k++)
   {
       iSum += (px[k]*px[k])>>nQ;
   }
   
   return iSum;
}
 
 
/* ÇóÈ¡s32ParamA/s32ParamB */  
unsigned int CalcInverse(unsigned int s32ParamA,unsigned short s32ParamB, short s16QB)
{
   unsigned int i32Result;
   if( 0 == s32ParamA)
   {
       return 0;
   }
 
   i32Result = I32MultI16(s32ParamA, s32ParamB);
   return i32Result;
}
 
int xcorr(short* px, short* py, int inLen,unsigned short us16Thresh)
{
   int nOverLapLen;                  // Öغϳ¤¶È
   int x,y;
 
   int i32CrossCorr;                 // »¥Ïà¹ØÖµ
 
   int k;
   int index = 0;
 
   short  nQx, nQy, nQxy;            // sum(xi*yj), sum(xi^2), sum(yj^2)µÄ×óÒÆÖµ
 
   unsigned int i32Denomx,i32Denomy; // sum(xi^2), sum(yj^2) 
   unsigned short s16Denom;          // 1/sqrt(sum(xi^2)*sum(yj^2)) Í¨¹ý²é±í»ñµÃ
   short  nDQ;                       // sum(xi^2), sum(yj^2)  QÖµ
 
   unsigned int i32Result;
 
   short nQ;
   unsigned short sqrt_2 = 46341;   // Q15
   /* Çósum(xi*yj), sum(xi^2), sum(yj^2)µÄ×óÒÆÖµ */
   //GetQ(px,py,inLen,&nQxy,&nQx,&nQy);
   nQx = ORIGINAL_AUDIO_LEFTNUM;
   nQy = RECORD_AUDIO_LEFTNUM;
   nQxy = MOLE_LEFTNUM;
   if(0 != (nQx+nQy)%2)
   {
       nQ = (nQx+nQy+1)/2 - nQxy;
   }
   else
   {
       nQ = (nQx+nQy)/2 - nQxy;
   }
   /* Çósum(xi*yj), sum(xi^2) */
   i32Denomx = GetDenomValue(px,inLen,nQx);
   i32Denomy = GetDenomValue(py,inLen,nQy);
 
   /* ²é±íÇó1/sqrt(sum(yj^2)*sum(xi^2))ºÍ¶ÔÓ¦µÄQÖµ */
   s16Denom = GetDenomValueByTable(i32Denomx,i32Denomy,&nDQ);
 
   /* ¼ÆËãsum(xi*yj) */
   index = 0;
   /* Öغϳ¤¶È´Ó1 --> inLen*/
   for(nOverLapLen = 1; nOverLapLen <= inLen; nOverLapLen++ )
   {
       i32CrossCorr = 0;
       for(k = 0;k < nOverLapLen;k++)
       {
           x = px[k];
           y = py[inLen-nOverLapLen+k];
 
           i32CrossCorr += ((x*y)>>nQxy);
       }    
       if(i32CrossCorr < 0)  /* ¸ºÊýÈ«²¿¹éÁã */
       {
           i32CrossCorr = 0;
           i32Result = 0;
           continue;
       }
 
       i32Result = CalcInverse(i32CrossCorr,s16Denom,nDQ);
       if(0 == (nQx+nQy)%2)
       {            
           i32Result = (i32Result >>(16-nDQ+nQ));
       }
       else
       {
           i32Result = (I32MultI16(i32Result,sqrt_2) >> (15-nDQ+nQ));
       }
       if(i32Result > 32767)
           i32Result  =  32767;
 
       if(i32Result>us16Thresh)
       {
           return i32Result;
       }
 
   }
   /* Öغϳ¤¶È´ÓinLen-1 --> 1 */
   for(nOverLapLen = inLen-1;nOverLapLen>=1;nOverLapLen--)
   {
       i32CrossCorr = 0;
       for(k = 0;k < nOverLapLen;k++)
       {
           y = py[k];
           x = px[inLen-nOverLapLen+k];
           i32CrossCorr += ((x*y)>>nQxy);
       }
 
       if(i32CrossCorr < 0)  /* ¸ºÊýÈ«²¿¹éÁã */
       {
           i32CrossCorr = 0;
           i32Result = 0;    
           continue;
       }    
 
       i32Result = CalcInverse(i32CrossCorr,s16Denom,nDQ);
       if(0 == (nQx+nQy)%2)
       {            
           i32Result = (i32Result >> (16-nDQ+nQ));
       }
       else
       {
           i32Result = (I32MultI16(i32Result,sqrt_2) >> (15-nDQ+nQ));
       }
       if(i32Result > 32767)
           i32Result  =  32767;
 
       if(i32Result>us16Thresh)
       {
           return i32Result;
       }
   }
   
   return i32Result;
}
 
int CheckMicTable(int num)
{
   int ret = 0, j = 0;
   for(j = 0; j < 5; j++)
   {
       if(num == g_mic_table[j])
       {
           return j + 1;
       }
   }
    return ret;
}
 
int* recordTestWr(int audio_data[], int audio_length)
{    
   int *buf;  
   static int ccids[13];
    
   int signal = 0;
   int i = 0, ret = 0;
   int rlength = audio_length / 10;
   int pcmIndex[12];
   int *pcmInput[12];
 
   printf("start to check audio , recordtest!!!\n");
   buf = audio_data;
   memset(pcmIndex, 0, sizeof(pcmIndex));
   memset(ccids, 0, sizeof(ccids));
   if(NULL == buf) 
   {  
       return NULL;
   }
   
   for(i = 0; i <12; i++)
   {
       pcmInput[i] = malloc(sizeof(int) * rlength);
       if(pcmInput[i] == NULL)
       {
           printf("pcmInput malloc failed \n");
           return NULL;
       }
   }
   
   printf("split audio begin\n");
   while(i < audio_length)
   {
        signal = (buf[i]>>8)&15;
       if(signal > 0 && signal <= 12)
        {
           signal--;
           if(pcmIndex[signal] < rlength)
           {    
                *(pcmInput[signal] + pcmIndex[signal]) = buf[i];
               pcmIndex[signal] ++;
           }    
        }
       else 
       {
            printf("signal : %d , less than 1 or larger than 12 \n", signal);
       }    
       i++;
   }
   printf("split audio success\n");
 
   // ¼ì²é±êºÅ
   for(i = 0; i < 12; i++)
   {
       ret = check_index(i, pcmInput[i], pcmIndex[i]);
       if(ret < 0)
       {
           printf("check index fail\n");
           if(ret != 0) 
           {
               ccids[i] = 1;
           }    
       }
    }
 
    // ¼ì²éÒ»ÖÂÐÔ
   for(i = 0; i < 12; i++)
   {
       ret = check_agreement(i, pcmInput[i], pcmIndex[i]);
       if(ret < 0)
       {
           
           if(ret != 0) 
           {
               printf("check agreement fail\n");
               ccids[i] = 1;
           }else {
               printf("check agreement success\n");
           }    
               
       }
   }
 
   for(i = 0; i <12; i++)
   {
       free(pcmInput[i]);
   }
 
   return ccids;
}
 
// ¼ì²éÒôƵÎļþÖбêºÅ
static int check_index(int signal, int input[], int input_length)
{
   int i = 0, j =0;
   int buf;
   int channel_index, first_index;
 
   // Ìø¹ý×î³õµÄ20¸ö×Ö½Ú
   i = 20;
   first_index = (input[i] >> 8) & 0xf;
   i++;
   while(i < input_length)
   {
       channel_index = (input[i] >> 8) & 0xf;
       if(channel_index != first_index)
       {
           printf("check index fail, lost data, file index is %d, first_index is %d, channel_index is %d", signal, first_index, channel_index);
           return -12;
       }
       i++;
   }
 
   printf("check index success, channel_index is %d\n", channel_index);
 
   return 0;
}
 
// ¼ì²éÒ»ÖÂÐÔ
static int check_agreement(int signal, int input[], int input_length)
{
   int i = 0, j = 0, offset = 0;
   printf("start to check agreement, signal is %d\n", signal);
   if(input == NULL || input_length < 40000)
   {
       printf("error in input pcm, content : %d , length less than one frame\n", input_length);
       return -21;
   }
 
   int buf[256]; // ÒôƵÎļþÖеÄԭʼÊý¾Ý
   short buf_shift[256]; // ÒÆÎ»ÒÔºóµÄÊý¾Ý
   unsigned long pass_count = 0; // Í¨¹ýµÄ´ÎÊý
   unsigned long total_count = 0; // ×ܵĴÎÊý
   int pass_rate = 0;
   int first_index = 0;
 
   // Ìø¹ý×î³õµÄ20¸ö×Ö½Ú
   i = 20;
 
   // ¶ÁÈ¡ÎļþµÄµÚÒ»¸ösample£¬È¡Æäindex×îΪ±È½ÏµÄ±ê×¼
   //fread(&first_index_in_file, sizeof(int), 1, fp_out[file_index]);
    first_index = input[i];
    i ++;
   first_index = (first_index >> 8) & 0xf;
   //if(first_index == 0x06)
   //{
   //    printf("check pass");
   //    return 0;
   //}
   
   //fseek(fp_out[file_index], 64000, SEEK_SET); // Ìø¹ý¿ªÊ¼µÄ1s
   //fseek(fp_out[file_index], 10240, SEEK_SET); //Ìø¹ý0.08s
    i = i + 10240;
 
    while( i < input_length && input_length - i > 256)
    {
        total_count++;
       for(j = i, offset = 0; offset < 256; j++, offset++)
       {
           buf_shift[offset] = (input[j] >> 16) & 0xffff;
       }
        i = i + 256;
       
       if(xcorr(buf_shift, rec_400hz, 256, 20480) >= THRES_REC_FREQ)
       {
           printf("check agreemnt, %d success\n", pass_count);
           pass_count++;            
       }else {
           printf("check agreemnt, %d failed\n", pass_count);
       }
    }    
 
   pass_rate = pass_count * 100 / total_count;
   printf("signal : %d, pass_count is %d, total_count is %d, pass_rate is %d\n", signal, pass_count, total_count, pass_rate);
   if(pass_rate < THRES_REC_PASS_RATE)
   {
       //printf("check agreement fail");
       return -1;
   }
   return 0;
}