PandA-2024.02
coef1.cpp
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdint.h>
9 #define _USE_MATH_DEFINES
10 #include <array> // array
11 #include <utility> // index_sequence, make_index_sequence
12 #include <cmath> // sinf,cosf
13 #include <cstddef> // size_t
14 #include <unistd.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <fcntl.h>
18 #include <cassert>
19 
21 // Filter Code Definitions
23 
24 
25 template <int filter_len>
27 {
28  constexpr static short int compute_coeff(size_t index)
29  {
30  // parameters and simulation options
31  float fc = 0.20; // normalized cutoff frequency
32  // generate time vector, centered at zero
33  float t = (float)index + 0.5f - 0.5f*(float)filter_len;
34  // generate sinc function (time offset in 't' prevents divide by zero)
35  float s = sinf(2*M_PI*fc*t + 1e-6f) / (2*M_PI*fc*t + 1e-6f);
36  // generate Hamming window
37  float w = 0.53836 - 0.46164*cosf((2*M_PI*(float)index)/((float)(filter_len-1)));
38  // generate composite filter coefficient
39  return (s * w)*(1<<14);
40  }
41  template <std::size_t... I>
42  constexpr static std::array<short int, sizeof...(I)> coeff_fill(std::index_sequence<I...>)
43  {
44  return std::array<short int, sizeof...(I)>{compute_coeff(I)...};
45  }
46  template <std::size_t N>
47  constexpr static std::array<short int, N> coeff_fill()
48  {
49  return coeff_fill(std::make_index_sequence<N>{});
50  }
51 
53  static const ::std::array<short int,filter_len> coeffs;
54 
55  // array to hold input samples
56  int16_t insamp[ filter_len ];
57 
58 
59 
60  public:
61 
62  short int operator()(short int input)
63  {
64  int16_t output;
65  int32_t acc; // accumulator for MACs
66  int n;
67  int k;
68 
69  // put the new samples at the high end of the buffer
70  insamp[filter_len - 1] = input;
71  // apply the filter to each input sample
72 
73  // calculate output
74  // load rounding constant
75  acc = (1 << 14)+((int32_t)(coeffs[0]) * (int32_t)(input));
76  // perform the multiply-accumulate
77  for ( k = 0; k < filter_len-1; k++ ) {
78  acc += (int32_t)(coeffs[filter_len - 1 -k]) * (int32_t)(insamp[k]);
79  // shift input samples back in time for next time
80  insamp[k] = insamp[1+k];
81  }
82  // saturate the result
83  if ( acc > 0x3fffffff ) {
84  acc = 0x3fffffff;
85  } else if ( acc < -0x40000000 ) {
86  acc = -0x40000000;
87  }
88  // convert from Q30 to Q15
89  output = (int16_t)(acc >> 15);
90 
91  return output;
92  }
94 };
95 
96 template <int filter_len>
97 constexpr const ::std::array<short int,filter_len> firFixedClass<filter_len>::coeffs = coeff_fill<filter_len>();
98 
99 
100 // maximum length of filter than can be handled
101 #define FILTER_LEN 63
102 
103 
104 // the FIR filter function
105 #ifdef WITH_EXTERNC
106 extern "C"
107 #endif
108 short int
109 __attribute__ ((noinline))
110 firFixed( short int input)
111 {
112  static firFixedClass<FILTER_LEN> fir;
113  return fir(input);
114 }
115 
117 // Test program
119 
120 int main( void )
121 {
122  int size;
123  int16_t input;
124  int16_t output;
125  int in_fid;
126  int out_fid;
127 
128  // open the input waveform file
129  in_fid = open( "input.pcm", O_RDONLY );
130  if ( in_fid < 0 ) {
131 #ifndef NDEBUG
132  printf("couldn't open input.pcm");
133 #endif
134  return 1;
135  }
136 
137  // open the output waveform file
138  out_fid = open( "outputFixed.pcm", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH );
139  if ( out_fid < 0 ) {
140 #ifndef NDEBUG
141  printf("couldn't open outputFixed.pcm");
142 #endif
143  return 1;
144  }
145 
146  // process all of the samples
147  do {
148  // read samples from file
149  size = read(in_fid, &input, sizeof(int16_t))/sizeof(int16_t);
150  // perform the filtering
151  if(size)
152  {
153  assert(size==1);
154  output = firFixed( input );
155  // write samples to file
156  write(out_fid, &output, sizeof(int16_t));
157  }
158  } while ( size != 0 );
159 
160  close( in_fid );
161  close( out_fid );
162 
163  return 0;
164 }
int main(void)
Definition: coef1.cpp:120
int input[SIZE]
Definition: hash.h:1
short int operator()(short int input)
Definition: coef1.cpp:62
static const ::std::array< short int, filter_len > coeffs
filter coefficients
Definition: coef1.cpp:53
volatile int output[DIM_Y][DIM_X]
Definition: los.h:1
firFixedClass()
Definition: coef1.cpp:93
short int __attribute__((noinline)) firFixed(short int input)
Definition: coef1.cpp:109
static const uint32_t k[]
Definition: sha-256.c:22
#define index(x, y)
Definition: Keccak.c:74
static constexpr std::array< short int, N > coeff_fill()
Definition: coef1.cpp:47
static constexpr std::array< short int, sizeof...(I)> coeff_fill(std::index_sequence< I... >)
Definition: coef1.cpp:42
static constexpr short int compute_coeff(size_t index)
Definition: coef1.cpp:28
unsigned int size_t
Definition: test.c:1
int16_t insamp[filter_len]
Definition: coef1.cpp:56
int array[ARRAY_SIZE]
Definition: add.h:1
short int read(short int *data)
Definition: read.c:3

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13