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