00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
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
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
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
00056 void set_freq (double angle_rate){
00057 phase_inc = angle_rate;
00058 }
00059
00060
00061 void adjust_freq (double delta_angle_rate)
00062 {
00063 phase_inc += delta_angle_rate;
00064 }
00065
00066
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
00095 double get_phase () const { return phase; }
00096 double get_freq () const { return phase_inc; }
00097
00098
00099 void sincos (float *sinx, float *cosx) const;
00100
00101
00102 float cos () const { return std::cos (phase); }
00103 float sin () const { return std::sin (phase); }
00104
00105
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