Imported Upstream version 3.0
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_frequency_modulator_fc.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 2, 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_frequency_modulator_fc.h>
28 #include <gr_io_signature.h>
29 #include <gr_sincos.h>
30 #include <math.h>
31
32
33 gr_frequency_modulator_fc_sptr gr_make_frequency_modulator_fc (double sensitivity)
34 {
35   return gr_frequency_modulator_fc_sptr (new gr_frequency_modulator_fc (sensitivity));
36 }
37
38 gr_frequency_modulator_fc::gr_frequency_modulator_fc (double sensitivity)
39   : gr_sync_block ("frequency_modulator_fc",
40                    gr_make_io_signature (1, 1, sizeof (float)),
41                    gr_make_io_signature (1, 1, sizeof (gr_complex))),
42     d_sensitivity (sensitivity), d_phase (0)
43 {
44 }
45
46 int
47 gr_frequency_modulator_fc::work (int noutput_items,
48                                  gr_vector_const_void_star &input_items,
49                                  gr_vector_void_star &output_items)
50 {
51   const float *in = (const float *) input_items[0];
52   gr_complex *out = (gr_complex *) output_items[0];
53
54   for (int i = 0; i < noutput_items; i++){
55     d_phase = d_phase + d_sensitivity * in[i];
56     float oi, oq;
57     gr_sincosf (d_phase, &oq, &oi);
58     out[i] = gr_complex (oi, oq);
59   }
60
61   // Limit the phase accumulator to [-16*pi,16*pi]
62   // to avoid loss of precision in the addition above.
63
64   if (fabs (d_phase) > 16 * M_PI){
65     double ii = trunc (d_phase / (2 * M_PI));
66     d_phase = d_phase - (ii * 2 * M_PI);
67   }
68
69   return noutput_items;
70 }