Imported Upstream version 3.2.2
[debian/gnuradio] / gnuradio-core / src / lib / general / gr_nco.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002 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 _GR_NCO_H_
23 #define _GR_NCO_H_
24
25
26 #include <vector>
27 #include <gr_sincos.h>
28 #include <cmath>
29 #include <gr_complex.h>
30
31 /*!
32  * \brief base class template for Numerically Controlled Oscillator (NCO)
33  * \ingroup misc
34  */
35
36
37 //FIXME  Eventually generalize this to fixed point
38
39 template<class o_type, class i_type> 
40 class gr_nco {
41 public:
42   gr_nco () : phase (0), phase_inc(0) {}
43
44   virtual ~gr_nco () {}
45
46   // radians
47   void set_phase (double angle) {
48     phase = angle;
49   }
50
51   void adjust_phase (double delta_phase) {
52     phase += delta_phase;
53   }
54
55
56   // angle_rate is in radians / step
57   void set_freq (double angle_rate){
58     phase_inc = angle_rate;
59   }
60
61   // angle_rate is a delta in radians / step
62   void adjust_freq (double delta_angle_rate)
63   {
64     phase_inc += delta_angle_rate;
65   }
66
67   // increment current phase angle
68
69   void step () 
70   { 
71     phase += phase_inc; 
72     if (fabs (phase) > M_PI){
73       
74       while (phase > M_PI)
75         phase -= 2*M_PI;
76
77       while (phase < -M_PI)
78         phase += 2*M_PI;
79     }
80   }
81
82   void step (int n)
83   {
84     phase += phase_inc * n;
85     if (fabs (phase) > M_PI){
86       
87       while (phase > M_PI)
88         phase -= 2*M_PI;
89
90       while (phase < -M_PI)
91         phase += 2*M_PI;
92     }
93   }
94
95   // units are radians / step
96   double get_phase () const { return phase; }
97   double get_freq () const { return phase_inc; }
98
99   // compute sin and cos for current phase angle
100   void sincos (float *sinx, float *cosx) const;
101
102   // compute cos or sin for current phase angle
103   float cos () const { return std::cos (phase); }
104   float sin () const { return std::sin (phase); }
105
106   // compute a block at a time
107   void sin (float *output, int noutput_items, double ampl = 1.0);
108   void cos (float *output, int noutput_items, double ampl = 1.0);
109   void sincos (gr_complex *output, int noutput_items, double ampl = 1.0);
110   void sin (short *output, int noutput_items, double ampl = 1.0);
111   void cos (short *output, int noutput_items, double ampl = 1.0);
112   void sin (int *output, int noutput_items, double ampl = 1.0);
113   void cos (int *output, int noutput_items, double ampl = 1.0);
114
115 protected:
116   double phase;
117   double phase_inc;
118 };
119
120 template<class o_type, class i_type> 
121 void
122 gr_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const
123 {
124   gr_sincosf (phase, sinx, cosx);
125 }
126
127 template<class o_type, class i_type> 
128 void
129 gr_nco<o_type,i_type>::sin (float *output, int noutput_items, double ampl)
130 {
131   for (int i = 0; i < noutput_items; i++){
132     output[i] = (float)(sin () * ampl);
133     step ();
134   }
135 }
136
137 template<class o_type, class i_type> 
138 void
139 gr_nco<o_type,i_type>::cos (float *output, int noutput_items, double ampl)
140 {
141   for (int i = 0; i < noutput_items; i++){
142     output[i] = (float)(cos () * ampl);
143     step ();
144   }
145 }
146
147 template<class o_type, class i_type> 
148 void
149 gr_nco<o_type,i_type>::sin (short *output, int noutput_items, double ampl)
150 {
151   for (int i = 0; i < noutput_items; i++){
152     output[i] = (short)(sin() * ampl);
153     step ();
154   }
155 }
156
157 template<class o_type, class i_type> 
158 void
159 gr_nco<o_type,i_type>::cos (short *output, int noutput_items, double ampl)
160 {
161   for (int i = 0; i < noutput_items; i++){
162     output[i] = (short)(cos () * ampl);
163     step ();
164   }
165 }
166
167 template<class o_type, class i_type> 
168 void
169 gr_nco<o_type,i_type>::sin (int *output, int noutput_items, double ampl)
170 {
171   for (int i = 0; i < noutput_items; i++){
172     output[i] = (int)(sin () * ampl);
173     step ();
174   }
175 }
176
177 template<class o_type, class i_type> 
178 void
179 gr_nco<o_type,i_type>::cos (int *output, int noutput_items, double ampl)
180 {
181   for (int i = 0; i < noutput_items; i++){
182     output[i] = (int)(cos () * ampl);
183     step ();
184   }
185 }
186
187 template<class o_type, class i_type> 
188 void
189 gr_nco<o_type,i_type>::sincos (gr_complex *output, int noutput_items, double ampl)
190 {
191   for (int i = 0; i < noutput_items; i++){
192     float cosx, sinx;
193     sincos (&sinx, &cosx);
194     output[i] = gr_complex(cosx * ampl, sinx * ampl);
195     step ();
196   }
197 }
198 #endif /* _NCO_H_ */