Cleaning up the new FIR filter implementation. Protects against some corner cases...
[debian/gnuradio] / gnuradio-core / src / lib / filter / gri_fir_filter_with_buffer_ccf.h
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 /*
24  * WARNING: This file is automatically generated by generate_gr_fir_XXX.py
25  * Any changes made to this file will be overwritten.
26  */
27
28
29 #ifndef INCLUDED_GRI_FIR_FILTER_WITH_BUFFER_H
30 #define INCLUDED_GRI_FIR_FILTER_WITH_BUFFER_H
31
32 #include <vector>
33 #include <gr_types.h>
34 #include <gr_reverse.h>
35 #include <string.h>
36 #include <cstdio>
37
38 /*!
39  * \brief FIR with internal buffer for gr_complex input, 
40           gr_complex output and float taps
41  * \ingroup filter
42  * 
43  */
44
45 class gri_fir_filter_with_buffer_ccf {
46
47 protected:
48   std::vector<float>    d_taps;         // reversed taps
49   gr_complex           *d_buffer;
50   unsigned int          d_idx;
51
52 public:
53
54   // CONSTRUCTORS
55
56   /*!
57    * \brief construct new FIR with given taps.
58    *
59    * Note that taps must be in forward order, e.g., coefficient 0 is
60    * stored in new_taps[0], coefficient 1 is stored in
61    * new_taps[1], etc.
62    */
63   gri_fir_filter_with_buffer_ccf (const std::vector<float> &taps);
64
65   ~gri_fir_filter_with_buffer_ccf ();
66
67   // MANIPULATORS
68
69   /*!
70    * \brief compute a single output value.
71    *
72    * \p input must have ntaps() valid entries.
73    * input[0] .. input[ntaps() - 1] are referenced to compute the output value.
74    *
75    * \returns the filtered input value.
76    */
77   gr_complex filter (gr_complex input);
78
79   /*!
80    * \brief compute an array of N output values.
81    *
82    * \p input must have (n - 1 + ntaps()) valid entries.
83    * input[0] .. input[n - 1 + ntaps() - 1] are referenced to compute the output values.
84    */
85   void filterN (gr_complex output[], const gr_complex input[],
86                 unsigned long n);
87
88   /*!
89    * \brief compute an array of N output values, decimating the input
90    *
91    * \p input must have (decimate * (n - 1) + ntaps()) valid entries.
92    * input[0] .. input[decimate * (n - 1) + ntaps() - 1] are referenced to 
93    * compute the output values.
94    */
95   void filterNdec (gr_complex output[], const gr_complex input[],
96                    unsigned long n, unsigned decimate);
97
98   /*!
99    * \brief install \p new_taps as the current taps.
100    */
101   void set_taps (const std::vector<float> &taps)
102   {
103     d_taps = gr_reverse(taps);
104
105     if(d_buffer != NULL) {
106       free(d_buffer);
107       d_buffer = NULL;
108     }
109     
110     // FIXME: memalign this to 16-byte boundaries for SIMD later
111     size_t t = sizeof(gr_complex) * 2 * d_taps.size();
112     d_buffer = (gr_complex*)malloc(t);
113     memset(d_buffer, 0x00, t);
114     d_idx = 0;
115   }
116
117   // ACCESSORS
118
119   /*!
120    * \return number of taps in filter.
121    */
122   unsigned ntaps () const { return d_taps.size (); }
123
124   /*!
125    * \return current taps
126    */
127   const std::vector<float> get_taps () const
128   {
129     return gr_reverse(d_taps);
130   }
131 };
132
133 #endif /* INCLUDED_GR_GR_FIR_FILTER_WITH_BUFFER_CCF_H */