3 * Copyright 2002 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
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)
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.
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.
22 #ifndef INCLUDED_UI_NCO_H
23 #define INCLUDED_UI_NCO_H
27 #include <ui_sincos.h>
31 typedef std::complex<float> gr_complex;
35 * \brief base class template for Numerically Controlled Oscillator (NCO)
39 //FIXME Eventually generalize this to fixed point
41 template<class o_type, class i_type>
44 ui_nco () : phase (0), phase_inc(0) {}
49 void set_phase (double angle) {
53 void adjust_phase (double delta_phase) {
58 // angle_rate is in radians / step
59 void set_freq (double angle_rate){
60 phase_inc = angle_rate;
63 // angle_rate is a delta in radians / step
64 void adjust_freq (double delta_angle_rate)
66 phase_inc += delta_angle_rate;
69 // increment current phase angle
74 if (fabs (phase) > M_PI){
86 phase += phase_inc * n;
87 if (fabs (phase) > M_PI){
97 // units are radians / step
98 double get_phase () const { return phase; }
99 double get_freq () const { return phase_inc; }
101 // compute sin and cos for current phase angle
102 void sincos (float *sinx, float *cosx) const;
104 // compute cos or sin for current phase angle
105 float cos () const { return std::cos (phase); }
106 float sin () const { return std::sin (phase); }
108 // compute a block at a time
109 void sin (float *output, int noutput_items, double ampl = 1.0);
110 void cos (float *output, int noutput_items, double ampl = 1.0);
111 void sincos (gr_complex *output, int noutput_items, double ampl = 1.0);
112 void sin (short *output, int noutput_items, double ampl = 1.0);
113 void cos (short *output, int noutput_items, double ampl = 1.0);
114 void sin (int *output, int noutput_items, double ampl = 1.0);
115 void cos (int *output, int noutput_items, double ampl = 1.0);
122 template<class o_type, class i_type>
124 ui_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const
126 ui_sincosf (phase, sinx, cosx);
129 template<class o_type, class i_type>
131 ui_nco<o_type,i_type>::sin (float *output, int noutput_items, double ampl)
133 for (int i = 0; i < noutput_items; i++){
134 output[i] = (float)(sin () * ampl);
139 template<class o_type, class i_type>
141 ui_nco<o_type,i_type>::cos (float *output, int noutput_items, double ampl)
143 for (int i = 0; i < noutput_items; i++){
144 output[i] = (float)(cos () * ampl);
149 template<class o_type, class i_type>
151 ui_nco<o_type,i_type>::sin (short *output, int noutput_items, double ampl)
153 for (int i = 0; i < noutput_items; i++){
154 output[i] = (short)(sin() * ampl);
159 template<class o_type, class i_type>
161 ui_nco<o_type,i_type>::cos (short *output, int noutput_items, double ampl)
163 for (int i = 0; i < noutput_items; i++){
164 output[i] = (short)(cos () * ampl);
169 template<class o_type, class i_type>
171 ui_nco<o_type,i_type>::sin (int *output, int noutput_items, double ampl)
173 for (int i = 0; i < noutput_items; i++){
174 output[i] = (int)(sin () * ampl);
179 template<class o_type, class i_type>
181 ui_nco<o_type,i_type>::cos (int *output, int noutput_items, double ampl)
183 for (int i = 0; i < noutput_items; i++){
184 output[i] = (int)(cos () * ampl);
189 template<class o_type, class i_type>
191 ui_nco<o_type,i_type>::sincos (gr_complex *output, int noutput_items, double ampl)
193 for (int i = 0; i < noutput_items; i++){
195 sincos (&sinx, &cosx);
196 output[i] = gr_complex(cosx * ampl, sinx * ampl);
201 #endif /* INCLUDED_UI_NCO_H */