GNU Radio 3.5.1 C++ API
gr_fxpt.h
Go to the documentation of this file.
00001 /* -*- c++ -*- */
00002 /*
00003  * Copyright 2004 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 #ifndef INCLUDED_GR_FXPT_H
00023 #define INCLUDED_GR_FXPT_H
00024 
00025 #include <gr_core_api.h>
00026 #include <gr_types.h>
00027 
00028 /*!
00029  * \brief fixed point sine and cosine and friend GR_CORE_APIs.
00030  * \ingroup misc
00031  *
00032  *   fixed pt   radians
00033  *  ---------   --------
00034  *   -2**31       -pi
00035  *        0         0
00036  *  2**31-1        pi - epsilon
00037  *
00038  */
00039 class GR_CORE_API gr_fxpt
00040 {
00041   static const int WORDBITS = 32;
00042   static const int NBITS = 10;
00043   static const float s_sine_table[1 << NBITS][2];
00044   static const float PI;
00045   static const float TWO_TO_THE_31;
00046 public:
00047 
00048   static gr_int32
00049   float_to_fixed (float x)
00050   {
00051     return (gr_int32) ((float) x * TWO_TO_THE_31 / PI);
00052   }
00053 
00054   static float
00055   fixed_to_float (gr_int32 x)
00056   {
00057     return x * (PI / TWO_TO_THE_31);
00058   }
00059 
00060   /*!
00061    * \brief Given a fixed point angle x, return float sine (x)
00062    */
00063   static float
00064   sin (gr_int32 x)
00065   {
00066     gr_uint32 ux = x;
00067     int index = ux >> (WORDBITS - NBITS);
00068     return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
00069   }
00070 
00071   /*
00072    * \brief Given a fixed point angle x, return float cosine (x)
00073    */
00074   static float
00075   cos (gr_int32 x)
00076   {
00077     gr_uint32 ux = x + 0x40000000;
00078     int index = ux >> (WORDBITS - NBITS);
00079     return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1];
00080   }
00081 
00082 };
00083 
00084 #endif /* INCLUDED_GR_FXPT_H */