Blame fft/hc_radix2.c

Packit 67cb25
/* fft/hc_radix2.c
Packit 67cb25
 * 
Packit 67cb25
 * Copyright (C) 1996, 1997, 1998, 1999, 2000, 2007 Brian Gough
Packit 67cb25
 * 
Packit 67cb25
 * This program is free software; you can redistribute it and/or modify
Packit 67cb25
 * it under the terms of the GNU General Public License as published by
Packit 67cb25
 * the Free Software Foundation; either version 3 of the License, or (at
Packit 67cb25
 * your option) any later version.
Packit 67cb25
 * 
Packit 67cb25
 * This program is distributed in the hope that it will be useful, but
Packit 67cb25
 * WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 67cb25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 67cb25
 * General Public License for more details.
Packit 67cb25
 * 
Packit 67cb25
 * You should have received a copy of the GNU General Public License
Packit 67cb25
 * along with this program; if not, write to the Free Software
Packit 67cb25
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Packit 67cb25
 */
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
FUNCTION(gsl_fft_halfcomplex,radix2_backward) (BASE data[],
Packit 67cb25
                                               const size_t stride,
Packit 67cb25
                                               const size_t n)
Packit 67cb25
{
Packit 67cb25
  int status = FUNCTION(gsl_fft_halfcomplex,radix2_transform) (data, stride, n) ;
Packit 67cb25
  return status ;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
FUNCTION(gsl_fft_halfcomplex,radix2_inverse) (BASE data[],
Packit 67cb25
                                              const size_t stride,
Packit 67cb25
                                              const size_t n)
Packit 67cb25
{
Packit 67cb25
  int status = FUNCTION(gsl_fft_halfcomplex,radix2_transform) (data, stride, n);
Packit 67cb25
Packit 67cb25
  if (status)
Packit 67cb25
    {
Packit 67cb25
      return status;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* normalize inverse fft with 1/n */
Packit 67cb25
Packit 67cb25
  {
Packit 67cb25
    const ATOMIC norm = 1.0 / n;
Packit 67cb25
    size_t i;
Packit 67cb25
    for (i = 0; i < n; i++)
Packit 67cb25
      {
Packit 67cb25
        data[stride*i] *= norm;
Packit 67cb25
      }
Packit 67cb25
  }
Packit 67cb25
  return status;
Packit 67cb25
}
Packit 67cb25
Packit 67cb25
int
Packit 67cb25
FUNCTION(gsl_fft_halfcomplex,radix2_transform) (BASE data[],
Packit 67cb25
                                                const size_t stride,
Packit 67cb25
                                                const size_t n)
Packit 67cb25
{
Packit 67cb25
  int result ;
Packit 67cb25
  size_t p, p_1, q;
Packit 67cb25
  size_t i; 
Packit 67cb25
  size_t logn = 0;
Packit 67cb25
  int status;
Packit 67cb25
Packit 67cb25
  if (n == 1) /* identity operation */
Packit 67cb25
    {
Packit 67cb25
      return 0 ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* make sure that n is a power of 2 */
Packit 67cb25
Packit 67cb25
  result = fft_binary_logn(n) ;
Packit 67cb25
Packit 67cb25
  if (result == -1) 
Packit 67cb25
    {
Packit 67cb25
      GSL_ERROR ("n is not a power of 2", GSL_EINVAL);
Packit 67cb25
    } 
Packit 67cb25
  else 
Packit 67cb25
    {
Packit 67cb25
      logn = result ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* apply fft recursion */
Packit 67cb25
Packit 67cb25
  p = n; q = 1 ; p_1 = n/2 ;
Packit 67cb25
Packit 67cb25
  for (i = 1; i <= logn; i++)
Packit 67cb25
    {
Packit 67cb25
      size_t a, b;
Packit 67cb25
Packit 67cb25
      /* a = 0 */
Packit 67cb25
Packit 67cb25
      for (b = 0; b < q; b++)
Packit 67cb25
        {
Packit 67cb25
          const ATOMIC z0 = VECTOR(data,stride,b*p);
Packit 67cb25
          const ATOMIC z1 = VECTOR(data,stride,b*p + p_1);
Packit 67cb25
          
Packit 67cb25
          const ATOMIC t0_real = z0 + z1 ;
Packit 67cb25
          const ATOMIC t1_real = z0 - z1 ;
Packit 67cb25
          
Packit 67cb25
          VECTOR(data,stride,b*p) = t0_real;
Packit 67cb25
          VECTOR(data,stride,b*p + p_1) = t1_real ;
Packit 67cb25
        }
Packit 67cb25
Packit 67cb25
      /* a = 1 ... p_{i-1}/2 - 1 */
Packit 67cb25
Packit 67cb25
      {
Packit 67cb25
        ATOMIC w_real = 1.0;
Packit 67cb25
        ATOMIC w_imag = 0.0;
Packit 67cb25
Packit 67cb25
        const ATOMIC theta = 2.0 * M_PI / p;
Packit 67cb25
        
Packit 67cb25
        const ATOMIC s = sin (theta);
Packit 67cb25
        const ATOMIC t = sin (theta / 2.0);
Packit 67cb25
        const ATOMIC s2 = 2.0 * t * t;
Packit 67cb25
        
Packit 67cb25
        for (a = 1; a < (p_1)/2; a++)
Packit 67cb25
          {
Packit 67cb25
            /* trignometric recurrence for w-> exp(i theta) w */
Packit 67cb25
            
Packit 67cb25
            {
Packit 67cb25
              const ATOMIC tmp_real = w_real - s * w_imag - s2 * w_real;
Packit 67cb25
              const ATOMIC tmp_imag = w_imag + s * w_real - s2 * w_imag;
Packit 67cb25
              w_real = tmp_real;
Packit 67cb25
              w_imag = tmp_imag;
Packit 67cb25
            }
Packit 67cb25
            
Packit 67cb25
            for (b = 0; b < q; b++)
Packit 67cb25
              {
Packit 67cb25
                ATOMIC z0_real = VECTOR(data,stride,b*p + a) ;
Packit 67cb25
                ATOMIC z0_imag = VECTOR(data,stride,b*p + p - a) ;
Packit 67cb25
                ATOMIC z1_real = VECTOR(data,stride,b*p + p_1 - a) ;
Packit 67cb25
                ATOMIC z1_imag = -VECTOR(data,stride,b*p + p_1 + a) ;
Packit 67cb25
                
Packit 67cb25
                /* t0 = z0 + z1 */
Packit 67cb25
                
Packit 67cb25
                ATOMIC t0_real = z0_real + z1_real;
Packit 67cb25
                ATOMIC t0_imag = z0_imag + z1_imag;
Packit 67cb25
                
Packit 67cb25
                /* t1 = (z0 - z1) */
Packit 67cb25
                
Packit 67cb25
                ATOMIC t1_real = z0_real -  z1_real;
Packit 67cb25
                ATOMIC t1_imag = z0_imag -  z1_imag;
Packit 67cb25
                
Packit 67cb25
                VECTOR(data,stride,b*p + a) = t0_real ;
Packit 67cb25
                VECTOR(data,stride,b*p + p_1 - a) = t0_imag ;
Packit 67cb25
                
Packit 67cb25
                VECTOR(data,stride,b*p + p_1 + a) = (w_real * t1_real - w_imag * t1_imag) ;
Packit 67cb25
                VECTOR(data,stride,b*p + p - a) = (w_real * t1_imag + w_imag * t1_real) ;
Packit 67cb25
              }
Packit 67cb25
          }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
      if (p_1 >  1) {
Packit 67cb25
        for (b = 0; b < q; b++) {
Packit 67cb25
          VECTOR(data,stride,b*p + p_1/2) *= 2 ;
Packit 67cb25
          VECTOR(data,stride,b*p + p_1 + p_1/2) *= -2 ;
Packit 67cb25
        }
Packit 67cb25
      }
Packit 67cb25
Packit 67cb25
      p_1 = p_1 / 2 ;
Packit 67cb25
      p = p / 2 ;
Packit 67cb25
      q = q * 2 ;
Packit 67cb25
    }
Packit 67cb25
Packit 67cb25
  /* bit reverse the ordering of output data for decimation in
Packit 67cb25
     frequency algorithm */
Packit 67cb25
  
Packit 67cb25
  status = FUNCTION(fft_real,bitreverse_order)(data, stride, n, logn) ;
Packit 67cb25
Packit 67cb25
  return 0;
Packit 67cb25
Packit 67cb25
}