[PD-cvs] externals/fftease/lib MSPd.h, NONE, 1.1 PenroseOscil.c, NONE, 1.1 PenroseOscil.h, NONE, 1.1 PenroseRand.c, NONE, 1.1 PenroseRand.h, NONE, 1.1 bloscbank.c, NONE, 1.1 convert.c, NONE, 1.1 fft.c, NONE, 1.1 fft4.c, NONE, 1.1 fftease.h, NONE, 1.1 fftease_setup.c, NONE, 1.1 fold.c, NONE, 1.1 leanconvert.c, NONE, 1.1 leanunconvert.c, NONE, 1.1 limit_fftsize.c, NONE, 1.1 makewindows.c, NONE, 1.1 overlapadd.c, NONE, 1.1 power_of_two.c, NONE, 1.1 qsortE.c, NONE, 1.1 unconvert.c, NONE, 1.1

Hans-Christoph Steiner eighthave at users.sourceforge.net
Thu Feb 9 17:18:41 CET 2006


Update of /cvsroot/pure-data/externals/fftease/lib
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv13719/lib

Added Files:
	MSPd.h PenroseOscil.c PenroseOscil.h PenroseRand.c 
	PenroseRand.h bloscbank.c convert.c fft.c fft4.c fftease.h 
	fftease_setup.c fold.c leanconvert.c leanunconvert.c 
	limit_fftsize.c makewindows.c overlapadd.c power_of_two.c 
	qsortE.c unconvert.c 
Log Message:
got everything building and working, including building single-object/single-file objects with a shared dylib.  Now got to get it integrated into the build system

--- NEW FILE: PenroseOscil.c ---
#include <math.h>
#include "PenroseOscil.h"


float frequencyToIncrement( float samplingRate, float frequency, int bufferLength ) {

  return (frequency / samplingRate) * (float) bufferLength;
} 

void makeSineBuffer( float *buffer, int bufferLength ) {
  
  int   i;

  float myTwoPi = 8. * atan(1.);

  for ( i=0; i <= bufferLength; i++ )
    *(buffer+i) = sin( myTwoPi * ((float) i / (float) bufferLength) );

  return;
}


float bufferOscil( float *phase, float increment, float *buffer,
                   int bufferLength )
{

  float sample;

  while ( *phase > bufferLength )
    *phase -= bufferLength;

  while ( *phase < 0. )
    *phase += bufferLength;

  sample = *( buffer + (int) (*phase) );

  *phase += increment;

  return sample;
}

--- NEW FILE: leanunconvert.c ---
#include "fftease.h"

/* unconvert essentially undoes what convert does, i.e., it
  turns N2+1 PAIRS of amplitude and frequency values in
  C into N2 PAIR of complex spectrum data (in rfft format)
  in output array S; sampling rate R and interpolation factor
  I are used to recompute phase values from frequencies */

void leanunconvert( float *C, float *S, int N2 )

{
double cos(), sin();
  int		real, imag,
		amp, phase;
  register int		i;
  
  for ( i = 0; i <= N2; i++ ) {
    imag = phase = ( real = amp = i<<1 ) + 1;
    S[real] = *(C+amp) * cos( *(C+phase) );
    if ( i != N2 )
      S[imag] = -*(C+amp) * sin( *(C+phase) );
  }
}

--- NEW FILE: leanconvert.c ---
#include "fftease.h"

void leanconvert( float *S, float *C, int N2 )

{

 int		real, imag,
		amp, phase;
 float		a, b;
  int		i;
	double hypot(), atan2();
	
 for ( i = 0; i <= N2; i++ ) {
   imag = phase = ( real = amp = i<<1 ) + 1;
   a = ( i == N2 ? S[1] : S[real] );
   b = ( i == 0 || i == N2 ? 0. : S[imag] );
   C[amp] = hypot( a, b );
   C[phase] = -atan2( b, a );
 }
}

--- NEW FILE: PenroseRand.h ---

float rrand(int *seed);
float prand(int *seed);

--- NEW FILE: power_of_two.c ---

int power_of_two(int test)
{
  int limit = 8192;
  int compare = 1;
  //  post("testing what we thing is an int:%d",test);
  do {
    if(test == compare){
      //      post("good power of 2 found!");
      return 1;
    } 
    compare *= 2;
  } while (compare <= limit);
  
  return 0;
}


--- NEW FILE: convert.c ---
#include "fftease.h"


/* S is a spectrum in rfft format, i.e., it contains N real values
   arranged as real followed by imaginary values, except for first
   two values, which are real parts of 0 and Nyquist frequencies;
   convert first changes these into N/2+1 PAIRS of magnitude and
   phase values to be stored in output array C; the phases are then
   unwrapped and successive phase differences are used to compute
   estimates of the instantaneous frequencies for each phase vocoder
   analysis channel; decimation rate D and sampling rate R are used
   to render these frequency values directly in Hz. */

