Adding ability for FIR filter with internal buffer to decimate.
[debian/gnuradio] / gnuradio-core / src / lib / filter / gri_fir_filter_with_buffer_XXX.cc.t
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2010 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio
6  * 
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <@NAME@.h>
28
29 @NAME@::@NAME@(const std::vector<@TAP_TYPE@> &taps)
30 {
31   d_buffer = NULL;
32   set_taps(taps);
33 }
34
35 @NAME@::~@NAME@()
36 {
37   if(d_buffer != NULL)
38     free(d_buffer);
39 }
40
41 void
42 @NAME@::set_taps (const std::vector<@TAP_TYPE@> &taps)
43 {
44   d_taps = gr_reverse(taps);
45   
46   if(d_buffer != NULL) {
47     free(d_buffer);
48     d_buffer = NULL;
49   }
50   
51   // FIXME: memalign this to 16-byte boundaries for SIMD later
52   size_t t = sizeof(@I_TYPE@) * 2 * d_taps.size();
53   d_buffer = (@I_TYPE@*)malloc(t);
54   memset(d_buffer, 0x00, t);
55   d_idx = 0;
56 }
57
58 @O_TYPE@
59 @NAME@::filter (@I_TYPE@ input)
60 {
61   unsigned int i;
62
63   d_buffer[d_idx] = input;
64   d_buffer[d_idx+ntaps()] = input;
65
66   // using the later for the case when ntaps=0;
67   // profiling shows this doesn't make a difference
68   //d_idx = (d_idx + 1) % ntaps();
69   d_idx++;
70   if(d_idx >= ntaps())
71     d_idx = 0;
72
73   @ACC_TYPE@ out = 0;
74   for(i = 0; i < ntaps(); i++) {
75     out += @INPUT_CAST@ d_buffer[d_idx + i] * d_taps[i];
76   }
77   return (@O_TYPE@)out;
78 }
79
80 @O_TYPE@
81 @NAME@::filter (const @I_TYPE@ input[], unsigned long dec)
82 {
83   unsigned int i;
84
85   for(i = 0; i < dec; i++) {
86     d_buffer[d_idx] = input[i];
87     d_buffer[d_idx+ntaps()] = input[i];
88     d_idx++;
89     if(d_idx >= ntaps())
90       d_idx = 0;
91   }
92
93   @ACC_TYPE@ out = 0;
94   for(i = 0; i < ntaps(); i++) {
95     out += @INPUT_CAST@ d_buffer[d_idx + i] * d_taps[i];
96   }
97   return (@O_TYPE@)out;
98 }
99
100 void
101 @NAME@::filterN (@O_TYPE@ output[],
102                  const @I_TYPE@ input[],
103                  unsigned long n)
104 {
105   for(unsigned long i = 0; i < n; i++) {
106     output[i] = filter(input[i]);
107   }
108 }
109
110 void
111 @NAME@::filterNdec (@O_TYPE@ output[],
112                     const @I_TYPE@ input[],
113                     unsigned long n,
114                     unsigned long decimate)
115 {
116   unsigned long j = 0;
117   for(unsigned long i = 0; i < n; i++) {
118     output[i] = filter(&input[j], decimate);
119     j += decimate;
120   }
121 }