Updated license from GPL version 2 or later to GPL version 3 or later.
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_complex_to_xxx.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_complex_to_xxx.h>
28 #include <gr_io_signature.h>
29 #include <gr_math.h>
30
31 // ----------------------------------------------------------------
32
33 gr_complex_to_float_sptr
34 gr_make_complex_to_float (unsigned int vlen)
35 {
36   return gr_complex_to_float_sptr (new gr_complex_to_float (vlen));
37 }
38
39 gr_complex_to_float::gr_complex_to_float (unsigned int vlen)
40   : gr_sync_block ("complex_to_float",
41                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
42                    gr_make_io_signature (1, 2, sizeof (float) * vlen)),
43     d_vlen(vlen)
44 {
45 }
46
47 int
48 gr_complex_to_float::work (int noutput_items,
49                            gr_vector_const_void_star &input_items,
50                            gr_vector_void_star &output_items)
51 {
52   const gr_complex *in = (const gr_complex *) input_items[0];
53   float *out0 = (float *) output_items[0];
54   float *out1 = (float *) output_items[1];
55   int noi = noutput_items * d_vlen;
56
57   switch (output_items.size ()){
58   case 1:
59     for (int i = 0; i < noi; i++){
60       out0[i] = in[i].real ();
61     }
62     break;
63
64   case 2:
65     for (int i = 0; i < noi; i++){
66       out0[i] = in[i].real ();
67       out1[i] = in[i].imag ();
68     }
69     break;
70
71   default:
72     abort ();
73   }
74
75   return noutput_items;
76 }
77
78 // ----------------------------------------------------------------
79
80 gr_complex_to_real_sptr
81 gr_make_complex_to_real (unsigned int vlen)
82 {
83   return gr_complex_to_real_sptr (new gr_complex_to_real (vlen));
84 }
85
86 gr_complex_to_real::gr_complex_to_real (unsigned int vlen)
87   : gr_sync_block ("complex_to_real",
88                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
89                    gr_make_io_signature (1, 1, sizeof (float) * vlen)),
90     d_vlen(vlen)
91 {
92 }
93
94 int
95 gr_complex_to_real::work (int noutput_items,
96                           gr_vector_const_void_star &input_items,
97                           gr_vector_void_star &output_items)
98 {
99   const gr_complex *in = (const gr_complex *) input_items[0];
100   float *out = (float *) output_items[0];
101   int noi = noutput_items * d_vlen;
102
103   for (int i = 0; i < noi; i++){
104     out[i] = in[i].real ();
105   }
106   return noutput_items;
107 }
108
109 // ----------------------------------------------------------------
110
111 gr_complex_to_imag_sptr
112 gr_make_complex_to_imag (unsigned int vlen)
113 {
114   return gr_complex_to_imag_sptr (new gr_complex_to_imag (vlen));
115 }
116
117 gr_complex_to_imag::gr_complex_to_imag (unsigned int vlen)
118   : gr_sync_block ("complex_to_imag",
119                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
120                    gr_make_io_signature (1, 1, sizeof (float) * vlen)),
121     d_vlen(vlen)
122 {
123 }
124
125 int
126 gr_complex_to_imag::work (int noutput_items,
127                           gr_vector_const_void_star &input_items,
128                           gr_vector_void_star &output_items)
129 {
130   const gr_complex *in = (const gr_complex *) input_items[0];
131   float *out = (float *) output_items[0];
132   int noi = noutput_items * d_vlen;
133
134   for (int i = 0; i < noi; i++){
135     out[i] = in[i].imag ();
136   }
137   return noutput_items;
138 }
139
140 // ----------------------------------------------------------------
141
142 gr_complex_to_mag_sptr
143 gr_make_complex_to_mag (unsigned int vlen)
144 {
145   return gr_complex_to_mag_sptr (new gr_complex_to_mag (vlen));
146 }
147
148 gr_complex_to_mag::gr_complex_to_mag (unsigned int vlen)
149   : gr_sync_block ("complex_to_mag",
150                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
151                    gr_make_io_signature (1, 1, sizeof (float) * vlen)),
152     d_vlen(vlen)
153 {
154 }
155
156 int
157 gr_complex_to_mag::work (int noutput_items,
158                           gr_vector_const_void_star &input_items,
159                           gr_vector_void_star &output_items)
160 {
161   const gr_complex *in = (const gr_complex *) input_items[0];
162   float *out = (float *) output_items[0];
163   int noi = noutput_items * d_vlen;
164
165   for (int i = 0; i < noi; i++){
166     out[i] = std::abs (in[i]);
167   }
168   return noutput_items;
169 }
170
171 // ----------------------------------------------------------------
172
173 gr_complex_to_mag_squared_sptr
174 gr_make_complex_to_mag_squared (unsigned int vlen)
175 {
176   return gr_complex_to_mag_squared_sptr (new gr_complex_to_mag_squared (vlen));
177 }
178
179 gr_complex_to_mag_squared::gr_complex_to_mag_squared (unsigned int vlen)
180   : gr_sync_block ("complex_to_mag_squared",
181                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
182                    gr_make_io_signature (1, 1, sizeof (float) * vlen)),
183     d_vlen(vlen)
184 {
185 }
186
187 int
188 gr_complex_to_mag_squared::work (int noutput_items,
189                                  gr_vector_const_void_star &input_items,
190                                  gr_vector_void_star &output_items)
191 {
192   const gr_complex *in = (const gr_complex *) input_items[0];
193   float *out = (float *) output_items[0];
194   int noi = noutput_items * d_vlen;
195
196   for (int i = 0; i < noi; i++){
197     const float __x = in[i].real();
198     const float __y = in[i].imag();
199     out[i] = __x * __x + __y * __y;
200   }
201   return noutput_items;
202 }
203
204 // ----------------------------------------------------------------
205
206 gr_complex_to_arg_sptr
207 gr_make_complex_to_arg (unsigned int vlen)
208 {
209   return gr_complex_to_arg_sptr (new gr_complex_to_arg (vlen));
210 }
211
212 gr_complex_to_arg::gr_complex_to_arg (unsigned int vlen)
213   : gr_sync_block ("complex_to_arg",
214                    gr_make_io_signature (1, 1, sizeof (gr_complex) * vlen),
215                    gr_make_io_signature (1, 1, sizeof (float) * vlen)),
216     d_vlen(vlen)
217 {
218 }
219
220 int
221 gr_complex_to_arg::work (int noutput_items,
222                           gr_vector_const_void_star &input_items,
223                           gr_vector_void_star &output_items)
224 {
225   const gr_complex *in = (const gr_complex *) input_items[0];
226   float *out = (float *) output_items[0];
227   int noi = noutput_items * d_vlen;
228
229   for (int i = 0; i < noi; i++){
230     //    out[i] = std::arg (in[i]);
231     out[i] = gr_fast_atan2f(in[i]);
232   }
233   return noutput_items;
234 }