void convert(float *S, float *C, int N2, float *lastphase, float fundamental, float factor )
{
  float 	phase,
		phasediff;
  int 		real,
		imag,
		amp,
		freq;
  float 	a,
		b;
  int 		i;

/*  float myTWOPI, myPI; */
/*  double sin(), cos(), atan(), hypot();*/
  
/*  myTWOPI = 8.*atan(1.);
  myPI = 4.*atan(1.); */


    for ( i = 0; i <= N2; i++ ) {
      imag = freq = ( real = amp = i<<1 ) + 1;
      a = ( i == N2 ? S[1] : S[real] );
      b = ( i == 0 || i == N2 ? 0. : S[imag] );

      C[amp] = hypot( a, b );
      if ( C[amp] == 0. )
	phasediff = 0.;
      else {
	phasediff = ( phase = -atan2( b, a ) ) - lastphase[i];
	lastphase[i] = phase;
	
	while ( phasediff > PI )
	  phasediff -= TWOPI;
	while ( phasediff < -PI )
	  phasediff += TWOPI;
      }
      C[freq] = phasediff*factor + i*fundamental;
    }
}
--- NEW FILE: PenroseOscil.h ---

float frequencyToIncrement( float samplingRate, float frequency,
			    int bufferLength );

void makeSineBuffer( float *buffer, int bufferLength );

float bufferOscil( float *phase, float increment, float *buffer,
                   int bufferLength );

--- NEW FILE: MSPd.h ---
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>


#ifndef PIOVERTWO
#define PIOVERTWO 1.5707963268
#define TWOPI 6.2831853072
#endif

#if MSP
#include "ext.h"
#include "z_dsp.h"
#include "buffer.h"
#include "ext_obex.h"
#define t_floatarg double
#define resizebytes t_resizebytes
#define getbytes t_getbytes
#define freebytes t_freebytes

#endif

#if PD
#include "m_pd.h"
#define t_floatarg float
#endif

/* because Max and Pd have different ideas of what A_FLOAT is, use t_floatarg
to force consistency. Otherwise functions that look good will fail on some
hardware. Also note that Pd messages cannot accept arguments of type A_LONG. */






--- NEW FILE: bloscbank.c ---

#include "fftease.h"

void bloscbank( float *S, float *O, int D, float iD, float *lf, float *la, float *index, float *tab, 
	int len, float synt, int lo, int hi )
{
  int    amp,freq,chan, i;

  float    a,ainc,f,finc,address;
  
  for ( chan = lo; chan < hi; chan++ ) {

    freq = ( amp = ( chan << 1 ) ) + 1;
    if ( S[amp] > synt ){ 
      finc = ( S[freq] - ( f = lf[chan] ) )* iD;
      ainc = ( S[amp] - ( a = la[chan] ) )* iD;
      address = index[chan];
      for ( i = 0; i < D ; i++ ) {
	O[i] += a*tab[ (int) address ];
	    
	address += f;
	while ( address >= len )
	  address -= len;
	while ( address < 0 )
	  address += len;
	a += ainc;
	f += finc;
      }
      lf[chan] = S[freq];
      la[chan] = S[amp];
      index[chan] = address;
    }    
  }
}
--- NEW FILE: overlapadd.c ---
/*
 * input I is a folded spectrum of length N; output O and
 * synthesis window W are of length Nw--overlap-add windowed,
 * unrotated, unfolded input data into output O
 */

#include "fftease.h"

void overlapadd( float *I, int N, float *W, float *O, int Nw, int n )

{
 int i ;
    while ( n < 0 )
	n += N ;
    n %= N ;
    for ( i = 0 ; i < Nw ; i++ ) {
	O[i] += I[n]*W[i] ;
	if ( ++n == N )
	    n = 0 ;
    }
}
--- NEW FILE: PenroseRand.c ---
#include "PenroseRand.h"

float rrand(int *seed)
{
    int i = ((*seed = *seed * 1103515245 + 12345)>>16) & 077777;
    return((float)i/16384. - 1.);
}

float prand(int *seed)
{
    int i = ((*seed = *seed * 1103515245 + 12345)>>16) & 077777;
    return((float)i/32768.);
}

--- NEW FILE: fold.c ---
#include "fftease.h"

/*
 * multiply current input I by window W (both of length Nw);
 * using modulus arithmetic, fold and rotate windowed input
 * into output array O of (FFT) length N according to current
 * input time n
 */
void fold( float *I, float *W, int Nw, float *O, int N, int n )

{
 
    int i;

    for ( i = 0; i < N; i++ )
	O[i] = 0.;

    while ( n < 0 )
      	n += N;
    n %= N;
    for ( i = 0; i < Nw; i++ ) {
	      O[n] += I[i]*W[i];
      	if ( ++n == N )
	      n = 0;
    }
}

--- NEW FILE: qsortE.c ---
/* Plug-compatible replacement for UNIX qsort.
   Copyright (C) 1989 Free Software Foundation, Inc.
   Written by Douglas C. Schmidt (schmidt at ics.uci.edu)

This file is part of GNU CC.

GNU QSORT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU QSORT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU QSORT; see the file COPYING.  If not, write to
the Free the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */

/* Synched up with: FSF 19.28. */

#ifdef sparc
#include <alloca.h>
#endif

#include <stdlib.h>

