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
cmsis_dsp/SupportFunctions/math_helper.c@2:da51fb522205, 2013-05-30 (annotated)
- Committer:
- emilmont
- Date:
- Thu May 30 17:10:11 2013 +0100
- Revision:
- 2:da51fb522205
- Parent:
- 1:fdd22bb7aa52
- Child:
- 3:7a284390b0ce
Keep "cmsis-dsp" module in synch with its source
Who changed what in which revision?
User | Revision | Line number | New 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: 29. November 2010 |
emilmont | 2:da51fb522205 | 5 | * $Revision: V1.0.3 |
emilmont | 1:fdd22bb7aa52 | 6 | * |
emilmont | 2:da51fb522205 | 7 | * Project: CMSIS DSP Library |
emilmont | 1:fdd22bb7aa52 | 8 | * |
emilmont | 2:da51fb522205 | 9 | * Title: math_helper.c |
emilmont | 1:fdd22bb7aa52 | 10 | * |
emilmont | 2:da51fb522205 | 11 | * Description: Definition of all helper functions required. |
emilmont | 1:fdd22bb7aa52 | 12 | * |
emilmont | 1:fdd22bb7aa52 | 13 | * Target Processor: Cortex-M4/Cortex-M3 |
emilmont | 1:fdd22bb7aa52 | 14 | * |
emilmont | 1:fdd22bb7aa52 | 15 | * Version 1.0.3 2010/11/29 |
emilmont | 1:fdd22bb7aa52 | 16 | * Re-organized the CMSIS folders and updated documentation. |
emilmont | 1:fdd22bb7aa52 | 17 | * |
emilmont | 1:fdd22bb7aa52 | 18 | * Version 1.0.2 2010/11/11 |
emilmont | 1:fdd22bb7aa52 | 19 | * Documentation updated. |
emilmont | 1:fdd22bb7aa52 | 20 | * |
emilmont | 1:fdd22bb7aa52 | 21 | * Version 1.0.1 2010/10/05 |
emilmont | 1:fdd22bb7aa52 | 22 | * Production release and review comments incorporated. |
emilmont | 1:fdd22bb7aa52 | 23 | * |
emilmont | 1:fdd22bb7aa52 | 24 | * Version 1.0.0 2010/09/20 |
emilmont | 1:fdd22bb7aa52 | 25 | * Production release and review comments incorporated. |
emilmont | 1:fdd22bb7aa52 | 26 | * |
emilmont | 1:fdd22bb7aa52 | 27 | * Version 0.0.7 2010/06/10 |
emilmont | 1:fdd22bb7aa52 | 28 | * Misra-C changes done |
emilmont | 1:fdd22bb7aa52 | 29 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 30 | |
emilmont | 1:fdd22bb7aa52 | 31 | /* ---------------------------------------------------------------------- |
emilmont | 2:da51fb522205 | 32 | * Include standard header files |
emilmont | 1:fdd22bb7aa52 | 33 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 34 | #include<math.h> |
emilmont | 1:fdd22bb7aa52 | 35 | |
emilmont | 1:fdd22bb7aa52 | 36 | /* ---------------------------------------------------------------------- |
emilmont | 2:da51fb522205 | 37 | * Include project header files |
emilmont | 1:fdd22bb7aa52 | 38 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 39 | #include "math_helper.h" |
emilmont | 1:fdd22bb7aa52 | 40 | |
emilmont | 1:fdd22bb7aa52 | 41 | /** |
emilmont | 1:fdd22bb7aa52 | 42 | * @brief Caluclation of SNR |
emilmont | 2:da51fb522205 | 43 | * @param float* Pointer to the reference buffer |
emilmont | 2:da51fb522205 | 44 | * @param float* Pointer to the test buffer |
emilmont | 2:da51fb522205 | 45 | * @param uint32_t total number of samples |
emilmont | 2:da51fb522205 | 46 | * @return float SNR |
emilmont | 1:fdd22bb7aa52 | 47 | * The function Caluclates signal to noise ratio for the reference output |
emilmont | 1:fdd22bb7aa52 | 48 | * and test output |
emilmont | 1:fdd22bb7aa52 | 49 | */ |
emilmont | 1:fdd22bb7aa52 | 50 | |
emilmont | 1:fdd22bb7aa52 | 51 | float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize) |
emilmont | 1:fdd22bb7aa52 | 52 | { |
emilmont | 1:fdd22bb7aa52 | 53 | float EnergySignal = 0.0, EnergyError = 0.0; |
emilmont | 1:fdd22bb7aa52 | 54 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 55 | float SNR; |
emilmont | 1:fdd22bb7aa52 | 56 | int temp; |
emilmont | 1:fdd22bb7aa52 | 57 | int *test; |
emilmont | 1:fdd22bb7aa52 | 58 | |
emilmont | 1:fdd22bb7aa52 | 59 | for (i = 0; i < buffSize; i++) |
emilmont | 1:fdd22bb7aa52 | 60 | { |
emilmont | 2:da51fb522205 | 61 | /* Checking for a NAN value in pRef array */ |
emilmont | 2:da51fb522205 | 62 | test = (int *)(&pRef[i]); |
emilmont | 1:fdd22bb7aa52 | 63 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 64 | |
emilmont | 2:da51fb522205 | 65 | if(temp == 0x7FC00000) |
emilmont | 2:da51fb522205 | 66 | { |
emilmont | 2:da51fb522205 | 67 | return(0); |
emilmont | 2:da51fb522205 | 68 | } |
emilmont | 1:fdd22bb7aa52 | 69 | |
emilmont | 2:da51fb522205 | 70 | /* Checking for a NAN value in pTest array */ |
emilmont | 2:da51fb522205 | 71 | test = (int *)(&pTest[i]); |
emilmont | 1:fdd22bb7aa52 | 72 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 73 | |
emilmont | 2:da51fb522205 | 74 | if(temp == 0x7FC00000) |
emilmont | 2:da51fb522205 | 75 | { |
emilmont | 2:da51fb522205 | 76 | return(0); |
emilmont | 2:da51fb522205 | 77 | } |
emilmont | 1:fdd22bb7aa52 | 78 | EnergySignal += pRef[i] * pRef[i]; |
emilmont | 1:fdd22bb7aa52 | 79 | EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]); |
emilmont | 1:fdd22bb7aa52 | 80 | } |
emilmont | 1:fdd22bb7aa52 | 81 | |
emilmont | 2:da51fb522205 | 82 | /* Checking for a NAN value in EnergyError */ |
emilmont | 2:da51fb522205 | 83 | test = (int *)(&EnergyError); |
emilmont | 1:fdd22bb7aa52 | 84 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 85 | |
emilmont | 1:fdd22bb7aa52 | 86 | if(temp == 0x7FC00000) |
emilmont | 1:fdd22bb7aa52 | 87 | { |
emilmont | 2:da51fb522205 | 88 | return(0); |
emilmont | 1:fdd22bb7aa52 | 89 | } |
emilmont | 2:da51fb522205 | 90 | |
emilmont | 1:fdd22bb7aa52 | 91 | |
emilmont | 1:fdd22bb7aa52 | 92 | SNR = 10 * log10 (EnergySignal / EnergyError); |
emilmont | 1:fdd22bb7aa52 | 93 | |
emilmont | 1:fdd22bb7aa52 | 94 | return (SNR); |
emilmont | 1:fdd22bb7aa52 | 95 | |
emilmont | 1:fdd22bb7aa52 | 96 | } |
emilmont | 1:fdd22bb7aa52 | 97 | |
emilmont | 1:fdd22bb7aa52 | 98 | |
emilmont | 1:fdd22bb7aa52 | 99 | /** |
emilmont | 1:fdd22bb7aa52 | 100 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 101 | * @param q15_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 102 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 103 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 104 | * @return none |
emilmont | 1:fdd22bb7aa52 | 105 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 106 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 107 | */ |
emilmont | 1:fdd22bb7aa52 | 108 | |
emilmont | 1:fdd22bb7aa52 | 109 | void arm_provide_guard_bits_q15 (q15_t * input_buf, uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 110 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 111 | { |
emilmont | 1:fdd22bb7aa52 | 112 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 113 | |
emilmont | 1:fdd22bb7aa52 | 114 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 115 | { |
emilmont | 1:fdd22bb7aa52 | 116 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 117 | } |
emilmont | 1:fdd22bb7aa52 | 118 | } |
emilmont | 1:fdd22bb7aa52 | 119 | |
emilmont | 1:fdd22bb7aa52 | 120 | /** |
emilmont | 1:fdd22bb7aa52 | 121 | * @brief Converts float to fixed in q12.20 format |
emilmont | 2:da51fb522205 | 122 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 123 | * @return none |
emilmont | 1:fdd22bb7aa52 | 124 | * The function converts floating point values to fixed point(q12.20) values |
emilmont | 1:fdd22bb7aa52 | 125 | */ |
emilmont | 1:fdd22bb7aa52 | 126 | |
emilmont | 1:fdd22bb7aa52 | 127 | void arm_float_to_q12_20(float *pIn, q31_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 128 | { |
emilmont | 1:fdd22bb7aa52 | 129 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 130 | |
emilmont | 1:fdd22bb7aa52 | 131 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 132 | { |
emilmont | 2:da51fb522205 | 133 | /* 1048576.0f corresponds to pow(2, 20) */ |
emilmont | 1:fdd22bb7aa52 | 134 | pOut[i] = (q31_t) (pIn[i] * 1048576.0f); |
emilmont | 1:fdd22bb7aa52 | 135 | |
emilmont | 1:fdd22bb7aa52 | 136 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 137 | |
emilmont | 1:fdd22bb7aa52 | 138 | if (pIn[i] == (float) 1.0) |
emilmont | 1:fdd22bb7aa52 | 139 | { |
emilmont | 1:fdd22bb7aa52 | 140 | pOut[i] = 0x000FFFFF; |
emilmont | 1:fdd22bb7aa52 | 141 | } |
emilmont | 1:fdd22bb7aa52 | 142 | } |
emilmont | 1:fdd22bb7aa52 | 143 | } |
emilmont | 1:fdd22bb7aa52 | 144 | |
emilmont | 1:fdd22bb7aa52 | 145 | /** |
emilmont | 1:fdd22bb7aa52 | 146 | * @brief Compare MATLAB Reference Output and ARM Test output |
emilmont | 2:da51fb522205 | 147 | * @param q15_t* Pointer to Ref buffer |
emilmont | 2:da51fb522205 | 148 | * @param q15_t* Pointer to Test buffer |
emilmont | 2:da51fb522205 | 149 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 150 | * @return none |
emilmont | 1:fdd22bb7aa52 | 151 | */ |
emilmont | 1:fdd22bb7aa52 | 152 | |
emilmont | 1:fdd22bb7aa52 | 153 | uint32_t arm_compare_fixed_q15(q15_t *pIn, q15_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 154 | { |
emilmont | 1:fdd22bb7aa52 | 155 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 156 | int32_t diff, diffCrnt = 0; |
emilmont | 1:fdd22bb7aa52 | 157 | uint32_t maxDiff = 0; |
emilmont | 1:fdd22bb7aa52 | 158 | |
emilmont | 1:fdd22bb7aa52 | 159 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 160 | { |
emilmont | 2:da51fb522205 | 161 | diff = pIn[i] - pOut[i]; |
emilmont | 2:da51fb522205 | 162 | diffCrnt = (diff > 0) ? diff : -diff; |
emilmont | 1:fdd22bb7aa52 | 163 | |
emilmont | 2:da51fb522205 | 164 | if(diffCrnt > maxDiff) |
emilmont | 2:da51fb522205 | 165 | { |
emilmont | 2:da51fb522205 | 166 | maxDiff = diffCrnt; |
emilmont | 2:da51fb522205 | 167 | } |
emilmont | 1:fdd22bb7aa52 | 168 | } |
emilmont | 1:fdd22bb7aa52 | 169 | |
emilmont | 1:fdd22bb7aa52 | 170 | return(maxDiff); |
emilmont | 1:fdd22bb7aa52 | 171 | } |
emilmont | 1:fdd22bb7aa52 | 172 | |
emilmont | 1:fdd22bb7aa52 | 173 | /** |
emilmont | 1:fdd22bb7aa52 | 174 | * @brief Compare MATLAB Reference Output and ARM Test output |
emilmont | 2:da51fb522205 | 175 | * @param q31_t* Pointer to Ref buffer |
emilmont | 2:da51fb522205 | 176 | * @param q31_t* Pointer to Test buffer |
emilmont | 2:da51fb522205 | 177 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 178 | * @return none |
emilmont | 1:fdd22bb7aa52 | 179 | */ |
emilmont | 1:fdd22bb7aa52 | 180 | |
emilmont | 1:fdd22bb7aa52 | 181 | uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 182 | { |
emilmont | 1:fdd22bb7aa52 | 183 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 184 | int32_t diff, diffCrnt = 0; |
emilmont | 1:fdd22bb7aa52 | 185 | uint32_t maxDiff = 0; |
emilmont | 1:fdd22bb7aa52 | 186 | |
emilmont | 1:fdd22bb7aa52 | 187 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 188 | { |
emilmont | 2:da51fb522205 | 189 | diff = pIn[i] - pOut[i]; |
emilmont | 2:da51fb522205 | 190 | diffCrnt = (diff > 0) ? diff : -diff; |
emilmont | 1:fdd22bb7aa52 | 191 | |
emilmont | 2:da51fb522205 | 192 | if(diffCrnt > maxDiff) |
emilmont | 2:da51fb522205 | 193 | { |
emilmont | 2:da51fb522205 | 194 | maxDiff = diffCrnt; |
emilmont | 2:da51fb522205 | 195 | } |
emilmont | 1:fdd22bb7aa52 | 196 | } |
emilmont | 1:fdd22bb7aa52 | 197 | |
emilmont | 1:fdd22bb7aa52 | 198 | return(maxDiff); |
emilmont | 1:fdd22bb7aa52 | 199 | } |
emilmont | 1:fdd22bb7aa52 | 200 | |
emilmont | 1:fdd22bb7aa52 | 201 | /** |
emilmont | 1:fdd22bb7aa52 | 202 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 203 | * @param q31_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 204 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 205 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 206 | * @return none |
emilmont | 1:fdd22bb7aa52 | 207 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 208 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 209 | */ |
emilmont | 1:fdd22bb7aa52 | 210 | |
emilmont | 1:fdd22bb7aa52 | 211 | void arm_provide_guard_bits_q31 (q31_t * input_buf, |
emilmont | 2:da51fb522205 | 212 | uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 213 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 214 | { |
emilmont | 1:fdd22bb7aa52 | 215 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 216 | |
emilmont | 1:fdd22bb7aa52 | 217 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 218 | { |
emilmont | 1:fdd22bb7aa52 | 219 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 220 | } |
emilmont | 1:fdd22bb7aa52 | 221 | } |
emilmont | 1:fdd22bb7aa52 | 222 | |
emilmont | 1:fdd22bb7aa52 | 223 | /** |
emilmont | 1:fdd22bb7aa52 | 224 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 225 | * @param q31_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 226 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 227 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 228 | * @return none |
emilmont | 1:fdd22bb7aa52 | 229 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 230 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 231 | */ |
emilmont | 1:fdd22bb7aa52 | 232 | |
emilmont | 1:fdd22bb7aa52 | 233 | void arm_provide_guard_bits_q7 (q7_t * input_buf, |
emilmont | 2:da51fb522205 | 234 | uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 235 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 236 | { |
emilmont | 1:fdd22bb7aa52 | 237 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 238 | |
emilmont | 1:fdd22bb7aa52 | 239 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 240 | { |
emilmont | 1:fdd22bb7aa52 | 241 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 242 | } |
emilmont | 1:fdd22bb7aa52 | 243 | } |
emilmont | 1:fdd22bb7aa52 | 244 | |
emilmont | 1:fdd22bb7aa52 | 245 | |
emilmont | 1:fdd22bb7aa52 | 246 | |
emilmont | 1:fdd22bb7aa52 | 247 | /** |
emilmont | 1:fdd22bb7aa52 | 248 | * @brief Caluclates number of guard bits |
emilmont | 2:da51fb522205 | 249 | * @param uint32_t number of additions |
emilmont | 1:fdd22bb7aa52 | 250 | * @return none |
emilmont | 1:fdd22bb7aa52 | 251 | * The function Caluclates the number of guard bits |
emilmont | 1:fdd22bb7aa52 | 252 | * depending on the numtaps |
emilmont | 1:fdd22bb7aa52 | 253 | */ |
emilmont | 1:fdd22bb7aa52 | 254 | |
emilmont | 1:fdd22bb7aa52 | 255 | uint32_t arm_calc_guard_bits (uint32_t num_adds) |
emilmont | 1:fdd22bb7aa52 | 256 | { |
emilmont | 1:fdd22bb7aa52 | 257 | uint32_t i = 1, j = 0; |
emilmont | 1:fdd22bb7aa52 | 258 | |
emilmont | 1:fdd22bb7aa52 | 259 | if (num_adds == 1) |
emilmont | 1:fdd22bb7aa52 | 260 | { |
emilmont | 1:fdd22bb7aa52 | 261 | return (0); |
emilmont | 1:fdd22bb7aa52 | 262 | } |
emilmont | 1:fdd22bb7aa52 | 263 | |
emilmont | 1:fdd22bb7aa52 | 264 | while (i < num_adds) |
emilmont | 1:fdd22bb7aa52 | 265 | { |
emilmont | 1:fdd22bb7aa52 | 266 | i = i * 2; |
emilmont | 1:fdd22bb7aa52 | 267 | j++; |
emilmont | 1:fdd22bb7aa52 | 268 | } |
emilmont | 1:fdd22bb7aa52 | 269 | |
emilmont | 1:fdd22bb7aa52 | 270 | return (j); |
emilmont | 1:fdd22bb7aa52 | 271 | } |
emilmont | 1:fdd22bb7aa52 | 272 | |
emilmont | 1:fdd22bb7aa52 | 273 | /** |
emilmont | 1:fdd22bb7aa52 | 274 | * @brief Converts Q15 to floating-point |
emilmont | 2:da51fb522205 | 275 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 276 | * @return none |
emilmont | 1:fdd22bb7aa52 | 277 | */ |
emilmont | 1:fdd22bb7aa52 | 278 | |
emilmont | 1:fdd22bb7aa52 | 279 | void arm_apply_guard_bits (float32_t * pIn, |
emilmont | 2:da51fb522205 | 280 | uint32_t numSamples, |
emilmont | 2:da51fb522205 | 281 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 282 | { |
emilmont | 1:fdd22bb7aa52 | 283 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 284 | |
emilmont | 1:fdd22bb7aa52 | 285 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 286 | { |
emilmont | 1:fdd22bb7aa52 | 287 | pIn[i] = pIn[i] * arm_calc_2pow(guard_bits); |
emilmont | 1:fdd22bb7aa52 | 288 | } |
emilmont | 1:fdd22bb7aa52 | 289 | } |
emilmont | 1:fdd22bb7aa52 | 290 | |
emilmont | 1:fdd22bb7aa52 | 291 | /** |
emilmont | 1:fdd22bb7aa52 | 292 | * @brief Calculates pow(2, numShifts) |
emilmont | 2:da51fb522205 | 293 | * @param uint32_t number of shifts |
emilmont | 1:fdd22bb7aa52 | 294 | * @return pow(2, numShifts) |
emilmont | 1:fdd22bb7aa52 | 295 | */ |
emilmont | 1:fdd22bb7aa52 | 296 | uint32_t arm_calc_2pow(uint32_t numShifts) |
emilmont | 1:fdd22bb7aa52 | 297 | { |
emilmont | 1:fdd22bb7aa52 | 298 | |
emilmont | 1:fdd22bb7aa52 | 299 | uint32_t i, val = 1; |
emilmont | 1:fdd22bb7aa52 | 300 | |
emilmont | 1:fdd22bb7aa52 | 301 | for (i = 0; i < numShifts; i++) |
emilmont | 1:fdd22bb7aa52 | 302 | { |
emilmont | 1:fdd22bb7aa52 | 303 | val = val * 2; |
emilmont | 2:da51fb522205 | 304 | } |
emilmont | 1:fdd22bb7aa52 | 305 | |
emilmont | 1:fdd22bb7aa52 | 306 | return(val); |
emilmont | 1:fdd22bb7aa52 | 307 | } |
emilmont | 1:fdd22bb7aa52 | 308 | |
emilmont | 1:fdd22bb7aa52 | 309 | |
emilmont | 1:fdd22bb7aa52 | 310 | |
emilmont | 1:fdd22bb7aa52 | 311 | /** |
emilmont | 1:fdd22bb7aa52 | 312 | * @brief Converts float to fixed q14 |
emilmont | 2:da51fb522205 | 313 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 314 | * @return none |
emilmont | 1:fdd22bb7aa52 | 315 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 316 | */ |
emilmont | 1:fdd22bb7aa52 | 317 | |
emilmont | 1:fdd22bb7aa52 | 318 | void arm_float_to_q14 (float *pIn, q15_t * pOut, |
emilmont | 1:fdd22bb7aa52 | 319 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 320 | { |
emilmont | 1:fdd22bb7aa52 | 321 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 322 | |
emilmont | 1:fdd22bb7aa52 | 323 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 324 | { |
emilmont | 2:da51fb522205 | 325 | /* 16384.0f corresponds to pow(2, 14) */ |
emilmont | 1:fdd22bb7aa52 | 326 | pOut[i] = (q15_t) (pIn[i] * 16384.0f); |
emilmont | 1:fdd22bb7aa52 | 327 | |
emilmont | 1:fdd22bb7aa52 | 328 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 329 | |
emilmont | 1:fdd22bb7aa52 | 330 | if (pIn[i] == (float) 2.0) |
emilmont | 1:fdd22bb7aa52 | 331 | { |
emilmont | 1:fdd22bb7aa52 | 332 | pOut[i] = 0x7FFF; |
emilmont | 1:fdd22bb7aa52 | 333 | } |
emilmont | 1:fdd22bb7aa52 | 334 | |
emilmont | 1:fdd22bb7aa52 | 335 | } |
emilmont | 1:fdd22bb7aa52 | 336 | |
emilmont | 1:fdd22bb7aa52 | 337 | } |
emilmont | 1:fdd22bb7aa52 | 338 | |
emilmont | 1:fdd22bb7aa52 | 339 | |
emilmont | 1:fdd22bb7aa52 | 340 | /** |
emilmont | 1:fdd22bb7aa52 | 341 | * @brief Converts float to fixed q30 format |
emilmont | 2:da51fb522205 | 342 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 343 | * @return none |
emilmont | 1:fdd22bb7aa52 | 344 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 345 | */ |
emilmont | 1:fdd22bb7aa52 | 346 | |
emilmont | 1:fdd22bb7aa52 | 347 | void arm_float_to_q30 (float *pIn, q31_t * pOut, |
emilmont | 2:da51fb522205 | 348 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 349 | { |
emilmont | 1:fdd22bb7aa52 | 350 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 351 | |
emilmont | 1:fdd22bb7aa52 | 352 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 353 | { |
emilmont | 2:da51fb522205 | 354 | /* 1073741824.0f corresponds to pow(2, 30) */ |
emilmont | 1:fdd22bb7aa52 | 355 | pOut[i] = (q31_t) (pIn[i] * 1073741824.0f); |
emilmont | 1:fdd22bb7aa52 | 356 | |
emilmont | 1:fdd22bb7aa52 | 357 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 358 | |
emilmont | 1:fdd22bb7aa52 | 359 | if (pIn[i] == (float) 2.0) |
emilmont | 1:fdd22bb7aa52 | 360 | { |
emilmont | 1:fdd22bb7aa52 | 361 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 362 | } |
emilmont | 1:fdd22bb7aa52 | 363 | } |
emilmont | 1:fdd22bb7aa52 | 364 | } |
emilmont | 1:fdd22bb7aa52 | 365 | |
emilmont | 1:fdd22bb7aa52 | 366 | /** |
emilmont | 1:fdd22bb7aa52 | 367 | * @brief Converts float to fixed q30 format |
emilmont | 2:da51fb522205 | 368 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 369 | * @return none |
emilmont | 1:fdd22bb7aa52 | 370 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 371 | */ |
emilmont | 1:fdd22bb7aa52 | 372 | |
emilmont | 1:fdd22bb7aa52 | 373 | void arm_float_to_q29 (float *pIn, q31_t * pOut, |
emilmont | 2:da51fb522205 | 374 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 375 | { |
emilmont | 1:fdd22bb7aa52 | 376 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 377 | |
emilmont | 1:fdd22bb7aa52 | 378 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 379 | { |
emilmont | 2:da51fb522205 | 380 | /* 1073741824.0f corresponds to pow(2, 30) */ |
emilmont | 1:fdd22bb7aa52 | 381 | pOut[i] = (q31_t) (pIn[i] * 536870912.0f); |
emilmont | 1:fdd22bb7aa52 | 382 | |
emilmont | 1:fdd22bb7aa52 | 383 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 384 | |
emilmont | 1:fdd22bb7aa52 | 385 | if (pIn[i] == (float) 4.0) |
emilmont | 1:fdd22bb7aa52 | 386 | { |
emilmont | 1:fdd22bb7aa52 | 387 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 388 | } |
emilmont | 1:fdd22bb7aa52 | 389 | } |
emilmont | 1:fdd22bb7aa52 | 390 | } |
emilmont | 1:fdd22bb7aa52 | 391 | |
emilmont | 1:fdd22bb7aa52 | 392 | |
emilmont | 1:fdd22bb7aa52 | 393 | /** |
emilmont | 1:fdd22bb7aa52 | 394 | * @brief Converts float to fixed q28 format |
emilmont | 2:da51fb522205 | 395 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 396 | * @return none |
emilmont | 1:fdd22bb7aa52 | 397 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 398 | */ |
emilmont | 1:fdd22bb7aa52 | 399 | |
emilmont | 1:fdd22bb7aa52 | 400 | void arm_float_to_q28 (float *pIn, q31_t * pOut, |
emilmont | 1:fdd22bb7aa52 | 401 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 402 | { |
emilmont | 1:fdd22bb7aa52 | 403 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 404 | |
emilmont | 1:fdd22bb7aa52 | 405 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 406 | { |
emilmont | 2:da51fb522205 | 407 | /* 268435456.0f corresponds to pow(2, 28) */ |
emilmont | 1:fdd22bb7aa52 | 408 | pOut[i] = (q31_t) (pIn[i] * 268435456.0f); |
emilmont | 1:fdd22bb7aa52 | 409 | |
emilmont | 1:fdd22bb7aa52 | 410 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 411 | |
emilmont | 1:fdd22bb7aa52 | 412 | if (pIn[i] == (float) 8.0) |
emilmont | 1:fdd22bb7aa52 | 413 | { |
emilmont | 1:fdd22bb7aa52 | 414 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 415 | } |
emilmont | 1:fdd22bb7aa52 | 416 | } |
emilmont | 1:fdd22bb7aa52 | 417 | } |
emilmont | 1:fdd22bb7aa52 | 418 | |
emilmont | 1:fdd22bb7aa52 | 419 | /** |
emilmont | 1:fdd22bb7aa52 | 420 | * @brief Clip the float values to +/- 1 |
emilmont | 2:da51fb522205 | 421 | * @param pIn input buffer |
emilmont | 2:da51fb522205 | 422 | * @param numSamples number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 423 | * @return none |
emilmont | 1:fdd22bb7aa52 | 424 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 425 | */ |
emilmont | 1:fdd22bb7aa52 | 426 | |
emilmont | 1:fdd22bb7aa52 | 427 | void arm_clip_f32 (float *pIn, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 428 | { |
emilmont | 1:fdd22bb7aa52 | 429 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 430 | |
emilmont | 1:fdd22bb7aa52 | 431 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 432 | { |
emilmont | 1:fdd22bb7aa52 | 433 | if(pIn[i] > 1.0f) |
emilmont | 2:da51fb522205 | 434 | { |
emilmont | 2:da51fb522205 | 435 | pIn[i] = 1.0; |
emilmont | 2:da51fb522205 | 436 | } |
emilmont | 2:da51fb522205 | 437 | else if( pIn[i] < -1.0f) |
emilmont | 2:da51fb522205 | 438 | { |
emilmont | 2:da51fb522205 | 439 | pIn[i] = -1.0; |
emilmont | 2:da51fb522205 | 440 | } |
emilmont | 2:da51fb522205 | 441 | |
emilmont | 1:fdd22bb7aa52 | 442 | } |
emilmont | 1:fdd22bb7aa52 | 443 | } |
emilmont | 1:fdd22bb7aa52 | 444 | |
emilmont | 1:fdd22bb7aa52 | 445 | |
emilmont | 1:fdd22bb7aa52 | 446 | |
emilmont | 1:fdd22bb7aa52 | 447 |