GNU Radio Manual and C++ API Reference  3.7.9.2
The Free & Open Software Radio Ecosystem
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fxpt_nco.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2002,2004,2013 Free Software Foundation, Inc.
4  *
5  * This file is part of GNU Radio
6  *
7  * GNU Radio is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3, or (at your option)
10  * any later version.
11  *
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING. If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22 
23 #ifndef INCLUDED_GR_FXPT_NCO_H
24 #define INCLUDED_GR_FXPT_NCO_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/fxpt.h>
28 #include <gnuradio/gr_complex.h>
29 #include <stdint.h>
30 
31 namespace gr {
32 
33  /*!
34  * \brief Numerically Controlled Oscillator (NCO)
35  * \ingroup misc
36  */
37  class /*GR_RUNTIME_API*/ fxpt_nco
38  {
39  uint32_t d_phase;
40  int32_t d_phase_inc;
41 
42  public:
43  fxpt_nco() : d_phase(0), d_phase_inc(0) {}
44 
45  ~fxpt_nco() {}
46 
47  // radians
48  void set_phase(float angle) {
49  d_phase = gr::fxpt::float_to_fixed(angle);
50  }
51 
52  void adjust_phase(float delta_phase) {
53  d_phase += gr::fxpt::float_to_fixed(delta_phase);
54  }
55 
56  // angle_rate is in radians / step
57  void set_freq(float angle_rate){
58  d_phase_inc = gr::fxpt::float_to_fixed(angle_rate);
59  }
60 
61  // angle_rate is a delta in radians / step
62  void adjust_freq(float delta_angle_rate)
63  {
64  d_phase_inc += gr::fxpt::float_to_fixed(delta_angle_rate);
65  }
66 
67  // increment current phase angle
68 
69  void step()
70  {
71  d_phase += d_phase_inc;
72  }
73 
74  void step(int n)
75  {
76  d_phase += d_phase_inc * n;
77  }
78 
79  // units are radians / step
80  float get_phase() const { return gr::fxpt::fixed_to_float(d_phase); }
81  float get_freq() const { return gr::fxpt::fixed_to_float(d_phase_inc); }
82 
83  // compute sin and cos for current phase angle
84  void sincos(float *sinx, float *cosx) const
85  {
86  *sinx = gr::fxpt::sin(d_phase);
87  *cosx = gr::fxpt::cos(d_phase);
88  }
89 
90  // compute cos and sin for a block of phase angles
91  void sincos(gr_complex *output, int noutput_items, double ampl=1.0)
92  {
93  for(int i = 0; i < noutput_items; i++) {
94  output[i] = gr_complex(gr::fxpt::cos(d_phase) * ampl, gr::fxpt::sin(d_phase) * ampl);
95  step();
96  }
97  }
98 
99  // compute sin for a block of phase angles
100  void sin(float *output, int noutput_items, double ampl=1.0)
101  {
102  for(int i = 0; i < noutput_items; i++) {
103  output[i] = (float)(gr::fxpt::sin(d_phase) * ampl);
104  step();
105  }
106  }
107 
108  // compute cos for a block of phase angles
109  void cos(float *output, int noutput_items, double ampl=1.0)
110  {
111  for(int i = 0; i < noutput_items; i++) {
112  output[i] = (float)(gr::fxpt::cos(d_phase) * ampl);
113  step ();
114  }
115  }
116 
117  // compute sin for a block of phase angles
118  void sin(short *output, int noutput_items, double ampl=1.0)
119  {
120  for(int i = 0; i < noutput_items; i++) {
121  output[i] = (short)(gr::fxpt::sin(d_phase) * ampl);
122  step();
123  }
124  }
125 
126  // compute cos for a block of phase angles
127  void cos(short *output, int noutput_items, double ampl=1.0)
128  {
129  for(int i = 0; i < noutput_items; i++) {
130  output[i] = (short)(gr::fxpt::cos(d_phase) * ampl);
131  step();
132  }
133  }
134 
135  // compute sin for a block of phase angles
136  void sin(int *output, int noutput_items, double ampl=1.0)
137  {
138  for(int i = 0; i < noutput_items; i++) {
139  output[i] = (int)(gr::fxpt::sin(d_phase) * ampl);
140  step();
141  }
142  }
143 
144  // compute cos for a block of phase angles
145  void cos(int *output, int noutput_items, double ampl=1.0)
146  {
147  for(int i = 0; i < noutput_items; i++) {
148  output[i] = (int)(gr::fxpt::cos(d_phase) * ampl);
149  step();
150  }
151  }
152 
153  // compute cos or sin for current phase angle
154  float cos() const { return gr::fxpt::cos(d_phase); }
155  float sin() const { return gr::fxpt::sin(d_phase); }
156  };
157 
158 } /* namespace gr */
159 
160 #endif /* INCLUDED_GR_FXPT_NCO_H */
void sincos(float *sinx, float *cosx) const
Definition: fxpt_nco.h:84
void sincos(gr_complex *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:91
~fxpt_nco()
Definition: fxpt_nco.h:45
Numerically Controlled Oscillator (NCO)
Definition: fxpt_nco.h:37
void sin(short *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:118
void step()
Definition: fxpt_nco.h:69
std::complex< float > gr_complex
Definition: gr_complex.h:27
void sin(float *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:100
static float cos(int32_t x)
Definition: fxpt.h:82
Include this header to use the message passing features.
Definition: logger.h:131
static float sin(int32_t x)
Given a fixed point angle x, return float sine (x)
Definition: fxpt.h:71
static float fixed_to_float(int32_t x)
Definition: fxpt.h:62
float get_freq() const
Definition: fxpt_nco.h:81
void set_phase(float angle)
Definition: fxpt_nco.h:48
static int32_t float_to_fixed(float x)
Definition: fxpt.h:52
void step(int n)
Definition: fxpt_nco.h:74
void set_freq(float angle_rate)
Definition: fxpt_nco.h:57
float cos() const
Definition: fxpt_nco.h:154
void sin(int *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:136
void adjust_phase(float delta_phase)
Definition: fxpt_nco.h:52
float get_phase() const
Definition: fxpt_nco.h:80
void cos(int *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:145
void adjust_freq(float delta_angle_rate)
Definition: fxpt_nco.h:62
float sin() const
Definition: fxpt_nco.h:155
fxpt_nco()
Definition: fxpt_nco.h:43
void cos(float *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:109
void cos(short *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:127