CMSIS DSP library

Dependents:   KL25Z_FFT_Demo Hat_Board_v5_1 KL25Z_FFT_Demo_tony KL25Z_FFT_Demo_tony ... more

Fork of mbed-dsp by mbed official

Committer:
emilmont
Date:
Wed Nov 28 12:30:09 2012 +0000
Revision:
1:fdd22bb7aa52
Child:
2:da51fb522205
DSP library code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
emilmont 1:fdd22bb7aa52 1 /* ----------------------------------------------------------------------
emilmont 1:fdd22bb7aa52 2 * Copyright (C) 2010 ARM Limited. All rights reserved.
emilmont 1:fdd22bb7aa52 3 *
emilmont 1:fdd22bb7aa52 4 * $Date: 15. February 2012
emilmont 1:fdd22bb7aa52 5 * $Revision: V1.1.0
emilmont 1:fdd22bb7aa52 6 *
emilmont 1:fdd22bb7aa52 7 * Project: CMSIS DSP Library
emilmont 1:fdd22bb7aa52 8 * Title: arm_cos_q15.c
emilmont 1:fdd22bb7aa52 9 *
emilmont 1:fdd22bb7aa52 10 * Description: Fast cosine calculation for Q15 values.
emilmont 1:fdd22bb7aa52 11 *
emilmont 1:fdd22bb7aa52 12 * Target Processor: Cortex-M4/Cortex-M3/Cortex-M0
emilmont 1:fdd22bb7aa52 13 *
emilmont 1:fdd22bb7aa52 14 * Version 1.1.0 2012/02/15
emilmont 1:fdd22bb7aa52 15 * Updated with more optimizations, bug fixes and minor API changes.
emilmont 1:fdd22bb7aa52 16 *
emilmont 1:fdd22bb7aa52 17 * Version 1.0.10 2011/7/15
emilmont 1:fdd22bb7aa52 18 * Big Endian support added and Merged M0 and M3/M4 Source code.
emilmont 1:fdd22bb7aa52 19 *
emilmont 1:fdd22bb7aa52 20 * Version 1.0.3 2010/11/29
emilmont 1:fdd22bb7aa52 21 * Re-organized the CMSIS folders and updated documentation.
emilmont 1:fdd22bb7aa52 22 *
emilmont 1:fdd22bb7aa52 23 * Version 1.0.2 2010/11/11
emilmont 1:fdd22bb7aa52 24 * Documentation updated.
emilmont 1:fdd22bb7aa52 25 *
emilmont 1:fdd22bb7aa52 26 * Version 1.0.1 2010/10/05
emilmont 1:fdd22bb7aa52 27 * Production release and review comments incorporated.
emilmont 1:fdd22bb7aa52 28 *
emilmont 1:fdd22bb7aa52 29 * Version 1.0.0 2010/09/20
emilmont 1:fdd22bb7aa52 30 * Production release and review comments incorporated.
emilmont 1:fdd22bb7aa52 31 * -------------------------------------------------------------------- */
emilmont 1:fdd22bb7aa52 32
emilmont 1:fdd22bb7aa52 33 #include "arm_math.h"
emilmont 1:fdd22bb7aa52 34
emilmont 1:fdd22bb7aa52 35 /**
emilmont 1:fdd22bb7aa52 36 * @ingroup groupFastMath
emilmont 1:fdd22bb7aa52 37 */
emilmont 1:fdd22bb7aa52 38
emilmont 1:fdd22bb7aa52 39 /**
emilmont 1:fdd22bb7aa52 40 * @addtogroup cos
emilmont 1:fdd22bb7aa52 41 * @{
emilmont 1:fdd22bb7aa52 42 */
emilmont 1:fdd22bb7aa52 43
emilmont 1:fdd22bb7aa52 44 /**
emilmont 1:fdd22bb7aa52 45 * \par
emilmont 1:fdd22bb7aa52 46 * Table Values are in Q15(1.15 Fixed point format) and generation is done in three steps
emilmont 1:fdd22bb7aa52 47 * \par
emilmont 1:fdd22bb7aa52 48 * First Generate cos values in floating point:
emilmont 1:fdd22bb7aa52 49 * tableSize = 256;
emilmont 1:fdd22bb7aa52 50 * <pre>for(n = -1; n < (tableSize + 1); n++)
emilmont 1:fdd22bb7aa52 51 * {
emilmont 1:fdd22bb7aa52 52 * cosTable[n+1]= cos(2*pi*n/tableSize);
emilmont 1:fdd22bb7aa52 53 * }</pre>
emilmont 1:fdd22bb7aa52 54 * where pi value is 3.14159265358979
emilmont 1:fdd22bb7aa52 55 * \par
emilmont 1:fdd22bb7aa52 56 * Secondly Convert Floating point to Q15(Fixed point):
emilmont 1:fdd22bb7aa52 57 * (cosTable[i] * pow(2, 15))
emilmont 1:fdd22bb7aa52 58 * \par
emilmont 1:fdd22bb7aa52 59 * Finally Rounding to nearest integer is done
emilmont 1:fdd22bb7aa52 60 * cosTable[i] += (cosTable[i] > 0 ? 0.5 :-0.5);
emilmont 1:fdd22bb7aa52 61 */
emilmont 1:fdd22bb7aa52 62
emilmont 1:fdd22bb7aa52 63 static const q15_t cosTableQ15[259] = {
emilmont 1:fdd22bb7aa52 64 0x7ff6, 0x7fff, 0x7ff6, 0x7fd9, 0x7fa7, 0x7f62, 0x7f0a, 0x7e9d,
emilmont 1:fdd22bb7aa52 65 0x7e1e, 0x7d8a, 0x7ce4, 0x7c2a, 0x7b5d, 0x7a7d, 0x798a, 0x7885,
emilmont 1:fdd22bb7aa52 66 0x776c, 0x7642, 0x7505, 0x73b6, 0x7255, 0x70e3, 0x6f5f, 0x6dca,
emilmont 1:fdd22bb7aa52 67 0x6c24, 0x6a6e, 0x68a7, 0x66d0, 0x64e9, 0x62f2, 0x60ec, 0x5ed7,
emilmont 1:fdd22bb7aa52 68 0x5cb4, 0x5a82, 0x5843, 0x55f6, 0x539b, 0x5134, 0x4ec0, 0x4c40,
emilmont 1:fdd22bb7aa52 69 0x49b4, 0x471d, 0x447b, 0x41ce, 0x3f17, 0x3c57, 0x398d, 0x36ba,
emilmont 1:fdd22bb7aa52 70 0x33df, 0x30fc, 0x2e11, 0x2b1f, 0x2827, 0x2528, 0x2224, 0x1f1a,
emilmont 1:fdd22bb7aa52 71 0x1c0c, 0x18f9, 0x15e2, 0x12c8, 0xfab, 0xc8c, 0x96b, 0x648,
emilmont 1:fdd22bb7aa52 72 0x324, 0x0, 0xfcdc, 0xf9b8, 0xf695, 0xf374, 0xf055, 0xed38,
emilmont 1:fdd22bb7aa52 73 0xea1e, 0xe707, 0xe3f4, 0xe0e6, 0xdddc, 0xdad8, 0xd7d9, 0xd4e1,
emilmont 1:fdd22bb7aa52 74 0xd1ef, 0xcf04, 0xcc21, 0xc946, 0xc673, 0xc3a9, 0xc0e9, 0xbe32,
emilmont 1:fdd22bb7aa52 75 0xbb85, 0xb8e3, 0xb64c, 0xb3c0, 0xb140, 0xaecc, 0xac65, 0xaa0a,
emilmont 1:fdd22bb7aa52 76 0xa7bd, 0xa57e, 0xa34c, 0xa129, 0x9f14, 0x9d0e, 0x9b17, 0x9930,
emilmont 1:fdd22bb7aa52 77 0x9759, 0x9592, 0x93dc, 0x9236, 0x90a1, 0x8f1d, 0x8dab, 0x8c4a,
emilmont 1:fdd22bb7aa52 78 0x8afb, 0x89be, 0x8894, 0x877b, 0x8676, 0x8583, 0x84a3, 0x83d6,
emilmont 1:fdd22bb7aa52 79 0x831c, 0x8276, 0x81e2, 0x8163, 0x80f6, 0x809e, 0x8059, 0x8027,
emilmont 1:fdd22bb7aa52 80 0x800a, 0x8000, 0x800a, 0x8027, 0x8059, 0x809e, 0x80f6, 0x8163,
emilmont 1:fdd22bb7aa52 81 0x81e2, 0x8276, 0x831c, 0x83d6, 0x84a3, 0x8583, 0x8676, 0x877b,
emilmont 1:fdd22bb7aa52 82 0x8894, 0x89be, 0x8afb, 0x8c4a, 0x8dab, 0x8f1d, 0x90a1, 0x9236,
emilmont 1:fdd22bb7aa52 83 0x93dc, 0x9592, 0x9759, 0x9930, 0x9b17, 0x9d0e, 0x9f14, 0xa129,
emilmont 1:fdd22bb7aa52 84 0xa34c, 0xa57e, 0xa7bd, 0xaa0a, 0xac65, 0xaecc, 0xb140, 0xb3c0,
emilmont 1:fdd22bb7aa52 85 0xb64c, 0xb8e3, 0xbb85, 0xbe32, 0xc0e9, 0xc3a9, 0xc673, 0xc946,
emilmont 1:fdd22bb7aa52 86 0xcc21, 0xcf04, 0xd1ef, 0xd4e1, 0xd7d9, 0xdad8, 0xdddc, 0xe0e6,
emilmont 1:fdd22bb7aa52 87 0xe3f4, 0xe707, 0xea1e, 0xed38, 0xf055, 0xf374, 0xf695, 0xf9b8,
emilmont 1:fdd22bb7aa52 88 0xfcdc, 0x0, 0x324, 0x648, 0x96b, 0xc8c, 0xfab, 0x12c8,
emilmont 1:fdd22bb7aa52 89 0x15e2, 0x18f9, 0x1c0c, 0x1f1a, 0x2224, 0x2528, 0x2827, 0x2b1f,
emilmont 1:fdd22bb7aa52 90 0x2e11, 0x30fc, 0x33df, 0x36ba, 0x398d, 0x3c57, 0x3f17, 0x41ce,
emilmont 1:fdd22bb7aa52 91 0x447b, 0x471d, 0x49b4, 0x4c40, 0x4ec0, 0x5134, 0x539b, 0x55f6,
emilmont 1:fdd22bb7aa52 92 0x5843, 0x5a82, 0x5cb4, 0x5ed7, 0x60ec, 0x62f2, 0x64e9, 0x66d0,
emilmont 1:fdd22bb7aa52 93 0x68a7, 0x6a6e, 0x6c24, 0x6dca, 0x6f5f, 0x70e3, 0x7255, 0x73b6,
emilmont 1:fdd22bb7aa52 94 0x7505, 0x7642, 0x776c, 0x7885, 0x798a, 0x7a7d, 0x7b5d, 0x7c2a,
emilmont 1:fdd22bb7aa52 95 0x7ce4, 0x7d8a, 0x7e1e, 0x7e9d, 0x7f0a, 0x7f62, 0x7fa7, 0x7fd9,
emilmont 1:fdd22bb7aa52 96 0x7ff6, 0x7fff, 0x7ff6
emilmont 1:fdd22bb7aa52 97 };
emilmont 1:fdd22bb7aa52 98
emilmont 1:fdd22bb7aa52 99
emilmont 1:fdd22bb7aa52 100 /**
emilmont 1:fdd22bb7aa52 101 * @brief Fast approximation to the trigonometric cosine function for Q15 data.
emilmont 1:fdd22bb7aa52 102 * @param[in] x Scaled input value in radians.
emilmont 1:fdd22bb7aa52 103 * @return cos(x).
emilmont 1:fdd22bb7aa52 104 *
emilmont 1:fdd22bb7aa52 105 * The Q15 input value is in the range [0 +0.9999] and is mapped to a radian value in the range [0 2*pi), Here range excludes 2*pi.
emilmont 1:fdd22bb7aa52 106 */
emilmont 1:fdd22bb7aa52 107
emilmont 1:fdd22bb7aa52 108 q15_t arm_cos_q15(
emilmont 1:fdd22bb7aa52 109 q15_t x)
emilmont 1:fdd22bb7aa52 110 {
emilmont 1:fdd22bb7aa52 111 q31_t cosVal; /* Temporary variable for output */
emilmont 1:fdd22bb7aa52 112 q15_t *tablePtr; /* Pointer to table */
emilmont 1:fdd22bb7aa52 113 q15_t in, in2; /* Temporary variables for input */
emilmont 1:fdd22bb7aa52 114 q31_t wa, wb, wc, wd; /* Cubic interpolation coefficients */
emilmont 1:fdd22bb7aa52 115 q15_t a, b, c, d; /* Four nearest output values */
emilmont 1:fdd22bb7aa52 116 q15_t fract, fractCube, fractSquare; /* Variables for fractional value */
emilmont 1:fdd22bb7aa52 117 q15_t oneBy6 = 0x1555; /* Fixed point value of 1/6 */
emilmont 1:fdd22bb7aa52 118 q15_t tableSpacing = TABLE_SPACING_Q15; /* Table spacing */
emilmont 1:fdd22bb7aa52 119 int32_t index; /* Index variable */
emilmont 1:fdd22bb7aa52 120
emilmont 1:fdd22bb7aa52 121 in = x;
emilmont 1:fdd22bb7aa52 122
emilmont 1:fdd22bb7aa52 123 /* Calculate the nearest index */
emilmont 1:fdd22bb7aa52 124 index = (int32_t) in / tableSpacing;
emilmont 1:fdd22bb7aa52 125
emilmont 1:fdd22bb7aa52 126 /* Calculate the nearest value of input */
emilmont 1:fdd22bb7aa52 127 in2 = (q15_t) index *tableSpacing;
emilmont 1:fdd22bb7aa52 128
emilmont 1:fdd22bb7aa52 129 /* Calculation of fractional value */
emilmont 1:fdd22bb7aa52 130 fract = (in - in2) << 8;
emilmont 1:fdd22bb7aa52 131
emilmont 1:fdd22bb7aa52 132 /* fractSquare = fract * fract */
emilmont 1:fdd22bb7aa52 133 fractSquare = (q15_t) ((fract * fract) >> 15);
emilmont 1:fdd22bb7aa52 134
emilmont 1:fdd22bb7aa52 135 /* fractCube = fract * fract * fract */
emilmont 1:fdd22bb7aa52 136 fractCube = (q15_t) ((fractSquare * fract) >> 15);
emilmont 1:fdd22bb7aa52 137
emilmont 1:fdd22bb7aa52 138 /* Checking min and max index of table */
emilmont 1:fdd22bb7aa52 139 if(index < 0)
emilmont 1:fdd22bb7aa52 140 {
emilmont 1:fdd22bb7aa52 141 index = 0;
emilmont 1:fdd22bb7aa52 142 }
emilmont 1:fdd22bb7aa52 143 else if(index > 256)
emilmont 1:fdd22bb7aa52 144 {
emilmont 1:fdd22bb7aa52 145 index = 256;
emilmont 1:fdd22bb7aa52 146 }
emilmont 1:fdd22bb7aa52 147
emilmont 1:fdd22bb7aa52 148 /* Initialise table pointer */
emilmont 1:fdd22bb7aa52 149 tablePtr = (q15_t *) & cosTableQ15[index];
emilmont 1:fdd22bb7aa52 150
emilmont 1:fdd22bb7aa52 151 /* Cubic interpolation process */
emilmont 1:fdd22bb7aa52 152 /* Calculation of wa */
emilmont 1:fdd22bb7aa52 153 /* wa = -(oneBy6)*fractCube + (fractSquare >> 1u) - (0x2AAA)*fract; */
emilmont 1:fdd22bb7aa52 154 wa = (q31_t) oneBy6 *fractCube;
emilmont 1:fdd22bb7aa52 155 wa += (q31_t) 0x2AAA *fract;
emilmont 1:fdd22bb7aa52 156 wa = -(wa >> 15);
emilmont 1:fdd22bb7aa52 157 wa += (fractSquare >> 1u);
emilmont 1:fdd22bb7aa52 158
emilmont 1:fdd22bb7aa52 159 /* Read first nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 160 a = *tablePtr++;
emilmont 1:fdd22bb7aa52 161
emilmont 1:fdd22bb7aa52 162 /* cosVal = a * wa */
emilmont 1:fdd22bb7aa52 163 cosVal = a * wa;
emilmont 1:fdd22bb7aa52 164
emilmont 1:fdd22bb7aa52 165 /* Calculation of wb */
emilmont 1:fdd22bb7aa52 166 wb = (((fractCube >> 1u) - fractSquare) - (fract >> 1u)) + 0x7FFF;
emilmont 1:fdd22bb7aa52 167
emilmont 1:fdd22bb7aa52 168 /* Read second nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 169 b = *tablePtr++;
emilmont 1:fdd22bb7aa52 170
emilmont 1:fdd22bb7aa52 171 /* cosVal += b*wb */
emilmont 1:fdd22bb7aa52 172 cosVal += b * wb;
emilmont 1:fdd22bb7aa52 173
emilmont 1:fdd22bb7aa52 174 /* Calculation of wc */
emilmont 1:fdd22bb7aa52 175 wc = -(q31_t) fractCube + fractSquare;
emilmont 1:fdd22bb7aa52 176 wc = (wc >> 1u) + fract;
emilmont 1:fdd22bb7aa52 177
emilmont 1:fdd22bb7aa52 178 /* Read third nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 179 c = *tablePtr++;
emilmont 1:fdd22bb7aa52 180
emilmont 1:fdd22bb7aa52 181 /* cosVal += c*wc */
emilmont 1:fdd22bb7aa52 182 cosVal += c * wc;
emilmont 1:fdd22bb7aa52 183
emilmont 1:fdd22bb7aa52 184 /* Calculation of wd */
emilmont 1:fdd22bb7aa52 185 /* wd = (oneBy6)*fractCube - (oneBy6)*fract; */
emilmont 1:fdd22bb7aa52 186 fractCube = fractCube - fract;
emilmont 1:fdd22bb7aa52 187 wd = ((q15_t) (((q31_t) oneBy6 * fractCube) >> 15));
emilmont 1:fdd22bb7aa52 188
emilmont 1:fdd22bb7aa52 189 /* Read fourth nearest value of output from the cos table */
emilmont 1:fdd22bb7aa52 190 d = *tablePtr++;
emilmont 1:fdd22bb7aa52 191
emilmont 1:fdd22bb7aa52 192 /* cosVal += d*wd; */
emilmont 1:fdd22bb7aa52 193 cosVal += d * wd;
emilmont 1:fdd22bb7aa52 194
emilmont 1:fdd22bb7aa52 195 /* Convert output value in 1.15(q15) format and saturate */
emilmont 1:fdd22bb7aa52 196 cosVal = __SSAT((cosVal >> 15), 16);
emilmont 1:fdd22bb7aa52 197
emilmont 1:fdd22bb7aa52 198 /* Return the output value in 1.15(q15) format */
emilmont 1:fdd22bb7aa52 199 return ((q15_t) cosVal);
emilmont 1:fdd22bb7aa52 200
emilmont 1:fdd22bb7aa52 201 }
emilmont 1:fdd22bb7aa52 202
emilmont 1:fdd22bb7aa52 203 /**
emilmont 1:fdd22bb7aa52 204 * @} end of cos group
emilmont 1:fdd22bb7aa52 205 */