CMSIS DSP Lib
Fork of mbed-dsp by
cmsis_dsp/SupportFunctions/math_helper.c@4:9cee975aadce, 2014-06-23 (annotated)
- Committer:
- mbed_official
- Date:
- Mon Jun 23 09:30:09 2014 +0100
- Revision:
- 4:9cee975aadce
- Parent:
- 3:7a284390b0ce
Synchronized with git revision 6e7c7bcec41226f536474daae3c13d49e4c0e865
Full URL: https://github.com/mbedmicro/mbed/commit/6e7c7bcec41226f536474daae3c13d49e4c0e865/
Fix signed unsigned compare in dsp library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 3:7a284390b0ce | 1 | /* ---------------------------------------------------------------------- |
mbed_official | 3:7a284390b0ce | 2 | * Copyright (C) 2010-2012 ARM Limited. All rights reserved. |
mbed_official | 3:7a284390b0ce | 3 | * |
mbed_official | 3:7a284390b0ce | 4 | * $Date: 17. January 2013 |
mbed_official | 3:7a284390b0ce | 5 | * $Revision: V1.4.0 |
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 | * |
mbed_official | 3:7a284390b0ce | 15 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 3:7a284390b0ce | 16 | * modification, are permitted provided that the following conditions |
mbed_official | 3:7a284390b0ce | 17 | * are met: |
mbed_official | 3:7a284390b0ce | 18 | * - Redistributions of source code must retain the above copyright |
mbed_official | 3:7a284390b0ce | 19 | * notice, this list of conditions and the following disclaimer. |
mbed_official | 3:7a284390b0ce | 20 | * - Redistributions in binary form must reproduce the above copyright |
mbed_official | 3:7a284390b0ce | 21 | * notice, this list of conditions and the following disclaimer in |
mbed_official | 3:7a284390b0ce | 22 | * the documentation and/or other materials provided with the |
mbed_official | 3:7a284390b0ce | 23 | * distribution. |
mbed_official | 3:7a284390b0ce | 24 | * - Neither the name of ARM LIMITED nor the names of its contributors |
mbed_official | 3:7a284390b0ce | 25 | * may be used to endorse or promote products derived from this |
mbed_official | 3:7a284390b0ce | 26 | * software without specific prior written permission. |
mbed_official | 3:7a284390b0ce | 27 | * |
mbed_official | 3:7a284390b0ce | 28 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
mbed_official | 3:7a284390b0ce | 29 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
mbed_official | 3:7a284390b0ce | 30 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
mbed_official | 3:7a284390b0ce | 31 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
mbed_official | 3:7a284390b0ce | 32 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
mbed_official | 3:7a284390b0ce | 33 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
mbed_official | 3:7a284390b0ce | 34 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
mbed_official | 3:7a284390b0ce | 35 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mbed_official | 3:7a284390b0ce | 36 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
mbed_official | 3:7a284390b0ce | 37 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 3:7a284390b0ce | 38 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 3:7a284390b0ce | 39 | * POSSIBILITY OF SUCH DAMAGE. |
emilmont | 1:fdd22bb7aa52 | 40 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 41 | |
emilmont | 1:fdd22bb7aa52 | 42 | /* ---------------------------------------------------------------------- |
emilmont | 2:da51fb522205 | 43 | * Include standard header files |
emilmont | 1:fdd22bb7aa52 | 44 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 45 | #include<math.h> |
emilmont | 1:fdd22bb7aa52 | 46 | |
emilmont | 1:fdd22bb7aa52 | 47 | /* ---------------------------------------------------------------------- |
emilmont | 2:da51fb522205 | 48 | * Include project header files |
emilmont | 1:fdd22bb7aa52 | 49 | * -------------------------------------------------------------------- */ |
emilmont | 1:fdd22bb7aa52 | 50 | #include "math_helper.h" |
emilmont | 1:fdd22bb7aa52 | 51 | |
emilmont | 1:fdd22bb7aa52 | 52 | /** |
emilmont | 1:fdd22bb7aa52 | 53 | * @brief Caluclation of SNR |
emilmont | 2:da51fb522205 | 54 | * @param float* Pointer to the reference buffer |
emilmont | 2:da51fb522205 | 55 | * @param float* Pointer to the test buffer |
emilmont | 2:da51fb522205 | 56 | * @param uint32_t total number of samples |
emilmont | 2:da51fb522205 | 57 | * @return float SNR |
emilmont | 1:fdd22bb7aa52 | 58 | * The function Caluclates signal to noise ratio for the reference output |
emilmont | 1:fdd22bb7aa52 | 59 | * and test output |
emilmont | 1:fdd22bb7aa52 | 60 | */ |
emilmont | 1:fdd22bb7aa52 | 61 | |
emilmont | 1:fdd22bb7aa52 | 62 | float arm_snr_f32(float *pRef, float *pTest, uint32_t buffSize) |
emilmont | 1:fdd22bb7aa52 | 63 | { |
emilmont | 1:fdd22bb7aa52 | 64 | float EnergySignal = 0.0, EnergyError = 0.0; |
emilmont | 1:fdd22bb7aa52 | 65 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 66 | float SNR; |
emilmont | 1:fdd22bb7aa52 | 67 | int temp; |
emilmont | 1:fdd22bb7aa52 | 68 | int *test; |
emilmont | 1:fdd22bb7aa52 | 69 | |
emilmont | 1:fdd22bb7aa52 | 70 | for (i = 0; i < buffSize; i++) |
emilmont | 1:fdd22bb7aa52 | 71 | { |
emilmont | 2:da51fb522205 | 72 | /* Checking for a NAN value in pRef array */ |
emilmont | 2:da51fb522205 | 73 | test = (int *)(&pRef[i]); |
emilmont | 1:fdd22bb7aa52 | 74 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 75 | |
emilmont | 2:da51fb522205 | 76 | if(temp == 0x7FC00000) |
emilmont | 2:da51fb522205 | 77 | { |
emilmont | 2:da51fb522205 | 78 | return(0); |
emilmont | 2:da51fb522205 | 79 | } |
emilmont | 1:fdd22bb7aa52 | 80 | |
emilmont | 2:da51fb522205 | 81 | /* Checking for a NAN value in pTest array */ |
emilmont | 2:da51fb522205 | 82 | test = (int *)(&pTest[i]); |
emilmont | 1:fdd22bb7aa52 | 83 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 84 | |
emilmont | 2:da51fb522205 | 85 | if(temp == 0x7FC00000) |
emilmont | 2:da51fb522205 | 86 | { |
emilmont | 2:da51fb522205 | 87 | return(0); |
emilmont | 2:da51fb522205 | 88 | } |
emilmont | 1:fdd22bb7aa52 | 89 | EnergySignal += pRef[i] * pRef[i]; |
emilmont | 1:fdd22bb7aa52 | 90 | EnergyError += (pRef[i] - pTest[i]) * (pRef[i] - pTest[i]); |
emilmont | 1:fdd22bb7aa52 | 91 | } |
emilmont | 1:fdd22bb7aa52 | 92 | |
emilmont | 2:da51fb522205 | 93 | /* Checking for a NAN value in EnergyError */ |
emilmont | 2:da51fb522205 | 94 | test = (int *)(&EnergyError); |
emilmont | 1:fdd22bb7aa52 | 95 | temp = *test; |
emilmont | 1:fdd22bb7aa52 | 96 | |
emilmont | 1:fdd22bb7aa52 | 97 | if(temp == 0x7FC00000) |
emilmont | 1:fdd22bb7aa52 | 98 | { |
emilmont | 2:da51fb522205 | 99 | return(0); |
emilmont | 1:fdd22bb7aa52 | 100 | } |
emilmont | 2:da51fb522205 | 101 | |
emilmont | 1:fdd22bb7aa52 | 102 | |
emilmont | 1:fdd22bb7aa52 | 103 | SNR = 10 * log10 (EnergySignal / EnergyError); |
emilmont | 1:fdd22bb7aa52 | 104 | |
emilmont | 1:fdd22bb7aa52 | 105 | return (SNR); |
emilmont | 1:fdd22bb7aa52 | 106 | |
emilmont | 1:fdd22bb7aa52 | 107 | } |
emilmont | 1:fdd22bb7aa52 | 108 | |
emilmont | 1:fdd22bb7aa52 | 109 | |
emilmont | 1:fdd22bb7aa52 | 110 | /** |
emilmont | 1:fdd22bb7aa52 | 111 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 112 | * @param q15_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 113 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 114 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 115 | * @return none |
emilmont | 1:fdd22bb7aa52 | 116 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 117 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 118 | */ |
emilmont | 1:fdd22bb7aa52 | 119 | |
emilmont | 1:fdd22bb7aa52 | 120 | void arm_provide_guard_bits_q15 (q15_t * input_buf, uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 121 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 122 | { |
emilmont | 1:fdd22bb7aa52 | 123 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 124 | |
emilmont | 1:fdd22bb7aa52 | 125 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 126 | { |
emilmont | 1:fdd22bb7aa52 | 127 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 128 | } |
emilmont | 1:fdd22bb7aa52 | 129 | } |
emilmont | 1:fdd22bb7aa52 | 130 | |
emilmont | 1:fdd22bb7aa52 | 131 | /** |
emilmont | 1:fdd22bb7aa52 | 132 | * @brief Converts float to fixed in q12.20 format |
emilmont | 2:da51fb522205 | 133 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 134 | * @return none |
emilmont | 1:fdd22bb7aa52 | 135 | * The function converts floating point values to fixed point(q12.20) values |
emilmont | 1:fdd22bb7aa52 | 136 | */ |
emilmont | 1:fdd22bb7aa52 | 137 | |
emilmont | 1:fdd22bb7aa52 | 138 | void arm_float_to_q12_20(float *pIn, q31_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 139 | { |
emilmont | 1:fdd22bb7aa52 | 140 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 141 | |
emilmont | 1:fdd22bb7aa52 | 142 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 143 | { |
emilmont | 2:da51fb522205 | 144 | /* 1048576.0f corresponds to pow(2, 20) */ |
emilmont | 1:fdd22bb7aa52 | 145 | pOut[i] = (q31_t) (pIn[i] * 1048576.0f); |
emilmont | 1:fdd22bb7aa52 | 146 | |
emilmont | 1:fdd22bb7aa52 | 147 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 148 | |
emilmont | 1:fdd22bb7aa52 | 149 | if (pIn[i] == (float) 1.0) |
emilmont | 1:fdd22bb7aa52 | 150 | { |
emilmont | 1:fdd22bb7aa52 | 151 | pOut[i] = 0x000FFFFF; |
emilmont | 1:fdd22bb7aa52 | 152 | } |
emilmont | 1:fdd22bb7aa52 | 153 | } |
emilmont | 1:fdd22bb7aa52 | 154 | } |
emilmont | 1:fdd22bb7aa52 | 155 | |
emilmont | 1:fdd22bb7aa52 | 156 | /** |
emilmont | 1:fdd22bb7aa52 | 157 | * @brief Compare MATLAB Reference Output and ARM Test output |
emilmont | 2:da51fb522205 | 158 | * @param q15_t* Pointer to Ref buffer |
emilmont | 2:da51fb522205 | 159 | * @param q15_t* Pointer to Test buffer |
emilmont | 2:da51fb522205 | 160 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 161 | * @return none |
emilmont | 1:fdd22bb7aa52 | 162 | */ |
emilmont | 1:fdd22bb7aa52 | 163 | |
emilmont | 1:fdd22bb7aa52 | 164 | uint32_t arm_compare_fixed_q15(q15_t *pIn, q15_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 165 | { |
emilmont | 1:fdd22bb7aa52 | 166 | uint32_t i; |
mbed_official | 4:9cee975aadce | 167 | int32_t diff; |
mbed_official | 4:9cee975aadce | 168 | uint32_t diffCrnt = 0; |
emilmont | 1:fdd22bb7aa52 | 169 | uint32_t maxDiff = 0; |
emilmont | 1:fdd22bb7aa52 | 170 | |
emilmont | 1:fdd22bb7aa52 | 171 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 172 | { |
emilmont | 2:da51fb522205 | 173 | diff = pIn[i] - pOut[i]; |
emilmont | 2:da51fb522205 | 174 | diffCrnt = (diff > 0) ? diff : -diff; |
emilmont | 1:fdd22bb7aa52 | 175 | |
emilmont | 2:da51fb522205 | 176 | if(diffCrnt > maxDiff) |
emilmont | 2:da51fb522205 | 177 | { |
emilmont | 2:da51fb522205 | 178 | maxDiff = diffCrnt; |
emilmont | 2:da51fb522205 | 179 | } |
emilmont | 1:fdd22bb7aa52 | 180 | } |
emilmont | 1:fdd22bb7aa52 | 181 | |
emilmont | 1:fdd22bb7aa52 | 182 | return(maxDiff); |
emilmont | 1:fdd22bb7aa52 | 183 | } |
emilmont | 1:fdd22bb7aa52 | 184 | |
emilmont | 1:fdd22bb7aa52 | 185 | /** |
emilmont | 1:fdd22bb7aa52 | 186 | * @brief Compare MATLAB Reference Output and ARM Test output |
emilmont | 2:da51fb522205 | 187 | * @param q31_t* Pointer to Ref buffer |
emilmont | 2:da51fb522205 | 188 | * @param q31_t* Pointer to Test buffer |
emilmont | 2:da51fb522205 | 189 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 190 | * @return none |
emilmont | 1:fdd22bb7aa52 | 191 | */ |
emilmont | 1:fdd22bb7aa52 | 192 | |
emilmont | 1:fdd22bb7aa52 | 193 | uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t * pOut, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 194 | { |
emilmont | 1:fdd22bb7aa52 | 195 | uint32_t i; |
mbed_official | 4:9cee975aadce | 196 | int32_t diff; |
mbed_official | 4:9cee975aadce | 197 | uint32_t diffCrnt = 0; |
emilmont | 1:fdd22bb7aa52 | 198 | uint32_t maxDiff = 0; |
emilmont | 1:fdd22bb7aa52 | 199 | |
emilmont | 1:fdd22bb7aa52 | 200 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 201 | { |
emilmont | 2:da51fb522205 | 202 | diff = pIn[i] - pOut[i]; |
emilmont | 2:da51fb522205 | 203 | diffCrnt = (diff > 0) ? diff : -diff; |
emilmont | 1:fdd22bb7aa52 | 204 | |
emilmont | 2:da51fb522205 | 205 | if(diffCrnt > maxDiff) |
emilmont | 2:da51fb522205 | 206 | { |
emilmont | 2:da51fb522205 | 207 | maxDiff = diffCrnt; |
emilmont | 2:da51fb522205 | 208 | } |
emilmont | 1:fdd22bb7aa52 | 209 | } |
emilmont | 1:fdd22bb7aa52 | 210 | |
emilmont | 1:fdd22bb7aa52 | 211 | return(maxDiff); |
emilmont | 1:fdd22bb7aa52 | 212 | } |
emilmont | 1:fdd22bb7aa52 | 213 | |
emilmont | 1:fdd22bb7aa52 | 214 | /** |
emilmont | 1:fdd22bb7aa52 | 215 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 216 | * @param q31_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 217 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 218 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 219 | * @return none |
emilmont | 1:fdd22bb7aa52 | 220 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 221 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 222 | */ |
emilmont | 1:fdd22bb7aa52 | 223 | |
emilmont | 1:fdd22bb7aa52 | 224 | void arm_provide_guard_bits_q31 (q31_t * input_buf, |
emilmont | 2:da51fb522205 | 225 | uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 226 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 227 | { |
emilmont | 1:fdd22bb7aa52 | 228 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 229 | |
emilmont | 1:fdd22bb7aa52 | 230 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 231 | { |
emilmont | 1:fdd22bb7aa52 | 232 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 233 | } |
emilmont | 1:fdd22bb7aa52 | 234 | } |
emilmont | 1:fdd22bb7aa52 | 235 | |
emilmont | 1:fdd22bb7aa52 | 236 | /** |
emilmont | 1:fdd22bb7aa52 | 237 | * @brief Provide guard bits for Input buffer |
emilmont | 2:da51fb522205 | 238 | * @param q31_t* Pointer to input buffer |
emilmont | 2:da51fb522205 | 239 | * @param uint32_t blockSize |
emilmont | 2:da51fb522205 | 240 | * @param uint32_t guard_bits |
emilmont | 1:fdd22bb7aa52 | 241 | * @return none |
emilmont | 1:fdd22bb7aa52 | 242 | * The function Provides the guard bits for the buffer |
emilmont | 1:fdd22bb7aa52 | 243 | * to avoid overflow |
emilmont | 1:fdd22bb7aa52 | 244 | */ |
emilmont | 1:fdd22bb7aa52 | 245 | |
emilmont | 1:fdd22bb7aa52 | 246 | void arm_provide_guard_bits_q7 (q7_t * input_buf, |
emilmont | 2:da51fb522205 | 247 | uint32_t blockSize, |
emilmont | 1:fdd22bb7aa52 | 248 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 249 | { |
emilmont | 1:fdd22bb7aa52 | 250 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 251 | |
emilmont | 1:fdd22bb7aa52 | 252 | for (i = 0; i < blockSize; i++) |
emilmont | 1:fdd22bb7aa52 | 253 | { |
emilmont | 1:fdd22bb7aa52 | 254 | input_buf[i] = input_buf[i] >> guard_bits; |
emilmont | 1:fdd22bb7aa52 | 255 | } |
emilmont | 1:fdd22bb7aa52 | 256 | } |
emilmont | 1:fdd22bb7aa52 | 257 | |
emilmont | 1:fdd22bb7aa52 | 258 | |
emilmont | 1:fdd22bb7aa52 | 259 | |
emilmont | 1:fdd22bb7aa52 | 260 | /** |
emilmont | 1:fdd22bb7aa52 | 261 | * @brief Caluclates number of guard bits |
emilmont | 2:da51fb522205 | 262 | * @param uint32_t number of additions |
emilmont | 1:fdd22bb7aa52 | 263 | * @return none |
emilmont | 1:fdd22bb7aa52 | 264 | * The function Caluclates the number of guard bits |
emilmont | 1:fdd22bb7aa52 | 265 | * depending on the numtaps |
emilmont | 1:fdd22bb7aa52 | 266 | */ |
emilmont | 1:fdd22bb7aa52 | 267 | |
emilmont | 1:fdd22bb7aa52 | 268 | uint32_t arm_calc_guard_bits (uint32_t num_adds) |
emilmont | 1:fdd22bb7aa52 | 269 | { |
emilmont | 1:fdd22bb7aa52 | 270 | uint32_t i = 1, j = 0; |
emilmont | 1:fdd22bb7aa52 | 271 | |
emilmont | 1:fdd22bb7aa52 | 272 | if (num_adds == 1) |
emilmont | 1:fdd22bb7aa52 | 273 | { |
emilmont | 1:fdd22bb7aa52 | 274 | return (0); |
emilmont | 1:fdd22bb7aa52 | 275 | } |
emilmont | 1:fdd22bb7aa52 | 276 | |
emilmont | 1:fdd22bb7aa52 | 277 | while (i < num_adds) |
emilmont | 1:fdd22bb7aa52 | 278 | { |
emilmont | 1:fdd22bb7aa52 | 279 | i = i * 2; |
emilmont | 1:fdd22bb7aa52 | 280 | j++; |
emilmont | 1:fdd22bb7aa52 | 281 | } |
emilmont | 1:fdd22bb7aa52 | 282 | |
emilmont | 1:fdd22bb7aa52 | 283 | return (j); |
emilmont | 1:fdd22bb7aa52 | 284 | } |
emilmont | 1:fdd22bb7aa52 | 285 | |
emilmont | 1:fdd22bb7aa52 | 286 | /** |
emilmont | 1:fdd22bb7aa52 | 287 | * @brief Converts Q15 to floating-point |
emilmont | 2:da51fb522205 | 288 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 289 | * @return none |
emilmont | 1:fdd22bb7aa52 | 290 | */ |
emilmont | 1:fdd22bb7aa52 | 291 | |
emilmont | 1:fdd22bb7aa52 | 292 | void arm_apply_guard_bits (float32_t * pIn, |
emilmont | 2:da51fb522205 | 293 | uint32_t numSamples, |
emilmont | 2:da51fb522205 | 294 | uint32_t guard_bits) |
emilmont | 1:fdd22bb7aa52 | 295 | { |
emilmont | 1:fdd22bb7aa52 | 296 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 297 | |
emilmont | 1:fdd22bb7aa52 | 298 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 299 | { |
emilmont | 1:fdd22bb7aa52 | 300 | pIn[i] = pIn[i] * arm_calc_2pow(guard_bits); |
emilmont | 1:fdd22bb7aa52 | 301 | } |
emilmont | 1:fdd22bb7aa52 | 302 | } |
emilmont | 1:fdd22bb7aa52 | 303 | |
emilmont | 1:fdd22bb7aa52 | 304 | /** |
emilmont | 1:fdd22bb7aa52 | 305 | * @brief Calculates pow(2, numShifts) |
emilmont | 2:da51fb522205 | 306 | * @param uint32_t number of shifts |
emilmont | 1:fdd22bb7aa52 | 307 | * @return pow(2, numShifts) |
emilmont | 1:fdd22bb7aa52 | 308 | */ |
emilmont | 1:fdd22bb7aa52 | 309 | uint32_t arm_calc_2pow(uint32_t numShifts) |
emilmont | 1:fdd22bb7aa52 | 310 | { |
emilmont | 1:fdd22bb7aa52 | 311 | |
emilmont | 1:fdd22bb7aa52 | 312 | uint32_t i, val = 1; |
emilmont | 1:fdd22bb7aa52 | 313 | |
emilmont | 1:fdd22bb7aa52 | 314 | for (i = 0; i < numShifts; i++) |
emilmont | 1:fdd22bb7aa52 | 315 | { |
emilmont | 1:fdd22bb7aa52 | 316 | val = val * 2; |
emilmont | 2:da51fb522205 | 317 | } |
emilmont | 1:fdd22bb7aa52 | 318 | |
emilmont | 1:fdd22bb7aa52 | 319 | return(val); |
emilmont | 1:fdd22bb7aa52 | 320 | } |
emilmont | 1:fdd22bb7aa52 | 321 | |
emilmont | 1:fdd22bb7aa52 | 322 | |
emilmont | 1:fdd22bb7aa52 | 323 | |
emilmont | 1:fdd22bb7aa52 | 324 | /** |
emilmont | 1:fdd22bb7aa52 | 325 | * @brief Converts float to fixed q14 |
emilmont | 2:da51fb522205 | 326 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 327 | * @return none |
emilmont | 1:fdd22bb7aa52 | 328 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 329 | */ |
emilmont | 1:fdd22bb7aa52 | 330 | |
emilmont | 1:fdd22bb7aa52 | 331 | void arm_float_to_q14 (float *pIn, q15_t * pOut, |
emilmont | 1:fdd22bb7aa52 | 332 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 333 | { |
emilmont | 1:fdd22bb7aa52 | 334 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 335 | |
emilmont | 1:fdd22bb7aa52 | 336 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 337 | { |
emilmont | 2:da51fb522205 | 338 | /* 16384.0f corresponds to pow(2, 14) */ |
emilmont | 1:fdd22bb7aa52 | 339 | pOut[i] = (q15_t) (pIn[i] * 16384.0f); |
emilmont | 1:fdd22bb7aa52 | 340 | |
emilmont | 1:fdd22bb7aa52 | 341 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 342 | |
emilmont | 1:fdd22bb7aa52 | 343 | if (pIn[i] == (float) 2.0) |
emilmont | 1:fdd22bb7aa52 | 344 | { |
emilmont | 1:fdd22bb7aa52 | 345 | pOut[i] = 0x7FFF; |
emilmont | 1:fdd22bb7aa52 | 346 | } |
emilmont | 1:fdd22bb7aa52 | 347 | |
emilmont | 1:fdd22bb7aa52 | 348 | } |
emilmont | 1:fdd22bb7aa52 | 349 | |
emilmont | 1:fdd22bb7aa52 | 350 | } |
emilmont | 1:fdd22bb7aa52 | 351 | |
emilmont | 1:fdd22bb7aa52 | 352 | |
emilmont | 1:fdd22bb7aa52 | 353 | /** |
emilmont | 1:fdd22bb7aa52 | 354 | * @brief Converts float to fixed q30 format |
emilmont | 2:da51fb522205 | 355 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 356 | * @return none |
emilmont | 1:fdd22bb7aa52 | 357 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 358 | */ |
emilmont | 1:fdd22bb7aa52 | 359 | |
emilmont | 1:fdd22bb7aa52 | 360 | void arm_float_to_q30 (float *pIn, q31_t * pOut, |
emilmont | 2:da51fb522205 | 361 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 362 | { |
emilmont | 1:fdd22bb7aa52 | 363 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 364 | |
emilmont | 1:fdd22bb7aa52 | 365 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 366 | { |
emilmont | 2:da51fb522205 | 367 | /* 1073741824.0f corresponds to pow(2, 30) */ |
emilmont | 1:fdd22bb7aa52 | 368 | pOut[i] = (q31_t) (pIn[i] * 1073741824.0f); |
emilmont | 1:fdd22bb7aa52 | 369 | |
emilmont | 1:fdd22bb7aa52 | 370 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 371 | |
emilmont | 1:fdd22bb7aa52 | 372 | if (pIn[i] == (float) 2.0) |
emilmont | 1:fdd22bb7aa52 | 373 | { |
emilmont | 1:fdd22bb7aa52 | 374 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 375 | } |
emilmont | 1:fdd22bb7aa52 | 376 | } |
emilmont | 1:fdd22bb7aa52 | 377 | } |
emilmont | 1:fdd22bb7aa52 | 378 | |
emilmont | 1:fdd22bb7aa52 | 379 | /** |
emilmont | 1:fdd22bb7aa52 | 380 | * @brief Converts float to fixed q30 format |
emilmont | 2:da51fb522205 | 381 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 382 | * @return none |
emilmont | 1:fdd22bb7aa52 | 383 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 384 | */ |
emilmont | 1:fdd22bb7aa52 | 385 | |
emilmont | 1:fdd22bb7aa52 | 386 | void arm_float_to_q29 (float *pIn, q31_t * pOut, |
emilmont | 2:da51fb522205 | 387 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 388 | { |
emilmont | 1:fdd22bb7aa52 | 389 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 390 | |
emilmont | 1:fdd22bb7aa52 | 391 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 392 | { |
emilmont | 2:da51fb522205 | 393 | /* 1073741824.0f corresponds to pow(2, 30) */ |
emilmont | 1:fdd22bb7aa52 | 394 | pOut[i] = (q31_t) (pIn[i] * 536870912.0f); |
emilmont | 1:fdd22bb7aa52 | 395 | |
emilmont | 1:fdd22bb7aa52 | 396 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 397 | |
emilmont | 1:fdd22bb7aa52 | 398 | if (pIn[i] == (float) 4.0) |
emilmont | 1:fdd22bb7aa52 | 399 | { |
emilmont | 1:fdd22bb7aa52 | 400 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 401 | } |
emilmont | 1:fdd22bb7aa52 | 402 | } |
emilmont | 1:fdd22bb7aa52 | 403 | } |
emilmont | 1:fdd22bb7aa52 | 404 | |
emilmont | 1:fdd22bb7aa52 | 405 | |
emilmont | 1:fdd22bb7aa52 | 406 | /** |
emilmont | 1:fdd22bb7aa52 | 407 | * @brief Converts float to fixed q28 format |
emilmont | 2:da51fb522205 | 408 | * @param uint32_t number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 409 | * @return none |
emilmont | 1:fdd22bb7aa52 | 410 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 411 | */ |
emilmont | 1:fdd22bb7aa52 | 412 | |
emilmont | 1:fdd22bb7aa52 | 413 | void arm_float_to_q28 (float *pIn, q31_t * pOut, |
emilmont | 1:fdd22bb7aa52 | 414 | uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 415 | { |
emilmont | 1:fdd22bb7aa52 | 416 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 417 | |
emilmont | 1:fdd22bb7aa52 | 418 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 419 | { |
emilmont | 2:da51fb522205 | 420 | /* 268435456.0f corresponds to pow(2, 28) */ |
emilmont | 1:fdd22bb7aa52 | 421 | pOut[i] = (q31_t) (pIn[i] * 268435456.0f); |
emilmont | 1:fdd22bb7aa52 | 422 | |
emilmont | 1:fdd22bb7aa52 | 423 | pOut[i] += pIn[i] > 0 ? 0.5 : -0.5; |
emilmont | 1:fdd22bb7aa52 | 424 | |
emilmont | 1:fdd22bb7aa52 | 425 | if (pIn[i] == (float) 8.0) |
emilmont | 1:fdd22bb7aa52 | 426 | { |
emilmont | 1:fdd22bb7aa52 | 427 | pOut[i] = 0x7FFFFFFF; |
emilmont | 1:fdd22bb7aa52 | 428 | } |
emilmont | 1:fdd22bb7aa52 | 429 | } |
emilmont | 1:fdd22bb7aa52 | 430 | } |
emilmont | 1:fdd22bb7aa52 | 431 | |
emilmont | 1:fdd22bb7aa52 | 432 | /** |
emilmont | 1:fdd22bb7aa52 | 433 | * @brief Clip the float values to +/- 1 |
emilmont | 2:da51fb522205 | 434 | * @param pIn input buffer |
emilmont | 2:da51fb522205 | 435 | * @param numSamples number of samples in the buffer |
emilmont | 1:fdd22bb7aa52 | 436 | * @return none |
emilmont | 1:fdd22bb7aa52 | 437 | * The function converts floating point values to fixed point values |
emilmont | 1:fdd22bb7aa52 | 438 | */ |
emilmont | 1:fdd22bb7aa52 | 439 | |
emilmont | 1:fdd22bb7aa52 | 440 | void arm_clip_f32 (float *pIn, uint32_t numSamples) |
emilmont | 1:fdd22bb7aa52 | 441 | { |
emilmont | 1:fdd22bb7aa52 | 442 | uint32_t i; |
emilmont | 1:fdd22bb7aa52 | 443 | |
emilmont | 1:fdd22bb7aa52 | 444 | for (i = 0; i < numSamples; i++) |
emilmont | 1:fdd22bb7aa52 | 445 | { |
emilmont | 1:fdd22bb7aa52 | 446 | if(pIn[i] > 1.0f) |
emilmont | 2:da51fb522205 | 447 | { |
emilmont | 2:da51fb522205 | 448 | pIn[i] = 1.0; |
emilmont | 2:da51fb522205 | 449 | } |
emilmont | 2:da51fb522205 | 450 | else if( pIn[i] < -1.0f) |
emilmont | 2:da51fb522205 | 451 | { |
emilmont | 2:da51fb522205 | 452 | pIn[i] = -1.0; |
emilmont | 2:da51fb522205 | 453 | } |
emilmont | 2:da51fb522205 | 454 | |
emilmont | 1:fdd22bb7aa52 | 455 | } |
emilmont | 1:fdd22bb7aa52 | 456 | } |
emilmont | 1:fdd22bb7aa52 | 457 | |
emilmont | 1:fdd22bb7aa52 | 458 | |
emilmont | 1:fdd22bb7aa52 | 459 | |
emilmont | 1:fdd22bb7aa52 | 460 |