Set blackbox file handler to NULL after closing file
[inav.git] / lib / main / CMSIS / DSP / Source / TransformFunctions / arm_dct4_q15.c
blob918f0bd2c7577e93da26e12b10b6d5b13c1227b5
1 /* ----------------------------------------------------------------------
2 * Project: CMSIS DSP Library
3 * Title: arm_dct4_q15.c
4 * Description: Processing function of DCT4 & IDCT4 Q15
6 * $Date: 27. January 2017
7 * $Revision: V.1.5.1
9 * Target Processor: Cortex-M cores
10 * -------------------------------------------------------------------- */
12 * Copyright (C) 2010-2017 ARM Limited or its affiliates. All rights reserved.
14 * SPDX-License-Identifier: Apache-2.0
16 * Licensed under the Apache License, Version 2.0 (the License); you may
17 * not use this file except in compliance with the License.
18 * You may obtain a copy of the License at
20 * www.apache.org/licenses/LICENSE-2.0
22 * Unless required by applicable law or agreed to in writing, software
23 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
24 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
25 * See the License for the specific language governing permissions and
26 * limitations under the License.
29 #include "arm_math.h"
31 /**
32 * @addtogroup DCT4_IDCT4
33 * @{
36 /**
37 * @brief Processing function for the Q15 DCT4/IDCT4.
38 * @param[in] *S points to an instance of the Q15 DCT4 structure.
39 * @param[in] *pState points to state buffer.
40 * @param[in,out] *pInlineBuffer points to the in-place input and output buffer.
41 * @return none.
43 * \par Input an output formats:
44 * Internally inputs are downscaled in the RFFT process function to avoid overflows.
45 * Number of bits downscaled, depends on the size of the transform.
46 * The input and output formats for different DCT sizes and number of bits to upscale are mentioned in the table below:
48 * \image html dct4FormatsQ15Table.gif
51 void arm_dct4_q15(
52 const arm_dct4_instance_q15 * S,
53 q15_t * pState,
54 q15_t * pInlineBuffer)
56 uint32_t i; /* Loop counter */
57 q15_t *weights = S->pTwiddle; /* Pointer to the Weights table */
58 q15_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
59 q15_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
60 q15_t in; /* Temporary variable */
63 /* DCT4 computation involves DCT2 (which is calculated using RFFT)
64 * along with some pre-processing and post-processing.
65 * Computational procedure is explained as follows:
66 * (a) Pre-processing involves multiplying input with cos factor,
67 * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
68 * where,
69 * r(n) -- output of preprocessing
70 * u(n) -- input to preprocessing(actual Source buffer)
71 * (b) Calculation of DCT2 using FFT is divided into three steps:
72 * Step1: Re-ordering of even and odd elements of input.
73 * Step2: Calculating FFT of the re-ordered input.
74 * Step3: Taking the real part of the product of FFT output and weights.
75 * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
76 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
77 * where,
78 * Y4 -- DCT4 output, Y2 -- DCT2 output
79 * (d) Multiplying the output with the normalizing factor sqrt(2/N).
82 /*-------- Pre-processing ------------*/
83 /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
84 arm_mult_q15(pInlineBuffer, cosFact, pInlineBuffer, S->N);
85 arm_shift_q15(pInlineBuffer, 1, pInlineBuffer, S->N);
87 /* ----------------------------------------------------------------
88 * Step1: Re-ordering of even and odd elements as
89 * pState[i] = pInlineBuffer[2*i] and
90 * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
91 ---------------------------------------------------------------------*/
93 /* pS1 initialized to pState */
94 pS1 = pState;
96 /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
97 pS2 = pState + (S->N - 1U);
99 /* pbuff initialized to input buffer */
100 pbuff = pInlineBuffer;
103 #if defined (ARM_MATH_DSP)
105 /* Run the below code for Cortex-M4 and Cortex-M3 */
107 /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
108 i = (uint32_t) S->Nby2 >> 2U;
110 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
111 ** a second loop below computes the remaining 1 to 3 samples. */
114 /* Re-ordering of even and odd elements */
115 /* pState[i] = pInlineBuffer[2*i] */
116 *pS1++ = *pbuff++;
117 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
118 *pS2-- = *pbuff++;
120 *pS1++ = *pbuff++;
121 *pS2-- = *pbuff++;
123 *pS1++ = *pbuff++;
124 *pS2-- = *pbuff++;
126 *pS1++ = *pbuff++;
127 *pS2-- = *pbuff++;
129 /* Decrement the loop counter */
130 i--;
131 } while (i > 0U);
133 /* pbuff initialized to input buffer */
134 pbuff = pInlineBuffer;
136 /* pS1 initialized to pState */
137 pS1 = pState;
139 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
140 i = (uint32_t) S->N >> 2U;
142 /* Processing with loop unrolling 4 times as N is always multiple of 4.
143 * Compute 4 outputs at a time */
146 /* Writing the re-ordered output back to inplace input buffer */
147 *pbuff++ = *pS1++;
148 *pbuff++ = *pS1++;
149 *pbuff++ = *pS1++;
150 *pbuff++ = *pS1++;
152 /* Decrement the loop counter */
153 i--;
154 } while (i > 0U);
157 /* ---------------------------------------------------------
158 * Step2: Calculate RFFT for N-point input
159 * ---------------------------------------------------------- */
160 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
161 arm_rfft_q15(S->pRfft, pInlineBuffer, pState);
163 /*----------------------------------------------------------------------
164 * Step3: Multiply the FFT output with the weights.
165 *----------------------------------------------------------------------*/
166 arm_cmplx_mult_cmplx_q15(pState, weights, pState, S->N);
168 /* The output of complex multiplication is in 3.13 format.
169 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
170 arm_shift_q15(pState, 2, pState, S->N * 2);
172 /* ----------- Post-processing ---------- */
173 /* DCT-IV can be obtained from DCT-II by the equation,
174 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
175 * Hence, Y4(0) = Y2(0)/2 */
176 /* Getting only real part from the output and Converting to DCT-IV */
178 /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
179 i = ((uint32_t) S->N - 1U) >> 2U;
181 /* pbuff initialized to input buffer. */
182 pbuff = pInlineBuffer;
184 /* pS1 initialized to pState */
185 pS1 = pState;
187 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
188 in = *pS1++ >> 1U;
189 /* input buffer acts as inplace, so output values are stored in the input itself. */
190 *pbuff++ = in;
192 /* pState pointer is incremented twice as the real values are located alternatively in the array */
193 pS1++;
195 /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
196 ** a second loop below computes the remaining 1 to 3 samples. */
199 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
200 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
201 in = *pS1++ - in;
202 *pbuff++ = in;
203 /* points to the next real value */
204 pS1++;
206 in = *pS1++ - in;
207 *pbuff++ = in;
208 pS1++;
210 in = *pS1++ - in;
211 *pbuff++ = in;
212 pS1++;
214 in = *pS1++ - in;
215 *pbuff++ = in;
216 pS1++;
218 /* Decrement the loop counter */
219 i--;
220 } while (i > 0U);
222 /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
223 ** No loop unrolling is used. */
224 i = ((uint32_t) S->N - 1U) % 0x4U;
226 while (i > 0U)
228 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
229 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
230 in = *pS1++ - in;
231 *pbuff++ = in;
232 /* points to the next real value */
233 pS1++;
235 /* Decrement the loop counter */
236 i--;
240 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
242 /* Initializing the loop counter to N/4 instead of N for loop unrolling */
243 i = (uint32_t) S->N >> 2U;
245 /* pbuff initialized to the pInlineBuffer(now contains the output values) */
246 pbuff = pInlineBuffer;
248 /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
251 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
252 in = *pbuff;
253 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
255 in = *pbuff;
256 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
258 in = *pbuff;
259 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
261 in = *pbuff;
262 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
264 /* Decrement the loop counter */
265 i--;
266 } while (i > 0U);
269 #else
271 /* Run the below code for Cortex-M0 */
273 /* Initializing the loop counter to N/2 */
274 i = (uint32_t) S->Nby2;
278 /* Re-ordering of even and odd elements */
279 /* pState[i] = pInlineBuffer[2*i] */
280 *pS1++ = *pbuff++;
281 /* pState[N-i-1] = pInlineBuffer[2*i+1] */
282 *pS2-- = *pbuff++;
284 /* Decrement the loop counter */
285 i--;
286 } while (i > 0U);
288 /* pbuff initialized to input buffer */
289 pbuff = pInlineBuffer;
291 /* pS1 initialized to pState */
292 pS1 = pState;
294 /* Initializing the loop counter */
295 i = (uint32_t) S->N;
299 /* Writing the re-ordered output back to inplace input buffer */
300 *pbuff++ = *pS1++;
302 /* Decrement the loop counter */
303 i--;
304 } while (i > 0U);
307 /* ---------------------------------------------------------
308 * Step2: Calculate RFFT for N-point input
309 * ---------------------------------------------------------- */
310 /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
311 arm_rfft_q15(S->pRfft, pInlineBuffer, pState);
313 /*----------------------------------------------------------------------
314 * Step3: Multiply the FFT output with the weights.
315 *----------------------------------------------------------------------*/
316 arm_cmplx_mult_cmplx_q15(pState, weights, pState, S->N);
318 /* The output of complex multiplication is in 3.13 format.
319 * Hence changing the format of N (i.e. 2*N elements) complex numbers to 1.15 format by shifting left by 2 bits. */
320 arm_shift_q15(pState, 2, pState, S->N * 2);
322 /* ----------- Post-processing ---------- */
323 /* DCT-IV can be obtained from DCT-II by the equation,
324 * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
325 * Hence, Y4(0) = Y2(0)/2 */
326 /* Getting only real part from the output and Converting to DCT-IV */
328 /* Initializing the loop counter */
329 i = ((uint32_t) S->N - 1U);
331 /* pbuff initialized to input buffer. */
332 pbuff = pInlineBuffer;
334 /* pS1 initialized to pState */
335 pS1 = pState;
337 /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
338 in = *pS1++ >> 1U;
339 /* input buffer acts as inplace, so output values are stored in the input itself. */
340 *pbuff++ = in;
342 /* pState pointer is incremented twice as the real values are located alternatively in the array */
343 pS1++;
347 /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
348 /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
349 in = *pS1++ - in;
350 *pbuff++ = in;
351 /* points to the next real value */
352 pS1++;
354 /* Decrement the loop counter */
355 i--;
356 } while (i > 0U);
358 /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
360 /* Initializing the loop counter */
361 i = (uint32_t) S->N;
363 /* pbuff initialized to the pInlineBuffer(now contains the output values) */
364 pbuff = pInlineBuffer;
368 /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
369 in = *pbuff;
370 *pbuff++ = ((q15_t) (((q31_t) in * S->normalize) >> 15));
372 /* Decrement the loop counter */
373 i--;
374 } while (i > 0U);
376 #endif /* #if defined (ARM_MATH_DSP) */
381 * @} end of DCT4_IDCT4 group