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