154e1d4b14dc024a67effe39a13f382484439fe0
[debian/gnuradio] / gnuradio-core / src / lib / gengen / gr_sig_source_c.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 // WARNING: this file is machine generated.  Edits will be over written
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <gr_sig_source_c.h>
29 #include <algorithm>
30 #include <gr_io_signature.h>
31 #include <stdexcept>
32 #include <gr_complex.h>
33
34
35 gr_sig_source_c::gr_sig_source_c (double sampling_freq, gr_waveform_t waveform,
36                 double frequency, double ampl, gr_complex offset)
37   : gr_sync_block ("sig_source_c",
38                    gr_make_io_signature (0, 0, 0),
39                    gr_make_io_signature (1, 1, sizeof (gr_complex))),
40     d_sampling_freq (sampling_freq), d_waveform (waveform), d_frequency (frequency),
41     d_ampl (ampl), d_offset (offset)
42 {
43   d_nco.set_freq (2 * M_PI * d_frequency / d_sampling_freq);
44 }
45
46 gr_sig_source_c_sptr
47 gr_make_sig_source_c (double sampling_freq, gr_waveform_t waveform,
48                      double frequency, double ampl, gr_complex offset)
49 {
50   return gr_sig_source_c_sptr (new gr_sig_source_c (sampling_freq, waveform, frequency, ampl, offset));
51 }
52
53 int
54 gr_sig_source_c::work (int noutput_items,
55                     gr_vector_const_void_star &input_items,
56                     gr_vector_void_star &output_items)
57 {
58   gr_complex *optr = (gr_complex *) output_items[0];
59   gr_complex t;
60
61   switch (d_waveform){
62
63 #if 1   // complex?
64
65   case GR_CONST_WAVE:
66     t = (gr_complex) d_ampl + d_offset;
67     for (int i = 0; i < noutput_items; i++)     // FIXME unroll
68       optr[i] = t;
69     break;
70     
71   case GR_SIN_WAVE:
72   case GR_COS_WAVE:
73     d_nco.sincos (optr, noutput_items, d_ampl);
74     if (d_offset == gr_complex(0,0))
75       break;
76
77     for (int i = 0; i < noutput_items; i++){
78       optr[i] += d_offset;
79     }
80     break;
81     
82   /* Implements a real square wave high from -PI to 0.  
83   * The imaginary square wave leads by 90 deg.
84   */
85   case GR_SQR_WAVE:     
86     for (int i = 0; i < noutput_items; i++){
87       if (d_nco.get_phase() < -1*M_PI/2)
88         optr[i] = gr_complex(d_ampl, 0)+d_offset;
89       else if (d_nco.get_phase() < 0)
90         optr[i] = gr_complex(d_ampl, d_ampl)+d_offset;
91       else if (d_nco.get_phase() < M_PI/2)
92         optr[i] = gr_complex(0, d_ampl)+d_offset;
93       else
94         optr[i] = d_offset;             
95       d_nco.step();
96     }
97     break;
98         
99   /* Implements a real triangle wave rising from -PI to 0 and  
100   * falling from 0 to PI. The imaginary triangle wave leads by 90 deg.
101   */
102   case GR_TRI_WAVE:     
103     for (int i = 0; i < noutput_items; i++){
104       if (d_nco.get_phase() < -1*M_PI/2){
105         optr[i] = gr_complex(d_ampl*d_nco.get_phase()/M_PI + d_ampl, 
106           -1*d_ampl*d_nco.get_phase()/M_PI - d_ampl/2)+d_offset;
107       }
108       else if (d_nco.get_phase() < 0){
109         optr[i] = gr_complex(d_ampl*d_nco.get_phase()/M_PI + d_ampl,
110           d_ampl*d_nco.get_phase()/M_PI + d_ampl/2)+d_offset;
111       }
112       else if (d_nco.get_phase() < M_PI/2){
113         optr[i] = gr_complex(-1*d_ampl*d_nco.get_phase()/M_PI + d_ampl, 
114           d_ampl*d_nco.get_phase()/M_PI + d_ampl/2)+d_offset;
115       }
116       else{
117         optr[i] = gr_complex(-1*d_ampl*d_nco.get_phase()/M_PI + d_ampl, 
118           -1*d_ampl*d_nco.get_phase()/M_PI + 3*d_ampl/2)+d_offset;
119       }
120       d_nco.step();
121     }
122     break;
123         
124   /* Implements a real saw tooth wave rising from -PI to PI. 
125   * The imaginary saw tooth wave leads by 90 deg.
126   */
127   case GR_SAW_WAVE:     
128     for (int i = 0; i < noutput_items; i++){
129       if (d_nco.get_phase() < -1*M_PI/2){
130         optr[i] = gr_complex(d_ampl*d_nco.get_phase()/(2*M_PI) + d_ampl/2, 
131           d_ampl*d_nco.get_phase()/(2*M_PI) + 5*d_ampl/4)+d_offset;
132       }
133       else{
134         optr[i] = gr_complex(d_ampl*d_nco.get_phase()/(2*M_PI) + d_ampl/2,
135           d_ampl*d_nco.get_phase()/(2*M_PI) + d_ampl/4)+d_offset;
136       }
137       d_nco.step();
138     }  
139     break;
140
141 #else                   // nope...
142
143   case GR_CONST_WAVE:
144     t = (gr_complex) d_ampl + d_offset;
145     for (int i = 0; i < noutput_items; i++)     // FIXME unroll
146       optr[i] = t;
147     break;
148     
149   case GR_SIN_WAVE:
150     d_nco.sin (optr, noutput_items, d_ampl);
151     if (d_offset == 0)
152       break;
153
154     for (int i = 0; i < noutput_items; i++){
155       optr[i] += d_offset;
156     }
157     break;
158
159   case GR_COS_WAVE:
160     d_nco.cos (optr, noutput_items, d_ampl);
161     if (d_offset == 0)
162       break;
163
164     for (int i = 0; i < noutput_items; i++){
165       optr[i] += d_offset;
166     }
167     break;
168    
169   /* The square wave is high from -PI to 0.     */ 
170   case GR_SQR_WAVE:             
171     t = (gr_complex) d_ampl + d_offset;
172     for (int i = 0; i < noutput_items; i++){
173       if (d_nco.get_phase() < 0)
174         optr[i] = t;
175       else
176         optr[i] = d_offset;
177       d_nco.step();
178     }
179     break;
180         
181   /* The triangle wave rises from -PI to 0 and falls from 0 to PI.      */ 
182   case GR_TRI_WAVE:     
183     for (int i = 0; i < noutput_items; i++){
184       double t = d_ampl*d_nco.get_phase()/M_PI;
185       if (d_nco.get_phase() < 0)
186         optr[i] = static_cast<gr_complex>(t + d_ampl + d_offset);
187       else
188         optr[i] = static_cast<gr_complex>(-1*t + d_ampl + d_offset);
189       d_nco.step();
190     }
191     break;
192         
193   /* The saw tooth wave rises from -PI to PI.   */
194   case GR_SAW_WAVE:     
195     for (int i = 0; i < noutput_items; i++){
196       t = static_cast<gr_complex>(d_ampl*d_nco.get_phase()/(2*M_PI) + d_ampl/2 + d_offset);
197       optr[i] = t;              
198       d_nco.step();
199     }
200     break;
201
202 #endif
203
204   default:
205     throw std::runtime_error ("gr_sig_source: invalid waveform");
206   }
207
208   return noutput_items;
209 }
210
211 void
212 gr_sig_source_c::set_sampling_freq (double sampling_freq)
213 {
214   d_sampling_freq = sampling_freq;
215   d_nco.set_freq (2 * M_PI * d_frequency / d_sampling_freq);
216 }
217
218 void
219 gr_sig_source_c::set_waveform (gr_waveform_t waveform)
220 {
221   d_waveform = waveform;
222 }
223
224 void
225 gr_sig_source_c::set_frequency (double frequency)
226 {
227   d_frequency = frequency;
228   d_nco.set_freq (2 * M_PI * d_frequency / d_sampling_freq);
229 }
230
231 void
232 gr_sig_source_c::set_amplitude (double ampl)
233 {
234   d_ampl = ampl;
235 }
236
237 void
238 gr_sig_source_c::set_offset (gr_complex offset)
239 {
240   d_offset = offset;
241 }
242