Main Page | Modules | Namespace List | Class Hierarchy | Class List | File List | Class Members | File Members

gr_nco.h

Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2002 Free Software Foundation, Inc.
00004  * 
00005  * This file is part of GNU Radio
00006  * 
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2, or (at your option)
00010  * any later version.
00011  * 
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00020  * Boston, MA 02111-1307, USA.
00021  */
00022 #ifndef _GR_NCO_H_
00023 #define _GR_NCO_H_
00024 
00025 
00026 #include <vector>
00027 #include <gr_sincos.h>
00028 #include <cmath>
00029 #include <gr_complex.h>
00030 
00036 //FIXME  Eventually generalize this to fixed point
00037 
00038 template<class o_type, class i_type> 
00039 class gr_nco {
00040 public:
00041   gr_nco () : phase (0), phase_inc(0) {}
00042 
00043   virtual ~gr_nco () {}
00044 
00045   // radians
00046   void set_phase (double angle) {
00047     phase = angle;
00048   }
00049 
00050   void adjust_phase (double delta_phase) {
00051     phase += delta_phase;
00052   }
00053 
00054 
00055   // angle_rate is in radians / step
00056   void set_freq (double angle_rate){
00057     phase_inc = angle_rate;
00058   }
00059 
00060   // angle_rate is a delta in radians / step
00061   void adjust_freq (double delta_angle_rate)
00062   {
00063     phase_inc += delta_angle_rate;
00064   }
00065 
00066   // increment current phase angle
00067 
00068   void step () 
00069   { 
00070     phase += phase_inc; 
00071     if (fabs (phase) > M_PI){
00072       
00073       while (phase > M_PI)
00074         phase -= 2*M_PI;
00075 
00076       while (phase < -M_PI)
00077         phase += 2*M_PI;
00078     }
00079   }
00080 
00081   void step (int n)
00082   {
00083     phase += phase_inc * n;
00084     if (fabs (phase) > M_PI){
00085       
00086       while (phase > M_PI)
00087         phase -= 2*M_PI;
00088 
00089       while (phase < -M_PI)
00090         phase += 2*M_PI;
00091     }
00092   }
00093 
00094   // units are radians / step
00095   double get_phase () const { return phase; }
00096   double get_freq () const { return phase_inc; }
00097 
00098   // compute sin and cos for current phase angle
00099   void sincos (float *sinx, float *cosx) const;
00100 
00101   // compute cos or sin for current phase angle
00102   float cos () const { return std::cos (phase); }
00103   float sin () const { return std::sin (phase); }
00104 
00105   // compute a block at a time
00106   void sin (float *output, int noutput_items, double ampl = 1.0);
00107   void cos (float *output, int noutput_items, double ampl = 1.0);
00108   void sincos (gr_complex *output, int noutput_items, double ampl = 1.0);
00109   void sin (short *output, int noutput_items, double ampl = 1.0);
00110   void cos (short *output, int noutput_items, double ampl = 1.0);
00111   void sin (int *output, int noutput_items, double ampl = 1.0);
00112   void cos (int *output, int noutput_items, double ampl = 1.0);
00113 
00114 protected:
00115   double phase;
00116   double phase_inc;
00117 };
00118 
00119 template<class o_type, class i_type> 
00120 void
00121 gr_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const
00122 {
00123   gr_sincosf (phase, sinx, cosx);
00124 }
00125 
00126 template<class o_type, class i_type> 
00127 void
00128 gr_nco<o_type,i_type>::sin (float *output, int noutput_items, double ampl)
00129 {
00130   for (int i = 0; i < noutput_items; i++){
00131     output[i] = (float)(sin () * ampl);
00132     step ();
00133   }
00134 }
00135 
00136 template<class o_type, class i_type> 
00137 void
00138 gr_nco<o_type,i_type>::cos (float *output, int noutput_items, double ampl)
00139 {
00140   for (int i = 0; i < noutput_items; i++){
00141     output[i] = (float)(cos () * ampl);
00142     step ();
00143   }
00144 }
00145 
00146 template<class o_type, class i_type> 
00147 void
00148 gr_nco<o_type,i_type>::sin (short *output, int noutput_items, double ampl)
00149 {
00150   for (int i = 0; i < noutput_items; i++){
00151     output[i] = (short)(sin() * ampl);
00152     step ();
00153   }
00154 }
00155 
00156 template<class o_type, class i_type> 
00157 void
00158 gr_nco<o_type,i_type>::cos (short *output, int noutput_items, double ampl)
00159 {
00160   for (int i = 0; i < noutput_items; i++){
00161     output[i] = (short)(cos () * ampl);
00162     step ();
00163   }
00164 }
00165 
00166 template<class o_type, class i_type> 
00167 void
00168 gr_nco<o_type,i_type>::sin (int *output, int noutput_items, double ampl)
00169 {
00170   for (int i = 0; i < noutput_items; i++){
00171     output[i] = (int)(sin () * ampl);
00172     step ();
00173   }
00174 }
00175 
00176 template<class o_type, class i_type> 
00177 void
00178 gr_nco<o_type,i_type>::cos (int *output, int noutput_items, double ampl)
00179 {
00180   for (int i = 0; i < noutput_items; i++){
00181     output[i] = (int)(cos () * ampl);
00182     step ();
00183   }
00184 }
00185 
00186 template<class o_type, class i_type> 
00187 void
00188 gr_nco<o_type,i_type>::sincos (gr_complex *output, int noutput_items, double ampl)
00189 {
00190   for (int i = 0; i < noutput_items; i++){
00191     float cosx, sinx;
00192     sincos (&sinx, &cosx);
00193     output[i] = gr_complex(cosx * ampl, sinx * ampl);
00194     step ();
00195   }
00196 }
00197 #endif /* _NCO_H_ */

Generated on Sat Jul 8 17:04:51 2006 for GNU Radio 2.x by  doxygen 1.4.1