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.cc
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 <gri_fir_filter_with_buffer_ccf.h>
28
29 gri_fir_filter_with_buffer_ccf::gri_fir_filter_with_buffer_ccf(const std::vector<float> &taps)
30 {
31   d_buffer = NULL;
32   set_taps(taps);
33 }
34
35 gri_fir_filter_with_buffer_ccf::~gri_fir_filter_with_buffer_ccf()
36 {
37   if(d_buffer != NULL)
38     free(d_buffer);
39 }
40
41 gr_complex 
42 gri_fir_filter_with_buffer_ccf::filter (gr_complex input)
43 {
44   unsigned int i;
45
46   d_buffer[d_idx] = input;
47   d_buffer[d_idx+ntaps()] = input;
48
49   // using the later for the case when ntaps=0;
50   // profiling shows this doesn't make a difference
51   //d_idx = (d_idx + 1) % ntaps();
52   d_idx++;
53   if(d_idx >= ntaps())
54     d_idx = 0;
55
56   gr_complex out = gr_complex(0,0);
57   for(i = 0; i < ntaps(); i++) {
58     out += d_buffer[d_idx + i]*d_taps[i];
59   }
60   return out;
61 }
62
63 void
64 gri_fir_filter_with_buffer_ccf::filterN (gr_complex output[],
65                                          const gr_complex input[],
66                                          unsigned long n)
67 {
68   for(unsigned long i = 0; i < n; i++) {
69     output[i] = filter(input[i]);
70   }
71 }