comparison homing.c @ 0:9008dbc8ca74

import original C code from GSM 06.06
author Mychaela Falconia <falcon@freecalypso.org>
date Fri, 14 Jun 2024 23:27:16 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9008dbc8ca74
1 /**************************************************************************
2 *
3 * File Name: homing.c
4 *
5 * Purpose:
6 * This file contains the following functions:
7 *
8 * decoderHomingFrameTest() - checks if a frame of input speech
9 * parameters matches the Decoder Homing
10 * Frame pattern.
11 *
12 * decoderReset() - called by resetDec() to reset all of the state
13 * variables for the decoder
14 *
15 * encoderHomingFrameTest() - checks if a frame of input samples
16 * matches the Encoder Homing Frame pattern.
17 *
18 * encoderReset() - called by resetEnc() to reset all the state
19 * variables for the encoder.
20 *
21 * resetDec() - calls functions to reset the state variables for the
22 * decoder, and for the receive DTX and Comfort Noise.
23 *
24 * resetEnc() - calls functions to reset the state variables for the
25 * encoder and VAD, and for the transmit DTX and
26 * Comfort Noise.
27 *
28 * dtxResetTx() - called by resetEnc() to reset all of the transmit
29 * DTX and Comfort Noise state variables
30 *
31 * dtxResetRx() - called by resetDec() to reset all of the receive
32 * DTX and Comfort Noise state variables
33 *
34 **************************************************************************/
35
36 /*_________________________________________________________________________
37 | |
38 | Include Files |
39 |_________________________________________________________________________|
40 */
41
42 #include "typedefs.h"
43 #include "vad.h"
44 #include "dtx.h"
45 #include "homing.h"
46
47 /*_________________________________________________________________________
48 | |
49 | Local Defines |
50 |_________________________________________________________________________|
51 */
52
53 #define EHF_MASK 0x0008 /* Encoder Homing Frame pattern */
54 #define LMAX 142 /* largest lag (integer sense) */
55 #define CG_INT_MACS 6 /* Number of multiply-accumulates in
56 * one interpolation */
57 #define NUM_CLOSED 3 /* maximum number of lags searched */
58 #define LPCSTARTINDEX 25 /* where the LPC analysis window
59 * starts */
60 #define INBUFFSZ LPCSTARTINDEX + A_LEN /* input buffer size */
61
62
63 #define LTP_LEN 147 /* maximum ltp lag */
64 #define LSMAX (LMAX + CG_INT_MACS/2)
65 #define HNW_BUFF_LEN LSMAX
66
67
68 /***************************************************************************
69 *
70 * FUNCTION NAME: decoderHomingFrameTest
71 *
72 * PURPOSE:
73 * Checks if a frame of input speech parameters matches the Decoder
74 * Homing Frame pattern, which is:
75 *
76 * parameter decimal value hexidecimal value
77 * --------- ------------- -----------------
78 * R0 0 0x0000
79 * LPC1 881 0x0371
80 * LPC2 350 0x015E
81 * LPC3 195 0x00c3
82 * INT_LPC 1 0x0001
83 * MODE 0 0x0000
84 * CODE1_1 71 0x0047
85 * CODE2_1 74 0x004a
86 * GSP0_1 0 0x0000
87 * CODE1_2 9 0x0009
88 * CODE2_2 38 0x0026
89 * GSP0_2 7 0x0007
90 * CODE1_3 0 0x0000
91 * CODE2_3 0 0x0000
92 * GSP0_3 0 0x0000
93 * CODE1_4 0 0x0000
94 * CODE2_4 0 0x0000
95 * GSP0_4 0 0x0000
96 *
97 * INPUT:
98 * pswSpeechPara[] - one frame of speech parameters
99 * in decoder input format
100 *
101 * iLastPara - the number of consecutive parameters in
102 * pswSpeechPara[] to match.
103 *
104 * OUTPUT:
105 * None
106 *
107 * RETURN:
108 * 0 input frame does not match the Decoder Homing Frame pattern.
109 * 1 input frame matches the Decoder Homing Frame pattern.
110 *
111 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
112 *
113 * KEYWORDS:
114 * pswSpeechPara
115 **************************************************************************/
116
117 int decoderHomingFrameTest(Shortword pswSpeechPara[], int iLastPara)
118 {
119 /* the n[] array contains the number of bits in each speech parameter */
120 static int n[] = {5, 11, 9, 8, 1, 2, 7, 7, 5, 7, 7, 5, 7, 7, 5, 7, 7, 5};
121
122 static Shortword dhf_mask[] =
123 {
124 0x0000, /* R0 */
125 0x0371, /* LPC1 */
126 0x015E, /* LPC2 */
127 0x00c3, /* LPC3 */
128 0x0001, /* INT_LPC */
129 0x0000, /* MODE */
130 0x0047, /* CODE1_1 */
131 0x004a, /* CODE2_1 */
132 0x0000, /* GSP0_1 */
133 0x0009, /* CODE1_2 */
134 0x0026, /* CODE2_2 */
135 0x0007, /* GSP0_2 */
136 0x0000, /* CODE1_3 */
137 0x0000, /* CODE2_3 */
138 0x0000, /* GSP0_3 */
139 0x0000, /* CODE1_4 */
140 0x0000, /* CODE2_4 */
141 0x0000 /* GSP0_4 */
142 };
143
144 int i;
145 int j;
146
147 for (i = 0; i < iLastPara; i++)
148 {
149 j = ((pswSpeechPara[i] & ~(~0 << n[i])) ^ dhf_mask[i]);
150 if (j)
151 break;
152 }
153
154 return !j;
155 }
156
157
158 /***************************************************************************
159 *
160 * FUNCTION NAME: decoderReset
161 *
162 * PURPOSE:
163 * resets all of the state variables for the decoder
164 *
165 * INPUT:
166 * None
167 *
168 * OUTPUT:
169 * None
170 *
171 * RETURN:
172 * None
173 *
174 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
175 *
176 * KEYWORDS:
177 **************************************************************************/
178
179 void decoderReset(void)
180 {
181 /*_________________________________________________________________________
182 | |
183 | External declarations for decoder variables which need to be reset |
184 |_________________________________________________________________________|
185 */
186
187 /* variables defined in sp_dec.c */
188 /* ----------------------------- */
189
190 extern Shortword gswPostFiltAgcGain,
191 gpswPostFiltStateNum[NP],
192 gpswPostFiltStateDenom[NP],
193 swPostEmphasisState,
194 pswSynthFiltState[NP],
195 pswOldFrmKsDec[NP],
196 pswOldFrmAsDec[NP],
197 pswOldFrmPFNum[NP],
198 pswOldFrmPFDenom[NP],
199 swOldR0Dec,
200 pswLtpStateBaseDec[LTP_LEN + S_LEN],
201 pswPPreState[LTP_LEN + S_LEN];
202
203 extern Shortword swMuteFlagOld; /* error concealment */
204
205
206 /* variables defined in err_conc.c *//* error concealment */
207 /* ------------------------------- *//* error concealment */
208
209 extern Longword plSubfrEnergyMem[4]; /* error concealment */
210 extern Shortword swLevelMem[4],
211 lastR0, /* error concealment */
212 pswLastGood[18], /* error concealment */
213 swState,
214 swLastFlag; /* error concealment */
215
216 /*_________________________________________________________________________
217 | |
218 | Automatic Variables |
219 |_________________________________________________________________________|
220 */
221
222 int i;
223
224 /*_________________________________________________________________________
225 | |
226 | Executable code |
227 |_________________________________________________________________________|
228 */
229
230 /* reset all the decoder state variables */
231 /* ------------------------------------- */
232
233 swOldR0Dec = 0;
234
235 gswPostFiltAgcGain = 0;
236
237 swPostEmphasisState = 0;
238
239 for (i = 0; i < NP; i++)
240 {
241 gpswPostFiltStateNum[i] = 0;
242 gpswPostFiltStateDenom[i] = 0;
243 pswSynthFiltState[i] = 0;
244 pswOldFrmKsDec[i] = 0;
245 pswOldFrmAsDec[i] = 0;
246 pswOldFrmPFNum[i] = 0;
247 pswOldFrmPFDenom[i] = 0;
248 }
249
250 for (i = 0; i < (LTP_LEN + S_LEN); i++)
251 {
252 pswLtpStateBaseDec[i] = 0;
253 pswPPreState[i] = 0;
254 }
255
256
257 /* reset all the error concealment state variables */
258 /* ----------------------------------------------- */
259
260 swMuteFlagOld = 0;
261
262 lastR0 = 0;
263 swState = 7;
264 swLastFlag = 0;
265 for (i = 0; i < 3; i++)
266 {
267 plSubfrEnergyMem[i] = 80;
268 swLevelMem[i] = -72;
269 }
270 for (i = 0; i < 18; i++)
271 {
272 pswLastGood[i] = 0;
273 }
274
275
276 }
277
278 /***************************************************************************
279 *
280 * FUNCTION NAME: encoderHomingFrameTest
281 *
282 * PURPOSE:
283 * Checks if a frame of input samples matches the Encoder Homing Frame
284 * pattern, which is 0x0008 for all 160 samples in the frame.
285 *
286 * INPUT:
287 * pswSpeech[] one frame of speech samples
288 *
289 * OUTPUT:
290 * None
291 *
292 * RETURN:
293 * 0 input frame does not match the Encoder Homing Frame pattern.
294 * 1 input frame matches the Encoder Homing Frame pattern.
295 *
296 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
297 *
298 * KEYWORDS:
299 * pswSpeech
300 **************************************************************************/
301
302 int encoderHomingFrameTest(Shortword pswSpeech[])
303 {
304 int i;
305 Shortword j;
306
307 for (i = 0; i < F_LEN; i++)
308 {
309 j = pswSpeech[i] ^ EHF_MASK;
310 if (j)
311 break;
312 }
313
314 return !j;
315 }
316
317 /***************************************************************************
318 *
319 * FUNCTION NAME: encoderReset
320 *
321 * PURPOSE:
322 * resets all of the state variables for the encoder
323 *
324 * INPUT:
325 * None
326 *
327 * OUTPUT:
328 * None
329 *
330 * RETURN:
331 * None
332 *
333 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
334 *
335 * KEYWORDS:
336 **************************************************************************/
337
338 void encoderReset(void)
339 {
340
341 /*_________________________________________________________________________
342 | |
343 | External declarations for encoder variables which need to be reset |
344 |_________________________________________________________________________|
345 */
346
347 /* variables defined in sp_enc.c */
348 /* ----------------------------- */
349
350 extern Shortword swOldR0;
351 extern Shortword swOldR0Index;
352
353 extern struct NormSw psnsWSfrmEngSpace[];
354
355 extern Shortword pswHPFXState[];
356 extern Shortword pswHPFYState[];
357 extern Shortword pswOldFrmKs[];
358 extern Shortword pswOldFrmAs[];
359 extern Shortword pswOldFrmSNWCoefs[];
360 extern Shortword pswWgtSpeechSpace[];
361
362 extern Shortword pswSpeech[]; /* input speech */
363
364 extern Shortword swPtch;
365
366
367 /* variables defined in sp_frm.c */
368 /* ----------------------------- */
369
370 extern Shortword pswAnalysisState[NP];
371
372 extern Shortword pswWStateNum[NP],
373 pswWStateDenom[NP];
374
375
376 /* variables defined in sp_sfrm.c */
377 /* ------------------------------ */
378
379 extern Shortword pswLtpStateBase[LTP_LEN + S_LEN];
380 extern Shortword pswHState[NP];
381 extern Shortword pswHNWState[HNW_BUFF_LEN];
382
383 /*_________________________________________________________________________
384 | |
385 | Automatic Variables |
386 |_________________________________________________________________________|
387 */
388
389 int i;
390
391 /*_________________________________________________________________________
392 | |
393 | Executable code |
394 |_________________________________________________________________________|
395 */
396
397 /* reset all the encoder state variables */
398 /* ------------------------------------- */
399
400 swOldR0Index = 0;
401 swOldR0 = 0;
402
403 for (i = 0; i < 2 * N_SUB; i++)
404 {
405 psnsWSfrmEngSpace[i].man = 0;
406 psnsWSfrmEngSpace[i].sh = 0;
407 }
408
409 for (i = 0; i < 4; i++)
410 pswHPFXState[i] = 0;
411
412 for (i = 0; i < 8; i++)
413 pswHPFYState[i] = 0;
414
415 for (i = 0; i < NP; i++)
416 {
417 pswOldFrmKs[i] = 0;
418 pswOldFrmAs[i] = 0;
419 pswOldFrmSNWCoefs[i] = 0;
420 pswAnalysisState[i] = 0;
421 pswWStateNum[i] = 0;
422 pswWStateDenom[i] = 0;
423 pswHState[i] = 0;
424 }
425
426 for (i = 0; i < (F_LEN + LMAX + CG_INT_MACS / 2); i++)
427 pswWgtSpeechSpace[i] = 0;
428
429 for (i = 0; i < INBUFFSZ; i++)
430 pswSpeech[i] = 0;
431
432 for (i = 0; i < (LTP_LEN + S_LEN); i++)
433 pswLtpStateBase[i] = 0;
434
435 for (i = 0; i < HNW_BUFF_LEN; i++)
436 pswHNWState[i] = 0;
437
438 swPtch = 1;
439 }
440
441 /***************************************************************************
442 *
443 * FUNCTION NAME: resetDec
444 *
445 * PURPOSE:
446 * resets all of the state variables for the decoder, and for the
447 * receive DTX and Comfort Noise.
448 *
449 * INPUT:
450 * None
451 *
452 * OUTPUT:
453 * None
454 *
455 * RETURN:
456 * None
457 *
458 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
459 *
460 * KEYWORDS:
461 **************************************************************************/
462
463 void resetDec(void)
464 {
465 decoderReset(); /* reset all the state variables in
466 * the speech decoder */
467 dtxResetRx(); /* reset all the receive DTX and CN
468 * state variables */
469 }
470
471 /***************************************************************************
472 *
473 * FUNCTION NAME: resetEnc
474 *
475 * PURPOSE:
476 * resets all of the state variables for the encoder and VAD, and for
477 * the transmit DTX and Comfort Noise.
478 *
479 * INPUT:
480 * None
481 *
482 * OUTPUT:
483 * None
484 *
485 * RETURN:
486 * None
487 *
488 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
489 *
490 * KEYWORDS:
491 **************************************************************************/
492
493 void resetEnc(void)
494 {
495
496 encoderReset(); /* reset all the state variables in
497 * the speech encoder */
498 vad_reset(); /* reset all the VAD state variables */
499
500 dtxResetTx(); /* reset all the transmit DTX and CN
501 * state variables */
502 }
503
504 /***************************************************************************
505 *
506 * FUNCTION NAME: dtxResetTx
507 *
508 * PURPOSE:
509 * reset all the transmit DTX and CN state variables
510 *
511 * INPUT:
512 * None
513 *
514 * OUTPUT:
515 * None
516 *
517 * RETURN:
518 * None
519 *
520 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
521 *
522 * KEYWORDS:
523 **************************************************************************/
524
525 void dtxResetTx(void)
526 {
527
528 /*_________________________________________________________________________
529 | |
530 | External declarations for encoder variables which need to be reset |
531 |_________________________________________________________________________|
532 */
533
534 /* variables defined in sp_enc.c */
535 /* ----------------------------- */
536
537 extern Shortword swTxGsHistPtr;
538
539
540 /* variables defined in dtx.c */
541 /* -------------------------- */
542
543 extern Shortword swVadFrmCnt; /* Indicates the number of sequential
544 * frames where VAD == 0 */
545 extern short int siUpdPointer;
546 extern Shortword swNElapsed;
547
548 /*_________________________________________________________________________
549 | |
550 | Automatic Variables |
551 |_________________________________________________________________________|
552 */
553
554
555 /*_________________________________________________________________________
556 | |
557 | Executable code |
558 |_________________________________________________________________________|
559 */
560
561 /* reset all the transmit DTX and CN state variables */
562 /* ------------------------------------------------- */
563
564 swTxGsHistPtr = 0;
565
566 swVadFrmCnt = 0;
567
568 siUpdPointer = 0;
569
570 swNElapsed = 50;
571
572 }
573
574 /***************************************************************************
575 *
576 * FUNCTION NAME: dtxResetRx
577 *
578 * PURPOSE:
579 * reset all the receive DTX and CN state variables
580 *
581 * INPUT:
582 * None
583 *
584 * OUTPUT:
585 * None
586 *
587 * RETURN:
588 * None
589 *
590 * REFERENCES: Sub-clause 10 of GSM Recomendation 06.02
591 *
592 * KEYWORDS:
593 **************************************************************************/
594
595 void dtxResetRx(void)
596 {
597
598 /*_________________________________________________________________________
599 | |
600 | External declarations for encoder variables which need to be reset |
601 |_________________________________________________________________________|
602 */
603
604 /* variables defined in sp_dec.c */
605 /* ----------------------------- */
606
607 extern Shortword swRxDTXState;
608 extern Shortword swDecoMode;
609 extern Shortword swDtxMuting;
610 extern Shortword swDtxBfiCnt;
611
612 extern Shortword swOldR0IndexDec;
613
614 extern Shortword swRxGsHistPtr;
615 extern Longword pL_RxGsHist[(OVERHANG - 1) * N_SUB];
616
617
618 /*_________________________________________________________________________
619 | |
620 | Automatic Variables |
621 |_________________________________________________________________________|
622 */
623
624 int i;
625
626
627 /*_________________________________________________________________________
628 | |
629 | Executable code |
630 |_________________________________________________________________________|
631 */
632
633 /* reset all the receive DTX and CN state variables */
634 /* ------------------------------------------------ */
635
636 swRxDTXState = CNINTPER - 1;
637 swDecoMode = SPEECH;
638 swDtxMuting = 0;
639 swDtxBfiCnt = 0;
640
641 swOldR0IndexDec = 0;
642
643 swRxGsHistPtr = 0;
644
645 for (i = 0; i < (OVERHANG - 1) * N_SUB; i++)
646 pL_RxGsHist[i] = 0;
647
648 }