gpclip.c revision 5d5c3a132bb446ac78a37dfaac24a46cacf0dd73
1/*
2 ** Copyright 2003-2010, VisualOn, Inc.
3 **
4 ** Licensed under the Apache License, Version 2.0 (the "License");
5 ** you may not use this file except in compliance with the License.
6 ** You may obtain a copy of the License at
7 **
8 **     http://www.apache.org/licenses/LICENSE-2.0
9 **
10 ** Unless required by applicable law or agreed to in writing, software
11 ** distributed under the License is distributed on an "AS IS" BASIS,
12 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 ** See the License for the specific language governing permissions and
14 ** limitations under the License.
15 */
16
17/**************************************************************************
18*      File: gpclip.c                                                     *
19*                                                                         *
20*      Description:To avoid unstable synthesis on frame erasure, the gain *
21*      need to be limited(gain pitch < 1.0) when the following            *
22*      case occurs                                                        *
23*      a resonance on LPC filter(lp_disp < 60Hz)                          *
24*      a good pitch prediction (lp_gp > 0.95)                             *
25*                                                                         *
26***************************************************************************/
27#include "typedef.h"
28#include "basic_op.h"
29
30#define DIST_ISF_MAX    307                /* 120 Hz (6400Hz=16384) */
31#define DIST_ISF_THRES  154                /* 60     (6400Hz=16384) */
32#define GAIN_PIT_THRES  14746              /* 0.9 in Q14 */
33#define GAIN_PIT_MIN    9830               /* 0.6 in Q14 */
34#define M               16
35
36
37void Init_gp_clip(
38        Word16 mem[]                          /* (o) : memory of gain of pitch clipping algorithm */
39        )
40{
41    mem[0] = DIST_ISF_MAX;
42    mem[1] = GAIN_PIT_MIN;
43}
44
45
46Word16 Gp_clip(
47        Word16 mem[]                          /* (i/o) : memory of gain of pitch clipping algorithm */
48          )
49{
50    Word16 clip = 0;
51    if ((mem[0] < DIST_ISF_THRES) && (mem[1] > GAIN_PIT_THRES))
52        clip = 1;
53
54    return (clip);
55}
56
57
58void Gp_clip_test_isf(
59        Word16 isf[],                         /* (i)   : isf values (in frequency domain)           */
60        Word16 mem[]                          /* (i/o) : memory of gain of pitch clipping algorithm */
61        )
62{
63    Word16 dist, dist_min;
64    Word32 i;
65
66    dist_min = vo_sub(isf[1], isf[0]);
67
68    for (i = 2; i < M - 1; i++)
69    {
70        dist = vo_sub(isf[i], isf[i - 1]);
71        if(dist < dist_min)
72        {
73            dist_min = dist;
74        }
75    }
76
77    dist = extract_h(L_mac(vo_L_mult(26214, mem[0]), 6554, dist_min));
78
79    if (dist > DIST_ISF_MAX)
80    {
81        dist = DIST_ISF_MAX;
82    }
83    mem[0] = dist;
84
85    return;
86}
87
88
89void Gp_clip_test_gain_pit(
90        Word16 gain_pit,                      /* (i) Q14 : gain of quantized pitch                    */
91        Word16 mem[]                          /* (i/o)   : memory of gain of pitch clipping algorithm */
92        )
93{
94    Word16 gain;
95    Word32 L_tmp;
96    L_tmp = (29491 * mem[1])<<1;
97    L_tmp += (3277 * gain_pit)<<1;
98
99    gain = extract_h(L_tmp);
100
101    if(gain < GAIN_PIT_MIN)
102    {
103        gain = GAIN_PIT_MIN;
104    }
105    mem[1] = gain;
106    return;
107}
108
109
110
111