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