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)
37 //FIXME Eventually generalize this to fixed point
39 template<class o_type, class i_type>
42 gr_nco () : phase (0), phase_inc(0) {}
47 void set_phase (double angle) {
51 void adjust_phase (double delta_phase) {
56 // angle_rate is in radians / step
57 void set_freq (double angle_rate){
58 phase_inc = angle_rate;
61 // angle_rate is a delta in radians / step
62 void adjust_freq (double delta_angle_rate)
64 phase_inc += delta_angle_rate;
67 // increment current phase angle
72 if (fabs (phase) > M_PI){
84 phase += phase_inc * n;
85 if (fabs (phase) > M_PI){
95 // units are radians / step
96 double get_phase () const { return phase; }
97 double get_freq () const { return phase_inc; }
99 // compute sin and cos for current phase angle
100 void sincos (float *sinx, float *cosx) const;
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); }
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);
120 template<class o_type, class i_type>
122 gr_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const
124 gr_sincosf (phase, sinx, cosx);
127 template<class o_type, class i_type>
129 gr_nco<o_type,i_type>::sin (float *output, int noutput_items, double ampl)
131 for (int i = 0; i < noutput_items; i++){
132 output[i] = (float)(sin () * ampl);
137 template<class o_type, class i_type>
139 gr_nco<o_type,i_type>::cos (float *output, int noutput_items, double ampl)
141 for (int i = 0; i < noutput_items; i++){
142 output[i] = (float)(cos () * ampl);
147 template<class o_type, class i_type>
149 gr_nco<o_type,i_type>::sin (short *output, int noutput_items, double ampl)
151 for (int i = 0; i < noutput_items; i++){
152 output[i] = (short)(sin() * ampl);
157 template<class o_type, class i_type>
159 gr_nco<o_type,i_type>::cos (short *output, int noutput_items, double ampl)
161 for (int i = 0; i < noutput_items; i++){
162 output[i] = (short)(cos () * ampl);
167 template<class o_type, class i_type>
169 gr_nco<o_type,i_type>::sin (int *output, int noutput_items, double ampl)
171 for (int i = 0; i < noutput_items; i++){
172 output[i] = (int)(sin () * ampl);
177 template<class o_type, class i_type>
179 gr_nco<o_type,i_type>::cos (int *output, int noutput_items, double ampl)
181 for (int i = 0; i < noutput_items; i++){
182 output[i] = (int)(cos () * ampl);
187 template<class o_type, class i_type>
189 gr_nco<o_type,i_type>::sincos (gr_complex *output, int noutput_items, double ampl)
191 for (int i = 0; i < noutput_items; i++){
193 sincos (&sinx, &cosx);
194 output[i] = gr_complex(cosx * ampl, sinx * ampl);