GNU Radio Manual and C++ API Reference  3.7.13.4
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  * 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_H
24 #define INCLUDED_GR_FXPT_H
25 
26 #include <gnuradio/api.h>
27 #include <gnuradio/types.h>
28 #include <stdint.h>
29 
30 namespace gr {
31 
32  /*!
33  * \brief fixed point sine and cosine and friends.
34  * \ingroup misc
35  *
36  * fixed pt radians
37  * --------- --------
38  * -2**31 -pi
39  * 0 0
40  * 2**31-1 pi - epsilon
41  */
43  {
44  static const int WORDBITS = 32;
45  static const int NBITS = 10;
46  static const float s_sine_table[1 << NBITS][2];
47  static const float PI;
48  static const float TAU;
49  static const float TWO_TO_THE_31;
50 
51  public:
52  static int32_t
53  float_to_fixed(float x)
54  {
55  // Fold x into -PI to PI.
56  int d = (int)std::floor(x/TAU+0.5);
57  x -= d*TAU;
58  // And convert to an integer.
59  return (int32_t) ((float) x * TWO_TO_THE_31 / PI);
60  }
61 
62  static float
63  fixed_to_float (int32_t x)
64  {
65  return x * (PI / TWO_TO_THE_31);
66  }
67 
68  /*!
69  * \brief Given a fixed point angle x, return float sine (x)
70  */
71  static float
72  sin(int32_t x)
73  {
74  uint32_t ux = x;
75  int index = ux >> (WORDBITS - NBITS);
76  return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
77  }
78 
79  /*
80  * \brief Given a fixed point angle x, return float cosine (x)
81  */
82  static float
83  cos (int32_t x)
84  {
85  uint32_t ux = x + 0x40000000;
86  int index = ux >> (WORDBITS - NBITS);
87  return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
88  }
89 
90  /*
91  * \brief Given a fixedpoint angle x, return float cos(x) and sin (x)
92  */
93  static void sincos(int32_t x, float *s, float *c)
94  {
95  uint32_t ux = x;
96  int sin_index = ux >> (WORDBITS - NBITS);
97  *s = s_sine_table[sin_index][0] * (ux >> 1) + s_sine_table[sin_index][1];
98 
99  ux = x + 0x40000000;
100  int cos_index = ux >> (WORDBITS - NBITS);
101  *c = s_sine_table[cos_index][0] * (ux >> 1) + s_sine_table[cos_index][1];
102 
103  return;
104  }
105 
106  };
107 
108 } /* namespace gr */
109 
110 #endif /* INCLUDED_GR_FXPT_H */
fixed point sine and cosine and friends.fixed pt radians -2**31 -pi 0 0 2**31-1 pi - epsilon ...
Definition: fxpt.h:42
#define GR_RUNTIME_API
Definition: gnuradio-runtime/include/gnuradio/api.h:30
static float cos(int32_t x)
Definition: fxpt.h:83
Include this header to use the message passing features.
Definition: logger.h:695
static float sin(int32_t x)
Given a fixed point angle x, return float sine (x)
Definition: fxpt.h:72
static float fixed_to_float(int32_t x)
Definition: fxpt.h:63
static void sincos(int32_t x, float *s, float *c)
Definition: fxpt.h:93
static int32_t float_to_fixed(float x)
Definition: fxpt.h:53