/* Invoke the comparison function, returns either 0, < 0, or > 0. */
#define CMP(A,B) ((*cmp)((A),(B)))

/* Byte-wise swap two items of size SIZE. */
#define SWAP(A,B,SIZE) do {int sz = (SIZE); char *a = (A); char *b = (B); \
    do { char _temp = *a;*a++ = *b;*b++ = _temp;} while (--sz);} while (0)

/* Copy SIZE bytes from item B to item A. */
#define COPY(A,B,SIZE) {int sz = (SIZE); do { *(A)++ = *(B)++; } while (--sz); }

/* This should be replaced by a standard ANSI macro. */
#define BYTES_PER_WORD 8

/* The next 4 #defines implement a very fast in-line stack abstraction. */
#define STACK_SIZE (BYTES_PER_WORD * sizeof (long))
#define PUSH(LOW,HIGH) do {top->lo = LOW;top++->hi = HIGH;} while (0)
#define POP(LOW,HIGH)  do {LOW = (--top)->lo;HIGH = top->hi;} while (0)
#define STACK_NOT_EMPTY (stack < top)                

/* Discontinue quicksort algorithm when partition gets below this size.
   This particular magic number was chosen to work best on a Sun 4/260. */
#define MAX_THRESH 4


/* requisite prototype */

int qsortE (char *base_ptr, int total_elems, int size, int (*cmp)());



/* Stack node declarations used to store unfulfilled partition obligations. */
typedef struct 
{
  char *lo;
  char *hi;
  
} stack_node;

/* Order size using quicksort.  This implementation incorporates
   four optimizations discussed in Sedgewick:
   
   1. Non-recursive, using an explicit stack of pointer that store the 
      next array partition to sort.  To save time, this maximum amount 
      of space required to store an array of MAX_INT is allocated on the 
      stack.  Assuming a 32-bit integer, this needs only 32 * 
      sizeof (stack_node) == 136 bits.  Pretty cheap, actually.

   2. Choose the pivot element using a median-of-three decision tree.
      This reduces the probability of selecting a bad pivot value and 
      eliminates certain extraneous comparisons.

   3. Only quicksorts TOTAL_ELEMS / MAX_THRESH partitions, leaving
      insertion sort to order the MAX_THRESH items within each partition.  
      This is a big win, since insertion sort is faster for small, mostly
      sorted array segments.
   
   4. The larger of the two sub-partitions is always pushed onto the
      stack first, with the algorithm then concentrating on the
      smaller partition.  This *guarantees* no more than log (n)
      stack size is needed (actually O(1) in this case)! */
      
int qsortE (char *base_ptr, int total_elems, int size, int (*cmp)())
{
  /* Allocating SIZE bytes for a pivot buffer facilitates a better 
     algorithm below since we can do comparisons directly on the pivot. */
  char *pivot_buffer = (char *)  malloc(size);
  int   max_thresh   = MAX_THRESH * size;

  if (total_elems > MAX_THRESH)
    {
      char       *lo = base_ptr;
      char       *hi = lo + size * (total_elems - 1);
      stack_node stack[STACK_SIZE]; /* Largest size needed for 32-bit int!!! */
      stack_node *top = stack + 1;

      while (STACK_NOT_EMPTY)
        {
          char *left_ptr;
          char *right_ptr;
          {
            char *pivot = pivot_buffer;
            {
              /* Select median value from among LO, MID, and HI. Rearrange
                 LO and HI so the three values are sorted. This lowers the 
                 probability of picking a pathological pivot value and 
                 skips a comparison for both the LEFT_PTR and RIGHT_PTR. */

              char *mid = lo + size * ((hi - lo) / size >> 1);

              if (CMP (mid, lo) < 0)
                SWAP (mid, lo, size);
              if (CMP (hi, mid) < 0)
                SWAP (mid, hi, size);
              else 
                goto jump_over;
              if (CMP (mid, lo) < 0)
                SWAP (mid, lo, size);
            jump_over:
              COPY (pivot, mid, size);
              pivot = pivot_buffer;
            }
            left_ptr  = lo + size;
            right_ptr = hi - size; 

            /* Here's the famous ``collapse the walls'' section of quicksort.  
               Gotta like those tight inner loops!  They are the main reason 
               that this algorithm runs much faster than others. */
            do 
              {
                while (CMP (left_ptr, pivot) < 0)
                  left_ptr += size;

                while (CMP (pivot, right_ptr) < 0)
                  right_ptr -= size;

                if (left_ptr < right_ptr) 
                  {
                    SWAP (left_ptr, right_ptr, size);
                    left_ptr += size;
                    right_ptr -= size;
                  }
                else if (left_ptr == right_ptr) 
                  {
                    left_ptr += size;
                    right_ptr -= size;
                    break;
                  }
              } 
            while (left_ptr <= right_ptr);

          }

          /* Set up pointers for next iteration.  First determine whether
             left and right partitions are below the threshold size. If so, 
             ignore one or both.  Otherwise, push the larger partition's
             bounds on the stack and continue sorting the smaller one. */

          if ((right_ptr - lo) <= max_thresh)
            {
              if ((hi - left_ptr) <= max_thresh) /* Ignore both small partitions. */
                POP (lo, hi); 
              else              /* Ignore small left partition. */  
                lo = left_ptr;
            }
          else if ((hi - left_ptr) <= max_thresh) /* Ignore small right partition. */
            hi = right_ptr;
          else if ((right_ptr - lo) > (hi - left_ptr)) /* Push larger left partition indices. */
            {                   
              PUSH (lo, right_ptr);
              lo = left_ptr;
            }
          else                  /* Push larger right partition indices. */
            {                   
              PUSH (left_ptr, hi);
              hi = right_ptr;
            }
        }
    }

  /* Once the BASE_PTR array is partially sorted by quicksort the rest
     is completely sorted using insertion sort, since this is efficient 
     for partitions below MAX_THRESH size. BASE_PTR points to the beginning 
     of the array to sort, and END_PTR points at the very last element in
     the array (*not* one beyond it!). */

#define MIN(X,Y) ((X) < (Y) ? (X) : (Y))

  {
    char *end_ptr = base_ptr + size * (total_elems - 1);
    char *run_ptr;
    char *tmp_ptr = base_ptr;
    char *thresh  = MIN (end_ptr, base_ptr + max_thresh);

    /* Find smallest element in first threshold and place it at the
       array's beginning.  This is the smallest array element,
       and the operation speeds up insertion sort's inner loop. */

    for (run_ptr = tmp_ptr + size; run_ptr <= thresh; run_ptr += size)
      if (CMP (run_ptr, tmp_ptr) < 0)
        tmp_ptr = run_ptr;

    if (tmp_ptr != base_ptr)
      SWAP (tmp_ptr, base_ptr, size);

    /* Insertion sort, running from left-hand-side up to `right-hand-side.' 
       Pretty much straight out of the original GNU qsort routine. */

    for (run_ptr = base_ptr + size; (tmp_ptr = run_ptr += size) <= end_ptr; )
      {

        while (CMP (run_ptr, tmp_ptr -= size) < 0)
          ;

        if ((tmp_ptr += size) != run_ptr)
          {
            char *trav;

            for (trav = run_ptr + size; --trav >= run_ptr;)
              {
                char c = *trav;
                char *hi, *lo;

                for (hi = lo = trav; (lo -= size) >= tmp_ptr; hi = lo)
                  *hi = *lo;
                *hi = c;
              }
          }

      }
  }
  return 1;
}
--- NEW FILE: unconvert.c ---
#include "fftease.h"



