GNU Radio Manual and C++ API Reference  3.9.0.0
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 <stdint.h>
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 const float s_sine_table[1 << NBITS][2];
35  static const float PI;
36  static const float TAU;
37  static const float TWO_TO_THE_31;
38 
39 public:
40  static int32_t float_to_fixed(float x)
41  {
42  // Fold x into -PI to PI.
43  int d = (int)std::floor(x / TAU + 0.5);
44  x -= d * TAU;
45  // And convert to an integer.
46  return (int32_t)((float)x * TWO_TO_THE_31 / PI);
47  }
48 
49  static float fixed_to_float(int32_t x) { return x * (PI / TWO_TO_THE_31); }
50 
51  /*!
52  * \brief Given a fixed point angle x, return float sine (x)
53  */
54  static float sin(int32_t x)
55  {
56  uint32_t ux = x;
57  int index = ux >> (WORDBITS - NBITS);
58  return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
59  }
60 
61  /*
62  * \brief Given a fixed point angle x, return float cosine (x)
63  */
64  static float cos(int32_t x)
65  {
66  uint32_t ux = x + 0x40000000;
67  int index = ux >> (WORDBITS - NBITS);
68  return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
69  }
70 
71  /*
72  * \brief Given a fixedpoint angle x, return float cos(x) and sin (x)
73  */
74  static void sincos(int32_t x, float* s, float* c)
75  {
76  uint32_t ux = x;
77  int sin_index = ux >> (WORDBITS - NBITS);
78  *s = s_sine_table[sin_index][0] * (ux >> 1) + s_sine_table[sin_index][1];
79 
80  ux = x + 0x40000000;
81  int cos_index = ux >> (WORDBITS - NBITS);
82  *c = s_sine_table[cos_index][0] * (ux >> 1) + s_sine_table[cos_index][1];
83 
84  return;
85  }
86 };
87 
88 } /* namespace gr */
89 
90 #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:74
static float cos(int32_t x)
Definition: fxpt.h:64
static int32_t float_to_fixed(float x)
Definition: fxpt.h:40
static float sin(int32_t x)
Given a fixed point angle x, return float sine (x)
Definition: fxpt.h:54
static float fixed_to_float(int32_t x)
Definition: fxpt.h:49
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:18
GNU Radio logging wrapper for log4cpp library (C++ port of log4j)
Definition: basic_block.h:29