Project Ne10
An open, optimized software library for the ARM architecture.
NE10_sample_fir.c
Go to the documentation of this file.
1 /*
2  * Copyright 2011-16 ARM Limited and Contributors.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  * * Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * * Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * * Neither the name of ARM Limited nor the
13  * names of its contributors may be used to endorse or promote products
14  * derived from this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY ARM LIMITED AND CONTRIBUTORS "AS IS" AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL ARM LIMITED AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 #include "NE10.h"
31 
32 #define NUMTAPS 4
33 #define NUMBLOCKS 2
34 #define BLOCKSIZE 8
35 #define BUFFSIZE (NUMBLOCKS * BLOCKSIZE)
36 
41 int fir_sample_main(void)
42 {
43  ne10_float32_t src[BUFFSIZE]; // A source array of input data
44  ne10_float32_t dst[BUFFSIZE]; // A destination array for the transformed data
45  ne10_float32_t st[NUMTAPS + BLOCKSIZE - 1]; // A "state" buffer for use within the FIR
46  ne10_float32_t coeffs[NUMTAPS]; // An array of FIR coefficients (in reverse order)
47  ne10_fir_instance_f32_t cfg; // An FIR "instance structure"
48 
49  // Initialise Ne10, using hardware auto-detection to set library function pointers
50  if (ne10_init() != NE10_OK)
51  {
52  fprintf(stderr, "Failed to initialise Ne10.\n");
53  return 1;
54  }
55 
56  // Prepare the FIR instance structure, storing `NUMTAPS`, `coeffs`, and `st` within
57  // it, and clearing the state buffer. (For constant parameters, this process can
58  // instead be performed manually.)
59  if (ne10_fir_init_float(&cfg, NUMTAPS, coeffs, st, BLOCKSIZE) != NE10_OK)
60  {
61  fprintf(stderr, "Failed to initialise FIR instance structure.\n");
62  return 1;
63  }
64 
65  // Generate test coefficient values
66  for (int i = 0; i < NUMTAPS; i++)
67  {
68  coeffs[i] = (ne10_float32_t)rand() / RAND_MAX * 5.0f;
69  }
70 
71  // Generate test input values
72  for (int i = 0; i < BUFFSIZE; i++)
73  {
74  src[i] = (ne10_float32_t)rand() / RAND_MAX * 20.0f;
75  }
76 
77  // Perform the FIR filtering of the input buffer in `NUMBLOCKS` blocks of `BLOCKSIZE`
78  // elements using the parameters set up in the FIR instance structure `cfg`.
79  for (int b = 0; b < NUMBLOCKS; b++)
80  {
81  ne10_fir_float(&cfg, src + (b * BLOCKSIZE), dst + (b * BLOCKSIZE), BLOCKSIZE);
82  }
83 
84  // Display the results (dst[i] = b[0] * src[i] + b[1] * src[i - 1] + b[2] * src[i - 2]
85  // + ... + b[NUMTAPS - 1] * src[i - (NUMTAPS - 1)])
86  printf("Coefficients:\n");
87  for (int i = NUMTAPS - 1; i >= 0; i--)
88  {
89  printf("\tb[%d] = %5.4f\n", NUMTAPS - (i + 1), coeffs[i]);
90  }
91  for (int i = 0; i < BUFFSIZE; i++)
92  {
93  printf( "IN[%2d]: %9.4f\t", i, src[i]);
94  printf("OUT[%2d]: %9.4f\n", i, dst[i]);
95  }
96 
97  return 0;
98 }
ne10_result_t ne10_fir_init_float(ne10_fir_instance_f32_t *S, ne10_uint16_t numTaps, ne10_float32_t *pCoeffs, ne10_float32_t *pState, ne10_uint32_t blockSize)
Definition: NE10_fir_init.c:53
float ne10_float32_t
Definition: NE10_types.h:80
#define BLOCKSIZE
#define NUMTAPS
Instance structure for the floating-point FIR filter.
Definition: NE10_types.h:361
#define NUMBLOCKS
ne10_result_t ne10_init(void)
Definition: NE10_init.c:44
#define BUFFSIZE
int fir_sample_main(void)
void(* ne10_fir_float)(const ne10_fir_instance_f32_t *S, ne10_float32_t *pSrc, ne10_float32_t *pDst, ne10_uint32_t blockSize)
#define NE10_OK
Definition: NE10_types.h:65