void unconvert( float *C, float *S, int N2, float *lastphase, float fundamental, float factor )

{
  int 		i,
		real,
		imag,
		amp,
		freq;
  float 	mag,
		phase;
double sin(), cos();

    for ( i = 0; i <= N2; i++ ) {

	imag = freq = ( real = amp = i<<1 ) + 1;

	if ( i == N2 )
	  real = 1;

	mag = C[amp];
	lastphase[i] += C[freq] - i*fundamental;
	phase = lastphase[i]*factor;
	S[real] = mag*cos( phase );

	if ( i != N2 )
	  S[imag] = -mag*sin( phase );

    }

}

--- NEW FILE: fft.c ---
#include "fftease.h"




/* If forward is true, rfft replaces 2*N real data points in x with
   N complex values representing the positive frequency half of their
   Fourier spectrum, with x[1] replaced with the real part of the Nyquist
   frequency value.  If forward is false, rfft expects x to contain a
   positive frequency spectrum arranged as before, and replaces it with
   2*N real values.  N MUST be a power of 2. */

void rfft( float *x, int N, int forward )

{
  float 	c1,c2,
  		h1r,h1i,
		h2r,h2i,
		wr,wi,
		wpr,wpi,
  		temp,
		theta;
  float 	xr,xi;
  int 		i,
		i1,i2,i3,i4,
		N2p1;
  static int 	first = 1;
/*float PI, TWOPI;*/
void cfft();

    if ( first ) {

	first = 0;
    }
    theta = PI/N;
    wr = 1.;
    wi = 0.;
    c1 = 0.5;
    if ( forward ) {
	c2 = -0.5;
	cfft( x, N, forward );
	xr = x[0];
	xi = x[1];
    } else {
	c2 = 0.5;
	theta = -theta;
	xr = x[1];
	xi = 0.;
	x[1] = 0.;
    }
    wpr = -2.*pow( sin( 0.5*theta ), 2. );
    wpi = sin( theta );
    N2p1 = (N<<1) + 1;
    for ( i = 0; i <= N>>1; i++ ) {
	i1 = i<<1;
	i2 = i1 + 1;
	i3 = N2p1 - i2;
	i4 = i3 + 1;
	if ( i == 0 ) {
	    h1r =  c1*(x[i1] + xr );
	    h1i =  c1*(x[i2] - xi );
	    h2r = -c2*(x[i2] + xi );
	    h2i =  c2*(x[i1] - xr );
	    x[i1] =  h1r + wr*h2r - wi*h2i;
	    x[i2] =  h1i + wr*h2i + wi*h2r;
	    xr =  h1r - wr*h2r + wi*h2i;
	    xi = -h1i + wr*h2i + wi*h2r;
	} else {
	    h1r =  c1*(x[i1] + x[i3] );
	    h1i =  c1*(x[i2] - x[i4] );
	    h2r = -c2*(x[i2] + x[i4] );
	    h2i =  c2*(x[i1] - x[i3] );
	    x[i1] =  h1r + wr*h2r - wi*h2i;
	    x[i2] =  h1i + wr*h2i + wi*h2r;
	    x[i3] =  h1r - wr*h2r + wi*h2i;
	    x[i4] = -h1i + wr*h2i + wi*h2r;
	}
	wr = (temp = wr)*wpr - wi*wpi + wr;
	wi = wi*wpr + temp*wpi + wi;
    }
    if ( forward )
	x[1] = xr;
    else
	cfft( x, N, forward );
}

