GNU Radio 3.6.5 C++ API
|
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 friends. 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 // Fold x into -PI to PI. 00052 int d = (int)floor(x/2/PI+0.5); 00053 x -= d*2*PI; 00054 // And convert to an integer. 00055 return (gr_int32) ((float) x * TWO_TO_THE_31 / PI); 00056 } 00057 00058 static float 00059 fixed_to_float (gr_int32 x) 00060 { 00061 return x * (PI / TWO_TO_THE_31); 00062 } 00063 00064 /*! 00065 * \brief Given a fixed point angle x, return float sine (x) 00066 */ 00067 static float 00068 sin (gr_int32 x) 00069 { 00070 gr_uint32 ux = x; 00071 int index = ux >> (WORDBITS - NBITS); 00072 return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1]; 00073 } 00074 00075 /* 00076 * \brief Given a fixed point angle x, return float cosine (x) 00077 */ 00078 static float 00079 cos (gr_int32 x) 00080 { 00081 gr_uint32 ux = x + 0x40000000; 00082 int index = ux >> (WORDBITS - NBITS); 00083 return s_sine_table[index][0] * (ux >> 1) + s_sine_table[index][1]; 00084 } 00085 00086 /* 00087 * \brief Given a fixedpoint angle x, return float cos(x) and sin (x) 00088 */ 00089 static void sincos(gr_int32 x, float *s, float *c) 00090 { 00091 gr_uint32 ux = x; 00092 int sin_index = ux >> (WORDBITS - NBITS); 00093 *s = s_sine_table[sin_index][0] * (ux >> 1) + s_sine_table[sin_index][1]; 00094 00095 ux = x + 0x40000000; 00096 int cos_index = ux >> (WORDBITS - NBITS); 00097 *c = s_sine_table[cos_index][0] * (ux >> 1) + s_sine_table[cos_index][1]; 00098 00099 return; 00100 } 00101 00102 }; 00103 00104 #endif /* INCLUDED_GR_FXPT_H */