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.
27 #include <gr_sincos.h>
29 #include <gr_complex.h>
32 * \brief base class template for Numerically Controlled Oscillator (NCO)
36 //FIXME Eventually generalize this to fixed point
38 template<class o_type, class i_type>
41 gr_nco () : phase (0), phase_inc(0) {}
46 void set_phase (double angle) {
50 void adjust_phase (double delta_phase) {
55 // angle_rate is in radians / step
56 void set_freq (double angle_rate){
57 phase_inc = angle_rate;
60 // angle_rate is a delta in radians / step
61 void adjust_freq (double delta_angle_rate)
63 phase_inc += delta_angle_rate;
66 // increment current phase angle
71 if (fabs (phase) > M_PI){
83 phase += phase_inc * n;
84 if (fabs (phase) > M_PI){
94 // units are radians / step
95 double get_phase () const { return phase; }
96 double get_freq () const { return phase_inc; }
98 // compute sin and cos for current phase angle
99 void sincos (float *sinx, float *cosx) const;
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); }
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);
119 template<class o_type, class i_type>
121 gr_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const
123 gr_sincosf (phase, sinx, cosx);
126 template<class o_type, class i_type>
128 gr_nco<o_type,i_type>::sin (float *output, int noutput_items, double ampl)
130 for (int i = 0; i < noutput_items; i++){
131 output[i] = (float)(sin () * ampl);
136 template<class o_type, class i_type>
138 gr_nco<o_type,i_type>::cos (float *output, int noutput_items, double ampl)
140 for (int i = 0; i < noutput_items; i++){
141 output[i] = (float)(cos () * ampl);
146 template<class o_type, class i_type>
148 gr_nco<o_type,i_type>::sin (short *output, int noutput_items, double ampl)
150 for (int i = 0; i < noutput_items; i++){
151 output[i] = (short)(sin() * ampl);
156 template<class o_type, class i_type>
158 gr_nco<o_type,i_type>::cos (short *output, int noutput_items, double ampl)
160 for (int i = 0; i < noutput_items; i++){
161 output[i] = (short)(cos () * ampl);
166 template<class o_type, class i_type>
168 gr_nco<o_type,i_type>::sin (int *output, int noutput_items, double ampl)
170 for (int i = 0; i < noutput_items; i++){
171 output[i] = (int)(sin () * ampl);
176 template<class o_type, class i_type>
178 gr_nco<o_type,i_type>::cos (int *output, int noutput_items, double ampl)
180 for (int i = 0; i < noutput_items; i++){
181 output[i] = (int)(cos () * ampl);
186 template<class o_type, class i_type>
188 gr_nco<o_type,i_type>::sincos (gr_complex *output, int noutput_items, double ampl)
190 for (int i = 0; i < noutput_items; i++){
192 sincos (&sinx, &cosx);
193 output[i] = gr_complex(cosx * ampl, sinx * ampl);