/* cfft replaces float array x containing NC complex values
   (2*NC float values alternating real, imagininary, etc.)
   by its Fourier transform if forward is true, or by its
   inverse Fourier transform if forward is false, using a
   recursive Fast Fourier transform method due to Danielson
   and Lanczos.  NC MUST be a power of 2. */

void cfft( float *x, int NC, int forward )

{
  float 	wr,wi,
		wpr,wpi,
		theta,
		scale;
  int 		mmax,
		ND,
		m,
		i,j,
		delta;

void bitreverse();

    ND = NC<<1;
    bitreverse( x, ND );
    for ( mmax = 2; mmax < ND; mmax = delta ) {
	delta = mmax<<1;
	theta = TWOPI/( forward? mmax : -mmax );
	wpr = -2.*pow( sin( 0.5*theta ), 2. );
	wpi = sin( theta );
	wr = 1.;
	wi = 0.;
	for ( m = 0; m < mmax; m += 2 ) {
	 register float rtemp, itemp;
	    for ( i = m; i < ND; i += delta ) {
		j = i + mmax;
		rtemp = wr*x[j] - wi*x[j+1];
		itemp = wr*x[j+1] + wi*x[j];
		x[j] = x[i] - rtemp;
		x[j+1] = x[i+1] - itemp;
		x[i] += rtemp;
		x[i+1] += itemp;
	    }
	    wr = (rtemp = wr)*wpr - wi*wpi + wr;
	    wi = wi*wpr + rtemp*wpi + wi;
	}
    }

/* scale output */

    scale = forward ? 1./ND : 2.;
    { register float *xi=x, *xe=x+ND;
	while ( xi < xe )
	    *xi++ *= scale;
    }
}

/* bitreverse places float array x containing N/2 complex values
   into bit-reversed order */

void bitreverse( float *x, int N )

{
  float 	rtemp,itemp;
  int 		i,j,
		m;

    for ( i = j = 0; i < N; i += 2, j += m ) {
	if ( j > i ) {
	    rtemp = x[j]; itemp = x[j+1]; /* complex exchange */
	    x[j] = x[i]; x[j+1] = x[i+1];
	    x[i] = rtemp; x[i+1] = itemp;
	}
	for ( m = N>>1; m >= 2 && j >= m; m >>= 1 )
	    j -= m;
    }
}
--- NEW FILE: fft4.c ---
#include <math.h>
#include "fftease.h"

void init_rdft(int n, int *ip, float *w)
{

  int	nw,
	nc;

  void	makewt(int nw, int *ip, float *w);
  void	makect(int nc, int *ip, float *c);

  nw = n >> 2;
  makewt(nw, ip, w);

  nc = n >> 2;
  makect(nc, ip, w + nw);

  return;
}


void rdft(int n, int isgn, float *a, int *ip, float *w)
{

  int		j,
		nw,
		nc;

  float		xi;

  void		bitrv2(int n, int *ip, float *a),
		cftsub(int n, float *a, float *w),
		rftsub(int n, float *a, int nc, float *c);

    
  nw = ip[0];
  nc = ip[1];
  
  if (isgn < 0) {
    a[1] = 0.5 * (a[1] - a[0]);
    a[0] += a[1];

    for (j = 3; j <= n - 1; j += 2) {
      a[j] = -a[j];
    }

    if (n > 4) {
      rftsub(n, a, nc, w + nw);
      bitrv2(n, ip + 2, a);
    }

    cftsub(n, a, w);

    for (j = 1; j <= n - 1; j += 2) {
      a[j] = -a[j];
    }
  }

  else {

    if (n > 4) {
      bitrv2(n, ip + 2, a);
    }

    cftsub(n, a, w);

    if (n > 4) {
      rftsub(n, a, nc, w + nw);
    }

    xi = a[0] - a[1];
    a[0] += a[1];
    a[1] = xi;
  }
}


