Exemplo n.º 1
0
int
FUNCTION(gsl_fft_real,radix2_transform) (BASE data[], const size_t stride,  const size_t n)
{
  int result ;
  size_t p, p_1, q;
  size_t i; 
  size_t logn = 0;
  int status;

  if (n == 1) /* identity operation */
    {
      return 0 ;
    }

  /* make sure that n is a power of 2 */

  result = fft_binary_logn(n) ;

  if (result == -1) 
    {
      GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
    } 
  else 
    {
      logn = result ;
    }

  /* bit reverse the ordering of input data for decimation in time algorithm */
  
  status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;

  /* apply fft recursion */

  p = 1; q = n ;

  for (i = 1; i <= logn; i++)
    {
      size_t a, b;

      p_1 = p ;
      p = 2 * p ;
      q = q / 2 ;

      /* a = 0 */

      for (b = 0; b < q; b++)
        {
          ATOMIC t0_real = VECTOR(data,stride,b*p) + VECTOR(data,stride,b*p + p_1) ;
          ATOMIC t1_real = VECTOR(data,stride,b*p) - VECTOR(data,stride,b*p + p_1) ;
          
          VECTOR(data,stride,b*p) = t0_real ;
          VECTOR(data,stride,b*p + p_1) = t1_real ;
        }

      /* a = 1 ... p_{i-1}/2 - 1 */

      {
        ATOMIC w_real = 1.0;
        ATOMIC w_imag = 0.0;

        const double theta = - 2.0 * M_PI / p;
        
        const ATOMIC s = sin (theta);
        const ATOMIC t = sin (theta / 2.0);
        const ATOMIC s2 = 2.0 * t * t;
        
        for (a = 1; a < (p_1)/2; a++)
          {
            /* trignometric recurrence for w-> exp(i theta) w */
            
            {
              const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
              const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
              w_real = tmp_real;
              w_imag = tmp_imag;
            }
            
            for (b = 0; b < q; b++)
              {
                ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
                ATOMIC z0_imag = VECTOR(data,stride,b*p + p_1 - a) ;
                ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 + a) ;
                ATOMIC z1_imag = VECTOR(data,stride,b*p + p - a) ;
                
                /* t0 = z0 + w * z1 */
                
                ATOMIC t0_real = z0_real + w_real * z1_real - w_imag * z1_imag;
                ATOMIC t0_imag = z0_imag + w_real * z1_imag + w_imag * z1_real;
                
                /* t1 = z0 - w * z1 */
                
                ATOMIC t1_real = z0_real - w_real * z1_real + w_imag * z1_imag;
                ATOMIC t1_imag = z0_imag - w_real * z1_imag - w_imag * z1_real;
                
                VECTOR(data,stride,b*p + a) = t0_real ;
                VECTOR(data,stride,b*p + p - a) = t0_imag ;
                
                VECTOR(data,stride,b*p + p_1 - a) = t1_real ;
                VECTOR(data,stride,b*p + p_1 + a) = -t1_imag ;
              }
          }
      }

      if (p_1 >  1) 
        {
          for (b = 0; b < q; b++) 
            {
              /* a = p_{i-1}/2 */
              
              VECTOR(data,stride,b*p + p - p_1/2) *= -1 ;
            }
        }
    }
  return 0;
}
Exemplo n.º 2
0
int
gsl_fft_complex_radix2_transform (double * data,
                                            const size_t stride,
                                            const size_t n,
                                            const gsl_fft_direction sign)
{
  int result ;
  size_t dual;
  size_t bit;
  size_t logn = 0;
  int status;

  if (n == 1)
    {
      return 0 ;
    }



  result = fft_binary_logn(n) ;

  if (result == -1)
    {
      do {printf("ERROR fft\n"); return 1; } while (0);
    }
  else
    {
      logn = result ;
    }



  status = fft_complex_bitreverse_order (data, stride, n, logn) ;



  dual = 1;

  for (bit = 0; bit < logn; bit++)
    {
      double w_real = 1.0;
      double w_imag = 0.0;

      const double theta = 2.0 * ((int) sign) * 3.14159265358979323846 / (2.0 * (double) dual);

      const double s = sin (theta);
      const double t = sin (theta / 2.0);
      const double s2 = 2.0 * t * t;

      size_t a, b;



      for (b = 0; b < n; b += 2 * dual)
        {
          const size_t i = b ;
          const size_t j = b + dual;

          const double z1_real = ((data)[2*(stride)*(j)]) ;
          const double z1_imag = ((data)[2*(stride)*(j)+1]) ;

          const double wd_real = z1_real ;
          const double wd_imag = z1_imag ;

          ((data)[2*(stride)*(j)]) = ((data)[2*(stride)*(i)]) - wd_real;
          ((data)[2*(stride)*(j)+1]) = ((data)[2*(stride)*(i)+1]) - wd_imag;
          ((data)[2*(stride)*(i)]) += wd_real;
          ((data)[2*(stride)*(i)+1]) += wd_imag;
        }


      for (a = 1; a < dual; a++)
        {



          {
            const double tmp_real = w_real - s * w_imag - s2 * w_real;
            const double tmp_imag = w_imag + s * w_real - s2 * w_imag;
            w_real = tmp_real;
            w_imag = tmp_imag;
          }

          for (b = 0; b < n; b += 2 * dual)
            {
              const size_t i = b + a;
              const size_t j = b + a + dual;

              const double z1_real = ((data)[2*(stride)*(j)]) ;
              const double z1_imag = ((data)[2*(stride)*(j)+1]) ;

              const double wd_real = w_real * z1_real - w_imag * z1_imag;
              const double wd_imag = w_real * z1_imag + w_imag * z1_real;

              ((data)[2*(stride)*(j)]) = ((data)[2*(stride)*(i)]) - wd_real;
              ((data)[2*(stride)*(j)+1]) = ((data)[2*(stride)*(i)+1]) - wd_imag;
              ((data)[2*(stride)*(i)]) += wd_real;
              ((data)[2*(stride)*(i)+1]) += wd_imag;
            }
        }
      dual *= 2;
    }

  return 0;

}
Exemplo n.º 3
0
int
FUNCTION(gsl_fft_halfcomplex,radix2_transform) (BASE data[],
                                                const size_t stride,
                                                const size_t n)
{
  int result ;
  size_t p, p_1, q;
  size_t i; 
  size_t logn = 0;
  int status;

  if (n == 1) /* identity operation */
    {
      return 0 ;
    }

  /* make sure that n is a power of 2 */

  result = fft_binary_logn(n) ;

  if (result == -1) 
    {
      GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
    } 
  else 
    {
      logn = result ;
    }

  /* apply fft recursion */

  p = n; q = 1 ; p_1 = n/2 ;

  for (i = 1; i <= logn; i++)
    {
      size_t a, b;

      /* a = 0 */

      for (b = 0; b < q; b++)
        {
          const ATOMIC z0 = VECTOR(data,stride,b*p);
          const ATOMIC z1 = VECTOR(data,stride,b*p + p_1);
          
          const ATOMIC t0_real = z0 + z1 ;
          const ATOMIC t1_real = z0 - z1 ;
          
          VECTOR(data,stride,b*p) = t0_real;
          VECTOR(data,stride,b*p + p_1) = t1_real ;
        }

      /* a = 1 ... p_{i-1}/2 - 1 */

      {
        ATOMIC w_real = 1.0;
        ATOMIC w_imag = 0.0;

        const ATOMIC theta = 2.0 * M_PI / p;
        
        const ATOMIC s = sin (theta);
        const ATOMIC t = sin (theta / 2.0);
        const ATOMIC s2 = 2.0 * t * t;
        
        for (a = 1; a < (p_1)/2; a++)
          {
            /* trignometric recurrence for w-> exp(i theta) w */
            
            {
              const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
              const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
              w_real = tmp_real;
              w_imag = tmp_imag;
            }
            
            for (b = 0; b < q; b++)
              {
                ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
                ATOMIC z0_imag = VECTOR(data,stride,b*p + p - a) ;
                ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 - a) ;
                ATOMIC z1_imag = -VECTOR(data,stride,b*p + p_1 + a) ;
                
                /* t0 = z0 + z1 */
                
                ATOMIC t0_real = z0_real + z1_real;
                ATOMIC t0_imag = z0_imag + z1_imag;
                
                /* t1 = (z0 - z1) */
                
                ATOMIC t1_real = z0_real -  z1_real;
                ATOMIC t1_imag = z0_imag -  z1_imag;
                
                VECTOR(data,stride,b*p + a) = t0_real ;
                VECTOR(data,stride,b*p + p_1 - a) = t0_imag ;
                
                VECTOR(data,stride,b*p + p_1 + a) = (w_real * t1_real - w_imag * t1_imag) ;
                VECTOR(data,stride,b*p + p - a) = (w_real * t1_imag + w_imag * t1_real) ;
              }
          }
      }

      if (p_1 >  1) {
        for (b = 0; b < q; b++) {
          VECTOR(data,stride,b*p + p_1/2) *= 2 ;
          VECTOR(data,stride,b*p + p_1 + p_1/2) *= -2 ;
        }
      }

      p_1 = p_1 / 2 ;
      p = p / 2 ;
      q = q * 2 ;
    }

  /* bit reverse the ordering of output data for decimation in
     frequency algorithm */
  
  status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;

  return 0;

}