1 /* ----------------------------------------------------------------------
2 * Copyright (C) 2010 ARM Limited. All rights reserved.
4 * $Date: 29. November 2010
7 * Project: CMSIS DSP Library
8 * Title: arm_signal_converge_example_f32.c
10 * Description: Example code demonstrating convergence of an adaptive
13 * Target Processor: Cortex-M4/Cortex-M3
16 * Version 1.0.3 2010/11/29
17 * Re-organized the CMSIS folders and updated documentation.
19 * Version 1.0.1 2010/10/05 KK
20 * Production release and review comments incorporated.
22 * Version 1.0.0 2010/09/20 KK
23 * Production release and review comments incorporated.
24 * ------------------------------------------------------------------- */
27 * @ingroup groupExamples
31 * @defgroup SignalConvergence Signal Convergence Example
35 * Demonstrates the ability of an adaptive filter to "learn" the transfer function of
36 * a FIR lowpass filter using the Normalized LMS Filter, Finite Impulse
37 * Response (FIR) Filter, and Basic Math Functions.
41 * The figure below illustrates the signal flow in this example. Uniformly distributed white
42 * noise is passed through an FIR lowpass filter. The output of the FIR filter serves as the
43 * reference input of the adaptive filter (normalized LMS filter). The white noise is input
44 * to the adaptive filter. The adaptive filter learns the transfer function of the FIR filter.
45 * The filter outputs two signals: (1) the output of the internal adaptive FIR filter, and
46 * (2) the error signal which is the difference between the adaptive filter and the reference
47 * output of the FIR filter. Over time as the adaptive filter learns the transfer function
48 * of the FIR filter, the first output approaches the reference output of the FIR filter,
49 * and the error signal approaches zero.
51 * The adaptive filter converges properly even if the input signal has a large dynamic
52 * range (i.e., varies from small to large values). The coefficients of the adaptive filter
53 * are initially zero, and then converge over 1536 samples. The internal function test_signal_converge()
54 * implements the stopping condition. The function checks if all of the values of the error signal have a
55 * magnitude below a threshold DELTA.
59 * \image html SignalFlow.gif
62 * \par Variables Description:
64 * \li \c testInput_f32 points to the input data
65 * \li \c firStateF32 points to FIR state buffer
66 * \li \c lmsStateF32 points to Normalised Least mean square FIR filter state buffer
67 * \li \c FIRCoeff_f32 points to coefficient buffer
68 * \li \c lmsNormCoeff_f32 points to Normalised Least mean square FIR filter coefficient buffer
69 * \li \c wire1, wir2, wire3 temporary buffers
70 * \li \c errOutput, err_signal temporary error buffers
72 * \par CMSIS DSP Software Library Functions Used:
74 * - arm_lms_norm_init_f32()
75 * - arm_fir_init_f32()
77 * - arm_lms_norm_f32()
85 * \link arm_signal_converge_example_f32.c \endlink
90 /** \example arm_signal_converge_example_f32.c
94 #include "math_helper.h"
96 /* ----------------------------------------------------------------------
97 ** Global defines for the simulation
98 * ------------------------------------------------------------------- */
100 #define TEST_LENGTH_SAMPLES 1536
103 #define DELTA_ERROR 0.000001f
104 #define DELTA_COEFF 0.0001f
107 #define NUMFRAMES (TEST_LENGTH_SAMPLES / BLOCKSIZE)
109 /* ----------------------------------------------------------------------
110 * Declare FIR state buffers and structure
111 * ------------------------------------------------------------------- */
113 float32_t firStateF32[NUMTAPS + BLOCKSIZE];
114 arm_fir_instance_f32 LPF_instance;
116 /* ----------------------------------------------------------------------
117 * Declare LMSNorm state buffers and structure
118 * ------------------------------------------------------------------- */
120 float32_t lmsStateF32[NUMTAPS + BLOCKSIZE];
121 float32_t errOutput[TEST_LENGTH_SAMPLES];
122 arm_lms_norm_instance_f32 lmsNorm_instance;
125 /* ----------------------------------------------------------------------
126 * Function Declarations for Signal Convergence Example
127 * ------------------------------------------------------------------- */
129 arm_status test_signal_converge_example( void );
132 /* ----------------------------------------------------------------------
134 * ------------------------------------------------------------------- */
135 arm_status test_signal_converge(float32_t* err_signal,
138 void getinput(float32_t* input,
142 /* ----------------------------------------------------------------------
143 * External Declarations for FIR F32 module Test
144 * ------------------------------------------------------------------- */
145 extern float32_t testInput_f32[TEST_LENGTH_SAMPLES];
146 extern float32_t lmsNormCoeff_f32[32];
147 extern const float32_t FIRCoeff_f32[32];
148 extern arm_lms_norm_instance_f32 lmsNorm_instance;
150 /* ----------------------------------------------------------------------
151 * Declare I/O buffers
152 * ------------------------------------------------------------------- */
154 float32_t wire1[BLOCKSIZE];
155 float32_t wire2[BLOCKSIZE];
156 float32_t wire3[BLOCKSIZE];
157 float32_t err_signal[BLOCKSIZE];
159 /* ----------------------------------------------------------------------
160 * Signal converge test
161 * ------------------------------------------------------------------- */
170 /* Initialize the LMSNorm data structure */
171 arm_lms_norm_init_f32(&lmsNorm_instance, NUMTAPS, lmsNormCoeff_f32, lmsStateF32, MU, BLOCKSIZE);
173 /* Initialize the FIR data structure */
174 arm_fir_init_f32(&LPF_instance, NUMTAPS, (float32_t *)FIRCoeff_f32, firStateF32, BLOCKSIZE);
176 /* ----------------------------------------------------------------------
177 * Loop over the frames of data and execute each of the processing
178 * functions in the system.
179 * ------------------------------------------------------------------- */
181 for(i=0; i < NUMFRAMES; i++)
183 /* Read the input data - uniformly distributed random noise - into wire1 */
184 arm_copy_f32(testInput_f32 + (i * BLOCKSIZE), wire1, BLOCKSIZE);
186 /* Execute the FIR processing function. Input wire1 and output wire2 */
187 arm_fir_f32(&LPF_instance, wire1, wire2, BLOCKSIZE);
189 /* Execute the LMS Norm processing function*/
191 arm_lms_norm_f32(&lmsNorm_instance, /* LMSNorm instance */
192 wire1, /* Input signal */
193 wire2, /* Reference Signal */
194 wire3, /* Converged Signal */
195 err_signal, /* Error Signal, this will become small as the signal converges */
196 BLOCKSIZE); /* BlockSize */
198 /* apply overall gain */
199 arm_scale_f32(wire3, 5, wire3, BLOCKSIZE); /* in-place buffer */
202 status = ARM_MATH_SUCCESS;
204 /* -------------------------------------------------------------------------------
205 * Test whether the error signal has reached towards 0.
206 * ----------------------------------------------------------------------------- */
208 arm_abs_f32(err_signal, err_signal, BLOCKSIZE);
209 arm_min_f32(err_signal, BLOCKSIZE, &minValue, &index);
211 if (minValue > DELTA_ERROR)
213 status = ARM_MATH_TEST_FAILURE;
216 /* ----------------------------------------------------------------------
217 * Test whether the filter coefficients have converged.
218 * ------------------------------------------------------------------- */
220 arm_sub_f32((float32_t *)FIRCoeff_f32, lmsNormCoeff_f32, lmsNormCoeff_f32, NUMTAPS);
222 arm_abs_f32(lmsNormCoeff_f32, lmsNormCoeff_f32, NUMTAPS);
223 arm_min_f32(lmsNormCoeff_f32, NUMTAPS, &minValue, &index);
225 if (minValue > DELTA_COEFF)
227 status = ARM_MATH_TEST_FAILURE;
230 /* ----------------------------------------------------------------------
231 * Loop here if the signals did not pass the convergence check.
232 * This denotes a test failure
233 * ------------------------------------------------------------------- */
235 if( status != ARM_MATH_SUCCESS)
240 while(1); /* main function does not return */