void bitrv2(int n, int *ip, float *a)
{
  int j, j1, k, k1, l, m, m2;
  float xr, xi;
    
  ip[0] = 0;
  l = n;
  m = 1;

  while ((m << 2) < l) {
    l >>= 1;
    for (j = 0; j <= m - 1; j++) {
      ip[m + j] = ip[j] + l;
    }
    m <<= 1;
  }

  if ((m << 2) > l) {

    for (k = 1; k <= m - 1; k++) {

      for (j = 0; j <= k - 1; j++) {
	j1 = (j << 1) + ip[k];
	k1 = (k << 1) + ip[j];
	xr = a[j1];
	xi = a[j1 + 1];
	a[j1] = a[k1];
	a[j1 + 1] = a[k1 + 1];
	a[k1] = xr;
	a[k1 + 1] = xi;
      }
    }
  }

  else {
    m2 = m << 1;

    for (k = 1; k <= m - 1; k++) {

      for (j = 0; j <= k - 1; j++) {
	j1 = (j << 1) + ip[k];
	k1 = (k << 1) + ip[j];
	xr = a[j1];
	xi = a[j1 + 1];
	a[j1] = a[k1];
	a[j1 + 1] = a[k1 + 1];
	a[k1] = xr;
	a[k1 + 1] = xi;
	j1 += m2;
	k1 += m2;
	xr = a[j1];
	xi = a[j1 + 1];
	a[j1] = a[k1];
	a[j1 + 1] = a[k1 + 1];
	a[k1] = xr;
	a[k1 + 1] = xi;
      }
    }
  }
}


void cftsub(int n, float *a, float *w)
{
  int j, j1, j2, j3, k, k1, ks, l, m;
  float wk1r, wk1i, wk2r, wk2i, wk3r, wk3i;
  float x0r, x0i, x1r, x1i, x2r, x2i, x3r, x3i;
    
  l = 2;

  while ((l << 1) < n) {
    m = l << 2;

    for (j = 0; j <= l - 2; j += 2) {
      j1 = j + l;
      j2 = j1 + l;
      j3 = j2 + l;
      x0r = a[j] + a[j1];
      x0i = a[j + 1] + a[j1 + 1];
      x1r = a[j] - a[j1];
      x1i = a[j + 1] - a[j1 + 1];
      x2r = a[j2] + a[j3];
      x2i = a[j2 + 1] + a[j3 + 1];
      x3r = a[j2] - a[j3];
      x3i = a[j2 + 1] - a[j3 + 1];
      a[j] = x0r + x2r;
      a[j + 1] = x0i + x2i;
      a[j2] = x0r - x2r;
      a[j2 + 1] = x0i - x2i;
      a[j1] = x1r - x3i;
      a[j1 + 1] = x1i + x3r;
      a[j3] = x1r + x3i;
      a[j3 + 1] = x1i - x3r;
    }

    if (m < n) {
      wk1r = w[2];

      for (j = m; j <= l + m - 2; j += 2) {
	j1 = j + l;
	j2 = j1 + l;
	j3 = j2 + l;
	x0r = a[j] + a[j1];
	x0i = a[j + 1] + a[j1 + 1];
	x1r = a[j] - a[j1];
	x1i = a[j + 1] - a[j1 + 1];
	x2r = a[j2] + a[j3];
	x2i = a[j2 + 1] + a[j3 + 1];
	x3r = a[j2] - a[j3];
	x3i = a[j2 + 1] - a[j3 + 1];
	a[j] = x0r + x2r;
	a[j + 1] = x0i + x2i;
	a[j2] = x2i - x0i;
	a[j2 + 1] = x0r - x2r;
	x0r = x1r - x3i;
	x0i = x1i + x3r;
	a[j1] = wk1r * (x0r - x0i);
	a[j1 + 1] = wk1r * (x0r + x0i);
	x0r = x3i + x1r;
	x0i = x3r - x1i;
	a[j3] = wk1r * (x0i - x0r);
	a[j3 + 1] = wk1r * (x0i + x0r);
      }

      k1 = 1;
      ks = -1;

      for (k = (m << 1); k <= n - m; k += m) {
	k1++;
	ks = -ks;
	wk1r = w[k1 << 1];
	wk1i = w[(k1 << 1) + 1];
	wk2r = ks * w[k1];
	wk2i = w[k1 + ks];
	wk3r = wk1r - 2 * wk2i * wk1i;
	wk3i = 2 * wk2i * wk1r - wk1i;

	for (j = k; j <= l + k - 2; j += 2) {
	  j1 = j + l;
	  j2 = j1 + l;
	  j3 = j2 + l;
	  x0r = a[j] + a[j1];
	  x0i = a[j + 1] + a[j1 + 1];
	  x1r = a[j] - a[j1];
	  x1i = a[j + 1] - a[j1 + 1];
	  x2r = a[j2] + a[j3];
	  x2i = a[j2 + 1] + a[j3 + 1];
	  x3r = a[j2] - a[j3];
	  x3i = a[j2 + 1] - a[j3 + 1];
	  a[j] = x0r + x2r;
	  a[j + 1] = x0i + x2i;
	  x0r -= x2r;
	  x0i -= x2i;
	  a[j2] = wk2r * x0r - wk2i * x0i;
	  a[j2 + 1] = wk2r * x0i + wk2i * x0r;
	  x0r = x1r - x3i;
	  x0i = x1i + x3r;
	  a[j1] = wk1r * x0r - wk1i * x0i;
	  a[j1 + 1] = wk1r * x0i + wk1i * x0r;
	  x0r = x1r + x3i;
	  x0i = x1i - x3r;
	  a[j3] = wk3r * x0r - wk3i * x0i;
	  a[j3 + 1] = wk3r * x0i + wk3i * x0r;
	}
      }
    }

    l = m;
  }

  if (l < n) {

    for (j = 0; j <= l - 2; j += 2) {
      j1 = j + l;
      x0r = a[j] - a[j1];
      x0i = a[j + 1] - a[j1 + 1];
      a[j] += a[j1];
      a[j + 1] += a[j1 + 1];
      a[j1] = x0r;
      a[j1 + 1] = x0i;
    }
  }
}


