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  * @addtogroup groupExamples
44  * @{
45  *
46  * @defgroup FrequencyBin Frequency Bin Example
47  *
48  * \par Description
49  * \par
50  * Demonstrates the calculation of the maximum energy bin in the frequency
51  * domain of the input signal with the use of Complex FFT, Complex
52  * Magnitude, and Maximum functions.
53  *
54  * \par Algorithm:
55  * \par
56  * The input test signal contains a 10 kHz signal with uniformly distributed white noise.
57  * Calculating the FFT of the input signal will give us the maximum energy of the
58  * bin corresponding to the input frequency of 10 kHz.
59  *
60  * \par Block Diagram:
61  * \image html FFTBin.gif "Block Diagram"
62  * \par
63  * The figure below shows the time domain signal of 10 kHz signal with
64  * uniformly distributed white noise, and the next figure shows the input
65  * in the frequency domain. The bin with maximum energy corresponds to 10 kHz signal.
66  * \par
67  * \image html FFTBinInput.gif "Input signal in Time domain"
68  * \image html FFTBinOutput.gif "Input signal in Frequency domain"
69  *
70  * \par Variables Description:
71  * \par
72  * \li \c testInput_f32_10khz points to the input data
73  * \li \c testOutput points to the output data
74  * \li \c fftSize length of FFT
75  * \li \c ifftFlag flag for the selection of CFFT/CIFFT
76  * \li \c doBitReverse Flag for selection of normal order or bit reversed order
77  * \li \c refIndex reference index value at which maximum energy of bin ocuurs
78  * \li \c testIndex calculated index value at which maximum energy of bin ocuurs
79  *
80  * \par CMSIS DSP Software Library Functions Used:
81  * \par
82  * - arm_cfft_f32()
83  * - arm_cmplx_mag_f32()
84  * - arm_max_f32()
85  *
86  * <b> Refer  </b>
87  * \link arm_fft_bin_example_f32.c \endlink
88  *
89  * \example arm_fft_bin_example_f32.c
90  *
91  * @}*/
92 
93 #include "arm_math.h"
94 #include "arm_const_structs.h"
95 
96 #if defined(SEMIHOSTING)
97 #include <stdio.h>
98 #endif
99 
100 #define TEST_LENGTH_SAMPLES 2048
101 
102 /* -------------------------------------------------------------------
103 * External Input and Output buffer Declarations for FFT Bin Example
104 * ------------------------------------------------------------------- */
105 extern float32_t testInput_f32_10khz[TEST_LENGTH_SAMPLES];
106 static float32_t testOutput[TEST_LENGTH_SAMPLES/2];
107 
108 /* ------------------------------------------------------------------
109 * Global variables for FFT Bin Example
110 * ------------------------------------------------------------------- */
111 uint32_t fftSize = 1024;
112 uint32_t ifftFlag = 0;
113 uint32_t doBitReverse = 1;
114 arm_cfft_instance_f32 varInstCfftF32;
115 
116 /* Reference index at which max energy of bin ocuurs */
117 uint32_t refIndex = 213, testIndex = 0;
118 
119 /* ----------------------------------------------------------------------
120 * Max magnitude FFT Bin test
121 * ------------------------------------------------------------------- */
122 
main(void)123 int32_t main(void)
124 {
125 
126   arm_status status;
127   float32_t maxValue;
128 
129   status = ARM_MATH_SUCCESS;
130 
131   status=arm_cfft_init_1024_f32(&varInstCfftF32);
132 
133   /* Process the data through the CFFT/CIFFT module */
134   arm_cfft_f32(&varInstCfftF32, testInput_f32_10khz, ifftFlag, doBitReverse);
135 
136   /* Process the data through the Complex Magnitude Module for
137   calculating the magnitude at each bin */
138   arm_cmplx_mag_f32(testInput_f32_10khz, testOutput, fftSize);
139 
140   /* Calculates maxValue and returns corresponding BIN value */
141   arm_max_f32(testOutput, fftSize, &maxValue, &testIndex);
142 
143   status = (testIndex != refIndex) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
144 
145   if (status != ARM_MATH_SUCCESS)
146   {
147 #if defined (SEMIHOSTING)
148     printf("FAILURE\n");
149 #else
150     while (1);                             /* main function does not return */
151 #endif
152   }
153   else
154   {
155 #if defined (SEMIHOSTING)
156     printf("SUCCESS\n");
157 #else
158     while (1);                             /* main function does not return */
159 #endif
160   }
161 }
162 
163  /** \endlink */
164