1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 /****************************************************************************************
19 Portions of this file are derived from the following 3GPP standard:
20
21 3GPP TS 26.073
22 ANSI-C code for the Adaptive Multi-Rate (AMR) speech codec
23 Available from http://www.3gpp.org
24
25 (C) 2004, 3GPP Organizational Partners (ARIB, ATIS, CCSA, ETSI, TTA, TTC)
26 Permission to distribute, modify and use this file under the standard license
27 terms listed above has been obtained from the copyright holder.
28 ****************************************************************************************/
29 /*
30 Pathname: ./audio/gsm-amr/c/src/gmed_n.c
31
32 ------------------------------------------------------------------------------
33 REVISION HISTORY
34
35 Description: Put file into template and first pass at optimization.
36
37 Description: Made changes based on comments from the review meeting. Used
38 pointers instead of index addressing in the arrays.
39
40 Description: Synchronized file with UMTS version 3.2.0. Updated coding
41 template. Removed unncessary include files.
42
43 Description: Replaced "int" and/or "char" with OSCL defined types.
44
45 Who: Date:
46 Description:
47
48 ------------------------------------------------------------------------------
49 */
50
51 /*----------------------------------------------------------------------------
52 ; INCLUDES
53 ----------------------------------------------------------------------------*/
54 #include "gmed_n.h"
55 #include "typedef.h"
56
57 /*----------------------------------------------------------------------------
58 ; MACROS
59 ; Define module specific macros here
60 ----------------------------------------------------------------------------*/
61
62
63 /*----------------------------------------------------------------------------
64 ; DEFINES
65 ; Include all pre-processor statements here. Include conditional
66 ; compile variables also.
67 ----------------------------------------------------------------------------*/
68 #define NMAX 9 /* largest N used in median calculation */
69
70 /*----------------------------------------------------------------------------
71 ; LOCAL FUNCTION DEFINITIONS
72 ; Function Prototype declaration
73 ----------------------------------------------------------------------------*/
74
75
76 /*----------------------------------------------------------------------------
77 ; LOCAL STORE/BUFFER/POINTER DEFINITIONS
78 ; Variable declaration - defined here and used outside this module
79 ----------------------------------------------------------------------------*/
80
81
82 /*
83 ------------------------------------------------------------------------------
84 FUNCTION NAME: gmed_n
85 ------------------------------------------------------------------------------
86 INPUT AND OUTPUT DEFINITIONS
87
88 Inputs:
89 ind = input values (Word16)
90 n = number of inputs to find the median (Word16)
91
92 Returns:
93 median value.
94
95 Outputs:
96 None.
97
98 Global Variables Used:
99 None.
100
101 Local Variables Needed:
102 None.
103
104 ------------------------------------------------------------------------------
105 FUNCTION DESCRIPTION
106
107 This function calculates N-point median of a data set. This routine is only
108 valid for a odd number of gains (n <= NMAX).
109
110 ------------------------------------------------------------------------------
111 REQUIREMENTS
112
113 None.
114
115 ------------------------------------------------------------------------------
116 REFERENCES
117
118 gmed_n.c, UMTS GSM AMR speech codec, R99 - Version 3.2.0, March 2, 2001
119
120 ------------------------------------------------------------------------------
121 PSEUDO-CODE
122
123 Word16 gmed_n ( // o : The median value (0...N-1)
124 Word16 ind[], // i : Past gain values
125 Word16 n // i : The number of gains; this routine
126 // is only valid for a odd number of gains
127 // (n <= NMAX)
128 )
129 {
130 Word16 i, j, ix = 0;
131 Word16 max;
132 Word16 medianIndex;
133 Word16 tmp[NMAX];
134 Word16 tmp2[NMAX];
135
136 for (i = 0; i < n; i++)
137 {
138 tmp2[i] = ind[i];
139 }
140
141 for (i = 0; i < n; i++)
142 {
143 max = -32767;
144 for (j = 0; j < n; j++)
145 {
146 if (sub (tmp2[j], max) >= 0)
147 {
148 max = tmp2[j];
149 ix = j;
150 }
151 }
152 tmp2[ix] = -32768;
153 tmp[i] = ix;
154 }
155
156 medianIndex=tmp[ shr(n,1) ]; // account for complex addressing
157 return (ind[medianIndex]);
158 }
159
160 ------------------------------------------------------------------------------
161 RESOURCES USED [optional]
162
163 When the code is written for a specific target processor the
164 the resources used should be documented below.
165
166 HEAP MEMORY USED: x bytes
167
168 STACK MEMORY USED: x bytes
169
170 CLOCK CYCLES: (cycle count equation for this function) + (variable
171 used to represent cycle count for each subroutine
172 called)
173 where: (cycle count variable) = cycle count for [subroutine
174 name]
175
176 ------------------------------------------------------------------------------
177 CAUTION [optional]
178 [State any special notes, constraints or cautions for users of this function]
179
180 ------------------------------------------------------------------------------
181 */
182
gmed_n(Word16 ind[],Word16 n)183 Word16 gmed_n( /* o : the median value */
184 Word16 ind[], /* i : input values */
185 Word16 n /* i : number of inputs */
186 )
187 {
188 Word16 i, j, ix = 0;
189 Word16 max;
190 Word16 medianIndex;
191 Word16 tmp[NMAX];
192 Word16 tmp2[NMAX];
193
194 for (i = 0; i < n; i++)
195 {
196 *(tmp2 + i) = *(ind + i);
197 }
198
199 for (i = 0; i < n; i++)
200 {
201 max = -32767;
202 for (j = 0; j < n; j++)
203 {
204 if (*(tmp2 + j) >= max)
205 {
206 max = *(tmp2 + j);
207 ix = j;
208 }
209 }
210 *(tmp2 + ix) = -32768;
211 *(tmp + i) = ix;
212 }
213
214 medianIndex = *(tmp + (n >> 1)); /* account for complex addressing */
215
216 return (*(ind + medianIndex));
217 }
218
219