void rftsub(int n, float *a, int nc, float *c)
{
  int j, k, kk, ks;
  float wkr, wki, xr, xi, yr, yi;
    
  ks = (nc << 2) / n;
  kk = 0;

  for (k = (n >> 1) - 2; k >= 2; k -= 2) {
    j = n - k;
    kk += ks;
    wkr = 0.5 - c[kk];
    wki = c[nc - kk];
    xr = a[k] - a[j];
    xi = a[k + 1] + a[j + 1];
    yr = wkr * xr - wki * xi;
    yi = wkr * xi + wki * xr;
    a[k] -= yr;
    a[k + 1] -= yi;
    a[j] += yr;
    a[j + 1] -= yi;
  }
}


void makewt(int nw, int *ip, float *w)
{
    void bitrv2(int n, int *ip, float *a);
    int nwh, j;
    float delta, x, y;
    
    ip[0] = nw;
    ip[1] = 1;
    if (nw > 2) {
        nwh = nw >> 1;
        delta = atan(1.0) / nwh;
        w[0] = 1;
        w[1] = 0;
        w[nwh] = cos(delta * nwh);
        w[nwh + 1] = w[nwh];
        for (j = 2; j <= nwh - 2; j += 2) {
            x = cos(delta * j);
            y = sin(delta * j);
            w[j] = x;
            w[j + 1] = y;
            w[nw - j] = y;
            w[nw - j + 1] = x;
        }
        bitrv2(nw, ip + 2, w);
    }
}


void makect(int nc, int *ip, float *c)
{
    int nch, j;
    float delta;
    
    ip[1] = nc;
    if (nc > 1) {
        nch = nc >> 1;
        delta = atan(1.0) / nch;
        c[0] = 0.5;
        c[nch] = 0.5 * cos(delta * nch);
        for (j = 1; j <= nch - 1; j++) {
            c[j] = 0.5 * cos(delta * j);
            c[nc - j] = 0.5 * sin(delta * j);
        }
    }
}

--- NEW FILE: makewindows.c ---
#include "fftease.h"

void makewindows( float *H, float *A, float *S, int Nw, int N, int I )

{
 int i ;
 float sum ;

    for ( i = 0 ; i < Nw ; i++ )
	H[i] = A[i] = S[i] = 0.54 - 0.46*cos( TWOPI*i/(Nw - 1) ) ;

    if ( Nw > N ) {
     float x ;

	x = -(Nw - 1)/2. ;
	for ( i = 0 ; i < Nw ; i++, x += 1. )
	    if ( x != 0. ) {
		A[i] *= N*sin( PI*x/N )/(PI*x) ;
		if ( I )
		    S[i] *= I*sin( PI*x/I )/(PI*x) ;
	    }
    }

    for ( sum = i = 0 ; i < Nw ; i++ )
	sum += A[i] ;

    for ( i = 0 ; i < Nw ; i++ ) {
     float afac = 2./sum ;
     float sfac = Nw > N ? 1./afac : afac ;
	A[i] *= afac ;
	S[i] *= sfac ;
    }

    if ( Nw <= N && I ) {
	for ( sum = i = 0 ; i < Nw ; i += I )
	    sum += S[i]*S[i] ;
	for ( sum = 1./sum, i = 0 ; i < Nw ; i++ )
	    S[i] *= sum ;
    }
}

void makehamming( float *H, float *A, float *S, int Nw, int N, int I, int odd )

