Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_fxpt_nco.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,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 #ifndef INCLUDED_GR_FXPT_NCO_H
23 #define INCLUDED_GR_FXPT_NCO_H
24
25 #include <gr_fxpt.h>
26 #include <gr_complex.h>
27
28 /*!
29  * \brief Numerically Controlled Oscillator (NCO)
30  * \ingroup misc
31  */
32 class gr_fxpt_nco {
33   gr_uint32     d_phase;
34   gr_int32      d_phase_inc;
35
36 public:
37   gr_fxpt_nco () : d_phase (0), d_phase_inc (0) {}
38
39   ~gr_fxpt_nco () {}
40
41   // radians
42   void set_phase (float angle) {
43     d_phase = gr_fxpt::float_to_fixed (angle);
44   }
45
46   void adjust_phase (float delta_phase) {
47     d_phase += gr_fxpt::float_to_fixed (delta_phase);
48   }
49
50   // angle_rate is in radians / step
51   void set_freq (float angle_rate){
52     d_phase_inc = gr_fxpt::float_to_fixed (angle_rate);
53   }
54
55   // angle_rate is a delta in radians / step
56   void adjust_freq (float delta_angle_rate)
57   {
58     d_phase_inc += gr_fxpt::float_to_fixed (delta_angle_rate);
59   }
60
61   // increment current phase angle
62
63   void step () 
64   { 
65     d_phase += d_phase_inc;
66   }
67
68   void step (int n)
69   {
70     d_phase += d_phase_inc * n;
71   }
72
73   // units are radians / step
74   float get_phase () const { return gr_fxpt::fixed_to_float (d_phase); }
75   float get_freq () const { return gr_fxpt::fixed_to_float (d_phase_inc); }
76
77   // compute sin and cos for current phase angle
78   void sincos (float *sinx, float *cosx) const
79   {
80     *sinx = gr_fxpt::sin (d_phase);
81     *cosx = gr_fxpt::cos (d_phase);
82   }
83
84   // compute cos and sin for a block of phase angles
85   void sincos (gr_complex *output, int noutput_items, double ampl=1.0)
86   {
87     for (int i = 0; i < noutput_items; i++){
88       output[i]   = gr_complex(gr_fxpt::cos (d_phase) * ampl, gr_fxpt::sin (d_phase) * ampl);
89       step ();
90     }
91   }
92
93   // compute sin for a block of phase angles
94   void sin (float *output, int noutput_items, double ampl=1.0)
95   {
96     for (int i = 0; i < noutput_items; i++){
97       output[i] = (float)(gr_fxpt::sin (d_phase) * ampl);
98       step ();
99     }
100   }
101
102   // compute cos for a block of phase angles
103   void cos (float *output, int noutput_items, double ampl=1.0)
104   {
105     for (int i = 0; i < noutput_items; i++){
106       output[i] = (float)(gr_fxpt::cos (d_phase) * ampl);
107       step ();
108     }
109   }
110
111   // compute sin for a block of phase angles
112   void sin (short *output, int noutput_items, double ampl=1.0)
113   {
114     for (int i = 0; i < noutput_items; i++){
115       output[i] = (short)(gr_fxpt::sin (d_phase) * ampl);
116       step ();
117     }
118   }
119
120   // compute cos for a block of phase angles
121   void cos (short *output, int noutput_items, double ampl=1.0)
122   {
123     for (int i = 0; i < noutput_items; i++){
124       output[i] = (short)(gr_fxpt::cos (d_phase) * ampl);
125       step ();
126     }
127   }
128
129   // compute sin for a block of phase angles
130   void sin (int *output, int noutput_items, double ampl=1.0)
131   {
132     for (int i = 0; i < noutput_items; i++){
133       output[i] = (int)(gr_fxpt::sin (d_phase) * ampl);
134       step ();
135     }
136   }
137
138   // compute cos for a block of phase angles
139   void cos (int *output, int noutput_items, double ampl=1.0)
140   {
141     for (int i = 0; i < noutput_items; i++){
142       output[i] = (int)(gr_fxpt::cos (d_phase) * ampl);
143       step ();
144     }
145   }
146
147   // compute cos or sin for current phase angle
148   float cos () const { return gr_fxpt::cos (d_phase); }
149   float sin () const { return gr_fxpt::sin (d_phase); }
150 };
151
152 #endif /* INCLUDED_GR_FXPT_NCO_H */