Exemplo n.º 1
0
static inline size_t FastReverseBits(size_t i, size_t NumBits)
{
   if (NumBits <= MaxFastBits)
      return gFFTBitTable[NumBits - 1][i];
   else
      return ReverseBits(i, NumBits);
}
Exemplo n.º 2
0
int main()
{
    int value = 11;
    printf("Entered value:%d\n", value);
    printf("swap:%d\n",ReverseBits(value));

}
Exemplo n.º 3
0
// Get the actual bit values for a tree of bit depths.
static void ConvertBitDepthsToSymbols(HuffmanTreeCode* const tree) {
  // 0 bit-depth means that the symbol does not exist.
  int i;
  int len;
  uint32_t next_code[MAX_ALLOWED_CODE_LENGTH + 1];
  int depth_count[MAX_ALLOWED_CODE_LENGTH + 1] = { 0 };

  assert(tree != NULL);
  len = tree->num_symbols;
  for (i = 0; i < len; ++i) {
    const int code_length = tree->code_lengths[i];
    assert(code_length <= MAX_ALLOWED_CODE_LENGTH);
    ++depth_count[code_length];
  }
  depth_count[0] = 0;  // ignore unused symbol
  next_code[0] = 0;
  {
    uint32_t code = 0;
    for (i = 1; i <= MAX_ALLOWED_CODE_LENGTH; ++i) {
      code = (code + depth_count[i - 1]) << 1;
      next_code[i] = code;
    }
  }
  for (i = 0; i < len; ++i) {
    const int code_length = tree->code_lengths[i];
    tree->codes[i] = ReverseBits(code_length, next_code[code_length]++);
  }
}
inline int FastReverseBits(int i, int NumBits)
{
	if (NumBits <= MaxFastBits)
		return gFFTBitTable[NumBits - 1][i];
	else
		return ReverseBits(i, NumBits);
}
Exemplo n.º 5
0
void InitFFT()
{
   gFFTBitTable.reinit(MaxFastBits);

   size_t len = 2;
   for (size_t b = 1; b <= MaxFastBits; b++) {
      auto &array = gFFTBitTable[b - 1];
      array.reinit(len);
      for (size_t i = 0; i < len; i++)
         array[i] = ReverseBits(i, b);

      len <<= 1;
   }
}
void InitFFT()
{
	gFFTBitTable = new int *[MaxFastBits];
	
	int len = 2;
	for (int b = 1; b <= MaxFastBits; b++) {
		
		gFFTBitTable[b - 1] = new int[len];
		
		for (int i = 0; i < len; i++)
			gFFTBitTable[b - 1][i] = ReverseBits(i, b);
		
		len <<= 1;
	}
}
Exemplo n.º 7
0
Arquivo: xns.c Projeto: mingpen/OpenNT
BOOL RplDirStatus( VOID) {
    USHORT      network_type;
    (VOID)memset((PVOID)&Ccb, '\0', sizeof(Ccb));
    Ccb.uchDlcCommand = LLC_DIR_STATUS;
    Ccb.u.pParameterTable = (PLLC_PARMS)&DirStatusParms;
    if ( !EtherAcsLan()) {
        return( FALSE);
    }
    network_type = DirStatusParms.usAdapterType;
    if ( network_type != RPL_ADAPTER_ETHERNET) {
        PRINTF(( "network_type=0x%x\n", network_type));
        return( FALSE);
    }
    ReverseBits( DirStatusParms.auchNodeAddress);
    return( TRUE);
}
Exemplo n.º 8
0
SymbEncodingTable::SymbEncodingTable( unsigned int rate_numer, 
                                      unsigned int rate_denom,
                                      unsigned int *inp_polys)
{
  //  rate_numer is the number of input bits processed each cycle
  //  rate_denom is the number of output bits generated each cycle
  //  polys points to a set of rate_numer*rate_denom polynomials
  //        the polynomial pointed to by polys[i][j] operates on
  //        the i-th input subsequence and contributes to the
  //        j-th output subsequence

  unsigned int j;
  unsigned int out_val;
  int composite;
  int *polys;
  int num_active_bits;

  polys = new int[rate_numer];

  composite = 0;
  for(j=0; j<rate_denom; j++)
    {
    composite |= inp_polys[j];
    }
  num_active_bits = ActiveBitCount(composite);

  for( j=0; j<rate_denom; j++)
    {
    polys[j] = ReverseBits(inp_polys[j], num_active_bits);
    cout << "P[" << j << "] = " << polys[j] << endl;
    }

  //---------------------------
  Table_Len = 1<<num_active_bits;
  Out_Table = new int[Table_Len];

  for(int n=0; n<Table_Len; n++)
    {
    out_val=0;
    for(unsigned int out_num=0; out_num<rate_denom; out_num++)
      {
      out_val <<= 1;
      out_val |= xor(n&polys[out_num]);
      }
    Out_Table[n] = out_val;
    }
}
Exemplo n.º 9
0
void InitFFT()
{
//	gFFTBitTable = new int *[MaxFastBits];
	//use malloc for 16 byte alignment
	gFFTBitTable = (int**) malloc(MaxFastBits * sizeof(int*));
	
	int len = 2;
	for (int b = 1; b <= MaxFastBits; b++) {
		
//		gFFTBitTable[b - 1] = new int[len];
		gFFTBitTable[b - 1] = (int*) malloc(len * sizeof(int));
		
		for (int i = 0; i < len; i++)
			gFFTBitTable[b - 1][i] = ReverseBits(i, b);
		
		len <<= 1;
	}
}
Exemplo n.º 10
0
void cDriverSED1520::Set8Pixels (int x, int y, unsigned char data)
{
	if (x >= width || y >= height)
		return;

	if (!config->upsideDown)
	{
		// normal orientation
		LCD[x / 8][y] = LCD[x / 8][y] | data;
	}
	else
	{
		// upside down orientation
		x = width - 1 - x;
		y = height - 1 - y;
		LCD[x / 8][y] = LCD[x / 8][y] | ReverseBits(data);
	}
}
Exemplo n.º 11
0
void ofxFftBasic::setup(int _bins, fftWindowType _windowType)
{
	ofxFft::setup(_bins, _windowType);

   gFFTBitTable = new int *[MaxFastBits];

   int len = 2;
   for (int b = 1; b <= MaxFastBits; b++) {

      gFFTBitTable[b - 1] = new int[len];

      for (int i = 0; i < len; i++)
         gFFTBitTable[b - 1][i] = ReverseBits(i, b);

      len <<= 1;
   }

    ready = true;
}
Exemplo n.º 12
0
void InitFFT()
{
    gFFTBitTable = (int**)calloc(MaxFastBits, sizeof(int*));
    if (gFFTBitTable == NULL)
        fprintf(stderr, "No se esta asignando memoria a gFFBitTable");

    int len = 2;
    int b = 0;
    int i = 0;

    for (b = 1; b <= MaxFastBits; b++) {
        gFFTBitTable[b - 1] = (int*)calloc(len, sizeof(int));
        if (gFFTBitTable[b - 1] == NULL)
            fprintf(stderr, "No se esta asignando memoria a los elementos de gFFTBitTable");
        for (i = 0; i < len; i++)
            gFFTBitTable[b - 1][i] = ReverseBits(i, b);

        len <<= 1;
    }
}
Exemplo n.º 13
0
void anim_fft_double (unsigned  NofSamples,
		      int       InverseTransform,
		      double   *RealIn,
		      double   *ImagIn,
		      double   *RealOut,
		      double   *ImagOut )
{
  unsigned NofBits;    /* Number of bits needed to store indices */
  unsigned LSample, j, k, n;
  unsigned BlockSize, BlockEnd;
  double AngleNumerator = 2.0 * M_PI;
  double TempReal, TempImagin;     /* temp real, temp imaginary */
  
  if ( !IsPowerOfTwo(NofSamples) ) {
    PrintError(("Error in fft():  NofSamples=%u is not power of two\n", NofSamples) );
    exit(1);
  }
  if ( InverseTransform ) AngleNumerator = -AngleNumerator;
  
  CHECKPOINTER ( RealIn );
  CHECKPOINTER ( RealOut );
  CHECKPOINTER ( ImagOut );

  NofBits = NumberOfBitsNeeded ( NofSamples );

  /*
  **   Do simultaneous data copy and bit-reversal ordering into outputs...
  */
  
  for ( LSample=0; LSample < NofSamples; LSample++ ) {
    j = ReverseBits ( LSample, NofBits );
    RealOut[j] = RealIn[LSample];
    ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[LSample];
  }
  
  /*
  **   Do the FFT itself...
  */
  
  BlockEnd = 1;
  for ( BlockSize = 2; BlockSize <= NofSamples; BlockSize <<= 1 ) {
    double delta_angle = AngleNumerator / (double)BlockSize;
    double sm2 = sin ( -2 * delta_angle );
    double sm1 = sin ( -delta_angle );
    double cm2 = cos ( -2 * delta_angle );
    double cm1 = cos ( -delta_angle );
    double w = 2 * cm1;
    double ar[3], ai[3];
    
    for ( LSample=0; LSample < NofSamples; LSample += BlockSize ) {
      ar[2] = cm2;
      ar[1] = cm1;
      
      ai[2] = sm2;
      ai[1] = sm1;
      
      for ( j=LSample, n=0; n < BlockEnd; j++, n++ ) {
	ar[0] = w*ar[1] - ar[2];
	ar[2] = ar[1];
	ar[1] = ar[0];
	  
	ai[0] = w*ai[1] - ai[2];
	ai[2] = ai[1];
	ai[1] = ai[0];
	
	k = j + BlockEnd;
	TempReal = ar[0]*RealOut[k] - ai[0]*ImagOut[k];
	TempImagin = ar[0]*ImagOut[k] + ai[0]*RealOut[k];
	
	RealOut[k] = RealOut[j] - TempReal;
	ImagOut[k] = ImagOut[j] - TempImagin;
	
	RealOut[j] += TempReal;
	ImagOut[j] += TempImagin;
      }
    }
    BlockEnd = BlockSize;
  }
  
  /*
  **   Need to normalize if inverse transform...
  */
    
  if ( InverseTransform ) {
    double denom = (double)NofSamples;
    
    for ( LSample=0; LSample < NofSamples; LSample++ ) {
      RealOut[LSample] /= denom;
      ImagOut[LSample] /= denom;
    }
  }
}
Exemplo n.º 14
0
extern void fft_float(
  _UINT32 __NumSamples,
  _INT32 __InverseTransform,
  _IEEE32 * __RealIn,
  _IEEE32 * __ImagIn,
  _IEEE32 * __RealOut,
  _IEEE32 * __ImagOut)
{
  
  register _INT32 _w2c___comma;
  register _UINT32 _w2c___comma0;
  register _INT32 _w2c___ompv_ok_to_fork;
  register _UINT64 _w2c_reg3;
  register _UINT32 _w2c___comma1;
  register _IEEE32 _w2c___cselect;
  register _IEEE64 _w2c___comma2;
  register _IEEE64 _w2c___comma3;
  register _IEEE64 _w2c___comma4;
  register _INT32 _w2c_trip_count;
  register _INT32 _w2c___ompv_ok_to_fork0;
  _IEEE64 delta_angle;
  _IEEE64 denom;
  _INT32 __localized_common_i;
  _INT32 __localized_common_j;
  _INT32 _w2c___localized_common_i0;
  _IEEE64 __localized_common_ar[3LL];
  _IEEE64 __localized_common_ai[3LL];
  _INT32 _w2c___localized_common_j0;
  _INT32 __localized_common_n;
  _INT32 __ompv_gtid_s1;
  
  /*Begin_of_nested_PU(s)*/
  
  NumSamples = __NumSamples;
  InverseTransform = __InverseTransform;
  * RealIn = *__RealIn;
  * ImagIn = *__ImagIn;
  * RealOut = *__RealOut;
  * ImagOut = *__ImagOut;
  _w2c___comma = IsPowerOfTwo(NumSamples);
  if(_w2c___comma == 0)
  {
    printf("Error in fft():  NumSamples=%u is not power of two\n", NumSamples);
    exit(1);
  }
  if(InverseTransform != 0)
  {
    angle_numerator = -angle_numerator;
  }
  CheckPointer(RealIn, "RealIn");
  CheckPointer(RealOut, "RealOut");
  CheckPointer(ImagOut, "ImagOut");
  _w2c___comma0 = NumberOfBitsNeeded(NumSamples);
  NumBits = _w2c___comma0;
  _w2c___ompv_ok_to_fork = 1;
  if(_w2c___ompv_ok_to_fork)
  {
    _w2c___ompv_ok_to_fork = __ompc_can_fork();
  }
  if(_w2c___ompv_ok_to_fork)
  {
    __ompc_fork(0, &__omprg_fft_float_1, _w2c_reg3);
  }
  else
  {
    __ompv_gtid_s1 = __ompc_get_local_thread_num();
    __ompc_serialized_parallel();
    for(__localized_common_i = 0; __localized_common_i < (_INT32) NumSamples; __localized_common_i = __localized_common_i + 1)
    {
      _w2c___comma1 = ReverseBits((_UINT32) __localized_common_i, NumBits);
      __localized_common_j = (_INT32)(_w2c___comma1);
      * (RealOut + (_UINT64)((_UINT64) __localized_common_j)) = *(RealIn + (_UINT64)((_UINT64) __localized_common_i));
      if((_UINT64)(ImagIn) != 0ULL)
      {
        _w2c___cselect = *(ImagIn + (_UINT64)((_UINT64) __localized_common_i));
      }
      else
      {
        _w2c___cselect = 0.0F;
      }
      * (ImagOut + (_UINT64)((_UINT64) __localized_common_j)) = _w2c___cselect;
    }
    __ompc_end_serialized_parallel();
  }
  _w2c___comma2 = log2((_IEEE64)(NumSamples));
  NumIter = _U4F8TRUNC(_w2c___comma2);
  m = 1;
  while(NumIter >= (_UINT32) m)
  {
    _514 :;
    _w2c___comma3 = pow(2.0, (_IEEE64)(m));
    BlockSize = _U4F8TRUNC(_w2c___comma3);
    _w2c___comma4 = pow(2.0, (_IEEE64)(m + -1));
    BlockEnd = _U4F8TRUNC(_w2c___comma4);
    delta_angle = angle_numerator / (_IEEE64)(BlockSize);
    sm2 = sin(delta_angle * -2.0);
    sm1 = sin(-delta_angle);
    cm2 = cos(delta_angle * -2.0);
    cm1 = cos(delta_angle);
    w = cm1 * 2.0;
    _w2c_trip_count = (((_INT32) NumSamples + (_INT32) BlockSize) + -1) / (_INT32) BlockSize;
    _w2c___ompv_ok_to_fork0 = _w2c_trip_count > 1;
    if(_w2c___ompv_ok_to_fork0)
    {
      _w2c___ompv_ok_to_fork0 = __ompc_can_fork();
    }
    if(_w2c___ompv_ok_to_fork0)
    {
      __ompc_fork(0, &__ompdo_fft_float_11, _w2c_reg3);
    }
    else
    {
      __ompv_gtid_s1 = __ompc_get_local_thread_num();
      __ompc_serialized_parallel();
      for(_w2c___localized_common_i0 = 0; _w2c___localized_common_i0 < (_INT32) NumSamples; _w2c___localized_common_i0 = _w2c___localized_common_i0 + (_INT32) BlockSize)
      {
        (__localized_common_ar)[2] = cm2;
        (__localized_common_ar)[1] = cm1;
        (__localized_common_ai)[2] = sm2;
        (__localized_common_ai)[1] = sm1;
        _w2c___localized_common_j0 = _w2c___localized_common_i0;
        __localized_common_n = 0;
        while((_UINT32) __localized_common_n < BlockEnd)
        {
          _1026 :;
          (__localized_common_ar)[0] = ((__localized_common_ar)[1] * w) - (__localized_common_ar)[2];
          (__localized_common_ar)[2] = (__localized_common_ar)[1];
          (__localized_common_ar)[1] = (__localized_common_ar)[0];
          (__localized_common_ai)[0] = ((__localized_common_ai)[1] * w) - (__localized_common_ai)[2];
          (__localized_common_ai)[2] = (__localized_common_ai)[1];
          (__localized_common_ai)[1] = (__localized_common_ai)[0];
          k = (_INT32)((_UINT32) _w2c___localized_common_j0 + BlockEnd);
          tr = ((_IEEE64)(*(RealOut + (_UINT64)((_UINT64) k))) * (__localized_common_ar)[0]) - ((_IEEE64)(*(ImagOut + (_UINT64)((_UINT64) k))) * (__localized_common_ai)[0]);
          ti = ((_IEEE64)(*(RealOut + (_UINT64)((_UINT64) k))) * (__localized_common_ai)[0]) + ((_IEEE64)(*(ImagOut + (_UINT64)((_UINT64) k))) * (__localized_common_ar)[0]);
          * (RealOut + (_UINT64)((_UINT64) k)) = (_IEEE32)((_IEEE64)(*(RealOut + (_UINT64)((_UINT64) _w2c___localized_common_j0))) - tr);
          * (ImagOut + (_UINT64)((_UINT64) k)) = (_IEEE32)((_IEEE64)(*(ImagOut + (_UINT64)((_UINT64) _w2c___localized_common_j0))) - ti);
          * (RealOut + (_UINT64)((_UINT64) _w2c___localized_common_j0)) = (_IEEE32)((_IEEE64)(*(RealOut + (_UINT64)((_UINT64) _w2c___localized_common_j0))) + tr);
          * (ImagOut + (_UINT64)((_UINT64) _w2c___localized_common_j0)) = (_IEEE32)((_IEEE64)(*(ImagOut + (_UINT64)((_UINT64) _w2c___localized_common_j0))) + ti);
          _w2c___localized_common_j0 = _w2c___localized_common_j0 + 1;
          __localized_common_n = __localized_common_n + 1;
          _770 :;
        }
        goto _1282;
        _1282 :;
      }
      __ompc_end_serialized_parallel();
    }
    m = m + 1;
    _258 :;
  }
  goto _1538;
  _1538 :;
  if(InverseTransform != 0)
  {
    denom = (_IEEE64)(NumSamples);
    i = 0;
    while(NumSamples > (_UINT32) i)
    {
      _2050 :;
      * (RealOut + (_UINT64)((_UINT64) i)) = (_IEEE32)((_IEEE64)(*(RealOut + (_UINT64)((_UINT64) i))) / denom);
      * (ImagOut + (_UINT64)((_UINT64) i)) = (_IEEE32)((_IEEE64)(*(ImagOut + (_UINT64)((_UINT64) i))) / denom);
      i = i + 1;
      _1794 :;
    }
    _2306 :;
  }
  return;
} /* fft_float */
Exemplo n.º 15
0
/// @brief Transform
/// @param n_samples
/// @param input
/// @param output_r
/// @param output_i
/// @param inverse
///
void FFT::DoTransform (size_t n_samples,float *input,float *output_r,float *output_i,bool inverse) {
	// Check if it's power of two
	if (!IsPowerOfTwo(n_samples)) {
		throw "FFT requires power of two input.";
	}

	// Inverse transform
	float angle_num = 2.0f * 3.1415926535897932384626433832795f;
	if (inverse) angle_num = -angle_num;

	// Variables
	unsigned int i, j, k, n;
	float tr, ti;

	// Calculate needed bits
	unsigned int NumBits;
	NumBits = NumberOfBitsNeeded(n_samples);

	// Copy samples to output buffers
	for (i=0;i<n_samples;i++) {
		j = ReverseBits (i,NumBits);
		output_r[j] = input[i];
		output_i[j] = 0.0f;
	}

	unsigned int BlockEnd = 1;
	unsigned int BlockSize;
	for (BlockSize = 2;BlockSize<=n_samples;BlockSize<<=1) {
		// Calculate variables for this iteration
		float delta_angle = angle_num / (float)BlockSize;
		float sm2 = sin (-2 * delta_angle);
		float sm1 = sin (-delta_angle);
		float cm2 = cos (-2 * delta_angle);
		float cm1 = cos (-delta_angle);
		float w = 2 * cm1;
		float ar0, ar1, ar2, ai0, ai1, ai2;

		// Apply for every sample
		for(i=0;i<n_samples;i+=BlockSize) {
			ar1 = cm1;
			ar2 = cm2;
			ai1 = sm1;
			ai2 = sm2;

			for (j=i,n=0;n<BlockEnd;j++,n++) {
				k = j + BlockEnd;

				ar0 = w*ar1 - ar2;
				ai0 = w*ai1 - ai2;
				ar2 = ar1;
				ai2 = ai1;
				ar1 = ar0;
				ai1 = ai0;

				tr = ar0*output_r[k] - ai0*output_i[k];
				ti = ar0*output_i[k] + ai0*output_r[k];

				output_r[k] = output_r[j] - tr;
				output_i[k] = output_i[j] - ti;

				output_r[j] += tr;
				output_i[j] += ti;
			}
		}

		// Set next block end to current size
		BlockEnd = BlockSize;
	}

	// Divide everything by number of samples if it's an inverse transform
	if (inverse) {
		float denom = 1.0f/(float)n_samples;
		for (i=0;i<n_samples;i++) {
			output_r[i] *= denom;
			output_i[i] *= denom;
		}
	}
}
Exemplo n.º 16
0
void fft_double (unsigned int p_nSamples, bool p_bInverseTransform, double *p_lpRealIn, double *p_lpImagIn, double *p_lpRealOut, double *p_lpImagOut)
{

	if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;


	unsigned int NumBits;
	unsigned int i, j, k, n;
	unsigned int BlockSize, BlockEnd;

	double angle_numerator = 2.0 * PI;
	double tr, ti;

	if( !IsPowerOfTwo(p_nSamples) )
	{
		return;
	}

	if( p_bInverseTransform ) angle_numerator = -angle_numerator;

	NumBits = NumberOfBitsNeeded ( p_nSamples );


	for( i=0; i < p_nSamples; i++ )
	{
		j = ReverseBits ( i, NumBits );
		p_lpRealOut[j] = p_lpRealIn[i];
		p_lpImagOut[j] = (p_lpImagIn == NULL) ? 0.0 : p_lpImagIn[i];
	}


	BlockEnd = 1;
	for( BlockSize = 2; BlockSize <= p_nSamples; BlockSize <<= 1 )
	{
		double delta_angle = angle_numerator / (double)BlockSize;
		double sm2 = sin ( -2 * delta_angle );
		double sm1 = sin ( -delta_angle );
		double cm2 = cos ( -2 * delta_angle );
		double cm1 = cos ( -delta_angle );
		double w = 2 * cm1;
		double ar[3], ai[3];

		for( i=0; i < p_nSamples; i += BlockSize )
		{

			ar[2] = cm2;
			ar[1] = cm1;

			ai[2] = sm2;
			ai[1] = sm1;

			for ( j=i, n=0; n < BlockEnd; j++, n++ )
			{

				ar[0] = w*ar[1] - ar[2];
				ar[2] = ar[1];
				ar[1] = ar[0];

				ai[0] = w*ai[1] - ai[2];
				ai[2] = ai[1];
				ai[1] = ai[0];

				k = j + BlockEnd;
				tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
				ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];

				p_lpRealOut[k] = p_lpRealOut[j] - tr;
				p_lpImagOut[k] = p_lpImagOut[j] - ti;

				p_lpRealOut[j] += tr;
				p_lpImagOut[j] += ti;

			}
		}

		BlockEnd = BlockSize;

	}


	if( p_bInverseTransform )
	{
		double denom = (double)p_nSamples;

		for ( i=0; i < p_nSamples; i++ )
		{
			p_lpRealOut[i] /= denom;
			p_lpImagOut[i] /= denom;
		}
	}

}
Exemplo n.º 17
0
BOOL CFourier::Fft(UINT nNumSamples, BOOL bInverse, float* pfRealIn, float* pfImagIn, float* pfRealOut, float* pfImagOut)
{
    register UINT i, j, k, n;
    register UINT nBlockSize;
    register UINT nBlockEnd = 1;

    if(!pfRealIn || !pfRealOut || !pfImagOut)
    {
        return FALSE;
    }

    if(!IsPowerOfTwo(nNumSamples))
    {
        return FALSE;
    }

    float fAngle = 2.f * PI;
    if(bInverse)
    {
        fAngle = -fAngle;
    }

    // Do simultaneous data copy and bit-reversal ordering into outputs
    UINT nNbBits = GetNumOfBitsNeeded(nNumSamples);
    for(i = 0; i < nNumSamples; i++)
    {
        j = ReverseBits(i, nNbBits);
        pfRealOut[j] = pfRealIn[i];
        pfImagOut[j] = ((pfImagIn == NULL) ? 0.f : pfImagIn[i]);
    }

    register float fDelta, fAReal[3], fAImag[3];
    register float fSin1, fSin2, fCos1, fCos2;
    register float fTmpReal, fTmpImag, fCoef;

    // Do fast fourier transform
    for(nBlockSize = 2; nBlockSize <= nNumSamples; nBlockSize <<= 1)
    {
        fDelta = fAngle / (float)nBlockSize;
        fSin1 = sinf(-fDelta);
        fSin2 = sinf(-2.f * fDelta);
        fCos1 = cosf(-fDelta);
        fCos2 = cosf(-2.f * fDelta);
        fCoef = 2.f * fCos1;
        for(i = 0; i < nNumSamples; i += nBlockSize)
        {
            fAReal[1] = fCos1;
            fAReal[2] = fCos2;
            fAImag[1] = fSin1;
            fAImag[2] = fSin2;
            for(j = i, n = 0; n < nBlockEnd; j++, n++)
            {
                fAReal[0] = fCoef*fAReal[1] - fAReal[2];
                fAReal[2] = fAReal[1];
                fAReal[1] = fAReal[0];

                fAImag[0] = fCoef*fAImag[1] - fAImag[2];
                fAImag[2] = fAImag[1];
                fAImag[1] = fAImag[0];

                k = j + nBlockEnd;
                fTmpReal = fAReal[0] * pfRealOut[k] - fAImag[0] * pfImagOut[k];
                fTmpImag = fAReal[0] * pfImagOut[k] + fAImag[0] * pfRealOut[k];
                pfRealOut[k] = pfRealOut[j] - fTmpReal;
                pfImagOut[k] = pfImagOut[j] - fTmpImag;
                pfRealOut[j] += fTmpReal;
                pfImagOut[j] += fTmpImag;
            }
        }
        nBlockEnd = nBlockSize;
    }

    // Need to normalize if inverse transform
    if(bInverse)
    {
        for(i = 0; i < nNumSamples; i++)
        {
            pfRealOut[i] /= (float)nNumSamples;
            pfImagOut[i] /= (float)nNumSamples;
        }
    }

    return TRUE;
}
Exemplo n.º 18
0
void CFft::FftDouble(unsigned int NumSamples, int InverseTransform, double *RealIn, double *ImagIn, double *RealOut, double *ImagOut)
{

	double PI=3.14;
    unsigned int NumBits;    /* Number of bits needed to store indices */
    unsigned int i, j, k, n;
    unsigned int BlockSize, BlockEnd;

    double angle_numerator = 2.0 * PI;
    double tr, ti;     /* temp real, temp imaginary */

    if ( !IsPowerOfTwo(NumSamples) )
    {
        fprintf (
            stderr,
            "Error in fft():  NumSamples=%u is not power of two\n",
            NumSamples );

        exit(1);
    }

    if ( InverseTransform )
        angle_numerator = -angle_numerator;

    
NumBits = NumberOfBitsNeeded ( NumSamples );

    /*
    **   Do simultaneous data copy and bit-reversal ordering into outputs...
    */

    for ( i=0; i < NumSamples; i++ )
    {
        j = ReverseBits ( i, NumBits );
        RealOut[j] = RealIn[i];
        ImagOut[j] = (ImagIn == NULL) ? 0.0 : ImagIn[i];
    }

    /*
    **   Do the FFT itself...
    */

    BlockEnd = 1;
    for ( BlockSize = 2; BlockSize <= NumSamples; BlockSize <<= 1 )
    {
        double delta_angle = angle_numerator / (double)BlockSize;
        double sm2 = sin ( -2 * delta_angle );
        double sm1 = sin ( -delta_angle );
        double cm2 = cos ( -2 * delta_angle );
        double cm1 = cos ( -delta_angle );
        double w = 2 * cm1;
        double ar[3], ai[3];

        for ( i=0; i < NumSamples; i += BlockSize )
        {
            ar[2] = cm2;
            ar[1] = cm1;

            ai[2] = sm2;
            ai[1] = sm1;

            for ( j=i, n=0; n < BlockEnd; j++, n++ )
            {
                ar[0] = w*ar[1] - ar[2];
                ar[2] = ar[1];
                ar[1] = ar[0];

                ai[0] = w*ai[1] - ai[2];
                ai[2] = ai[1];
                ai[1] = ai[0];

                k = j + BlockEnd;
                tr = ar[0]*RealOut[k] - ai[0]*ImagOut[k];
                ti = ar[0]*ImagOut[k] + ai[0]*RealOut[k];

                RealOut[k] = RealOut[j] - tr;
                ImagOut[k] = ImagOut[j] - ti;

                RealOut[j] += tr;
                ImagOut[j] += ti;
            }
        }

        BlockEnd = BlockSize;
    }

    /*
    **   Need to normalize if inverse transform...
    */

    if ( InverseTransform )
    {
        double denom = (double)NumSamples;

        for ( i=0; i < NumSamples; i++ )
        {
            RealOut[i] /= denom;
            ImagOut[i] /= denom;
        }
    }
	
}