preemph.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: preemph.c                                                *
19*                                                                     *
20*      Description: Preemphasis: filtering through 1 - g z^-1         *
21*              Preemph2 --> signal is multiplied by 2             *
22*                                                                     *
23************************************************************************/
24
25#include "typedef.h"
26#include "basic_op.h"
27
28void Preemph(
29        Word16 x[],                           /* (i/o)   : input signal overwritten by the output */
30        Word16 mu,                            /* (i) Q15 : preemphasis coefficient                */
31        Word16 lg,                            /* (i)     : lenght of filtering                    */
32        Word16 * mem                          /* (i/o)   : memory (x[-1])                         */
33        )
34{
35    Word16 temp;
36    Word32 i, L_tmp;
37
38    temp = x[lg - 1];
39
40    for (i = lg - 1; i > 0; i--)
41    {
42        L_tmp = L_deposit_h(x[i]);
43        L_tmp -= (x[i - 1] * mu)<<1;
44        x[i] = (L_tmp + 0x8000)>>16;
45    }
46
47    L_tmp = L_deposit_h(x[0]);
48    L_tmp -= ((*mem) * mu)<<1;
49    x[0] = (L_tmp + 0x8000)>>16;
50
51    *mem = temp;
52
53    return;
54}
55
56
57void Preemph2(
58        Word16 x[],                           /* (i/o)   : input signal overwritten by the output */
59        Word16 mu,                            /* (i) Q15 : preemphasis coefficient                */
60        Word16 lg,                            /* (i)     : lenght of filtering                    */
61        Word16 * mem                          /* (i/o)   : memory (x[-1])                         */
62         )
63{
64    Word16 temp;
65    Word32 i, L_tmp;
66
67    temp = x[lg - 1];
68
69    for (i = (Word16) (lg - 1); i > 0; i--)
70    {
71        L_tmp = L_deposit_h(x[i]);
72        L_tmp -= (x[i - 1] * mu)<<1; // only called with mu == 22282, so this won't overflow
73        if (L_tmp > INT32_MAX / 2) {
74            L_tmp = INT32_MAX / 2;
75        }
76        L_tmp = (L_tmp << 1);
77        x[i] = (L_tmp + 0x8000)>>16;
78    }
79
80    L_tmp = L_deposit_h(x[0]);
81    L_tmp -= ((*mem) * mu)<<1;
82    if (L_tmp > INT32_MAX / 2) {
83        L_tmp = INT32_MAX / 2;
84    }
85    L_tmp = (L_tmp << 1);
86    if (L_tmp > INT32_MAX - 0x8000) {
87        L_tmp = INT32_MAX - 0x8000;
88    }
89    x[0] = (L_tmp + 0x8000)>>16;
90
91    *mem = temp;
92
93    return;
94}
95
96
97
98