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