GNU Radio 3.7.1 C++ API
fxpt.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004,2013 Free Software Foundation, Inc.
00004  *
00005  * This file is part of GNU Radio
00006  *
00007  * GNU Radio is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 3, or (at your option)
00010  * any later version.
00011  *
00012  * GNU Radio is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with GNU Radio; see the file COPYING.  If not, write to
00019  * the Free Software Foundation, Inc., 51 Franklin Street,
00020  * Boston, MA 02110-1301, USA.
00021  */
00022 
00023 #ifndef INCLUDED_GR_FXPT_H
00024 #define INCLUDED_GR_FXPT_H
00025 
00026 #include <gnuradio/api.h>
00027 #include <gnuradio/types.h>
00028 
00029 namespace gr {
00030 
00031   /*!
00032    * \brief fixed point sine and cosine and friends.
00033    * \ingroup misc
00034    *
00035    *   fixed pt radians
00036    *  --------- --------
00037    *   -2**31       -pi
00038    *        0         0
00039    *  2**31-1        pi - epsilon
00040    */
00041   class GR_RUNTIME_API fxpt
00042   {
00043     static const int WORDBITS = 32;
00044     static const int NBITS = 10;
00045     static const float s_sine_table[1 << NBITS][2];
00046     static const float PI;
00047     static const float TWO_TO_THE_31;
00048 
00049   public:
00050     static gr_int32
00051       float_to_fixed(float x)
00052     {
00053       // Fold x into -PI to PI.
00054       int d = (int)floor(x/2/PI+0.5);
00055       x -= d*2*PI;
00056       // And convert to an integer.
00057       return (gr_int32) ((float) x * TWO_TO_THE_31 / PI);
00058     }
00059 
00060     static float
00061       fixed_to_float (gr_int32 x)
00062     {
00063       return x * (PI / TWO_TO_THE_31);
00064     }
00065 
00066     /*!
00067      * \brief Given a fixed point angle x, return float sine (x)
00068      */
00069     static float
00070       sin(gr_int32 x)
00071     {
00072       gr_uint32 ux = x;
00073       int index = ux >> (WORDBITS - NBITS);
00074       return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
00075     }
00076 
00077     /*
00078      * \brief Given a fixed point angle x, return float cosine (x)
00079      */
00080     static float
00081       cos (gr_int32 x)
00082     {
00083       gr_uint32 ux = x + 0x40000000;
00084       int index = ux >> (WORDBITS - NBITS);
00085       return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
00086     }
00087 
00088     /*
00089      * \brief Given a fixedpoint angle x, return float cos(x) and sin (x)
00090      */
00091     static void sincos(gr_int32 x, float *s, float *c)
00092     {
00093       gr_uint32 ux = x;
00094       int sin_index = ux >> (WORDBITS - NBITS);
00095       *s = s_sine_table[sin_index][0] * (ux >> 1) + s_sine_table[sin_index][1];
00096 
00097       ux = x + 0x40000000;
00098       int cos_index = ux >> (WORDBITS - NBITS);
00099       *c = s_sine_table[cos_index][0] * (ux >> 1) + s_sine_table[cos_index][1];
00100 
00101       return;
00102     }
00103 
00104   };
00105 
00106 } /* namespace gr */
00107 
00108 #endif /* INCLUDED_GR_FXPT_H */