arm_fir_example_f32.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_fir_example_f32.c
  9. *
  10. * Description: Example code demonstrating how an FIR filter can be used
  11. * as a low pass filter.
  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. * @addtogroup groupExamples
  43. * @{
  44. *
  45. * @defgroup FIRLPF FIR Lowpass Filter Example
  46. *
  47. * \par Description:
  48. * \par
  49. * Removes high frequency signal components from the input using an FIR lowpass filter.
  50. * The example demonstrates how to configure an FIR filter and then pass data through
  51. * it in a block-by-block fashion.
  52. * \image html FIRLPF_signalflow.gif
  53. *
  54. * \par Algorithm:
  55. * \par
  56. * The input signal is a sum of two sine waves: 1 kHz and 15 kHz.
  57. * This is processed by an FIR lowpass filter with cutoff frequency 6 kHz.
  58. * The lowpass filter eliminates the 15 kHz signal leaving only the 1 kHz sine wave at the output.
  59. * \par
  60. * The lowpass filter was designed using MATLAB with a sample rate of 48 kHz and
  61. * a length of 29 points.
  62. * The MATLAB code to generate the filter coefficients is shown below:
  63. * <pre>
  64. * h = fir1(28, 6/24);
  65. * </pre>
  66. * The first argument is the "order" of the filter and is always one less than the desired length.
  67. * The second argument is the normalized cutoff frequency. This is in the range 0 (DC) to 1.0 (Nyquist).
  68. * A 6 kHz cutoff with a Nyquist frequency of 24 kHz lies at a normalized frequency of 6/24 = 0.25.
  69. * The CMSIS FIR filter function requires the coefficients to be in time reversed order.
  70. * <pre>
  71. * fliplr(h)
  72. * </pre>
  73. * The resulting filter coefficients and are shown below.
  74. * Note that the filter is symmetric (a property of linear phase FIR filters)
  75. * and the point of symmetry is sample 14. Thus the filter will have a delay of
  76. * 14 samples for all frequencies.
  77. * \par
  78. * \image html FIRLPF_coeffs.gif
  79. * \par
  80. * The frequency response of the filter is shown next.
  81. * The passband gain of the filter is 1.0 and it reaches 0.5 at the cutoff frequency 6 kHz.
  82. * \par
  83. * \image html FIRLPF_response.gif
  84. * \par
  85. * The input signal is shown below.
  86. * The left hand side shows the signal in the time domain while the right hand side is a frequency domain representation.
  87. * The two sine wave components can be clearly seen.
  88. * \par
  89. * \image html FIRLPF_input.gif
  90. * \par
  91. * The output of the filter is shown below. The 15 kHz component has been eliminated.
  92. * \par
  93. * \image html FIRLPF_output.gif
  94. *
  95. * \par Variables Description:
  96. * \par
  97. * \li \c testInput_f32_1kHz_15kHz points to the input data
  98. * \li \c refOutput points to the reference output data
  99. * \li \c testOutput points to the test output data
  100. * \li \c firStateF32 points to state buffer
  101. * \li \c firCoeffs32 points to coefficient buffer
  102. * \li \c blockSize number of samples processed at a time
  103. * \li \c numBlocks number of frames
  104. *
  105. * \par CMSIS DSP Software Library Functions Used:
  106. * \par
  107. * - arm_fir_init_f32()
  108. * - arm_fir_f32()
  109. *
  110. * <b> Refer </b>
  111. * \link arm_fir_example_f32.c \endlink
  112. *
  113. * \example arm_fir_example_f32.c
  114. *
  115. * @} */
  116. /* ----------------------------------------------------------------------
  117. ** Include Files
  118. ** ------------------------------------------------------------------- */
  119. #include "arm_math.h"
  120. #include "math_helper.h"
  121. #if defined(SEMIHOSTING)
  122. #include <stdio.h>
  123. #endif
  124. /* ----------------------------------------------------------------------
  125. ** Macro Defines
  126. ** ------------------------------------------------------------------- */
  127. #define TEST_LENGTH_SAMPLES 320
  128. /*
  129. This SNR is a bit small. Need to understand why
  130. this example is not giving better SNR ...
  131. */
  132. #define SNR_THRESHOLD_F32 75.0f
  133. #define BLOCK_SIZE 32
  134. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  135. /* Must be a multiple of 16 */
  136. #define NUM_TAPS_ARRAY_SIZE 32
  137. #else
  138. #define NUM_TAPS_ARRAY_SIZE 29
  139. #endif
  140. #define NUM_TAPS 29
  141. /* -------------------------------------------------------------------
  142. * The input signal and reference output (computed with MATLAB)
  143. * are defined externally in arm_fir_lpf_data.c.
  144. * ------------------------------------------------------------------- */
  145. extern float32_t testInput_f32_1kHz_15kHz[TEST_LENGTH_SAMPLES];
  146. extern float32_t refOutput[TEST_LENGTH_SAMPLES];
  147. /* -------------------------------------------------------------------
  148. * Declare Test output buffer
  149. * ------------------------------------------------------------------- */
  150. static float32_t testOutput[TEST_LENGTH_SAMPLES];
  151. /* -------------------------------------------------------------------
  152. * Declare State buffer of size (numTaps + blockSize - 1)
  153. * ------------------------------------------------------------------- */
  154. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  155. static float32_t firStateF32[2 * BLOCK_SIZE + NUM_TAPS - 1];
  156. #else
  157. static float32_t firStateF32[BLOCK_SIZE + NUM_TAPS - 1];
  158. #endif
  159. /* ----------------------------------------------------------------------
  160. ** FIR Coefficients buffer generated using fir1() MATLAB function.
  161. ** fir1(28, 6/24)
  162. ** ------------------------------------------------------------------- */
  163. #if defined(ARM_MATH_MVEF) && !defined(ARM_MATH_AUTOVECTORIZE)
  164. const float32_t firCoeffs32[NUM_TAPS_ARRAY_SIZE] = {
  165. -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f,
  166. -0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f,
  167. +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f,
  168. +0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f, 0.0f,0.0f,0.0f
  169. };
  170. #else
  171. const float32_t firCoeffs32[NUM_TAPS_ARRAY_SIZE] = {
  172. -0.0018225230f, -0.0015879294f, +0.0000000000f, +0.0036977508f, +0.0080754303f, +0.0085302217f, -0.0000000000f, -0.0173976984f,
  173. -0.0341458607f, -0.0333591565f, +0.0000000000f, +0.0676308395f, +0.1522061835f, +0.2229246956f, +0.2504960933f, +0.2229246956f,
  174. +0.1522061835f, +0.0676308395f, +0.0000000000f, -0.0333591565f, -0.0341458607f, -0.0173976984f, -0.0000000000f, +0.0085302217f,
  175. +0.0080754303f, +0.0036977508f, +0.0000000000f, -0.0015879294f, -0.0018225230f
  176. };
  177. #endif
  178. /* ------------------------------------------------------------------
  179. * Global variables for FIR LPF Example
  180. * ------------------------------------------------------------------- */
  181. uint32_t blockSize = BLOCK_SIZE;
  182. uint32_t numBlocks = TEST_LENGTH_SAMPLES/BLOCK_SIZE;
  183. float32_t snr;
  184. /* ----------------------------------------------------------------------
  185. * FIR LPF Example
  186. * ------------------------------------------------------------------- */
  187. int32_t main(void)
  188. {
  189. uint32_t i;
  190. arm_fir_instance_f32 S;
  191. arm_status status;
  192. float32_t *inputF32, *outputF32;
  193. /* Initialize input and output buffer pointers */
  194. inputF32 = &testInput_f32_1kHz_15kHz[0];
  195. outputF32 = &testOutput[0];
  196. /* Call FIR init function to initialize the instance structure. */
  197. arm_fir_init_f32(&S, NUM_TAPS, (float32_t *)&firCoeffs32[0], &firStateF32[0], blockSize);
  198. /* ----------------------------------------------------------------------
  199. ** Call the FIR process function for every blockSize samples
  200. ** ------------------------------------------------------------------- */
  201. for(i=0; i < numBlocks; i++)
  202. {
  203. arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize);
  204. }
  205. /* ----------------------------------------------------------------------
  206. ** Compare the generated output against the reference output computed
  207. ** in MATLAB.
  208. ** ------------------------------------------------------------------- */
  209. snr = arm_snr_f32(&refOutput[0], &testOutput[0], TEST_LENGTH_SAMPLES);
  210. status = (snr < SNR_THRESHOLD_F32) ? ARM_MATH_TEST_FAILURE : ARM_MATH_SUCCESS;
  211. if (status != ARM_MATH_SUCCESS)
  212. {
  213. #if defined (SEMIHOSTING)
  214. printf("FAILURE\n");
  215. #else
  216. while (1); /* main function does not return */
  217. #endif
  218. }
  219. else
  220. {
  221. #if defined (SEMIHOSTING)
  222. printf("SUCCESS\n");
  223. #else
  224. while (1); /* main function does not return */
  225. #endif
  226. }
  227. }
  228. /** \endlink */