{
 int i;
 float sum ;


 
 if (odd) {
    for ( i = 0 ; i < Nw ; i++ )
	  H[i] = A[i] = S[i] = sqrt(0.54 - 0.46*cos( TWOPI*i/(Nw - 1) ));
 }
	
 else {

   for ( i = 0 ; i < Nw ; i++ )
	  H[i] = A[i] = S[i] = 0.54 - 0.46*cos( TWOPI*i/(Nw - 1) );

 }
 	
    if ( Nw > N ) {
     float x ;

	x = -(Nw - 1)/2. ;
	for ( i = 0 ; i < Nw ; i++, x += 1. )
	    if ( x != 0. ) {
		A[i] *= N*sin( PI*x/N )/(PI*x) ;
		if ( I )
		    S[i] *= I*sin( PI*x/I )/(PI*x) ;
	    }
    }
    for ( sum = i = 0 ; i < Nw ; i++ )
	sum += A[i] ;

    for ( i = 0 ; i < Nw ; i++ ) {
     float afac = 2./sum ;
     float sfac = Nw > N ? 1./afac : afac ;
	A[i] *= afac ;
	S[i] *= sfac ;
    }

    if ( Nw <= N && I ) {
	for ( sum = i = 0 ; i < Nw ; i += I )
	    sum += S[i]*S[i] ;
	for ( sum = 1./sum, i = 0 ; i < Nw ; i++ )
	    S[i] *= sum ;
    }
}

  
void makehanning( float *H, float *A, float *S, int Nw, int N, int I, int odd )
{
 int i;
 float sum ;
 
 
 if (odd) {
    for ( i = 0 ; i < Nw ; i++ )
	  H[i] = A[i] = S[i] = sqrt(0.5 * (1. + cos(PI + TWOPI * i / (Nw - 1))));
 }
	
 else {

   for ( i = 0 ; i < Nw ; i++ )
	  H[i] = A[i] = S[i] = 0.5 * (1. + cos(PI + TWOPI * i / (Nw - 1)));

 }
 	
    if ( Nw > N ) {
     float x ;

	x = -(Nw - 1)/2. ;
	for ( i = 0 ; i < Nw ; i++, x += 1. )
	    if ( x != 0. ) {
		A[i] *= N*sin( PI*x/N )/(PI*x) ;
		if ( I )
		    S[i] *= I*sin( PI*x/I )/(PI*x) ;
	    }
    }
    for ( sum = i = 0 ; i < Nw ; i++ )
	sum += A[i] ;

    for ( i = 0 ; i < Nw ; i++ ) {
     float afac = 2./sum ;
     float sfac = Nw > N ? 1./afac : afac ;
	A[i] *= afac ;
	S[i] *= sfac ;
    }

    if ( Nw <= N && I ) {
	for ( sum = i = 0 ; i < Nw ; i += I )
	    sum += S[i]*S[i] ;
	for ( sum = 1./sum, i = 0 ; i < Nw ; i++ )
	    S[i] *= sum ;
    }
}
--- NEW FILE: limit_fftsize.c ---
#include "fftease.h"

//extern void post(const char *fmt, ...);

void limit_fftsize(int *N, int *Nw, char *OBJECT_NAME)
{
	if(*N > MAX_N){
//		post("%s: N set to maximum FFT size of %d",OBJECT_NAME,MAX_N); 
		printf("%s: N set to maximum FFT size of %d",OBJECT_NAME,MAX_N); 
		*N = MAX_N;
	}
	if(*Nw > MAX_Nw){
//		post("%s: Nw set to maximum window size of %d",OBJECT_NAME,MAX_Nw); 
		printf("%s: Nw set to maximum window size of %d",OBJECT_NAME,MAX_Nw); 
		*Nw = MAX_Nw;
	}
}

--- NEW FILE: fftease_setup.c ---

#include "m_pd.h"
#include <stdio.h>

void fftease_setup(void)
{
//  post("Loaded FFTease Library");
  printf("Loaded FFTease Library(2)");
}

--- NEW FILE: fftease.h ---
#include <stdio.h>
#include <math.h>
#include <string.h>

#define FORWARD 1
#define INVERSE 0

#ifndef PI
#define PI 3.141592653589793115997963468544185161590576171875 
#endif

#ifndef TWOPI
#define TWOPI 6.28318530717958623199592693708837032318115234375
#endif

#define FFTEASE_ANNOUNCEMENT "- a member of FFTease 2.5\nby Eric Lyon and Christopher Penrose"

#define MAX_N (16384)
#define MAX_Nw (MAX_N * 4)
#define MAX_N2 (MAX_N/2)

void rfft( float *x, int N, int forward );
void cfft( float *x, int NC, int forward );
void convert(float *S, float *C, int N2, float *lastphase, float fundamental, float factor );
void unconvert( float *C, float *S, int N2, float *lastphase, float fundamental,  float factor );
void leanconvert( float *S, float *C, int N2 );
void leanunconvert( float *C, float *S, int N2 );
void makewindows( float *H, float *A, float *S, int Nw, int N, int I );
void makehamming( float *H, float *A, float *S, int Nw, int N, int I,int odd );
void makehanning( float *H, float *A, float *S, int Nw, int N, int I,int odd );
void fold( float *I, float *W, int Nw, float *O, int N, int n );
void overlapadd( float *I, int N, float *W, float *O, int Nw, int n );
void bitreverse( float *x, int N );
void bloscbank( float *S, float *O, int D, float iD, float *lf, float *la, float *bindex, float *tab, 
	int len, float synt, int lo, int hi );
/* fast fft calls */
void makect(int nc, int *ip, float *c);
void makewt(int nw, int *ip, float *w);
void rftsub(int n, float *a, int nc, float *c);
void cftsub(int n, float *a, float *w);
void bitrv2(int n, int *ip, float *a);
void rdft(int n, int isgn, float *a, int *ip, float *w);
void init_rdft(int n, int *ip, float *w);
/* rands */
float randf( float min, float max );
int randi( int min, int max );
int power_of_two(int test);

void limit_fftsize(int *N, int *Nw, char *OBJECT_NAME);






More information about the Pd-cvs mailing list