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