GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
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  * SPDX-License-Identifier: GPL-3.0-or-later
8  *
9  */
10 
11 #ifndef INCLUDED_GR_FXPT_NCO_H
12 #define INCLUDED_GR_FXPT_NCO_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/fxpt.h>
16 #include <gnuradio/gr_complex.h>
17 #include <cstdint>
18 
19 namespace gr {
20 
21 /*!
22  * \brief Numerically Controlled Oscillator (NCO)
23  * \ingroup misc
24  */
25 class /*GR_RUNTIME_API*/ fxpt_nco
26 {
27  uint32_t d_phase;
28  int32_t d_phase_inc;
29 
30 public:
31  fxpt_nco() : d_phase(0), d_phase_inc(0) {}
32 
33  ~fxpt_nco() {}
34 
35  // radians
36  void set_phase(float angle) { d_phase = gr::fxpt::float_to_fixed(angle); }
37 
38  void adjust_phase(float delta_phase)
39  {
40  d_phase += gr::fxpt::float_to_fixed(delta_phase);
41  }
42 
43  // angle_rate is in radians / step
44  void set_freq(float angle_rate)
45  {
46  d_phase_inc = gr::fxpt::float_to_fixed(angle_rate);
47  }
48 
49  // angle_rate is a delta in radians / step
50  void adjust_freq(float delta_angle_rate)
51  {
52  d_phase_inc += gr::fxpt::float_to_fixed(delta_angle_rate);
53  }
54 
55  // increment current phase angle
56 
57  void step() { d_phase += d_phase_inc; }
58 
59  void step(int n) { d_phase += d_phase_inc * n; }
60 
61  // units are radians / step
62  float get_phase() const { return gr::fxpt::fixed_to_float(d_phase); }
63  float get_freq() const { return gr::fxpt::fixed_to_float(d_phase_inc); }
64 
65  // compute sin and cos for current phase angle
66  void sincos(float* sinx, float* cosx) const
67  {
68  *sinx = gr::fxpt::sin(d_phase);
69  *cosx = gr::fxpt::cos(d_phase);
70  }
71 
72  // compute cos and sin for a block of phase angles
73  void sincos(gr_complex* output, int noutput_items, double ampl = 1.0)
74  {
75  for (int i = 0; i < noutput_items; i++) {
76  output[i] =
77  gr_complex(gr::fxpt::cos(d_phase) * ampl, gr::fxpt::sin(d_phase) * ampl);
78  step();
79  }
80  }
81 
82  // compute sin for a block of phase angles
83  void sin(float* output, int noutput_items, double ampl = 1.0)
84  {
85  for (int i = 0; i < noutput_items; i++) {
86  output[i] = (float)(gr::fxpt::sin(d_phase) * ampl);
87  step();
88  }
89  }
90 
91  // compute cos for a block of phase angles
92  void cos(float* output, int noutput_items, double ampl = 1.0)
93  {
94  for (int i = 0; i < noutput_items; i++) {
95  output[i] = (float)(gr::fxpt::cos(d_phase) * ampl);
96  step();
97  }
98  }
99 
100  // compute sin for a block of phase angles
101  void sin(std::int8_t* output, int noutput_items, double ampl = 1.0)
102  {
103  for (int i = 0; i < noutput_items; i++) {
104  output[i] = (std::int8_t)(gr::fxpt::sin(d_phase) * ampl);
105  step();
106  }
107  }
108 
109  // compute cos for a block of phase angles
110  void cos(std::int8_t* output, int noutput_items, double ampl = 1.0)
111  {
112  for (int i = 0; i < noutput_items; i++) {
113  output[i] = (std::int8_t)(gr::fxpt::cos(d_phase) * ampl);
114  step();
115  }
116  }
117 
118  // compute sin for a block of phase angles
119  void sin(short* output, int noutput_items, double ampl = 1.0)
120  {
121  for (int i = 0; i < noutput_items; i++) {
122  output[i] = (short)(gr::fxpt::sin(d_phase) * ampl);
123  step();
124  }
125  }
126 
127  // compute cos for a block of phase angles
128  void cos(short* output, int noutput_items, double ampl = 1.0)
129  {
130  for (int i = 0; i < noutput_items; i++) {
131  output[i] = (short)(gr::fxpt::cos(d_phase) * ampl);
132  step();
133  }
134  }
135 
136  // compute sin for a block of phase angles
137  void sin(int* output, int noutput_items, double ampl = 1.0)
138  {
139  for (int i = 0; i < noutput_items; i++) {
140  output[i] = (int)(gr::fxpt::sin(d_phase) * ampl);
141  step();
142  }
143  }
144 
145  // compute cos for a block of phase angles
146  void cos(int* output, int noutput_items, double ampl = 1.0)
147  {
148  for (int i = 0; i < noutput_items; i++) {
149  output[i] = (int)(gr::fxpt::cos(d_phase) * ampl);
150  step();
151  }
152  }
153 
154  // compute cos or sin for current phase angle
155  float cos() const { return gr::fxpt::cos(d_phase); }
156  float sin() const { return gr::fxpt::sin(d_phase); }
157 };
158 
159 } /* namespace gr */
160 
161 #endif /* INCLUDED_GR_FXPT_NCO_H */
Numerically Controlled Oscillator (NCO)
Definition: fxpt_nco.h:26
void sin(int *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:137
float get_phase() const
Definition: fxpt_nco.h:62
void adjust_freq(float delta_angle_rate)
Definition: fxpt_nco.h:50
void adjust_phase(float delta_phase)
Definition: fxpt_nco.h:38
void cos(short *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:128
fxpt_nco()
Definition: fxpt_nco.h:31
void sincos(gr_complex *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:73
float get_freq() const
Definition: fxpt_nco.h:63
void set_freq(float angle_rate)
Definition: fxpt_nco.h:44
void cos(int *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:146
void cos(std::int8_t *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:110
~fxpt_nco()
Definition: fxpt_nco.h:33
void cos(float *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:92
float cos() const
Definition: fxpt_nco.h:155
void sin(short *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:119
void sincos(float *sinx, float *cosx) const
Definition: fxpt_nco.h:66
void set_phase(float angle)
Definition: fxpt_nco.h:36
void step()
Definition: fxpt_nco.h:57
float sin() const
Definition: fxpt_nco.h:156
void sin(std::int8_t *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:101
void step(int n)
Definition: fxpt_nco.h:59
void sin(float *output, int noutput_items, double ampl=1.0)
Definition: fxpt_nco.h:83
static float cos(int32_t x)
Definition: fxpt.h:65
static int32_t float_to_fixed(float x)
Definition: fxpt.h:41
static float sin(int32_t x)
Given a fixed point angle x, return float sine (x)
Definition: fxpt.h:55
static float fixed_to_float(int32_t x)
Definition: fxpt.h:50
std::complex< float > gr_complex
Definition: gr_complex.h:15
GNU Radio logging wrapper.
Definition: basic_block.h:29