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 00035 //FIXME Eventually generalize this to fixed point 00036 00037 template<class o_type, class i_type> 00038 class gr_nco { 00039 public: 00040 gr_nco () : phase (0), phase_inc(0) {} 00041 00042 virtual ~gr_nco () {} 00043 00044 // radians 00045 void set_phase (float angle) { 00046 phase = angle; 00047 } 00048 00049 void adjust_phase (float delta_phase) { 00050 phase += delta_phase; 00051 } 00052 00053 00054 // angle_rate is in radians / step 00055 void set_freq (float angle_rate){ 00056 phase_inc = angle_rate; 00057 } 00058 00059 // angle_rate is a delta in radians / step 00060 void adjust_freq (float delta_angle_rate) 00061 { 00062 phase_inc += delta_angle_rate; 00063 } 00064 00065 // increment current phase angle 00066 00067 void step () 00068 { 00069 phase += phase_inc; 00070 if (fabs (phase) > M_PI){ 00071 00072 while (phase > M_PI) 00073 phase -= 2*M_PI; 00074 00075 while (phase < -M_PI) 00076 phase += 2*M_PI; 00077 } 00078 } 00079 00080 void step (int n) { phase += phase_inc * n; } 00081 00082 // units are radians / step 00083 float get_phase () const { return phase; } 00084 float get_freq () const { return phase_inc; } 00085 00086 // compute sin and cos for current phase angle 00087 void sincos (float *sinx, float *cosx) const; 00088 00089 // compute cos or sin for current phase angle 00090 float cos () const { return std::cos (phase); } 00091 float sin () const { return std::sin (phase); } 00092 00093 protected: 00094 float phase; 00095 float phase_inc; 00096 }; 00097 00098 template<class o_type, class i_type> 00099 void 00100 gr_nco<o_type,i_type>::sincos (float *sinx, float *cosx) const 00101 { 00102 gr_sincosf (phase, sinx, cosx); 00103 } 00104 00105 #endif /* _NCO_H_ */