GNU Radio 3.7.1 C++ API
|
00001 /* -*- c++ -*- */ 00002 /* 00003 * Copyright 2002,2004,2013 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 3, 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., 51 Franklin Street, 00020 * Boston, MA 02110-1301, USA. 00021 */ 00022 00023 #ifndef INCLUDED_GR_FXPT_NCO_H 00024 #define INCLUDED_GR_FXPT_NCO_H 00025 00026 #include <gnuradio/api.h> 00027 #include <gnuradio/fxpt.h> 00028 #include <gnuradio/gr_complex.h> 00029 #include <stdint.h> 00030 00031 namespace gr { 00032 00033 /*! 00034 * \brief Numerically Controlled Oscillator (NCO) 00035 * \ingroup misc 00036 */ 00037 class /*GR_RUNTIME_API*/ fxpt_nco 00038 { 00039 uint32_t d_phase; 00040 int32_t d_phase_inc; 00041 00042 public: 00043 fxpt_nco() : d_phase(0), d_phase_inc(0) {} 00044 00045 ~fxpt_nco() {} 00046 00047 // radians 00048 void set_phase(float angle) { 00049 d_phase = gr::fxpt::float_to_fixed(angle); 00050 } 00051 00052 void adjust_phase(float delta_phase) { 00053 d_phase += gr::fxpt::float_to_fixed(delta_phase); 00054 } 00055 00056 // angle_rate is in radians / step 00057 void set_freq(float angle_rate){ 00058 d_phase_inc = gr::fxpt::float_to_fixed(angle_rate); 00059 } 00060 00061 // angle_rate is a delta in radians / step 00062 void adjust_freq(float delta_angle_rate) 00063 { 00064 d_phase_inc += gr::fxpt::float_to_fixed(delta_angle_rate); 00065 } 00066 00067 // increment current phase angle 00068 00069 void step() 00070 { 00071 d_phase += d_phase_inc; 00072 } 00073 00074 void step(int n) 00075 { 00076 d_phase += d_phase_inc * n; 00077 } 00078 00079 // units are radians / step 00080 float get_phase() const { return gr::fxpt::fixed_to_float(d_phase); } 00081 float get_freq() const { return gr::fxpt::fixed_to_float(d_phase_inc); } 00082 00083 // compute sin and cos for current phase angle 00084 void sincos(float *sinx, float *cosx) const 00085 { 00086 *sinx = gr::fxpt::sin(d_phase); 00087 *cosx = gr::fxpt::cos(d_phase); 00088 } 00089 00090 // compute cos and sin for a block of phase angles 00091 void sincos(gr_complex *output, int noutput_items, double ampl=1.0) 00092 { 00093 for(int i = 0; i < noutput_items; i++) { 00094 output[i] = gr_complex(gr::fxpt::cos(d_phase) * ampl, gr::fxpt::sin(d_phase) * ampl); 00095 step(); 00096 } 00097 } 00098 00099 // compute sin for a block of phase angles 00100 void sin(float *output, int noutput_items, double ampl=1.0) 00101 { 00102 for(int i = 0; i < noutput_items; i++) { 00103 output[i] = (float)(gr::fxpt::sin(d_phase) * ampl); 00104 step(); 00105 } 00106 } 00107 00108 // compute cos for a block of phase angles 00109 void cos(float *output, int noutput_items, double ampl=1.0) 00110 { 00111 for(int i = 0; i < noutput_items; i++) { 00112 output[i] = (float)(gr::fxpt::cos(d_phase) * ampl); 00113 step (); 00114 } 00115 } 00116 00117 // compute sin for a block of phase angles 00118 void sin(short *output, int noutput_items, double ampl=1.0) 00119 { 00120 for(int i = 0; i < noutput_items; i++) { 00121 output[i] = (short)(gr::fxpt::sin(d_phase) * ampl); 00122 step(); 00123 } 00124 } 00125 00126 // compute cos for a block of phase angles 00127 void cos(short *output, int noutput_items, double ampl=1.0) 00128 { 00129 for(int i = 0; i < noutput_items; i++) { 00130 output[i] = (short)(gr::fxpt::cos(d_phase) * ampl); 00131 step(); 00132 } 00133 } 00134 00135 // compute sin for a block of phase angles 00136 void sin(int *output, int noutput_items, double ampl=1.0) 00137 { 00138 for(int i = 0; i < noutput_items; i++) { 00139 output[i] = (int)(gr::fxpt::sin(d_phase) * ampl); 00140 step(); 00141 } 00142 } 00143 00144 // compute cos for a block of phase angles 00145 void cos(int *output, int noutput_items, double ampl=1.0) 00146 { 00147 for(int i = 0; i < noutput_items; i++) { 00148 output[i] = (int)(gr::fxpt::cos(d_phase) * ampl); 00149 step(); 00150 } 00151 } 00152 00153 // compute cos or sin for current phase angle 00154 float cos() const { return gr::fxpt::cos(d_phase); } 00155 float sin() const { return gr::fxpt::sin(d_phase); } 00156 }; 00157 00158 } /* namespace gr */ 00159 00160 #endif /* INCLUDED_GR_FXPT_NCO_H */