GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
fxpt.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * Copyright 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_H
12 #define INCLUDED_GR_FXPT_H
13 
14 #include <gnuradio/api.h>
15 #include <gnuradio/types.h>
16 #include <cstdint>
17 
18 namespace gr {
19 
20 /*!
21  * \brief fixed point sine and cosine and friends.
22  * \ingroup misc
23  *
24  * fixed pt radians
25  * --------- --------
26  * -2**31 -pi
27  * 0 0
28  * 2**31-1 pi - epsilon
29  */
31 {
32  static constexpr int WORDBITS = 32;
33  static constexpr int NBITS = 10;
34  static constexpr uint32_t ACCUM_MASK = ((1 << (WORDBITS - NBITS)) - 1);
35  static const float s_sine_table[1 << NBITS][2];
36  static const float PI;
37  static const float TAU;
38  static const float TWO_TO_THE_31;
39 
40 public:
41  static int32_t float_to_fixed(float x)
42  {
43  // Fold x into -PI to PI.
44  int d = (int)std::floor(x / TAU + 0.5);
45  x -= d * TAU;
46  // And convert to an integer.
47  return (int32_t)((float)x * TWO_TO_THE_31 / PI);
48  }
49 
50  static float fixed_to_float(int32_t x) { return x * (PI / TWO_TO_THE_31); }
51 
52  /*!
53  * \brief Given a fixed point angle x, return float sine (x)
54  */
55  static float sin(int32_t x)
56  {
57  uint32_t ux = x;
58  int index = ux >> (WORDBITS - NBITS);
59  return s_sine_table[index][0] * (ux & ACCUM_MASK) + s_sine_table[index][1];
60  }
61 
62  /*
63  * \brief Given a fixed point angle x, return float cosine (x)
64  */
65  static float cos(int32_t x)
66  {
67  uint32_t ux = (uint32_t)x + 0x40000000;
68  int index = ux >> (WORDBITS - NBITS);
69  return s_sine_table[index][0] * (ux & ACCUM_MASK) + s_sine_table[index][1];
70  }
71 
72  /*
73  * \brief Given a fixedpoint angle x, return float cos(x) and sin (x)
74  */
75  static void sincos(int32_t x, float* s, float* c)
76  {
77  uint32_t ux = x;
78  int sin_index = ux >> (WORDBITS - NBITS);
79  *s = s_sine_table[sin_index][0] * (ux & ACCUM_MASK) + s_sine_table[sin_index][1];
80 
81  ux = (uint32_t)x + 0x40000000;
82  int cos_index = ux >> (WORDBITS - NBITS);
83  *c = s_sine_table[cos_index][0] * (ux & ACCUM_MASK) + s_sine_table[cos_index][1];
84 
85  return;
86  }
87 };
88 
89 } /* namespace gr */
90 
91 #endif /* INCLUDED_GR_FXPT_H */
fixed point sine and cosine and friends.
Definition: fxpt.h:31
static void sincos(int32_t x, float *s, float *c)
Definition: fxpt.h:75
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
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper.
Definition: basic_block.h:29