/ Drivers / CMSIS / DSP / Examples / ARM / arm_fft_bin_example / arm_fft_bin_example_f32.c
arm_fft_bin_example_f32.c
  1  /* ----------------------------------------------------------------------
  2  * Copyright (C) 2010-2012 ARM Limited. All rights reserved.
  3  *
  4  * $Date:         17. January 2013
  5  * $Revision:     V1.4.0
  6  *
  7  * Project:       CMSIS DSP Library
  8  * Title:	     arm_fft_bin_example_f32.c
  9  *
 10  * Description:   Example code demonstrating calculation of Max energy bin of
 11  *                frequency domain of input signal.
 12  *
 13  * Target Processor: Cortex-M4/Cortex-M3
 14  *
 15  * Redistribution and use in source and binary forms, with or without
 16  * modification, are permitted provided that the following conditions
 17  * are met:
 18  *   - Redistributions of source code must retain the above copyright
 19  *     notice, this list of conditions and the following disclaimer.
 20  *   - Redistributions in binary form must reproduce the above copyright
 21  *     notice, this list of conditions and the following disclaimer in
 22  *     the documentation and/or other materials provided with the
 23  *     distribution.
 24  *   - Neither the name of ARM LIMITED nor the names of its contributors
 25  *     may be used to endorse or promote products derived from this
 26  *     software without specific prior written permission.
 27  *
 28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 30  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 31  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 32  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 33  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 34  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 35  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 36  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 38  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 39  * POSSIBILITY OF SUCH DAMAGE.
 40   * -------------------------------------------------------------------- */
 41  
 42  /**
 43   * @ingroup groupExamples
 44   */
 45  
 46  /**
 47   * @defgroup FrequencyBin Frequency Bin Example
 48   *
 49   * \par Description
 50   * \par
 51   * Demonstrates the calculation of the maximum energy bin in the frequency
 52   * domain of the input signal with the use of Complex FFT, Complex
 53   * Magnitude, and Maximum functions.
 54   *
 55   * \par Algorithm:
 56   * \par
 57   * The input test signal contains a 10 kHz signal with uniformly distributed white noise.
 58   * Calculating the FFT of the input signal will give us the maximum energy of the
 59   * bin corresponding to the input frequency of 10 kHz.
 60   *
 61   * \par Block Diagram:
 62   * \image html FFTBin.gif "Block Diagram"
 63   * \par
 64   * The figure below shows the time domain signal of 10 kHz signal with
 65   * uniformly distributed white noise, and the next figure shows the input
 66   * in the frequency domain. The bin with maximum energy corresponds to 10 kHz signal.
 67   * \par
 68   * \image html FFTBinInput.gif "Input signal in Time domain"
 69   * \image html FFTBinOutput.gif "Input signal in Frequency domain"
 70   *
 71   * \par Variables Description:
 72   * \par
 73   * \li \c testInput_f32_10khz points to the input data
 74   * \li \c testOutput points to the output data
 75   * \li \c fftSize length of FFT
 76   * \li \c ifftFlag flag for the selection of CFFT/CIFFT
 77   * \li \c doBitReverse Flag for selection of normal order or bit reversed order
 78   * \li \c refIndex reference index value at which maximum energy of bin ocuurs
 79   * \li \c testIndex calculated index value at which maximum energy of bin ocuurs
 80   *
 81   * \par CMSIS DSP Software Library Functions Used:
 82   * \par
 83   * - arm_cfft_f32()
 84   * - arm_cmplx_mag_f32()
 85   * - arm_max_f32()
 86   *
 87   * <b> Refer  </b>
 88   * \link arm_fft_bin_example_f32.c \endlink
 89   *
 90   */
 91  
 92  
 93  /** \example arm_fft_bin_example_f32.c
 94    */
 95  
 96  
 97  #include "arm_math.h"
 98  #include "arm_const_structs.h"
 99  
100  #if defined(SEMIHOSTING)
101  #include <stdio.h>
102  #endif
103  
104  #define TEST_LENGTH_SAMPLES 2048
105  
106  /* -------------------------------------------------------------------
107  * External Input and Output buffer Declarations for FFT Bin Example
108  * ------------------------------------------------------------------- */
109  extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];
110  static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
111  
112  /* ------------------------------------------------------------------
113  * Global variables for FFT Bin Example
114  * ------------------------------------------------------------------- */
115  uint32_t fftSize = 1024;
116  uint32_t ifftFlag = 0;
117  uint32_t doBitReverse = 1;
118  arm_cfft_instance_f32 varInstCfftF32;
119  
120  /* Reference index at which max energy of bin ocuurs */
121  uint32_t refIndex = 213, testIndex = 0;
122  
123  /* ----------------------------------------------------------------------
124  * Max magnitude FFT Bin test
125  * ------------------------------------------------------------------- */
126  
127  int32_t main(void)
128  {
129  
130    arm_status status;
131    float32_t maxValue;
132  
133    status = ARM_MATH_SUCCESS;
134  
135    status=arm_cfft_init_f32(&varInstCfftF32,fftSize);
136  
137    /* Process the data through the CFFT/CIFFT module */
138    arm_cfft_f32(&varInstCfftF32, testInput_f32_10khz, ifftFlag, doBitReverse);
139  
140    /* Process the data through the Complex Magnitude Module for
141    calculating the magnitude at each bin */
142    arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
143  
144    /* Calculates maxValue and returns corresponding BIN value */
145    arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
146  
147    status = (testIndex != refIndex) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
148    
149    if (status != ARM_MATH_SUCCESS)
150    {
151  #if defined (SEMIHOSTING)
152      printf("FAILURE\n");
153  #else
154      while (1);                             /* main function does not return */
155  #endif
156    }
157    else
158    {
159  #if defined (SEMIHOSTING)
160      printf("SUCCESS\n");
161  #else
162      while (1);                             /* main function does not return */
163  #endif
164    }
165  }
166  
167   /** \endlink */