aboutsummaryrefslogtreecommitdiffstats
path: root/codecs/ilbc/filter.c
blob: f4ba63386d6046d5998bcfd7c986483b3d93211b (plain)
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
/******************************************************************

    iLBC Speech Coder ANSI-C Source Code

    filter.c 

    Copyright (C) The Internet Society (2004). 
    All Rights Reserved.

******************************************************************/



#include "iLBC_define.h"
#include "filter.h"

/*----------------------------------------------------------------*
 *  all-pole filter
 *---------------------------------------------------------------*/

void AllPoleFilter(
    float *InOut,   /* (i/o) on entrance InOut[-orderCoef] to 
                           InOut[-1] contain the state of the 
                           filter (delayed samples). InOut[0] to 
                           InOut[lengthInOut-1] contain the filter
                           input, on en exit InOut[-orderCoef] to
                           InOut[-1] is unchanged and InOut[0] to
                           InOut[lengthInOut-1] contain filtered 
                           samples */
    float *Coef,/* (i) filter coefficients, Coef[0] is assumed 
                           to be 1.0 */
    int lengthInOut,/* (i) number of input/output samples */
    int orderCoef   /* (i) number of filter coefficients */
){  
    int n,k;
    
    for(n=0;n<lengthInOut;n++){
        for(k=1;k<=orderCoef;k++){
            *InOut -= Coef[k]*InOut[-k];
        }
        InOut++;
    }
}

/*----------------------------------------------------------------*
 *  all-zero filter                         
 *---------------------------------------------------------------*/

void AllZeroFilter(
    float *In,      /* (i) In[0] to In[lengthInOut-1] contain 
                           filter input samples */
    float *Coef,/* (i) filter coefficients (Coef[0] is assumed 
                           to be 1.0) */
    int lengthInOut,/* (i) number of input/output samples */
    int orderCoef,  /* (i) number of filter coefficients */
    float *Out      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
                           contain the filter state, on exit Out[0]
                           to Out[lengthInOut-1] contain filtered
                           samples */
){  
    int n,k;
    
    for(n=0;n<lengthInOut;n++){
        *Out = Coef[0]*In[0];
        for(k=1;k<=orderCoef;k++){
            *Out += Coef[k]*In[-k];
        }


        Out++;
        In++;
    }
}

/*----------------------------------------------------------------*
 *  pole-zero filter
 *---------------------------------------------------------------*/

void ZeroPoleFilter(
    float *In,      /* (i) In[0] to In[lengthInOut-1] contain 
                           filter input samples In[-orderCoef] to 
                           In[-1] contain state of all-zero 
                           section */
    float *ZeroCoef,/* (i) filter coefficients for all-zero 
                           section (ZeroCoef[0] is assumed to 
                           be 1.0) */
    float *PoleCoef,/* (i) filter coefficients for all-pole section
                           (ZeroCoef[0] is assumed to be 1.0) */
    int lengthInOut,/* (i) number of input/output samples */
    int orderCoef,  /* (i) number of filter coefficients */
    float *Out      /* (i/o) on entrance Out[-orderCoef] to Out[-1]
                           contain state of all-pole section. On 
                           exit Out[0] to Out[lengthInOut-1] 
                           contain filtered samples */
){
    AllZeroFilter(In,ZeroCoef,lengthInOut,orderCoef,Out);
    AllPoleFilter(Out,PoleCoef,lengthInOut,orderCoef);
}

/*----------------------------------------------------------------*
 * downsample (LP filter and decimation)
 *---------------------------------------------------------------*/

void DownSample (
    float  *In,     /* (i) input samples */
    float  *Coef,   /* (i) filter coefficients */
    int lengthIn,   /* (i) number of input samples */ 
    float  *state,  /* (i) filter state */
    float  *Out     /* (o) downsampled output */
){
    float   o;
    float *Out_ptr = Out;
    float *Coef_ptr, *In_ptr;
    float *state_ptr;
    int i, j, stop;

    /* LP filter and decimate at the same time */

    for (i = DELAY_DS; i < lengthIn; i+=FACTOR_DS)
    {
        Coef_ptr = &Coef[0];
        In_ptr = &In[i];
        state_ptr = &state[FILTERORDER_DS-2];



        o = (float)0.0;
        
        stop = (i < FILTERORDER_DS) ? i + 1 : FILTERORDER_DS;

        for (j = 0; j < stop; j++) 
        {
            o += *Coef_ptr++ * (*In_ptr--);
        }
        for (j = i + 1; j < FILTERORDER_DS; j++) 
        {
            o += *Coef_ptr++ * (*state_ptr--);
        }

        *Out_ptr++ = o;
    }

    /* Get the last part (use zeros as input for the future) */

    for (i=(lengthIn+FACTOR_DS); i<(lengthIn+DELAY_DS); 
            i+=FACTOR_DS) {

        o=(float)0.0;
        
        if (i<lengthIn) {
            Coef_ptr = &Coef[0];
            In_ptr = &In[i];
            for (j=0; j<FILTERORDER_DS; j++) {
                    o += *Coef_ptr++ * (*Out_ptr--);
            }
        } else {
            Coef_ptr = &Coef[i-lengthIn];
            In_ptr = &In[lengthIn-1];
            for (j=0; j<FILTERORDER_DS-(i-lengthIn); j++) {
                    o += *Coef_ptr++ * (*In_ptr--);
            }
        }
        *Out_ptr++ = o;
    }
}