CMSIS DSP Library from CMSIS 2.0. See http://www.onarm.com/cmsis/ for full details

Dependents:   K22F_DSP_Matrix_least_square BNO055-ELEC3810 1BNO055 ECE4180Project--Slave2 ... more

Committer:
simon
Date:
Thu Mar 10 15:07:50 2011 +0000
Revision:
0:1014af42efd9

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:1014af42efd9 1 /* ----------------------------------------------------------------------
simon 0:1014af42efd9 2 * Copyright (C) 2010 ARM Limited. All rights reserved.
simon 0:1014af42efd9 3 *
simon 0:1014af42efd9 4 * $Date: 29. November 2010
simon 0:1014af42efd9 5 * $Revision: V1.0.3
simon 0:1014af42efd9 6 *
simon 0:1014af42efd9 7 * Project: CMSIS DSP Library
simon 0:1014af42efd9 8 * Title: arm_fir_decimate_f32.c
simon 0:1014af42efd9 9 *
simon 0:1014af42efd9 10 * Description: FIR decimation for floating-point sequences.
simon 0:1014af42efd9 11 *
simon 0:1014af42efd9 12 * Target Processor: Cortex-M4/Cortex-M3
simon 0:1014af42efd9 13 *
simon 0:1014af42efd9 14 * Version 1.0.3 2010/11/29
simon 0:1014af42efd9 15 * Re-organized the CMSIS folders and updated documentation.
simon 0:1014af42efd9 16 *
simon 0:1014af42efd9 17 * Version 1.0.2 2010/11/11
simon 0:1014af42efd9 18 * Documentation updated.
simon 0:1014af42efd9 19 *
simon 0:1014af42efd9 20 * Version 1.0.1 2010/10/05
simon 0:1014af42efd9 21 * Production release and review comments incorporated.
simon 0:1014af42efd9 22 *
simon 0:1014af42efd9 23 * Version 1.0.0 2010/09/20
simon 0:1014af42efd9 24 * Production release and review comments incorporated
simon 0:1014af42efd9 25 *
simon 0:1014af42efd9 26 * Version 0.0.7 2010/06/10
simon 0:1014af42efd9 27 * Misra-C changes done
simon 0:1014af42efd9 28 *
simon 0:1014af42efd9 29 * -------------------------------------------------------------------- */
simon 0:1014af42efd9 30
simon 0:1014af42efd9 31 #include "arm_math.h"
simon 0:1014af42efd9 32
simon 0:1014af42efd9 33 /**
simon 0:1014af42efd9 34 * @ingroup groupFilters
simon 0:1014af42efd9 35 */
simon 0:1014af42efd9 36
simon 0:1014af42efd9 37 /**
simon 0:1014af42efd9 38 * @defgroup FIR_decimate Finite Impulse Response (FIR) Decimator
simon 0:1014af42efd9 39 *
simon 0:1014af42efd9 40 * These functions combine an FIR filter together with a decimator.
simon 0:1014af42efd9 41 * They are used in multirate systems for reducing the sample rate of a signal without introducing aliasing distortion.
simon 0:1014af42efd9 42 * Conceptually, the functions are equivalent to the block diagram below:
simon 0:1014af42efd9 43 * \image html FIRDecimator.gif "Components included in the FIR Decimator functions"
simon 0:1014af42efd9 44 * When decimating by a factor of <code>M</code>, the signal should be prefiltered by a lowpass filter with a normalized
simon 0:1014af42efd9 45 * cutoff frequency of <code>1/M</code> in order to prevent aliasing distortion.
simon 0:1014af42efd9 46 * The user of the function is responsible for providing the filter coefficients.
simon 0:1014af42efd9 47 *
simon 0:1014af42efd9 48 * The FIR decimator functions provided in the CMSIS DSP Library combine the FIR filter and the decimator in an efficient manner.
simon 0:1014af42efd9 49 * Instead of calculating all of the FIR filter outputs and discarding <code>M-1</code> out of every <code>M</code>, only the
simon 0:1014af42efd9 50 * samples output by the decimator are computed.
simon 0:1014af42efd9 51 * The functions operate on blocks of input and output data.
simon 0:1014af42efd9 52 * <code>pSrc</code> points to an array of <code>blockSize</code> input values and
simon 0:1014af42efd9 53 * <code>pDst</code> points to an array of <code>blockSize/M</code> output values.
simon 0:1014af42efd9 54 * In order to have an integer number of output samples <code>blockSize</code>
simon 0:1014af42efd9 55 * must always be a multiple of the decimation factor <code>M</code>.
simon 0:1014af42efd9 56 *
simon 0:1014af42efd9 57 * The library provides separate functions for Q15, Q31 and floating-point data types.
simon 0:1014af42efd9 58 *
simon 0:1014af42efd9 59 * \par Algorithm:
simon 0:1014af42efd9 60 * The FIR portion of the algorithm uses the standard form filter:
simon 0:1014af42efd9 61 * <pre>
simon 0:1014af42efd9 62 * y[n] = b[0] * x[n] + b[1] * x[n-1] + b[2] * x[n-2] + ...+ b[numTaps-1] * x[n-numTaps+1]
simon 0:1014af42efd9 63 * </pre>
simon 0:1014af42efd9 64 * where, <code>b[n]</code> are the filter coefficients.
simon 0:1014af42efd9 65 * \par
simon 0:1014af42efd9 66 * The <code>pCoeffs</code> points to a coefficient array of size <code>numTaps</code>.
simon 0:1014af42efd9 67 * Coefficients are stored in time reversed order.
simon 0:1014af42efd9 68 * \par
simon 0:1014af42efd9 69 * <pre>
simon 0:1014af42efd9 70 * {b[numTaps-1], b[numTaps-2], b[N-2], ..., b[1], b[0]}
simon 0:1014af42efd9 71 * </pre>
simon 0:1014af42efd9 72 * \par
simon 0:1014af42efd9 73 * <code>pState</code> points to a state array of size <code>numTaps + blockSize - 1</code>.
simon 0:1014af42efd9 74 * Samples in the state buffer are stored in the order:
simon 0:1014af42efd9 75 * \par
simon 0:1014af42efd9 76 * <pre>
simon 0:1014af42efd9 77 * {x[n-numTaps+1], x[n-numTaps], x[n-numTaps-1], x[n-numTaps-2]....x[0], x[1], ..., x[blockSize-1]}
simon 0:1014af42efd9 78 * </pre>
simon 0:1014af42efd9 79 * The state variables are updated after each block of data is processed, the coefficients are untouched.
simon 0:1014af42efd9 80 *
simon 0:1014af42efd9 81 * \par Instance Structure
simon 0:1014af42efd9 82 * The coefficients and state variables for a filter are stored together in an instance data structure.
simon 0:1014af42efd9 83 * A separate instance structure must be defined for each filter.
simon 0:1014af42efd9 84 * Coefficient arrays may be shared among several instances while state variable array should be allocated separately.
simon 0:1014af42efd9 85 * There are separate instance structure declarations for each of the 3 supported data types.
simon 0:1014af42efd9 86 *
simon 0:1014af42efd9 87 * \par Initialization Functions
simon 0:1014af42efd9 88 * There is also an associated initialization function for each data type.
simon 0:1014af42efd9 89 * The initialization function performs the following operations:
simon 0:1014af42efd9 90 * - Sets the values of the internal structure fields.
simon 0:1014af42efd9 91 * - Zeros out the values in the state buffer.
simon 0:1014af42efd9 92 * - Checks to make sure that the size of the input is a multiple of the decimation factor.
simon 0:1014af42efd9 93 *
simon 0:1014af42efd9 94 * \par
simon 0:1014af42efd9 95 * Use of the initialization function is optional.
simon 0:1014af42efd9 96 * However, if the initialization function is used, then the instance structure cannot be placed into a const data section.
simon 0:1014af42efd9 97 * To place an instance structure into a const data section, the instance structure must be manually initialized.
simon 0:1014af42efd9 98 * The code below statically initializes each of the 3 different data type filter instance structures
simon 0:1014af42efd9 99 * <pre>
simon 0:1014af42efd9 100 *arm_fir_decimate_instance_f32 S = {M, numTaps, pCoeffs, pState};
simon 0:1014af42efd9 101 *arm_fir_decimate_instance_q31 S = {M, numTaps, pCoeffs, pState};
simon 0:1014af42efd9 102 *arm_fir_decimate_instance_q15 S = {M, numTaps, pCoeffs, pState};
simon 0:1014af42efd9 103 * </pre>
simon 0:1014af42efd9 104 * where <code>M</code> is the decimation factor; <code>numTaps</code> is the number of filter coefficients in the filter;
simon 0:1014af42efd9 105 * <code>pCoeffs</code> is the address of the coefficient buffer;
simon 0:1014af42efd9 106 * <code>pState</code> is the address of the state buffer.
simon 0:1014af42efd9 107 * Be sure to set the values in the state buffer to zeros when doing static initialization.
simon 0:1014af42efd9 108 *
simon 0:1014af42efd9 109 * \par Fixed-Point Behavior
simon 0:1014af42efd9 110 * Care must be taken when using the fixed-point versions of the FIR decimate filter functions.
simon 0:1014af42efd9 111 * In particular, the overflow and saturation behavior of the accumulator used in each function must be considered.
simon 0:1014af42efd9 112 * Refer to the function specific documentation below for usage guidelines.
simon 0:1014af42efd9 113 */
simon 0:1014af42efd9 114
simon 0:1014af42efd9 115 /**
simon 0:1014af42efd9 116 * @addtogroup FIR_decimate
simon 0:1014af42efd9 117 * @{
simon 0:1014af42efd9 118 */
simon 0:1014af42efd9 119
simon 0:1014af42efd9 120 /**
simon 0:1014af42efd9 121 * @brief Processing function for the floating-point FIR decimator.
simon 0:1014af42efd9 122 * @param[in] *S points to an instance of the floating-point FIR decimator structure.
simon 0:1014af42efd9 123 * @param[in] *pSrc points to the block of input data.
simon 0:1014af42efd9 124 * @param[out] *pDst points to the block of output data.
simon 0:1014af42efd9 125 * @param[in] blockSize number of input samples to process per call.
simon 0:1014af42efd9 126 * @return none.
simon 0:1014af42efd9 127 */
simon 0:1014af42efd9 128
simon 0:1014af42efd9 129 void arm_fir_decimate_f32(
simon 0:1014af42efd9 130 const arm_fir_decimate_instance_f32 * S,
simon 0:1014af42efd9 131 float32_t * pSrc,
simon 0:1014af42efd9 132 float32_t * pDst,
simon 0:1014af42efd9 133 uint32_t blockSize)
simon 0:1014af42efd9 134 {
simon 0:1014af42efd9 135 float32_t *pState = S->pState; /* State pointer */
simon 0:1014af42efd9 136 float32_t *pCoeffs = S->pCoeffs; /* Coefficient pointer */
simon 0:1014af42efd9 137 float32_t *pStateCurnt; /* Points to the current sample of the state */
simon 0:1014af42efd9 138 float32_t *px, *pb; /* Temporary pointers for state and coefficient buffers */
simon 0:1014af42efd9 139 float32_t sum0; /* Accumulator */
simon 0:1014af42efd9 140 float32_t x0, c0; /* Temporary variables to hold state and coefficient values */
simon 0:1014af42efd9 141 uint32_t numTaps = S->numTaps; /* Number of filter coefficients in the filter */
simon 0:1014af42efd9 142 uint32_t i, tapCnt, blkCnt, outBlockSize = blockSize / S->M; /* Loop counters */
simon 0:1014af42efd9 143
simon 0:1014af42efd9 144 /* S->pState buffer contains previous frame (numTaps - 1) samples */
simon 0:1014af42efd9 145 /* pStateCurnt points to the location where the new input data should be written */
simon 0:1014af42efd9 146 pStateCurnt = S->pState + (numTaps - 1u);
simon 0:1014af42efd9 147
simon 0:1014af42efd9 148 /* Total number of output samples to be computed */
simon 0:1014af42efd9 149 blkCnt = outBlockSize;
simon 0:1014af42efd9 150
simon 0:1014af42efd9 151 while(blkCnt > 0u)
simon 0:1014af42efd9 152 {
simon 0:1014af42efd9 153 /* Copy decimation factor number of new input samples into the state buffer */
simon 0:1014af42efd9 154 i = S->M;
simon 0:1014af42efd9 155
simon 0:1014af42efd9 156 do
simon 0:1014af42efd9 157 {
simon 0:1014af42efd9 158 *pStateCurnt++ = *pSrc++;
simon 0:1014af42efd9 159
simon 0:1014af42efd9 160 } while(--i);
simon 0:1014af42efd9 161
simon 0:1014af42efd9 162 /* Set accumulator to zero */
simon 0:1014af42efd9 163 sum0 = 0.0f;
simon 0:1014af42efd9 164
simon 0:1014af42efd9 165 /* Initialize state pointer */
simon 0:1014af42efd9 166 px = pState;
simon 0:1014af42efd9 167
simon 0:1014af42efd9 168 /* Initialize coeff pointer */
simon 0:1014af42efd9 169 pb = pCoeffs;
simon 0:1014af42efd9 170
simon 0:1014af42efd9 171 /* Loop unrolling. Process 4 taps at a time. */
simon 0:1014af42efd9 172 tapCnt = numTaps >> 2;
simon 0:1014af42efd9 173
simon 0:1014af42efd9 174 /* Loop over the number of taps. Unroll by a factor of 4.
simon 0:1014af42efd9 175 ** Repeat until we've computed numTaps-4 coefficients. */
simon 0:1014af42efd9 176 while(tapCnt > 0u)
simon 0:1014af42efd9 177 {
simon 0:1014af42efd9 178 /* Read the b[numTaps-1] coefficient */
simon 0:1014af42efd9 179 c0 = *(pb++);
simon 0:1014af42efd9 180
simon 0:1014af42efd9 181 /* Read x[n-numTaps-1] sample */
simon 0:1014af42efd9 182 x0 = *(px++);
simon 0:1014af42efd9 183
simon 0:1014af42efd9 184 /* Perform the multiply-accumulate */
simon 0:1014af42efd9 185 sum0 += x0 * c0;
simon 0:1014af42efd9 186
simon 0:1014af42efd9 187 /* Read the b[numTaps-2] coefficient */
simon 0:1014af42efd9 188 c0 = *(pb++);
simon 0:1014af42efd9 189
simon 0:1014af42efd9 190 /* Read x[n-numTaps-2] sample */
simon 0:1014af42efd9 191 x0 = *(px++);
simon 0:1014af42efd9 192
simon 0:1014af42efd9 193 /* Perform the multiply-accumulate */
simon 0:1014af42efd9 194 sum0 += x0 * c0;
simon 0:1014af42efd9 195
simon 0:1014af42efd9 196 /* Read the b[numTaps-3] coefficient */
simon 0:1014af42efd9 197 c0 = *(pb++);
simon 0:1014af42efd9 198
simon 0:1014af42efd9 199 /* Read x[n-numTaps-3] sample */
simon 0:1014af42efd9 200 x0 = *(px++);
simon 0:1014af42efd9 201
simon 0:1014af42efd9 202 /* Perform the multiply-accumulate */
simon 0:1014af42efd9 203 sum0 += x0 * c0;
simon 0:1014af42efd9 204
simon 0:1014af42efd9 205 /* Read the b[numTaps-4] coefficient */
simon 0:1014af42efd9 206 c0 = *(pb++);
simon 0:1014af42efd9 207
simon 0:1014af42efd9 208 /* Read x[n-numTaps-4] sample */
simon 0:1014af42efd9 209 x0 = *(px++);
simon 0:1014af42efd9 210
simon 0:1014af42efd9 211 /* Perform the multiply-accumulate */
simon 0:1014af42efd9 212 sum0 += x0 * c0;
simon 0:1014af42efd9 213
simon 0:1014af42efd9 214 /* Decrement the loop counter */
simon 0:1014af42efd9 215 tapCnt--;
simon 0:1014af42efd9 216 }
simon 0:1014af42efd9 217
simon 0:1014af42efd9 218 /* If the filter length is not a multiple of 4, compute the remaining filter taps */
simon 0:1014af42efd9 219 tapCnt = numTaps % 0x4u;
simon 0:1014af42efd9 220
simon 0:1014af42efd9 221 while(tapCnt > 0u)
simon 0:1014af42efd9 222 {
simon 0:1014af42efd9 223 /* Read coefficients */
simon 0:1014af42efd9 224 c0 = *(pb++);
simon 0:1014af42efd9 225
simon 0:1014af42efd9 226 /* Fetch 1 state variable */
simon 0:1014af42efd9 227 x0 = *(px++);
simon 0:1014af42efd9 228
simon 0:1014af42efd9 229 /* Perform the multiply-accumulate */
simon 0:1014af42efd9 230 sum0 += x0 * c0;
simon 0:1014af42efd9 231
simon 0:1014af42efd9 232 /* Decrement the loop counter */
simon 0:1014af42efd9 233 tapCnt--;
simon 0:1014af42efd9 234 }
simon 0:1014af42efd9 235
simon 0:1014af42efd9 236 /* Advance the state pointer by the decimation factor
simon 0:1014af42efd9 237 * to process the next group of decimation factor number samples */
simon 0:1014af42efd9 238 pState = pState + S->M;
simon 0:1014af42efd9 239
simon 0:1014af42efd9 240 /* The result is in the accumulator, store in the destination buffer. */
simon 0:1014af42efd9 241 *pDst++ = sum0;
simon 0:1014af42efd9 242
simon 0:1014af42efd9 243 /* Decrement the loop counter */
simon 0:1014af42efd9 244 blkCnt--;
simon 0:1014af42efd9 245 }
simon 0:1014af42efd9 246
simon 0:1014af42efd9 247 /* Processing is complete.
simon 0:1014af42efd9 248 ** Now copy the last numTaps - 1 samples to the satrt of the state buffer.
simon 0:1014af42efd9 249 ** This prepares the state buffer for the next function call. */
simon 0:1014af42efd9 250
simon 0:1014af42efd9 251 /* Points to the start of the state buffer */
simon 0:1014af42efd9 252 pStateCurnt = S->pState;
simon 0:1014af42efd9 253
simon 0:1014af42efd9 254 i = (numTaps - 1u) >> 2;
simon 0:1014af42efd9 255
simon 0:1014af42efd9 256 /* copy data */
simon 0:1014af42efd9 257 while(i > 0u)
simon 0:1014af42efd9 258 {
simon 0:1014af42efd9 259 *pStateCurnt++ = *pState++;
simon 0:1014af42efd9 260 *pStateCurnt++ = *pState++;
simon 0:1014af42efd9 261 *pStateCurnt++ = *pState++;
simon 0:1014af42efd9 262 *pStateCurnt++ = *pState++;
simon 0:1014af42efd9 263
simon 0:1014af42efd9 264 /* Decrement the loop counter */
simon 0:1014af42efd9 265 i--;
simon 0:1014af42efd9 266 }
simon 0:1014af42efd9 267
simon 0:1014af42efd9 268 i = (numTaps - 1u) % 0x04u;
simon 0:1014af42efd9 269
simon 0:1014af42efd9 270 /* copy data */
simon 0:1014af42efd9 271 while(i > 0u)
simon 0:1014af42efd9 272 {
simon 0:1014af42efd9 273 *pStateCurnt++ = *pState++;
simon 0:1014af42efd9 274
simon 0:1014af42efd9 275 /* Decrement the loop counter */
simon 0:1014af42efd9 276 i--;
simon 0:1014af42efd9 277 }
simon 0:1014af42efd9 278 }
simon 0:1014af42efd9 279
simon 0:1014af42efd9 280 /**
simon 0:1014af42efd9 281 * @} end of FIR_decimate group
simon 0:1014af42efd9 282 */