forked from osherbakov/MELPe_fxp
/
npp.c
1613 lines (1386 loc) · 57.3 KB
/
npp.c
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
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ================================================================== */
/* */
/* Microsoft Speech coder ANSI-C Source Code */
/* SC1200 1200 bps speech coder */
/* Fixed Point Implementation Version 7.0 */
/* Copyright (C) 2000, Microsoft Corp. */
/* All rights reserved. */
/* */
/* ================================================================== */
/*---------------------------------------------------------------------
* enh_fun.c - Speech Enhancement Functions
*
* Author: Rainer Martin, AT&T Labs-Research
*
* Last Update: $Id: enh_fun.c,v 1.2 1999/02/19 17:55:12 martinr Exp martinr $
*
*---------------------------------------------------------------------
*/
#include "npp.h"
#include "fs_lib.h"
#include "mat_lib.h"
#include "constant.h"
#include "dsp_sub.h"
#include "fft_lib.h"
#include "global.h"
#define X003_Q15 983 /* 0.03 * (1 << 15) */
#define X005_Q15 1638 /* 0.05 * (1 << 15) */
#define X006_Q15 1966 /* 0.06 * (1 << 15) */
#define X018_Q15 5898 /* 0.18 * (1 << 15) */
#define X065_Q15 21299 /* 0.65 * (1 << 15) */
#define X132_Q11 2703 /* 1.32 * (1 << 11) */
#define X15_Q13 12288 /* 1.5 * (1 << 13) */
#define X22_Q11 4506 /* 2.2 * (1 << 11) */
#define X44_Q11 9011 /* 4.4 * (1 << 11) */
#define X88_Q11 18022 /* 8.8 * (1 << 11) */
const static int16_t sqrt_tukey_256_180[ENH_WINLEN] = { /* Q15 */
677, 1354, 2030, 2705, 3380, 4053, 4724, 5393,
6060, 6724, 7385, 8044, 8698, 9349, 9996, 10639,
11277, 11911, 12539, 13162, 13780, 14391, 14996, 15595,
16188, 16773, 17351, 17922, 18485, 19040, 19587, 20126,
20656, 21177, 21690, 22193, 22686, 23170, 23644, 24108,
24561, 25004, 25437, 25858, 26268, 26668, 27056, 27432,
27796, 28149, 28490, 28818, 29134, 29438, 29729, 30008,
30273, 30526, 30766, 30992, 31205, 31405, 31592, 31765,
31924, 32070, 32202, 32321, 32425, 32516, 32593, 32656,
32705, 32740, 32761, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32767, 32767, 32767, 32767,
32767, 32767, 32767, 32767, 32761, 32740, 32705, 32656,
32593, 32516, 32425, 32321, 32202, 32070, 31924, 31765,
31592, 31405, 31205, 30992, 30766, 30526, 30273, 30008,
29729, 29438, 29134, 28818, 28490, 28149, 27796, 27432,
27056, 26668, 26268, 25858, 25437, 25004, 24561, 24108,
23644, 23170, 22686, 22193, 21690, 21177, 20656, 20126,
19587, 19040, 18485, 17922, 17351, 16773, 16188, 15595,
14996, 14391, 13780, 13162, 12539, 11911, 11277, 10639,
9996, 9349, 8698, 8044, 7385, 6724, 6060, 5393,
4724, 4053, 3380, 2705, 2030, 1354, 677, 0
};
/* ====== Entities from Enhanced_Data ====== */
static int16_t enh_i = 0; /* formerly D->I */
static int16_t lambdaD[ENH_VEC_LENF]; /* overestimated noise */
/* psd(noise_bias * noisespect)*/
static int16_t lambdaD_shift[ENH_VEC_LENF];
static int16_t SN_LT; /* long term SNR */
static int16_t SN_LT_shift;
static int16_t n_pwr;
static int16_t n_pwr_shift;
static int16_t YY[ENH_VEC_LENF]; /* signal periodogram of current frame */
static int16_t YY_shift[ENH_VEC_LENF];
static int16_t sm_shift[ENH_VEC_LENF];
static int16_t noise_shift[ENH_VEC_LENF];
static int16_t noise2_shift[ENH_VEC_LENF];
static int16_t av_shift[ENH_VEC_LENF];
static int16_t av2_shift[ENH_VEC_LENF];
static int16_t act_min[ENH_VEC_LENF]; /* current minimum of long window */
static int16_t act_min_shift[ENH_VEC_LENF];
static int16_t act_min_sub[ENH_VEC_LENF];
/* current minimum of sub-window */
static int16_t act_min_sub_shift[ENH_VEC_LENF];
static int16_t vk[ENH_VEC_LENF];
static int16_t vk_shift[ENH_VEC_LENF];
static int16_t ksi[ENH_VEC_LENF]; /* a priori SNR */
static int16_t ksi_shift[ENH_VEC_LENF];
static int16_t var_rel[ENH_VEC_LENF];
/* est. relative var. of smoothedspect */
static int16_t var_rel_av; /* average relative var. of smoothedspect */
/* only for minimum statistics */
static int16_t smoothedspect[ENH_VEC_LENF]; /* smoothed signal spectrum */
static int16_t var_sp_av[ENH_VEC_LENF];
/* estimated mean of smoothedspect */
static int16_t var_sp_2[ENH_VEC_LENF];
/* esitmated 2nd moment of smoothedspect */
static int16_t noisespect[ENH_VEC_LENF]; /* estimated noise psd */
static int16_t noisespect2[ENH_VEC_LENF];
static int16_t alphacorr; /* correction factor for alpha_var, Q15 */
static int16_t alpha_var[ENH_VEC_LENF]; /* variable smoothing */
/* parameter for psd */
static int16_t circb[NUM_MINWIN][ENH_VEC_LENF]; /* ring buffer */
static int16_t circb_shift[NUM_MINWIN][ENH_VEC_LENF];
static int16_t initial_noise[ENH_WINLEN]; /* look ahead noise */
/* samples (Q0) */
static int16_t speech_in_npp[ENH_WINLEN]; /* input of one frame */
static int16_t ybuf[2*ENH_WINLEN + 2];
/* buffer for FFT, this can be eliminated if */
/* we can write a better real-FFT program for DSP */
static int32_t temp_yy[ENH_WINLEN + 2];
/* ====== Prototypes ====== */
static void smoothing_win(int16_t initial_noise[]);
static void compute_qk(int16_t qk[], int16_t gamaK[],
int16_t gamaK_shift[], int16_t GammaQ_TH);
static void gain_log_mmse(int16_t qk[], int16_t Gain[],
int16_t gamaK[], int16_t gamaK_shift[],
int16_t m);
static int16_t ksi_min_adapt(BOOLEAN n_flag, int16_t ksi_min,
int16_t sn_lt, int16_t sn_lt_shift);
static void smoothed_periodogram(int16_t YY_av, int16_t yy_shift);
static void bias_compensation(int16_t biased_spect[],
int16_t bias_shift[],
int16_t biased_spect_sub[],
int16_t bias_sub_shift[]);
static int16_t noise_slope(void);
static int16_t comp_data_shift(int16_t num1, int16_t shift1,
int16_t num2, int16_t shift2);
static void min_search(int16_t biased_spect[], int16_t bias_shift[],
int16_t biased_spect_sub[],
int16_t bias_sub_shift[]);
void enh_init(void);
static void minstat_init(void);
static void process_frame(int16_t inspeech[], int16_t outspeech[]);
static void gain_mod(int16_t qk[], int16_t GainD[], int16_t m);
/****************************************************************************
**
** Function: npp
**
** Description: The noise pre-processor
**
** Arguments:
**
** int16_t sp_in[] ---- input speech data buffer (Q0)
** int16_t sp_out[] ---- output speech data buffer (Q0)
**
** Return value: None
**
*****************************************************************************/
void npp(int16_t sp_in[], int16_t sp_out[])
{
static int16_t speech_overlap[ENH_OVERLAP_LEN];
static BOOLEAN first_time = TRUE;
int16_t speech_out_npp[ENH_WINLEN]; /* output of one frame */
if (first_time){
if (rate == RATE1200)
v_equ(initial_noise, sp_in, ENH_WINLEN);
else {
v_zap(initial_noise, ENH_OVERLAP_LEN);
v_equ(&initial_noise[ENH_OVERLAP_LEN], sp_in, ENH_WIN_SHIFT);
}
enh_init(); /* Initialize enhancement routine */
v_zap(speech_in_npp, ENH_WINLEN);
first_time = FALSE;
}
/* Shift input buffer from the previous frame */
v_equ(speech_in_npp, &(speech_in_npp[ENH_WIN_SHIFT]), ENH_OVERLAP_LEN);
v_equ(&(speech_in_npp[ENH_OVERLAP_LEN]), sp_in, ENH_WIN_SHIFT);
/* ======== Process one frame ======== */
process_frame(speech_in_npp, speech_out_npp);
/* Overlap-add the output buffer. */
v_add(speech_out_npp, speech_overlap, ENH_OVERLAP_LEN);
v_equ(speech_overlap, &(speech_out_npp[ENH_WIN_SHIFT]), ENH_OVERLAP_LEN);
/* ======== Output enhanced speech ======== */
v_equ(sp_out, speech_out_npp, ENH_WIN_SHIFT);
}
/*****************************************************************************/
/* Subroutine gain_mod: compute gain modification factor based on */
/* signal absence probabilities qk */
/*****************************************************************************/
static void gain_mod(int16_t qk[], int16_t GainD[], int16_t m)
{
register int16_t i;
int16_t temp, temp1, temp2, temp3, temp4, shift;
int16_t temp_shift, temp2_shift;
int32_t L_sum, L_tmp;
for (i = 0; i < m; i++){
/* temp = 1.0 - qk[i]; */
temp = sub(ONE_Q15, qk[i]); /* Q15 */
if (temp == 0)
temp = 1;
shift = norm_s(temp);
temp = shl(temp, shift);
temp_shift = negate(shift);
/* temp2 = temp*temp; */
L_sum = L_mult(temp, temp);
shift = norm_l(L_sum);
temp2 = extract_h(L_shl(L_sum, shift));
temp2_shift = sub(shl(temp_shift, 1), shift);
/* GM[i] = temp2 / (temp2 + (temp + ksi[i])*qk[i]*exp(-vk[i])) */
/* exp(-vk) = 2^(2*vk*(-0.5*log2(e))), and -23637 is -0.5*log2(e). */
L_sum = L_mult(vk[i], -23637); /* vk*(-0.5*log2(e)), Q15 */
shift = add(vk_shift[i], 1);
L_sum = L_shr(L_sum, sub(15, shift)); /* 2^x x Q16 */
/* temp + ksi[i] */
shift = sub(ksi_shift[i], temp_shift);
if (shift > 0){
temp4 = add(ksi_shift[i], 1);
temp3 = add(shr(temp, add(shift, 1)), shr(ksi[i], 1));
} else {
temp4 = add(temp_shift, 1);
temp3 = add(shr(temp, 1), shl(ksi[i], sub(shift, 1)));
}
/* (temp + ksi[i])*qk[i] */
L_tmp = L_mult(temp3, qk[i]);
/* temp + ksi[i])*qk[i]*exp(-vk[i] */
L_sum = L_add(L_sum, L_deposit_h(temp4)); /* add exp */
shift = extract_h(L_sum); /* this is exp part */
temp4 = (int16_t) (extract_l(L_shr(L_sum, 1)) & 0x7fff);
temp1 = shr(mult(temp4, 9864), 3); /* change to base 10 Q12 */
temp1 = pow10_fxp(temp1, 14); /* out Q14 */
L_tmp = L_mpy_ls(L_tmp, temp1); /* Q30 */
temp3 = norm_l(L_tmp);
temp1 = extract_h(L_shl(L_tmp, temp3));
shift = add(shift, sub(1, temp3)); /* make L_tmp Q31 */
/* temp2 + (temp + ksi[i])*qk[i]*exp(-vk[i]) */
temp1 = shr(temp1, 1);
temp2 = shr(temp2, 1);
temp = sub(shift, temp2_shift);
if (temp > 0){
temp3 = add(temp1, shr(temp2, temp));
temp4 = shr(temp2, temp);
} else {
temp3 = add(shl(temp1, temp), temp2);
temp4 = temp2;
}
temp = divide_s(temp4, temp3); /* temp is previously known as GM[]. */
/* limit lowest GM value */
if (temp < GM_MIN)
temp = GM_MIN; /* Q15 */
GainD[i] = mult(GainD[i], temp); /* modified gain */
}
}
/*****************************************************************************/
/* Subroutine compute_qk: compute the probability of speech absence */
/* This uses an harddecision approach due to Malah (ICASSP 1999). */
/*****************************************************************************/
static void compute_qk(int16_t qk[], int16_t gamaK[],
int16_t gamaK_shift[], int16_t GammaQ_TH)
{
register int16_t i;
static BOOLEAN first_time = TRUE;
static int16_t qla[ENH_VEC_LENF];
/* Initializing qla[] */
if (first_time){
fill(qla, X05_Q15, ENH_VEC_LENF);
first_time = FALSE;
}
/* qla[] = alphaq * qla[]; */
v_scale(qla, ENH_ALPHAQ, ENH_VEC_LENF);
for (i = 0; i < ENH_VEC_LENF; i++){
/* if (gamaK[i] < GammaQ_TH) */
if (comp_data_shift(gamaK[i], gamaK_shift[i], GammaQ_TH, 0) < 0)
/* qla[] += betaq; */
qla[i] = add(qla[i], ENH_BETAQ);
}
v_equ(qk, qla, ENH_VEC_LENF);
}
/*****************************************************************************/
/* Subroutine gain_log_mmse: compute the gain factor and the auxiliary */
/* variable vk for the Ephraim&Malah 1985 log spectral estimator. */
/* Approximation of the exponential integral due to Malah, 1996 */
/*****************************************************************************/
static void gain_log_mmse(int16_t qk[], int16_t Gain[],
int16_t gamaK[], int16_t gamaK_shift[],
int16_t m)
{
register int16_t i;
int16_t ksi_vq, temp1, temp2, shift;
int32_t L_sum;
for (i = 0; i < m; i++){
/* 1.0 - qk[] */
temp1 = sub(ONE_Q15, qk[i]);
shift = norm_s(temp1);
temp1 = shl(temp1, shift);
temp2 = sub(ksi_shift[i], (int16_t) (-shift));
/* ksi[] + 1.0 - qk[] */
if (temp2 > 0){
temp1 = shr(temp1, add(temp2, 1));
temp1 = add(temp1, shr(ksi[i], 1));
temp2 = shr(ksi[i], 1);
} else {
temp1 = add(shr(temp1,1), shl(ksi[i], sub(temp2, 1)));
temp2 = shl(ksi[i], sub(temp2, 1));
}
/* ksi_vq = ksi[i] / (ksi[i] + 1.0 - qk[i]); */
ksi_vq = divide_s(temp2, temp1); /* Q15 */
/* vk[i] = ksi_vq * gamaK[i]; */
L_sum = L_mult(ksi_vq, gamaK[i]);
shift = norm_l(L_sum);
vk[i] = extract_h(L_shl(L_sum, shift));
vk_shift[i] = sub(gamaK_shift[i], shift);
/* The original floating version compares vk[] against 2^{-52} == */
/* 2.220446049250313e-16. Tian uses 32767*2^{-52} instead. */
if (comp_data_shift(vk[i], vk_shift[i], 32767, -52) < 0){
vk[i] = 32767; /* MATLAB eps */
vk_shift[i] = -52;
}
if (comp_data_shift(vk[i], vk_shift[i], 26214, -3) < 0){
/* eiv = -2.31 * log10(vk[i]) - 0.6; */
temp1 = log10_fxp(vk[i], 15); /* Q12 */
L_sum = L_shl(L_deposit_l(temp1), 14); /* Q26 */
L_sum = L_add(L_sum, L_shl(L_mult(vk_shift[i], 9864), 10));
L_sum = L_mpy_ls(L_sum, -18923);
/* -18923 = -2.31 (Q13). out Q24 */
L_sum = L_sub(L_sum, 10066330L); /* 10066330L = 0.6 (Q24), Q24 */
} else if (comp_data_shift(vk[i], vk_shift[i], 25600, 8) > 0){
/* eiv = pow(10, -0.52 * 200 - 0.26); */
L_sum = 1; /* Q24 */
/* vk[] = 200; */
vk[i] = 25600;
vk_shift[i] = 8;
} else if (comp_data_shift(vk[i], vk_shift[i], 32767, 0) > 0){
/* eiv = pow(10, -0.52 * vk[i] - 0.26); */
L_sum = L_mult(vk[i], -17039); /* -17039 == -0.52 Q15 */
L_sum = L_sub(L_sum, L_shr(L_deposit_h(8520), vk_shift[i]));
/* 8520 == 0.26 Q15 */
L_sum = L_shr(L_sum, sub(14, vk_shift[i]));
L_sum = L_mpy_ls(L_sum, 27213); /* Q15 to base 2 */
shift = extract_h(L_shl(L_sum, 1)); /* integer part */
temp1 = (int16_t) (extract_l(L_sum) & 0x7fff);
temp1 = shr(mult(temp1, 9864), 3); /* Q12 to base 10 */
temp1 = pow10_fxp(temp1, 14); /* output Q14 */
L_sum = L_shl(L_deposit_l(temp1), 10); /* Q24 now */
L_sum = L_shl(L_sum, shift); /* shift must < 0 */
} else {
/* eiv = -1.544 * log10(vk[i]) + 0.166; */
temp1 = vk[i];
if (vk_shift[i] != 0)
temp1 = shl(temp1, vk_shift[i]);
temp1 = log10_fxp(temp1, 15); /* Q12 */
L_sum = L_shl(L_deposit_l(temp1), 13); /* Q25 */
L_sum = L_mpy_ls(L_sum, -25297); /* -25297 = -1.544, Q14. Q24 */
L_sum = L_add(L_sum, 2785018L); /* 2785018L == 0.166, Q24 */
}
/* Now "eiv" is kept in L_sum. */
/* Gain[i] = ksi_vq * exp (0.5 * eiv); */
L_sum = L_mpy_ls(L_sum, 23637); /* 0.72135 to base 2 */
shift = shr(extract_h(L_sum), 8); /* get shift */
if (comp_data_shift(ksi_vq, shift, 32767, 0) > 0){
Gain[i] = 32767;
continue;
} else {
temp1 = extract_l(L_shr(L_sum, 9));
temp1 = (int16_t) (temp1 & 0x7fff);
temp1 = shr(mult(temp1, 9864), 3); /* change to base 10 Q12 */
temp1 = pow10_fxp(temp1, 14);
L_sum = L_shl(L_deposit_h(ksi_vq), shift);
L_sum = L_mpy_ls(L_sum, temp1);
if (L_sub(L_sum, 1073676288L) > 0)
Gain[i] = 32767;
else
Gain[i] = extract_h(L_shl(L_sum, 1));
}
}
}
/*****************************************************************************/
/* Subroutine ksi_min_adapt: computes the adaptive ksi_min */
/*****************************************************************************/
static int16_t ksi_min_adapt(BOOLEAN n_flag, int16_t ksi_min,
int16_t sn_lt, int16_t sn_lt_shift)
{
int16_t ksi_min_new, temp, shift;
int32_t L_sum;
if (n_flag) /* RM: adaptive modification of ksi limit (10/98) */
ksi_min_new = ksi_min;
else {
if (sn_lt_shift > 0){
L_sum = L_add(L_deposit_l(sn_lt), L_shr(X05_Q15, sn_lt_shift));
shift = sn_lt_shift;
} else {
L_sum = L_add(L_shl(L_deposit_l(sn_lt), sn_lt_shift), X05_Q15);
shift = 0;
}
/* L_sum is (sn_lt * 2^sn_lt_shift + 0.5) */
if (L_sum > ONE_Q15){
L_sum = L_shr(L_sum, 1);
shift = add(shift, 1);
}
temp = extract_l(L_sum);
/* We want to compute 2^{0.65*[log2(0.5+sn_lt)]-7.2134752} */
/* equiv. 2^{0.65*log10(temp)/log10(2) + 0.65*shift - 7.2134752} */
temp = log10_fxp(temp, 15); /* log(0.5 + sn_lt), Q12 */
L_sum = L_shr(L_mult(temp, 8844), 9); /* Q12*Q12->Q25 --> Q16 */
L_sum = L_add(L_sum, L_mult(shift, 21299)); /* 21299 = 0.65 Q15 */
L_sum = L_sub(L_sum, 472742L); /* 472742 = 7.2134752 Q16 */
shift = extract_h(L_sum); /* the pure shift */
temp = (int16_t) (extract_l(L_shr(L_sum, 1)) & 0x7fff);
temp = mult(temp, 9864); /* change to base 10 */
temp = shr(temp, 3); /* change to Q12 */
temp = pow10_fxp(temp, 14);
L_sum = L_shl(L_mult(ksi_min, temp), 1); /* Now Q31 */
temp = extract_h(L_sum);
/* if (ksi_min_new > 0.25) */
if (comp_data_shift(temp, shift, 8192, 0) > 0)
ksi_min_new = 8192;
else
ksi_min_new = shl(temp, shift);
}
return(ksi_min_new);
}
/*****************************************************************************/
/* Subroutine smoothing_win: applies the Parzen window. The window applies */
/* an inverse trapezoid window and wtr_front[] supplies the coefficients for */
/* the two edges. */
/*****************************************************************************/
static void smoothing_win(int16_t initial_noise[])
{
register int16_t i;
const static int16_t wtr_front[WTR_FRONT_LEN] = { /* Q15 */
32767, 32582, 32048, 31202, 30080, 28718, 27152, 25418,
23552, 21590, 19568, 17522, 15488, 13502, 11600, 9818,
8192, 6750, 5488, 4394, 3456, 2662, 2000, 1458,
1024, 686, 432, 250, 128, 54, 16, 2
};
for (i = 1; i < WTR_FRONT_LEN; i++)
initial_noise[i] = mult(initial_noise[i], wtr_front[i]);
for (i = ENH_WINLEN - WTR_FRONT_LEN + 1; i < ENH_WINLEN; i++)
initial_noise[i] = mult(initial_noise[i], wtr_front[ENH_WINLEN - i]);
/* Clearing the central part of initial_noise[]. */
v_zap(&(initial_noise[WTR_FRONT_LEN]), ENH_WINLEN - 2*WTR_FRONT_LEN + 1);
}
/***************************************************************************/
/* Subroutine smoothed_periodogram: compute short time psd with optimal */
/* smoothing */
/***************************************************************************/
static void smoothed_periodogram(int16_t YY_av, int16_t yy_shift)
{
register int16_t i;
int16_t smoothed_av, alphacorr_new, alpha_N_min_1, alpha_num;
int16_t smav_shift, shift, temp, temp1, tmpns, tmpalpha;
int16_t noise__shift, temp_shift, tmpns_shift, max_shift;
int32_t L_sum, L_tmp;
/* ---- compute smoothed_av ---- */
max_shift = SW_MIN;
for (i = 0; i < ENH_VEC_LENF; i++){
if (sm_shift[i] > max_shift)
max_shift = sm_shift[i];
}
L_sum = L_shl(L_deposit_l(smoothedspect[0]),
sub(7, sub(max_shift, sm_shift[0])));
L_sum = L_add(L_sum, L_shl(L_deposit_l(smoothedspect[ENH_VEC_LENF - 1]),
sub(7, sub(max_shift, sm_shift[ENH_VEC_LENF - 1]))));
for (i = 1; i < ENH_VEC_LENF - 1; i++)
L_sum = L_add(L_sum, L_shl(L_deposit_l(smoothedspect[i]),
sub(8, sub(max_shift, sm_shift[i]))));
/* Now L_sum contains smoothed_av. Here we do not multiply L_sum with */
/* win_len_inv because we do not do this on YY_av either. */
if (L_sum == 0)
L_sum = 1;
temp = norm_l(L_sum);
temp = sub(temp, 1); /* make sure smoothed_av < YY_av */
smoothed_av = extract_h(L_shl(L_sum, temp));
smav_shift = sub(add(max_shift, 1), temp);
/* ---- alphacorr_new = smoothed_av / YY_av - 1.0 ---- */
alphacorr_new = divide_s(smoothed_av, YY_av);
shift = sub(smav_shift, yy_shift);
if (shift <= 0){
if (shift > -15)
alphacorr_new = sub(shl(alphacorr_new, shift), ONE_Q15);
else
alphacorr_new = negate(ONE_Q15);
shift = 0;
} else {
if (shift < 15)
alphacorr_new = sub(alphacorr_new, shr(ONE_Q15, shift));
}
/* -- alphacorr_new = 1.0 / (1.0 + alphacorr_new * alphacorr_new) -- */
alphacorr_new = mult(alphacorr_new, alphacorr_new);
alphacorr_new = shr(alphacorr_new, 1); /* avoid overflow when +1 */
shift = shl(shift, 1);
if (shift < 15)
alphacorr_new = add(alphacorr_new, shr(ONE_Q14, shift));
if (alphacorr_new == 0)
alphacorr_new = ONE_Q15;
else {
if (shift < 15){
temp = shr(ONE_Q14, shift);
alphacorr_new = divide_s(temp, alphacorr_new);
} else /* too small */
alphacorr_new = 0;
}
/* -- alphacorr_new > 0.7 ? alphacorr_new : 0.7 -- */
if (alphacorr_new < X07_Q15)
alphacorr_new = X07_Q15;
/* -- alphacorr = 0.7*alphacorr + 0.3*alphacorr_new -- */
alphacorr = extract_h(L_add(L_mult(X07_Q15, alphacorr),
L_mult(X03_Q15, alphacorr_new)));
/* -- compute alpha_N_min_1 -- */
/* -- alpha_N_min_1 = pow(SN_LT, PDECAY_NUM) */
if (comp_data_shift(SN_LT, SN_LT_shift, 16384, 15) > 0)
L_sum = 536870912L; /* Q15 */
else if (comp_data_shift(SN_LT, SN_LT_shift, 32, 15) < 0)
L_sum = 1048576L;
else
L_sum = L_shl(L_deposit_l(SN_LT), SN_LT_shift);
temp = L_log10_fxp(L_sum, 15); /* output Q11 */
temp = shl(temp, 1); /* convert to Q12 */
temp = mult(temp, (int16_t) PDECAY_NUM);
alpha_N_min_1 = pow10_fxp(temp, 15);
/* -- alpha_N_min_1 = (0.3 < alpha_N_min_1 ? 0.3 : alpha_N_min_1) -- */
/* -- alpha_N_min_1 = (alpha_N_min_1 < 0.05 ? 0.05 : alpha_N_min_1) -- */
if (alpha_N_min_1 > X03_Q15)
alpha_N_min_1 = X03_Q15;
else if (alpha_N_min_1 < X005_Q15)
alpha_N_min_1 = X005_Q15;
/* -- alpha_num = ALPHA_N_MAX * alphacorr -- */
alpha_num = mult(ALPHA_N_MAX, alphacorr);
/* -- compute smoothed spectrum -- */
for (i = 0; i < ENH_VEC_LENF; i++){
tmpns = noisespect[i];
/* temp = smoothedspect[i] - tmpns; */
shift = sub(sm_shift[i], noise_shift[i]);
if (shift > 0){
L_tmp = L_sub(L_deposit_h(smoothedspect[i]),
L_shr(L_deposit_h(noisespect[i]), shift));
shift = sm_shift[i];
} else {
L_tmp = L_sub(L_shr(L_deposit_h(smoothedspect[i]), abs_s(shift)),
L_deposit_h(tmpns));
shift = noise_shift[i];
}
temp1 = norm_l(L_tmp);
temp = extract_h(L_shl(L_tmp, temp1));
shift = sub(shift, temp1);
/* tmpns = tmpns * tmpns; */
L_sum = L_mult(tmpns, tmpns); /* noise square, shift*2 */
noise__shift = norm_l(L_sum);
tmpns = extract_h(L_shl(L_sum, noise__shift));
tmpns_shift = sub(shl(noise_shift[i], 1), noise__shift);
noisespect2[i] = tmpns;
noise2_shift[i] = tmpns_shift;
if (temp == SW_MIN)
L_sum = 0x7fffffff;
else
L_sum = L_mult(temp, temp);
noise__shift = norm_l(L_sum);
temp = extract_h(L_shl(L_sum, noise__shift));
if (temp == 0)
temp_shift = -20;
else
temp_shift = sub(shl(shift, 1), noise__shift);
/* -- tmpalpha = alpha_num * tmpns / (tmpns + temp * temp) -- */
temp1 = sub(temp_shift, tmpns_shift);
if (temp1 > 0){
temp = shr(temp, 1);
tmpns = shr(tmpns, add(temp1, 1));
} else {
tmpns = shr(tmpns, 1);
temp = shl(temp, sub(temp1, 1));
}
tmpalpha = divide_s(tmpns, add(temp, tmpns));
tmpalpha = mult(tmpalpha, alpha_num);
/* tmpalpha = (tmpalpha < alpha_N_min_1 ? alpha_N_min_1 : tmpalpha) */
if (tmpalpha < alpha_N_min_1)
tmpalpha = alpha_N_min_1;
temp = sub(ONE_Q15, tmpalpha); /* 1 - tmpalpha */
alpha_var[i] = tmpalpha; /* save alpha */
/* smoothedspect[i] = tmpalpha * smoothedspect[i] + */
/* (1 - tmpalpha) * YY[i]; */
smav_shift = sub(YY_shift[i], sm_shift[i]);
if (smav_shift > 0){
L_sum = L_shr(L_mult(tmpalpha, smoothedspect[i]), smav_shift);
L_sum = L_add(L_sum, L_mult(temp, YY[i]));
sm_shift[i] = YY_shift[i];
} else {
L_sum = L_mult(tmpalpha, smoothedspect[i]);
L_sum = L_add(L_sum, L_shl(L_mult(temp, YY[i]), smav_shift));
}
if (L_sum < 1)
L_sum = 1;
shift = norm_l(L_sum);
smoothedspect[i] = extract_h(L_shl(L_sum, shift));
sm_shift[i] = sub(sm_shift[i], shift);
}
}
/*****************************************************************************/
/* Subroutine normalized_variance: compute variance of smoothed periodogram, */
/* normalize it, and use it to compute a biased smoothed periodogram */
/*****************************************************************************/
static void bias_compensation(int16_t biased_spect[],
int16_t bias_shift[],
int16_t biased_spect_sub[],
int16_t bias_sub_shift[])
{
register int16_t i;
int16_t tmp, tmp1, tmp2, tmp5;
int16_t beta_var, var_rel_av_sqrt;
int16_t av__shift, av2__shift, shift1, shift3, shift4;
int32_t L_max1, L_max2, L_sum, L_var_sum, L_tmp3, L_tmp4;
/* ---- compute variance of smoothed psd ---- */
L_var_sum = 0;
for (i = 0; i < ENH_VEC_LENF; i++){
/* tmp1 = alpha_var[i]*alpha_var[i]; */
/* beta_var = (tmp1 > 0.8 ? 0.8 : tmp1); */
beta_var = mult(alpha_var[i], alpha_var[i]);
if (beta_var > X08_Q15)
beta_var = X08_Q15;
/* tmp2 = (1 - beta_var) * smoothedspect[i]; */
L_sum = L_mult(sub(ONE_Q15, beta_var), smoothedspect[i]);
/* var_sp_av[i] = beta_var * var_sp_av[i] + tmp2; */
av__shift = sub(sm_shift[i], av_shift[i]);
if (av__shift > 0){
L_max1 = L_add(L_shr(L_mult(beta_var, var_sp_av[i]), av__shift),
L_sum);
av_shift[i] = sm_shift[i];
} else
L_max1 = L_add(L_mult(beta_var, var_sp_av[i]),
L_shl(L_sum, av__shift));
if (L_max1 < 1)
L_max1 = 1;
shift1 = norm_l(L_max1);
var_sp_av[i] = extract_h(L_shl(L_max1, shift1));
av_shift[i] = sub(av_shift[i], shift1);
/* tmp4 = tmp2 * smoothedspect[i]; */
/* var_sp_2[i] = beta_var * var_sp_2[i] + tmp4; */
av2__shift = sub(shl(sm_shift[i], 1), av2_shift[i]);
if (av2__shift > 0){
L_max2 = L_add(L_shr(L_mult(beta_var, var_sp_2[i]), av2__shift),
L_mpy_ls(L_sum, smoothedspect[i]));
av2_shift[i] = shl(sm_shift[i], 1);
} else
L_max2 = L_add(L_mult(beta_var, var_sp_2[i]),
L_shl(L_mpy_ls(L_sum, smoothedspect[i]),
av2__shift));
if (L_max2 < 1)
L_max2 = 1;
shift1 = norm_l(L_max2);
var_sp_2[i] = extract_h(L_shl(L_max2, shift1));
av2_shift[i] = sub(av2_shift[i], shift1);
/* tmp3 = var_sp_av[i] * var_sp_av[i]; */
L_sum = L_mult(var_sp_av[i], var_sp_av[i]);
/* var_sp = var_sp_2[i] - tmp3; */
shift3 = sub(av2_shift[i], shl(av_shift[i], 1));
if (shift3 > 0){
L_sum = L_sub(L_deposit_h(var_sp_2[i]), L_shr(L_sum, shift3));
shift4 = av2_shift[i];
} else {
L_sum = L_sub(L_shl(L_deposit_h(var_sp_2[i]), shift3), L_sum);
shift4 = shl(av_shift[i], 1);
}
shift1 = sub(norm_l(L_sum), 1);
tmp1 = extract_h(L_shl(L_sum, shift1));
tmp = sub(sub(shift4, shift1), noise2_shift[i]);
/* tmp1 = var_sp / noisespect2[i]; */
var_rel[i] = divide_s(tmp1, noisespect2[i]);
/* var_rel[i] = (tmp1 > 0.5 ? 0.5 : tmp1); */
if (comp_data_shift(var_rel[i], tmp, X05_Q15, 0) > 0)
var_rel[i] = X05_Q15;
else
var_rel[i] = shl(var_rel[i], tmp);
if (var_rel[i] < 0)
var_rel[i] = 0;
/* var_sum += var_rel[i]; */
L_var_sum = L_add(L_var_sum, L_deposit_l(var_rel[i]));
}
/* var_rel_av = (2 * var_sum - var_rel[0] - var_rel[ENH_VEC_LENF - 1]); */
L_var_sum = L_shl(L_var_sum, 1);
L_var_sum = L_sub(L_var_sum, L_deposit_l(var_rel[0]));
L_var_sum = L_sub(L_var_sum, L_deposit_l(var_rel[ENH_VEC_LENF - 1]));
var_rel_av = extract_l(L_shr(L_var_sum, 8)); /* Q15 */
/* var_rel_av = (var_rel_av < 0 ? 0 : var_rel_av); */
if (var_rel_av < 0)
var_rel_av = 0;
var_rel_av_sqrt = mult(X15_Q13, sqrt_Q15(var_rel_av)); /* Q13 */
var_rel_av_sqrt = add(ONE_Q13, var_rel_av_sqrt); /* Q13 */
/* tmp1 = var_rel_av_sqrt * Fvar; */
/* tmp2 = var_rel_av_sqrt * Fvar_sub; */
tmp1 = extract_h(L_shl(L_mult(var_rel_av_sqrt, FVAR), 1)); /* Q10 */
tmp2 = extract_h(L_shl(L_mult(var_rel_av_sqrt, FVAR_SUB), 1)); /* Q13 */
L_max1 = 0; /* for biased_spect[] */
L_max2 = 0; /* for biased_spect_sub[] */
for (i = 0; i < ENH_VEC_LENF; i++){
L_tmp3 = L_mult(var_rel_av_sqrt, smoothedspect[i]); /* Q29 */
tmp5 = var_rel[i]; /* Q15 */
L_tmp4 = L_mult(tmp5, smoothedspect[i]); /* Q31 */
/* quadratic approximation */
tmp = add(MINV, shr(tmp5, 1)); /* Q14 */
tmp = add(MINV2, shr(mult(tmp5, tmp), 1)); /* Q13 */
L_sum = L_mpy_ls(L_tmp4, tmp1); /* Q26 */
L_sum = L_mpy_ls(L_sum, tmp); /* Q24 */
L_sum = L_add(L_shr(L_tmp3, 6), L_shr(L_sum, 1)); /* Q23 */
if (L_sum < 1)
L_sum = 1;
shift1 = norm_l(L_sum);
biased_spect[i] = extract_h(L_shl(L_sum, shift1));
bias_shift[i] = add(sm_shift[i], sub(8, shift1));
tmp = add(MINV_SUB, shr(tmp5, 2)); /* Q13 */
tmp = add(MINV_SUB2, shr(mult(tmp5, tmp), 1)); /* Q12 */
L_sum = L_mpy_ls(L_tmp4, tmp2); /* Q29 */
L_sum = L_mpy_ls(L_sum, tmp); /* Q26 */
L_sum = L_add(L_shr(L_tmp3, 4), L_shr(L_sum, 1)); /* Q25 */
if (L_sum < 1)
L_sum = 1;
shift1 = norm_l(L_sum);
biased_spect_sub[i] = extract_h(L_shl(L_sum, shift1));
bias_sub_shift[i] = add(sm_shift[i], sub(6, shift1));
}
}
/***************************************************************************/
/* Subroutine noise_slope: compute maximum of the permitted increase of */
/* the noise estimate as a function of the mean signal variance */
/***************************************************************************/
static int16_t noise_slope()
{
int16_t noise_slope_max;
if (var_rel_av > X018_Q15)
noise_slope_max = X132_Q11;
else if ((var_rel_av < X003_Q15) || (enh_i < 50))
noise_slope_max = X88_Q11;
else if (var_rel_av < X005_Q15)
noise_slope_max = X44_Q11;
else if (var_rel_av < X006_Q15)
noise_slope_max = X22_Q11;
else
noise_slope_max = X132_Q11;
return(noise_slope_max);
}
/***************************************************************************/
/* Subroutine comp_data_shift: compare two block floating-point numbers. */
/* It actually compares x1 = (num1 * 2^shift1) and x2 = (num2 * 2^shift2). */
/* The sign of the returned value is the same as that of (x1 - x2). */
/***************************************************************************/
static int16_t comp_data_shift(int16_t num1, int16_t shift1,
int16_t num2, int16_t shift2)
{
int16_t shift;
if ((num1 > 0) && (num2 < 0))
return(1);
else if ((num1 < 0) && (num2 > 0))
return(-1);
else {
shift = sub(shift1, shift2);
if (shift > 0)
num2 = shr(num2, shift);
else
num1 = shl(num1, shift);
return(sub(num1, num2));
}
}
/***************************************************************************/
/* Subroutine min_search: find minimum of psd's in circular buffer */
/***************************************************************************/
static void min_search(int16_t biased_spect[], int16_t bias_shift[],
int16_t biased_spect_sub[],
int16_t bias_sub_shift[])
{
register int16_t i, k;
static int16_t localflag[ENH_VEC_LENF]; /* local minimum indicator */
static int16_t minspec_counter = 0; /* count sub-windows */
static int16_t circb_index = 0; /* ring buffer counter */
static int16_t circb_min[ENH_VEC_LENF];
/* minimum of circular buffer */
static int16_t circb_min_shift[ENH_VEC_LENF];
int16_t noise_slope_max, tmp, tmp_shift, temp1, temp2;
int32_t L_sum;
/* localflag[] is initialized to FALSE since it is static. */
if (minspec_counter == 0){
noise_slope_max = noise_slope();
for (i = 0; i < ENH_VEC_LENF; i++){
if (comp_data_shift(biased_spect[i], bias_shift[i],
act_min[i], act_min_shift[i]) < 0){
act_min[i] = biased_spect[i]; /* update minimum */
act_min_shift[i] = bias_shift[i];
act_min_sub[i] = biased_spect_sub[i];
act_min_sub_shift[i] = bias_sub_shift[i];
localflag[i] = FALSE;
}
}
/* write new minimum into ring buffer */
v_equ(circb[circb_index], act_min, ENH_VEC_LENF);
v_equ(circb_shift[circb_index], act_min_shift, ENH_VEC_LENF);
for (i = 0; i < ENH_VEC_LENF; i++){
/* Find minimum of ring buffer. Using temp1 and temp2 as cache */
/* for circb_min[i] and circb_min_shift[i]. */
temp1 = circb[0][i];
temp2 = circb_shift[0][i];
for (k = 1; k < NUM_MINWIN; k++){
if (comp_data_shift(circb[k][i], circb_shift[k][i], temp1,
temp2) < 0){
temp1 = circb[k][i];
temp2 = circb_shift[k][i];
}
}
circb_min[i] = temp1;
circb_min_shift[i] = temp2;
}
for (i = 0; i < ENH_VEC_LENF; i++){
/* rapid update in case of local minima which do not deviate
more than noise_slope_max from the current minima */
tmp = mult(noise_slope_max, circb_min[i]);
tmp_shift = add(circb_min_shift[i], 4); /* adjust for Q11 */
if (localflag[i] &&
comp_data_shift(act_min_sub[i], act_min_sub_shift[i],
circb_min[i], circb_min_shift[i]) > 0 &&
comp_data_shift(act_min_sub[i], act_min_sub_shift[i], tmp,
tmp_shift) < 0){
circb_min[i] = act_min_sub[i];
circb_min_shift[i] = act_min_sub_shift[i];
/* propagate new rapid update minimum into ring buffer */
for (k = 0; k < NUM_MINWIN; k++){
circb[k][i] = circb_min[i];
circb_shift[k][i] = circb_min_shift[i];
}
}
}
/* reset local minimum indicator */
fill(localflag, FALSE, ENH_VEC_LENF);
/* increment ring buffer pointer */
circb_index = add(circb_index, 1);
if (circb_index == NUM_MINWIN)
circb_index = 0;
} else if (minspec_counter == 1){
v_equ(act_min, biased_spect, ENH_VEC_LENF);
v_equ(act_min_shift, bias_shift, ENH_VEC_LENF);
v_equ(act_min_sub, biased_spect_sub, ENH_VEC_LENF);
v_equ(act_min_sub_shift, bias_sub_shift, ENH_VEC_LENF);
} else { /* minspec_counter > 1 */
/* At this point localflag[] is all FALSE. As we loop through */
/* minspec_counter, if any localflag[] is turned TRUE, it will be */
/* preserved until we go through the (minspec_counter == 0) branch. */
for (i = 0; i < ENH_VEC_LENF; i++){
if (comp_data_shift(biased_spect[i], bias_shift[i],
act_min[i], act_min_shift[i]) < 0){
/* update minimum */
act_min[i] = biased_spect[i];
act_min_shift[i] = bias_shift[i];
act_min_sub[i] = biased_spect_sub[i];
act_min_sub_shift[i] = bias_sub_shift[i];
localflag[i] = TRUE;
}
}
for (i = 0; i < ENH_VEC_LENF; i++){
if (comp_data_shift(act_min_sub[i], act_min_sub_shift[i],
circb_min[i], circb_min_shift[i]) < 0){
circb_min[i] = act_min_sub[i];
circb_min_shift[i] = act_min_sub_shift[i];
}
}
v_equ(noisespect, circb_min, ENH_VEC_LENF);
v_equ(noise_shift, circb_min_shift, ENH_VEC_LENF);
for (i = 0; i < ENH_VEC_LENF; i++){
L_sum = L_mult(ENH_NOISE_BIAS, noisespect[i]);