arm_dct4_f32.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* ----------------------------------------------------------------------
  2. * Copyright (C) 2010-2014 ARM Limited. All rights reserved.
  3. *
  4. * $Date: 31. July 2014
  5. * $Revision: V1.4.4
  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. #include "arm_math.h"
  41. /**
  42. * @ingroup groupTransforms
  43. */
  44. /**
  45. * @defgroup DCT4_IDCT4 DCT Type IV Functions
  46. * Representation of signals by minimum number of values is important for storage and transmission.
  47. * The possibility of large discontinuity between the beginning and end of a period of a signal
  48. * in DFT can be avoided by extending the signal so that it is even-symmetric.
  49. * Discrete Cosine Transform (DCT) is constructed such that its energy is heavily concentrated in the lower part of the
  50. * spectrum and is very widely used in signal and image coding applications.
  51. * The family of DCTs (DCT type- 1,2,3,4) is the outcome of different combinations of homogeneous boundary conditions.
  52. * DCT has an excellent energy-packing capability, hence has many applications and in data compression in particular.
  53. *
  54. * DCT is essentially the Discrete Fourier Transform(DFT) of an even-extended real signal.
  55. * Reordering of the input data makes the computation of DCT just a problem of
  56. * computing the DFT of a real signal with a few additional operations.
  57. * This approach provides regular, simple, and very efficient DCT algorithms for practical hardware and software implementations.
  58. *
  59. * DCT type-II can be implemented using Fast fourier transform (FFT) internally, as the transform is applied on real values, Real FFT can be used.
  60. * DCT4 is implemented using DCT2 as their implementations are similar except with some added pre-processing and post-processing.
  61. * DCT2 implementation can be described in the following steps:
  62. * - Re-ordering input
  63. * - Calculating Real FFT
  64. * - Multiplication of weights and Real FFT output and getting real part from the product.
  65. *
  66. * This process is explained by the block diagram below:
  67. * \image html DCT4.gif "Discrete Cosine Transform - type-IV"
  68. *
  69. * \par Algorithm:
  70. * The N-point type-IV DCT is defined as a real, linear transformation by the formula:
  71. * \image html DCT4Equation.gif
  72. * where <code>k = 0,1,2,.....N-1</code>
  73. *\par
  74. * Its inverse is defined as follows:
  75. * \image html IDCT4Equation.gif
  76. * where <code>n = 0,1,2,.....N-1</code>
  77. *\par
  78. * The DCT4 matrices become involutory (i.e. they are self-inverse) by multiplying with an overall scale factor of sqrt(2/N).
  79. * The symmetry of the transform matrix indicates that the fast algorithms for the forward
  80. * and inverse transform computation are identical.
  81. * Note that the implementation of Inverse DCT4 and DCT4 is same, hence same process function can be used for both.
  82. *
  83. * \par Lengths supported by the transform:
  84. * As DCT4 internally uses Real FFT, it supports all the lengths supported by arm_rfft_f32().
  85. * The library provides separate functions for Q15, Q31, and floating-point data types.
  86. * \par Instance Structure
  87. * The instances for Real FFT and FFT, cosine values table and twiddle factor table are stored in an instance data structure.
  88. * A separate instance structure must be defined for each transform.
  89. * There are separate instance structure declarations for each of the 3 supported data types.
  90. *
  91. * \par Initialization Functions
  92. * There is also an associated initialization function for each data type.
  93. * The initialization function performs the following operations:
  94. * - Sets the values of the internal structure fields.
  95. * - Initializes Real FFT as its process function is used internally in DCT4, by calling arm_rfft_init_f32().
  96. * \par
  97. * Use of the initialization function is optional.
  98. * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
  99. * To place an instance structure into a const data section, the instance structure must be manually initialized.
  100. * Manually initialize the instance structure as follows:
  101. * <pre>
  102. *arm_dct4_instance_f32 S = {N, Nby2, normalize, pTwiddle, pCosFactor, pRfft, pCfft};
  103. *arm_dct4_instance_q31 S = {N, Nby2, normalize, pTwiddle, pCosFactor, pRfft, pCfft};
  104. *arm_dct4_instance_q15 S = {N, Nby2, normalize, pTwiddle, pCosFactor, pRfft, pCfft};
  105. * </pre>
  106. * where \c N is the length of the DCT4; \c Nby2 is half of the length of the DCT4;
  107. * \c normalize is normalizing factor used and is equal to <code>sqrt(2/N)</code>;
  108. * \c pTwiddle points to the twiddle factor table;
  109. * \c pCosFactor points to the cosFactor table;
  110. * \c pRfft points to the real FFT instance;
  111. * \c pCfft points to the complex FFT instance;
  112. * The CFFT and RFFT structures also needs to be initialized, refer to arm_cfft_radix4_f32()
  113. * and arm_rfft_f32() respectively for details regarding static initialization.
  114. *
  115. * \par Fixed-Point Behavior
  116. * Care must be taken when using the fixed-point versions of the DCT4 transform functions.
  117. * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
  118. * Refer to the function specific documentation below for usage guidelines.
  119. */
  120. /**
  121. * @addtogroup DCT4_IDCT4
  122. * @{
  123. */
  124. /**
  125. * @brief Processing function for the floating-point DCT4/IDCT4.
  126. * @param[in] *S points to an instance of the floating-point DCT4/IDCT4 structure.
  127. * @param[in] *pState points to state buffer.
  128. * @param[in,out] *pInlineBuffer points to the in-place input and output buffer.
  129. * @return none.
  130. */
  131. void arm_dct4_f32(
  132. const arm_dct4_instance_f32 * S,
  133. float32_t * pState,
  134. float32_t * pInlineBuffer)
  135. {
  136. uint32_t i; /* Loop counter */
  137. float32_t *weights = S->pTwiddle; /* Pointer to the Weights table */
  138. float32_t *cosFact = S->pCosFactor; /* Pointer to the cos factors table */
  139. float32_t *pS1, *pS2, *pbuff; /* Temporary pointers for input buffer and pState buffer */
  140. float32_t in; /* Temporary variable */
  141. /* DCT4 computation involves DCT2 (which is calculated using RFFT)
  142. * along with some pre-processing and post-processing.
  143. * Computational procedure is explained as follows:
  144. * (a) Pre-processing involves multiplying input with cos factor,
  145. * r(n) = 2 * u(n) * cos(pi*(2*n+1)/(4*n))
  146. * where,
  147. * r(n) -- output of preprocessing
  148. * u(n) -- input to preprocessing(actual Source buffer)
  149. * (b) Calculation of DCT2 using FFT is divided into three steps:
  150. * Step1: Re-ordering of even and odd elements of input.
  151. * Step2: Calculating FFT of the re-ordered input.
  152. * Step3: Taking the real part of the product of FFT output and weights.
  153. * (c) Post-processing - DCT4 can be obtained from DCT2 output using the following equation:
  154. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  155. * where,
  156. * Y4 -- DCT4 output, Y2 -- DCT2 output
  157. * (d) Multiplying the output with the normalizing factor sqrt(2/N).
  158. */
  159. /*-------- Pre-processing ------------*/
  160. /* Multiplying input with cos factor i.e. r(n) = 2 * x(n) * cos(pi*(2*n+1)/(4*n)) */
  161. arm_scale_f32(pInlineBuffer, 2.0f, pInlineBuffer, S->N);
  162. arm_mult_f32(pInlineBuffer, cosFact, pInlineBuffer, S->N);
  163. /* ----------------------------------------------------------------
  164. * Step1: Re-ordering of even and odd elements as,
  165. * pState[i] = pInlineBuffer[2*i] and
  166. * pState[N-i-1] = pInlineBuffer[2*i+1] where i = 0 to N/2
  167. ---------------------------------------------------------------------*/
  168. /* pS1 initialized to pState */
  169. pS1 = pState;
  170. /* pS2 initialized to pState+N-1, so that it points to the end of the state buffer */
  171. pS2 = pState + (S->N - 1u);
  172. /* pbuff initialized to input buffer */
  173. pbuff = pInlineBuffer;
  174. #ifndef ARM_MATH_CM0_FAMILY
  175. /* Run the below code for Cortex-M4 and Cortex-M3 */
  176. /* Initializing the loop counter to N/2 >> 2 for loop unrolling by 4 */
  177. i = (uint32_t) S->Nby2 >> 2u;
  178. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  179. ** a second loop below computes the remaining 1 to 3 samples. */
  180. do
  181. {
  182. /* Re-ordering of even and odd elements */
  183. /* pState[i] = pInlineBuffer[2*i] */
  184. *pS1++ = *pbuff++;
  185. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  186. *pS2-- = *pbuff++;
  187. *pS1++ = *pbuff++;
  188. *pS2-- = *pbuff++;
  189. *pS1++ = *pbuff++;
  190. *pS2-- = *pbuff++;
  191. *pS1++ = *pbuff++;
  192. *pS2-- = *pbuff++;
  193. /* Decrement the loop counter */
  194. i--;
  195. } while(i > 0u);
  196. /* pbuff initialized to input buffer */
  197. pbuff = pInlineBuffer;
  198. /* pS1 initialized to pState */
  199. pS1 = pState;
  200. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  201. i = (uint32_t) S->N >> 2u;
  202. /* Processing with loop unrolling 4 times as N is always multiple of 4.
  203. * Compute 4 outputs at a time */
  204. do
  205. {
  206. /* Writing the re-ordered output back to inplace input buffer */
  207. *pbuff++ = *pS1++;
  208. *pbuff++ = *pS1++;
  209. *pbuff++ = *pS1++;
  210. *pbuff++ = *pS1++;
  211. /* Decrement the loop counter */
  212. i--;
  213. } while(i > 0u);
  214. /* ---------------------------------------------------------
  215. * Step2: Calculate RFFT for N-point input
  216. * ---------------------------------------------------------- */
  217. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  218. arm_rfft_f32(S->pRfft, pInlineBuffer, pState);
  219. /*----------------------------------------------------------------------
  220. * Step3: Multiply the FFT output with the weights.
  221. *----------------------------------------------------------------------*/
  222. arm_cmplx_mult_cmplx_f32(pState, weights, pState, S->N);
  223. /* ----------- Post-processing ---------- */
  224. /* DCT-IV can be obtained from DCT-II by the equation,
  225. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  226. * Hence, Y4(0) = Y2(0)/2 */
  227. /* Getting only real part from the output and Converting to DCT-IV */
  228. /* Initializing the loop counter to N >> 2 for loop unrolling by 4 */
  229. i = ((uint32_t) S->N - 1u) >> 2u;
  230. /* pbuff initialized to input buffer. */
  231. pbuff = pInlineBuffer;
  232. /* pS1 initialized to pState */
  233. pS1 = pState;
  234. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  235. in = *pS1++ * (float32_t) 0.5;
  236. /* input buffer acts as inplace, so output values are stored in the input itself. */
  237. *pbuff++ = in;
  238. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  239. pS1++;
  240. /* First part of the processing with loop unrolling. Compute 4 outputs at a time.
  241. ** a second loop below computes the remaining 1 to 3 samples. */
  242. do
  243. {
  244. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  245. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  246. in = *pS1++ - in;
  247. *pbuff++ = in;
  248. /* points to the next real value */
  249. pS1++;
  250. in = *pS1++ - in;
  251. *pbuff++ = in;
  252. pS1++;
  253. in = *pS1++ - in;
  254. *pbuff++ = in;
  255. pS1++;
  256. in = *pS1++ - in;
  257. *pbuff++ = in;
  258. pS1++;
  259. /* Decrement the loop counter */
  260. i--;
  261. } while(i > 0u);
  262. /* If the blockSize is not a multiple of 4, compute any remaining output samples here.
  263. ** No loop unrolling is used. */
  264. i = ((uint32_t) S->N - 1u) % 0x4u;
  265. while(i > 0u)
  266. {
  267. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  268. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  269. in = *pS1++ - in;
  270. *pbuff++ = in;
  271. /* points to the next real value */
  272. pS1++;
  273. /* Decrement the loop counter */
  274. i--;
  275. }
  276. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  277. /* Initializing the loop counter to N/4 instead of N for loop unrolling */
  278. i = (uint32_t) S->N >> 2u;
  279. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  280. pbuff = pInlineBuffer;
  281. /* Processing with loop unrolling 4 times as N is always multiple of 4. Compute 4 outputs at a time */
  282. do
  283. {
  284. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  285. in = *pbuff;
  286. *pbuff++ = in * S->normalize;
  287. in = *pbuff;
  288. *pbuff++ = in * S->normalize;
  289. in = *pbuff;
  290. *pbuff++ = in * S->normalize;
  291. in = *pbuff;
  292. *pbuff++ = in * S->normalize;
  293. /* Decrement the loop counter */
  294. i--;
  295. } while(i > 0u);
  296. #else
  297. /* Run the below code for Cortex-M0 */
  298. /* Initializing the loop counter to N/2 */
  299. i = (uint32_t) S->Nby2;
  300. do
  301. {
  302. /* Re-ordering of even and odd elements */
  303. /* pState[i] = pInlineBuffer[2*i] */
  304. *pS1++ = *pbuff++;
  305. /* pState[N-i-1] = pInlineBuffer[2*i+1] */
  306. *pS2-- = *pbuff++;
  307. /* Decrement the loop counter */
  308. i--;
  309. } while(i > 0u);
  310. /* pbuff initialized to input buffer */
  311. pbuff = pInlineBuffer;
  312. /* pS1 initialized to pState */
  313. pS1 = pState;
  314. /* Initializing the loop counter */
  315. i = (uint32_t) S->N;
  316. do
  317. {
  318. /* Writing the re-ordered output back to inplace input buffer */
  319. *pbuff++ = *pS1++;
  320. /* Decrement the loop counter */
  321. i--;
  322. } while(i > 0u);
  323. /* ---------------------------------------------------------
  324. * Step2: Calculate RFFT for N-point input
  325. * ---------------------------------------------------------- */
  326. /* pInlineBuffer is real input of length N , pState is the complex output of length 2N */
  327. arm_rfft_f32(S->pRfft, pInlineBuffer, pState);
  328. /*----------------------------------------------------------------------
  329. * Step3: Multiply the FFT output with the weights.
  330. *----------------------------------------------------------------------*/
  331. arm_cmplx_mult_cmplx_f32(pState, weights, pState, S->N);
  332. /* ----------- Post-processing ---------- */
  333. /* DCT-IV can be obtained from DCT-II by the equation,
  334. * Y4(k) = Y2(k) - Y4(k-1) and Y4(-1) = Y4(0)
  335. * Hence, Y4(0) = Y2(0)/2 */
  336. /* Getting only real part from the output and Converting to DCT-IV */
  337. /* pbuff initialized to input buffer. */
  338. pbuff = pInlineBuffer;
  339. /* pS1 initialized to pState */
  340. pS1 = pState;
  341. /* Calculating Y4(0) from Y2(0) using Y4(0) = Y2(0)/2 */
  342. in = *pS1++ * (float32_t) 0.5;
  343. /* input buffer acts as inplace, so output values are stored in the input itself. */
  344. *pbuff++ = in;
  345. /* pState pointer is incremented twice as the real values are located alternatively in the array */
  346. pS1++;
  347. /* Initializing the loop counter */
  348. i = ((uint32_t) S->N - 1u);
  349. do
  350. {
  351. /* Calculating Y4(1) to Y4(N-1) from Y2 using equation Y4(k) = Y2(k) - Y4(k-1) */
  352. /* pState pointer (pS1) is incremented twice as the real values are located alternatively in the array */
  353. in = *pS1++ - in;
  354. *pbuff++ = in;
  355. /* points to the next real value */
  356. pS1++;
  357. /* Decrement the loop counter */
  358. i--;
  359. } while(i > 0u);
  360. /*------------ Normalizing the output by multiplying with the normalizing factor ----------*/
  361. /* Initializing the loop counter */
  362. i = (uint32_t) S->N;
  363. /* pbuff initialized to the pInlineBuffer(now contains the output values) */
  364. pbuff = pInlineBuffer;
  365. do
  366. {
  367. /* Multiplying pInlineBuffer with the normalizing factor sqrt(2/N) */
  368. in = *pbuff;
  369. *pbuff++ = in * S->normalize;
  370. /* Decrement the loop counter */
  371. i--;
  372. } while(i > 0u);
  373. #endif /* #ifndef ARM_MATH_CM0_FAMILY */
  374. }
  375. /**
  376. * @} end of DCT4_IDCT4 group
  377. */