Merged gcell-wip -r8159:8202 into trunk. This includes the following
[debian/gnuradio] / gcell / src / lib / wrapper / gcp_fft_1d_r2.cc
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2008 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 along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24 #include <gcp_fft_1d_r2.h>
25 #include <stdint.h>
26 #include <stdexcept>
27 #include <math.h>
28
29 static void
30 init_jd(gc_job_desc *jd,
31         gc_proc_id_t proc_id,
32         unsigned log2_fft_length,
33         bool forward,
34         std::complex<float> *out,
35         const std::complex<float> *in,
36         const std::complex<float> *W)
37 {
38   jd->proc_id = proc_id;
39   jd->input.nargs = 2;
40   jd->output.nargs = 0;
41   jd->eaa.nargs = 3;
42
43   jd->input.arg[0].u32 = log2_fft_length;
44   jd->input.arg[1].u32 = forward;
45   unsigned int fft_length = 1 << log2_fft_length;
46
47   jd->eaa.arg[0].ea_addr = ptr_to_ea(out);
48   jd->eaa.arg[0].direction = GCJD_DMA_PUT;
49   jd->eaa.arg[0].put_size = sizeof(std::complex<float>) * fft_length;
50
51   jd->eaa.arg[1].ea_addr = ptr_to_ea(const_cast<std::complex<float>*>(in));
52   jd->eaa.arg[1].direction = GCJD_DMA_GET;
53   jd->eaa.arg[1].get_size = sizeof(std::complex<float>) * fft_length;
54
55   jd->eaa.arg[2].ea_addr = ptr_to_ea(const_cast<std::complex<float>*>(W));
56   jd->eaa.arg[2].direction = GCJD_DMA_GET;
57   jd->eaa.arg[2].get_size = sizeof(std::complex<float>) * fft_length / 4;
58 }
59
60
61 gc_job_desc *
62 gcp_fft_1d_r2_submit(gc_job_manager_sptr mgr,
63                      unsigned int log2_fft_length,
64                      bool forward,
65                      std::complex<float> *out,
66                      const std::complex<float> *in,
67                      const std::complex<float> *W)
68 {
69   unsigned int fft_length = 1 << log2_fft_length;
70   if (fft_length > 4096)
71     throw std::invalid_argument("fft_length > 4096");
72
73   if ((intptr_t)out & 0xf)
74     throw gc_bad_align("out");
75   if ((intptr_t)in & 0xf)
76     throw gc_bad_align("in");
77   if ((intptr_t)W & 0xf)
78     throw gc_bad_align("W");
79
80   gc_proc_id_t fft_id = mgr->lookup_proc("fft_1d_r2");
81   gc_job_desc *jd = mgr->alloc_job_desc();
82   init_jd(jd, fft_id, log2_fft_length, forward, out, in, W);
83   if (!mgr->submit_job(jd)){
84     gc_job_status_t s = jd->status;
85     mgr->free_job_desc(jd);
86     throw gc_bad_submit("fft_1d_r2", s);
87   }
88   return jd;
89 }
90
91 void
92 gcp_fft_1d_r2_forward_twiddle(unsigned int log2_fft_length, std::complex<float> *W)
93 {
94   unsigned int n = 1 << log2_fft_length;
95
96   W[0].real() = 1.0;
97   W[0].imag() = 0.0;
98   for (unsigned i=1; i < n/4; i++){
99     W[i].real() =  cos(i * 2*M_PI/n);
100     W[n/4 - i].imag() = -W[i].real();
101   }
102 }
103
104
105 void
106 gcp_fft_1d_r2_reverse_twiddle(unsigned int log2_fft_length, std::complex<float> *W)
107 {
108   // FIXME this is wrong/insufficient.  inverse is still incorrect
109
110   // reverse factors are the conjugate of the forward factors
111   gcp_fft_1d_r2_forward_twiddle(log2_fft_length, W);
112
113   unsigned int n = 1 << log2_fft_length;
114   for (unsigned i=0; i < n/4; i++)
115     W[i] = conj(W[i]);
116 }