STM32F769IDiscovery  1.00
uDANTE Audio Networking with STM32F7 DISCO board
arm_dct4_f32.c
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
3 *
4 * $Date: 19. March 2015
5 * $Revision: V.1.4.5
6 *
7 * Project: CMSIS DSP Library
8 * Title: arm_dct4_f32.c
9 *
10 * Description: Processing function of DCT4 & IDCT4 F32.
11 *
12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions
16 * are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 * - Redistributions in binary form must reproduce the above copyright
20 * notice, this list of conditions and the following disclaimer in
21 * the documentation and/or other materials provided with the
22 * distribution.
23 * - Neither the name of ARM LIMITED nor the names of its contributors
24 * may be used to endorse or promote products derived from this
25 * software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 * -------------------------------------------------------------------- */
40 
41 #include "arm_math.h"
42 
138  const arm_dct4_instance_f32 * S,
139  float32_t * pState,
140  float32_t * pInlineBuffer)
141 {
142  uint32_t i; /* Loop counter */
143  float32_t *weights = S->pTwiddle; /* Pointer to the Weights table */
144  float32_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
145  float32_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
146  float32_t in; /* Temporary variable */
147 
148 
149  /* DCT4 computation involves DCT2 (which is calculated using RFFT)
150  * along with some pre-processing and post-processing.
151  * Computational procedure is explained as follows:
152  * (a) Pre-processing involves multiplying input with cos factor,
153  * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
154  * where,
155  * r(n) -- output of preprocessing
156  * u(n) -- input to preprocessing(actual Source buffer)
157  * (b) Calculation of DCT2 using FFT is divided into three steps:
158  * Step1: Re-ordering of even and odd elements of input.
159  * Step2: Calculating FFT of the re-ordered input.
160  * Step3: Taking the real part of the product of FFT output and weights.
161  * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
162  * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
163  * where,
164  * Y4 -- DCT4 output, Y2 -- DCT2 output
165  * (d) Multiplying the output with the normalizing factor sqrt(2/N).
166  */
167 
168  /*-------- Pre-processing ------------*/
169  /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
170  arm_scale_f32(pInlineBuffer, 2.0f, pInlineBuffer, S->N);
171  arm_mult_f32(pInlineBuffer, cosFact, pInlineBuffer, S->N);
172 
173  /* ----------------------------------------------------------------
174  * Step1: Re-ordering of even and odd elements as,
175  * pState[i] = pInlineBuffer[2*i] and
176  * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
177  ---------------------------------------------------------------------*/
178 
179  /* pS1 initialized to pState */
180  pS1 = pState;
181 
182  /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
183  pS2 = pState + (S->N - 1u);
184 
185  /* pbuff initialized to input buffer */
186  pbuff = pInlineBuffer;
187 
188 #ifndef ARM_MATH_CM0_FAMILY
189 
190  /* Run the below code for Cortex-M4 and Cortex-M3 */
191 
192  /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
193  i = (uint32_t) S->Nby2 >> 2u;
194 
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. */
197  do
198  {
199  /* Re-ordering of even and odd elements */
200  /* pState[i] = pInlineBuffer[2*i] */
201  *pS1++ = *pbuff++;
202  /* pState[N-i-1] = pInlineBuffer[2*i+1] */
203  *pS2-- = *pbuff++;
204 
205  *pS1++ = *pbuff++;
206  *pS2-- = *pbuff++;
207 
208  *pS1++ = *pbuff++;
209  *pS2-- = *pbuff++;
210 
211  *pS1++ = *pbuff++;
212  *pS2-- = *pbuff++;
213 
214  /* Decrement the loop counter */
215  i--;
216  } while(i > 0u);
217 
218  /* pbuff initialized to input buffer */
219  pbuff = pInlineBuffer;
220 
221  /* pS1 initialized to pState */
222  pS1 = pState;
223 
224  /* Initializing the loop counter to N/4 instead of N for loop unrolling */
225  i = (uint32_t) S->N >> 2u;
226 
227  /* Processing with loop unrolling 4 times as N is always multiple of 4.
228  * Compute 4 outputs at a time */
229  do
230  {
231  /* Writing the re-ordered output back to inplace input buffer */
232  *pbuff++ = *pS1++;
233  *pbuff++ = *pS1++;
234  *pbuff++ = *pS1++;
235  *pbuff++ = *pS1++;
236 
237  /* Decrement the loop counter */
238  i--;
239  } while(i > 0u);
240 
241 
242  /* ---------------------------------------------------------
243  * Step2: Calculate RFFT for N-point input
244  * ---------------------------------------------------------- */
245  /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
246  arm_rfft_f32(S->pRfft, pInlineBuffer, pState);
247 
248  /*----------------------------------------------------------------------
249  * Step3: Multiply the FFT output with the weights.
250  *----------------------------------------------------------------------*/
251  arm_cmplx_mult_cmplx_f32(pState, weights, pState, S->N);
252 
253  /* ----------- Post-processing ---------- */
254  /* DCT-IV can be obtained from DCT-II by the equation,
255  * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
256  * Hence, Y4(0) = Y2(0)/2 */
257  /* Getting only real part from the output and Converting to DCT-IV */
258 
259  /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
260  i = ((uint32_t) S->N - 1u) >> 2u;
261 
262  /* pbuff initialized to input buffer. */
263  pbuff = pInlineBuffer;
264 
265  /* pS1 initialized to pState */
266  pS1 = pState;
267 
268  /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
269  in = *pS1++ * (float32_t) 0.5;
270  /* input buffer acts as inplace, so output values are stored in the input itself. */
271  *pbuff++ = in;
272 
273  /* pState pointer is incremented twice as the real values are located alternatively in the array */
274  pS1++;
275 
276  /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
277  ** a second loop below computes the remaining 1 to 3 samples. */
278  do
279  {
280  /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
281  /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
282  in = *pS1++ - in;
283  *pbuff++ = in;
284  /* points to the next real value */
285  pS1++;
286 
287  in = *pS1++ - in;
288  *pbuff++ = in;
289  pS1++;
290 
291  in = *pS1++ - in;
292  *pbuff++ = in;
293  pS1++;
294 
295  in = *pS1++ - in;
296  *pbuff++ = in;
297  pS1++;
298 
299  /* Decrement the loop counter */
300  i--;
301  } while(i > 0u);
302 
303  /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
304  ** No loop unrolling is used. */
305  i = ((uint32_t) S->N - 1u) % 0x4u;
306 
307  while(i > 0u)
308  {
309  /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
310  /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
311  in = *pS1++ - in;
312  *pbuff++ = in;
313  /* points to the next real value */
314  pS1++;
315 
316  /* Decrement the loop counter */
317  i--;
318  }
319 
320 
321  /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
322 
323  /* Initializing the loop counter to N/4 instead of N for loop unrolling */
324  i = (uint32_t) S->N >> 2u;
325 
326  /* pbuff initialized to the pInlineBuffer(now contains the output values) */
327  pbuff = pInlineBuffer;
328 
329  /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
330  do
331  {
332  /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
333  in = *pbuff;
334  *pbuff++ = in * S->normalize;
335 
336  in = *pbuff;
337  *pbuff++ = in * S->normalize;
338 
339  in = *pbuff;
340  *pbuff++ = in * S->normalize;
341 
342  in = *pbuff;
343  *pbuff++ = in * S->normalize;
344 
345  /* Decrement the loop counter */
346  i--;
347  } while(i > 0u);
348 
349 
350 #else
351 
352  /* Run the below code for Cortex-M0 */
353 
354  /* Initializing the loop counter to N/2 */
355  i = (uint32_t) S->Nby2;
356 
357  do
358  {
359  /* Re-ordering of even and odd elements */
360  /* pState[i] = pInlineBuffer[2*i] */
361  *pS1++ = *pbuff++;
362  /* pState[N-i-1] = pInlineBuffer[2*i+1] */
363  *pS2-- = *pbuff++;
364 
365  /* Decrement the loop counter */
366  i--;
367  } while(i > 0u);
368 
369  /* pbuff initialized to input buffer */
370  pbuff = pInlineBuffer;
371 
372  /* pS1 initialized to pState */
373  pS1 = pState;
374 
375  /* Initializing the loop counter */
376  i = (uint32_t) S->N;
377 
378  do
379  {
380  /* Writing the re-ordered output back to inplace input buffer */
381  *pbuff++ = *pS1++;
382 
383  /* Decrement the loop counter */
384  i--;
385  } while(i > 0u);
386 
387 
388  /* ---------------------------------------------------------
389  * Step2: Calculate RFFT for N-point input
390  * ---------------------------------------------------------- */
391  /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
392  arm_rfft_f32(S->pRfft, pInlineBuffer, pState);
393 
394  /*----------------------------------------------------------------------
395  * Step3: Multiply the FFT output with the weights.
396  *----------------------------------------------------------------------*/
397  arm_cmplx_mult_cmplx_f32(pState, weights, pState, S->N);
398 
399  /* ----------- Post-processing ---------- */
400  /* DCT-IV can be obtained from DCT-II by the equation,
401  * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
402  * Hence, Y4(0) = Y2(0)/2 */
403  /* Getting only real part from the output and Converting to DCT-IV */
404 
405  /* pbuff initialized to input buffer. */
406  pbuff = pInlineBuffer;
407 
408  /* pS1 initialized to pState */
409  pS1 = pState;
410 
411  /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
412  in = *pS1++ * (float32_t) 0.5;
413  /* input buffer acts as inplace, so output values are stored in the input itself. */
414  *pbuff++ = in;
415 
416  /* pState pointer is incremented twice as the real values are located alternatively in the array */
417  pS1++;
418 
419  /* Initializing the loop counter */
420  i = ((uint32_t) S->N - 1u);
421 
422  do
423  {
424  /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
425  /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
426  in = *pS1++ - in;
427  *pbuff++ = in;
428  /* points to the next real value */
429  pS1++;
430 
431 
432  /* Decrement the loop counter */
433  i--;
434  } while(i > 0u);
435 
436 
437  /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
438 
439  /* Initializing the loop counter */
440  i = (uint32_t) S->N;
441 
442  /* pbuff initialized to the pInlineBuffer(now contains the output values) */
443  pbuff = pInlineBuffer;
444 
445  do
446  {
447  /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
448  in = *pbuff;
449  *pbuff++ = in * S->normalize;
450 
451  /* Decrement the loop counter */
452  i--;
453  } while(i > 0u);
454 
455 #endif /* #ifndef ARM_MATH_CM0_FAMILY */
456 
457 }
458 
float32_t * pCosFactor
Definition: arm_math.h:2260
float float32_t
32-bit floating-point type definition.
Definition: arm_math.h:407
Instance structure for the floating-point DCT4/IDCT4 function.
Definition: arm_math.h:2254
void arm_rfft_f32(const arm_rfft_instance_f32 *S, float32_t *pSrc, float32_t *pDst)
Processing function for the floating-point RFFT/RIFFT.
Definition: arm_rfft_f32.c:100
void arm_mult_f32(float32_t *pSrcA, float32_t *pSrcB, float32_t *pDst, uint32_t blockSize)
Floating-point vector multiplication.
Definition: arm_mult_f32.c:73
void arm_dct4_f32(const arm_dct4_instance_f32 *S, float32_t *pState, float32_t *pInlineBuffer)
Processing function for the floating-point DCT4/IDCT4.
Definition: arm_dct4_f32.c:137
float32_t * pTwiddle
Definition: arm_math.h:2259
void arm_cmplx_mult_cmplx_f32(float32_t *pSrcA, float32_t *pSrcB, float32_t *pDst, uint32_t numSamples)
Floating-point complex-by-complex multiplication.
void arm_scale_f32(float32_t *pSrc, float32_t scale, float32_t *pDst, uint32_t blockSize)
Multiplies a floating-point vector by a scalar.
Definition: arm_scale_f32.c:89