Imported Upstream version 3.0.4
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_sig_source_X.cc.t
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@
24
25 #ifdef HAVE_CONFIG_H
26 #include <config.h>
27 #endif
28 #include <@NAME@.h>
29 #include <algorithm>
30 #include <gr_io_signature.h>
31 #include <stdexcept>
32 #include <gr_complex.h>
33
34
35 @NAME@::@NAME@ (double sampling_freq, gr_waveform_t waveform,
36                 double frequency, double ampl, @TYPE@ offset)
37   : gr_sync_block ("@BASE_NAME@",
38                    gr_make_io_signature (0, 0, 0),
39                    gr_make_io_signature (1, 1, sizeof (@TYPE@))),
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 @NAME@_sptr
47 gr_make_@BASE_NAME@ (double sampling_freq, gr_waveform_t waveform,
48                      double frequency, double ampl, @TYPE@ offset)
49 {
50   return @NAME@_sptr (new @NAME@ (sampling_freq, waveform, frequency, ampl, offset));
51 }
52
53 int
54 @NAME@::work (int noutput_items,
55                     gr_vector_const_void_star &input_items,
56                     gr_vector_void_star &output_items)
57 {
58   @TYPE@ *optr = (@TYPE@ *) output_items[0];
59   @TYPE@ t;
60
61   switch (d_waveform){
62
63 #if @IS_COMPLEX@        // 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 #else                   // nope...
83
84   case GR_CONST_WAVE:
85     t = (@TYPE@) d_ampl + d_offset;
86     for (int i = 0; i < noutput_items; i++)     // FIXME unroll
87       optr[i] = t;
88     break;
89     
90   case GR_SIN_WAVE:
91     d_nco.sin (optr, noutput_items, d_ampl);
92     if (d_offset == 0)
93       break;
94
95     for (int i = 0; i < noutput_items; i++){
96       optr[i] += d_offset;
97     }
98     break;
99
100   case GR_COS_WAVE:
101     d_nco.cos (optr, noutput_items, d_ampl);
102     if (d_offset == 0)
103       break;
104
105     for (int i = 0; i < noutput_items; i++){
106       optr[i] += d_offset;
107     }
108     break;
109 #endif
110
111   default:
112     throw std::runtime_error ("gr_sig_source: invalid waveform");
113   }
114
115   return noutput_items;
116 }
117
118 void
119 @NAME@::set_sampling_freq (double sampling_freq)
120 {
121   d_sampling_freq = sampling_freq;
122   d_nco.set_freq (2 * M_PI * d_frequency / d_sampling_freq);
123 }
124
125 void
126 @NAME@::set_waveform (gr_waveform_t waveform)
127 {
128   d_waveform = waveform;
129 }
130
131 void
132 @NAME@::set_frequency (double frequency)
133 {
134   d_frequency = frequency;
135   d_nco.set_freq (2 * M_PI * d_frequency / d_sampling_freq);
136 }
137
138 void
139 @NAME@::set_amplitude (double ampl)
140 {
141   d_ampl = ampl;
142 }
143
144 void
145 @NAME@::set_offset (@TYPE@ offset)
146 {
147   d_offset = offset;
148 }
149