Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_fft_vfc.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2004 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 2, 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 <gr_fft_vfc.h>
28 #include <gr_io_signature.h>
29 #include <gri_fft.h>
30 #include <math.h>
31 #include <stdexcept>
32
33
34 // FIXME after this is working, change to use native real to complex fft.
35 // It should run twice as fast.
36
37
38
39
40 gr_fft_vfc_sptr
41 gr_make_fft_vfc (int fft_size, bool forward, const std::vector<float> window)
42 {
43   return gr_fft_vfc_sptr (new gr_fft_vfc (fft_size, forward, window));
44 }
45
46 gr_fft_vfc::gr_fft_vfc (int fft_size, bool forward, const std::vector<float> window)
47   : gr_sync_block ("fft_vfc",
48                    gr_make_io_signature (1, 1, fft_size * sizeof (float)),
49                    gr_make_io_signature (1, 1, fft_size * sizeof (gr_complex))),
50     d_fft_size(fft_size), d_window()
51 {
52   if (!forward){
53     fprintf (stderr, "fft_vfc: forward must == true\n");
54     throw std::invalid_argument ("fft_vfc: forward must == true");
55   }
56
57   d_fft = new gri_fft_complex (d_fft_size, forward);
58
59   set_window(window);
60 }
61
62 gr_fft_vfc::~gr_fft_vfc ()
63 {
64   delete d_fft;
65 }
66
67 int
68 gr_fft_vfc::work (int noutput_items,
69                   gr_vector_const_void_star &input_items,
70                   gr_vector_void_star &output_items)
71 {
72   const float *in = (const float *) input_items[0];
73   gr_complex *out = (gr_complex *) output_items[0];
74
75   unsigned int output_data_size = output_signature()->sizeof_stream_item (0);
76
77   int count = 0;
78
79   while (count++ < noutput_items){
80
81     // copy input into optimally aligned buffer
82
83     if (d_window.size()){
84       gr_complex *dst = d_fft->get_inbuf();
85       for (unsigned int i = 0; i < d_fft_size; i++)             // apply window
86         dst[i] = in[i] * d_window[i];
87     }
88     else {
89       gr_complex *dst = d_fft->get_inbuf();
90       for (unsigned int i = 0; i < d_fft_size; i++)             // float to complex conversion
91         dst[i] = in[i];                                
92     }
93
94     // compute the fft
95     d_fft->execute ();
96
97     // cpoy result to our output
98     memcpy (out, d_fft->get_outbuf (), output_data_size);
99
100     in  += d_fft_size;
101     out += d_fft_size;
102   }
103
104   return noutput_items;
105 }
106
107 bool 
108 gr_fft_vfc::set_window(const std::vector<float> window)
109 {
110   if(window.size()==0 || window.size()==d_fft_size) {
111     d_window=window;
112     return true;
113   }
114   else 
115     return false;
116 }