mbed-os 6.10 versione

Committer:
mbed_official
Date:
Fri Nov 08 13:45:10 2013 +0000
Revision:
3:7a284390b0ce
Parent:
2:da51fb522205
Synchronized with git revision e69956aba2f68a2a26ac26b051f8d349deaa1ce8

Who changed what in which revision?

UserRevisionLine numberNew 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;
emilmont 1:fdd22bb7aa52 167 int32_t diff, diffCrnt = 0;
emilmont 1:fdd22bb7aa52 168 uint32_t maxDiff = 0;
emilmont 1:fdd22bb7aa52 169
emilmont 1:fdd22bb7aa52 170 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 171 {
emilmont 2:da51fb522205 172 diff = pIn[i] - pOut[i];
emilmont 2:da51fb522205 173 diffCrnt = (diff > 0) ? diff : -diff;
emilmont 1:fdd22bb7aa52 174
emilmont 2:da51fb522205 175 if(diffCrnt > maxDiff)
emilmont 2:da51fb522205 176 {
emilmont 2:da51fb522205 177 maxDiff = diffCrnt;
emilmont 2:da51fb522205 178 }
emilmont 1:fdd22bb7aa52 179 }
emilmont 1:fdd22bb7aa52 180
emilmont 1:fdd22bb7aa52 181 return(maxDiff);
emilmont 1:fdd22bb7aa52 182 }
emilmont 1:fdd22bb7aa52 183
emilmont 1:fdd22bb7aa52 184 /**
emilmont 1:fdd22bb7aa52 185 * @brief Compare MATLAB Reference Output and ARM Test output
emilmont 2:da51fb522205 186 * @param q31_t* Pointer to Ref buffer
emilmont 2:da51fb522205 187 * @param q31_t* Pointer to Test buffer
emilmont 2:da51fb522205 188 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 189 * @return none
emilmont 1:fdd22bb7aa52 190 */
emilmont 1:fdd22bb7aa52 191
emilmont 1:fdd22bb7aa52 192 uint32_t arm_compare_fixed_q31(q31_t *pIn, q31_t * pOut, uint32_t numSamples)
emilmont 1:fdd22bb7aa52 193 {
emilmont 1:fdd22bb7aa52 194 uint32_t i;
emilmont 1:fdd22bb7aa52 195 int32_t diff, diffCrnt = 0;
emilmont 1:fdd22bb7aa52 196 uint32_t maxDiff = 0;
emilmont 1:fdd22bb7aa52 197
emilmont 1:fdd22bb7aa52 198 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 199 {
emilmont 2:da51fb522205 200 diff = pIn[i] - pOut[i];
emilmont 2:da51fb522205 201 diffCrnt = (diff > 0) ? diff : -diff;
emilmont 1:fdd22bb7aa52 202
emilmont 2:da51fb522205 203 if(diffCrnt > maxDiff)
emilmont 2:da51fb522205 204 {
emilmont 2:da51fb522205 205 maxDiff = diffCrnt;
emilmont 2:da51fb522205 206 }
emilmont 1:fdd22bb7aa52 207 }
emilmont 1:fdd22bb7aa52 208
emilmont 1:fdd22bb7aa52 209 return(maxDiff);
emilmont 1:fdd22bb7aa52 210 }
emilmont 1:fdd22bb7aa52 211
emilmont 1:fdd22bb7aa52 212 /**
emilmont 1:fdd22bb7aa52 213 * @brief Provide guard bits for Input buffer
emilmont 2:da51fb522205 214 * @param q31_t* Pointer to input buffer
emilmont 2:da51fb522205 215 * @param uint32_t blockSize
emilmont 2:da51fb522205 216 * @param uint32_t guard_bits
emilmont 1:fdd22bb7aa52 217 * @return none
emilmont 1:fdd22bb7aa52 218 * The function Provides the guard bits for the buffer
emilmont 1:fdd22bb7aa52 219 * to avoid overflow
emilmont 1:fdd22bb7aa52 220 */
emilmont 1:fdd22bb7aa52 221
emilmont 1:fdd22bb7aa52 222 void arm_provide_guard_bits_q31 (q31_t * input_buf,
emilmont 2:da51fb522205 223 uint32_t blockSize,
emilmont 1:fdd22bb7aa52 224 uint32_t guard_bits)
emilmont 1:fdd22bb7aa52 225 {
emilmont 1:fdd22bb7aa52 226 uint32_t i;
emilmont 1:fdd22bb7aa52 227
emilmont 1:fdd22bb7aa52 228 for (i = 0; i < blockSize; i++)
emilmont 1:fdd22bb7aa52 229 {
emilmont 1:fdd22bb7aa52 230 input_buf[i] = input_buf[i] >> guard_bits;
emilmont 1:fdd22bb7aa52 231 }
emilmont 1:fdd22bb7aa52 232 }
emilmont 1:fdd22bb7aa52 233
emilmont 1:fdd22bb7aa52 234 /**
emilmont 1:fdd22bb7aa52 235 * @brief Provide guard bits for Input buffer
emilmont 2:da51fb522205 236 * @param q31_t* Pointer to input buffer
emilmont 2:da51fb522205 237 * @param uint32_t blockSize
emilmont 2:da51fb522205 238 * @param uint32_t guard_bits
emilmont 1:fdd22bb7aa52 239 * @return none
emilmont 1:fdd22bb7aa52 240 * The function Provides the guard bits for the buffer
emilmont 1:fdd22bb7aa52 241 * to avoid overflow
emilmont 1:fdd22bb7aa52 242 */
emilmont 1:fdd22bb7aa52 243
emilmont 1:fdd22bb7aa52 244 void arm_provide_guard_bits_q7 (q7_t * input_buf,
emilmont 2:da51fb522205 245 uint32_t blockSize,
emilmont 1:fdd22bb7aa52 246 uint32_t guard_bits)
emilmont 1:fdd22bb7aa52 247 {
emilmont 1:fdd22bb7aa52 248 uint32_t i;
emilmont 1:fdd22bb7aa52 249
emilmont 1:fdd22bb7aa52 250 for (i = 0; i < blockSize; i++)
emilmont 1:fdd22bb7aa52 251 {
emilmont 1:fdd22bb7aa52 252 input_buf[i] = input_buf[i] >> guard_bits;
emilmont 1:fdd22bb7aa52 253 }
emilmont 1:fdd22bb7aa52 254 }
emilmont 1:fdd22bb7aa52 255
emilmont 1:fdd22bb7aa52 256
emilmont 1:fdd22bb7aa52 257
emilmont 1:fdd22bb7aa52 258 /**
emilmont 1:fdd22bb7aa52 259 * @brief Caluclates number of guard bits
emilmont 2:da51fb522205 260 * @param uint32_t number of additions
emilmont 1:fdd22bb7aa52 261 * @return none
emilmont 1:fdd22bb7aa52 262 * The function Caluclates the number of guard bits
emilmont 1:fdd22bb7aa52 263 * depending on the numtaps
emilmont 1:fdd22bb7aa52 264 */
emilmont 1:fdd22bb7aa52 265
emilmont 1:fdd22bb7aa52 266 uint32_t arm_calc_guard_bits (uint32_t num_adds)
emilmont 1:fdd22bb7aa52 267 {
emilmont 1:fdd22bb7aa52 268 uint32_t i = 1, j = 0;
emilmont 1:fdd22bb7aa52 269
emilmont 1:fdd22bb7aa52 270 if (num_adds == 1)
emilmont 1:fdd22bb7aa52 271 {
emilmont 1:fdd22bb7aa52 272 return (0);
emilmont 1:fdd22bb7aa52 273 }
emilmont 1:fdd22bb7aa52 274
emilmont 1:fdd22bb7aa52 275 while (i < num_adds)
emilmont 1:fdd22bb7aa52 276 {
emilmont 1:fdd22bb7aa52 277 i = i * 2;
emilmont 1:fdd22bb7aa52 278 j++;
emilmont 1:fdd22bb7aa52 279 }
emilmont 1:fdd22bb7aa52 280
emilmont 1:fdd22bb7aa52 281 return (j);
emilmont 1:fdd22bb7aa52 282 }
emilmont 1:fdd22bb7aa52 283
emilmont 1:fdd22bb7aa52 284 /**
emilmont 1:fdd22bb7aa52 285 * @brief Converts Q15 to floating-point
emilmont 2:da51fb522205 286 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 287 * @return none
emilmont 1:fdd22bb7aa52 288 */
emilmont 1:fdd22bb7aa52 289
emilmont 1:fdd22bb7aa52 290 void arm_apply_guard_bits (float32_t * pIn,
emilmont 2:da51fb522205 291 uint32_t numSamples,
emilmont 2:da51fb522205 292 uint32_t guard_bits)
emilmont 1:fdd22bb7aa52 293 {
emilmont 1:fdd22bb7aa52 294 uint32_t i;
emilmont 1:fdd22bb7aa52 295
emilmont 1:fdd22bb7aa52 296 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 297 {
emilmont 1:fdd22bb7aa52 298 pIn[i] = pIn[i] * arm_calc_2pow(guard_bits);
emilmont 1:fdd22bb7aa52 299 }
emilmont 1:fdd22bb7aa52 300 }
emilmont 1:fdd22bb7aa52 301
emilmont 1:fdd22bb7aa52 302 /**
emilmont 1:fdd22bb7aa52 303 * @brief Calculates pow(2, numShifts)
emilmont 2:da51fb522205 304 * @param uint32_t number of shifts
emilmont 1:fdd22bb7aa52 305 * @return pow(2, numShifts)
emilmont 1:fdd22bb7aa52 306 */
emilmont 1:fdd22bb7aa52 307 uint32_t arm_calc_2pow(uint32_t numShifts)
emilmont 1:fdd22bb7aa52 308 {
emilmont 1:fdd22bb7aa52 309
emilmont 1:fdd22bb7aa52 310 uint32_t i, val = 1;
emilmont 1:fdd22bb7aa52 311
emilmont 1:fdd22bb7aa52 312 for (i = 0; i < numShifts; i++)
emilmont 1:fdd22bb7aa52 313 {
emilmont 1:fdd22bb7aa52 314 val = val * 2;
emilmont 2:da51fb522205 315 }
emilmont 1:fdd22bb7aa52 316
emilmont 1:fdd22bb7aa52 317 return(val);
emilmont 1:fdd22bb7aa52 318 }
emilmont 1:fdd22bb7aa52 319
emilmont 1:fdd22bb7aa52 320
emilmont 1:fdd22bb7aa52 321
emilmont 1:fdd22bb7aa52 322 /**
emilmont 1:fdd22bb7aa52 323 * @brief Converts float to fixed q14
emilmont 2:da51fb522205 324 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 325 * @return none
emilmont 1:fdd22bb7aa52 326 * The function converts floating point values to fixed point values
emilmont 1:fdd22bb7aa52 327 */
emilmont 1:fdd22bb7aa52 328
emilmont 1:fdd22bb7aa52 329 void arm_float_to_q14 (float *pIn, q15_t * pOut,
emilmont 1:fdd22bb7aa52 330 uint32_t numSamples)
emilmont 1:fdd22bb7aa52 331 {
emilmont 1:fdd22bb7aa52 332 uint32_t i;
emilmont 1:fdd22bb7aa52 333
emilmont 1:fdd22bb7aa52 334 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 335 {
emilmont 2:da51fb522205 336 /* 16384.0f corresponds to pow(2, 14) */
emilmont 1:fdd22bb7aa52 337 pOut[i] = (q15_t) (pIn[i] * 16384.0f);
emilmont 1:fdd22bb7aa52 338
emilmont 1:fdd22bb7aa52 339 pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
emilmont 1:fdd22bb7aa52 340
emilmont 1:fdd22bb7aa52 341 if (pIn[i] == (float) 2.0)
emilmont 1:fdd22bb7aa52 342 {
emilmont 1:fdd22bb7aa52 343 pOut[i] = 0x7FFF;
emilmont 1:fdd22bb7aa52 344 }
emilmont 1:fdd22bb7aa52 345
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 * @brief Converts float to fixed q30 format
emilmont 2:da51fb522205 353 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 354 * @return none
emilmont 1:fdd22bb7aa52 355 * The function converts floating point values to fixed point values
emilmont 1:fdd22bb7aa52 356 */
emilmont 1:fdd22bb7aa52 357
emilmont 1:fdd22bb7aa52 358 void arm_float_to_q30 (float *pIn, q31_t * pOut,
emilmont 2:da51fb522205 359 uint32_t numSamples)
emilmont 1:fdd22bb7aa52 360 {
emilmont 1:fdd22bb7aa52 361 uint32_t i;
emilmont 1:fdd22bb7aa52 362
emilmont 1:fdd22bb7aa52 363 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 364 {
emilmont 2:da51fb522205 365 /* 1073741824.0f corresponds to pow(2, 30) */
emilmont 1:fdd22bb7aa52 366 pOut[i] = (q31_t) (pIn[i] * 1073741824.0f);
emilmont 1:fdd22bb7aa52 367
emilmont 1:fdd22bb7aa52 368 pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
emilmont 1:fdd22bb7aa52 369
emilmont 1:fdd22bb7aa52 370 if (pIn[i] == (float) 2.0)
emilmont 1:fdd22bb7aa52 371 {
emilmont 1:fdd22bb7aa52 372 pOut[i] = 0x7FFFFFFF;
emilmont 1:fdd22bb7aa52 373 }
emilmont 1:fdd22bb7aa52 374 }
emilmont 1:fdd22bb7aa52 375 }
emilmont 1:fdd22bb7aa52 376
emilmont 1:fdd22bb7aa52 377 /**
emilmont 1:fdd22bb7aa52 378 * @brief Converts float to fixed q30 format
emilmont 2:da51fb522205 379 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 380 * @return none
emilmont 1:fdd22bb7aa52 381 * The function converts floating point values to fixed point values
emilmont 1:fdd22bb7aa52 382 */
emilmont 1:fdd22bb7aa52 383
emilmont 1:fdd22bb7aa52 384 void arm_float_to_q29 (float *pIn, q31_t * pOut,
emilmont 2:da51fb522205 385 uint32_t numSamples)
emilmont 1:fdd22bb7aa52 386 {
emilmont 1:fdd22bb7aa52 387 uint32_t i;
emilmont 1:fdd22bb7aa52 388
emilmont 1:fdd22bb7aa52 389 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 390 {
emilmont 2:da51fb522205 391 /* 1073741824.0f corresponds to pow(2, 30) */
emilmont 1:fdd22bb7aa52 392 pOut[i] = (q31_t) (pIn[i] * 536870912.0f);
emilmont 1:fdd22bb7aa52 393
emilmont 1:fdd22bb7aa52 394 pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
emilmont 1:fdd22bb7aa52 395
emilmont 1:fdd22bb7aa52 396 if (pIn[i] == (float) 4.0)
emilmont 1:fdd22bb7aa52 397 {
emilmont 1:fdd22bb7aa52 398 pOut[i] = 0x7FFFFFFF;
emilmont 1:fdd22bb7aa52 399 }
emilmont 1:fdd22bb7aa52 400 }
emilmont 1:fdd22bb7aa52 401 }
emilmont 1:fdd22bb7aa52 402
emilmont 1:fdd22bb7aa52 403
emilmont 1:fdd22bb7aa52 404 /**
emilmont 1:fdd22bb7aa52 405 * @brief Converts float to fixed q28 format
emilmont 2:da51fb522205 406 * @param uint32_t number of samples in the buffer
emilmont 1:fdd22bb7aa52 407 * @return none
emilmont 1:fdd22bb7aa52 408 * The function converts floating point values to fixed point values
emilmont 1:fdd22bb7aa52 409 */
emilmont 1:fdd22bb7aa52 410
emilmont 1:fdd22bb7aa52 411 void arm_float_to_q28 (float *pIn, q31_t * pOut,
emilmont 1:fdd22bb7aa52 412 uint32_t numSamples)
emilmont 1:fdd22bb7aa52 413 {
emilmont 1:fdd22bb7aa52 414 uint32_t i;
emilmont 1:fdd22bb7aa52 415
emilmont 1:fdd22bb7aa52 416 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 417 {
emilmont 2:da51fb522205 418 /* 268435456.0f corresponds to pow(2, 28) */
emilmont 1:fdd22bb7aa52 419 pOut[i] = (q31_t) (pIn[i] * 268435456.0f);
emilmont 1:fdd22bb7aa52 420
emilmont 1:fdd22bb7aa52 421 pOut[i] += pIn[i] > 0 ? 0.5 : -0.5;
emilmont 1:fdd22bb7aa52 422
emilmont 1:fdd22bb7aa52 423 if (pIn[i] == (float) 8.0)
emilmont 1:fdd22bb7aa52 424 {
emilmont 1:fdd22bb7aa52 425 pOut[i] = 0x7FFFFFFF;
emilmont 1:fdd22bb7aa52 426 }
emilmont 1:fdd22bb7aa52 427 }
emilmont 1:fdd22bb7aa52 428 }
emilmont 1:fdd22bb7aa52 429
emilmont 1:fdd22bb7aa52 430 /**
emilmont 1:fdd22bb7aa52 431 * @brief Clip the float values to +/- 1
emilmont 2:da51fb522205 432 * @param pIn input buffer
emilmont 2:da51fb522205 433 * @param numSamples number of samples in the buffer
emilmont 1:fdd22bb7aa52 434 * @return none
emilmont 1:fdd22bb7aa52 435 * The function converts floating point values to fixed point values
emilmont 1:fdd22bb7aa52 436 */
emilmont 1:fdd22bb7aa52 437
emilmont 1:fdd22bb7aa52 438 void arm_clip_f32 (float *pIn, uint32_t numSamples)
emilmont 1:fdd22bb7aa52 439 {
emilmont 1:fdd22bb7aa52 440 uint32_t i;
emilmont 1:fdd22bb7aa52 441
emilmont 1:fdd22bb7aa52 442 for (i = 0; i < numSamples; i++)
emilmont 1:fdd22bb7aa52 443 {
emilmont 1:fdd22bb7aa52 444 if(pIn[i] > 1.0f)
emilmont 2:da51fb522205 445 {
emilmont 2:da51fb522205 446 pIn[i] = 1.0;
emilmont 2:da51fb522205 447 }
emilmont 2:da51fb522205 448 else if( pIn[i] < -1.0f)
emilmont 2:da51fb522205 449 {
emilmont 2:da51fb522205 450 pIn[i] = -1.0;
emilmont 2:da51fb522205 451 }
emilmont 2:da51fb522205 452
emilmont 1:fdd22bb7aa52 453 }
emilmont 1:fdd22bb7aa52 454 }
emilmont 1:fdd22bb7aa52 455
emilmont 1:fdd22bb7aa52 456
emilmont 1:fdd22bb7aa52 457
emilmont 1:fdd22bb7aa52 458