#include "common.h"
#include "vector.h"

/***************************************************************

      FUNCTION: split_odd_even

      This rearranges an array.  The elements that were initially
      even numbered are packed in bottom half, and the initially
      odd elements are moved to the top half (the end).  This
      serves to convert a complex array as used by Numerical
      Recipes routines into separate real and imaginary arrays,
      suitable for passing into Matlab as a complex array.

***************************************************************/

#if PROTOTYPE_ALLOWED
void split_odd_even(float *a, int n)
#else
void split_odd_even(a, n)
   float *a;
   int n;
#endif
{
   float *re, *im;
   int i, nh;

   nh = n/2;
   if (nh*2 != n)
   {
      printf("\nERROR in split_odd_even: n = %d is odd.\n", n);
      exit(-1);
   }
   re = (float *) calloc(nh, sizeof(float));
   im = (float *) calloc(nh, sizeof(float));
   if (re == NULL || im == NULL)
   {
      printf("\nERROR in split_odd_even: out of memory.\n");
      exit(-1);
   }

   for (i=0; i<nh; i++)
   {
      re[i] = a[i+i];
      im[i] = a[i+i+1];
   }
   for (i=0; i<nh; i++)
   {
      a[i] = re[i];
      a[i+nh] = im[i];
   }
   free(im);
   free(re);
}                       /* split_odd_even */
