fftmisc.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*============================================================================
  2. fftmisc.c - Don Cross <dcross@intersrv.com>
  3. http://www.intersrv.com/~dcross/fft.html
  4. Helper routines for Fast Fourier Transform implementation.
  5. Contains common code for fft_float() and fft_double().
  6. See also:
  7. fourierf.c
  8. fourierd.c
  9. ..\include\fourier.h
  10. Revision history:
  11. 1998 September 19 [Don Cross]
  12. Improved the efficiency of IsPowerOfTwo().
  13. Updated coding standards.
  14. ============================================================================*/
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include <math.h>
  18. #include "fourier.h"
  19. #define TRUE 1
  20. #define FALSE 0
  21. #define BITS_PER_WORD (sizeof(unsigned) * 8)
  22. int IsPowerOfTwo ( unsigned x )
  23. {
  24. if ( x < 2 )
  25. return FALSE;
  26. if ( x & (x-1) ) // Thanks to 'byang' for this cute trick!
  27. return FALSE;
  28. return TRUE;
  29. }
  30. unsigned NumberOfBitsNeeded ( unsigned PowerOfTwo )
  31. {
  32. unsigned i;
  33. if ( PowerOfTwo < 2 )
  34. {
  35. // fprintf (
  36. // stderr,
  37. // ">>> Error in fftmisc.c: argument %d to NumberOfBitsNeeded is too small.\n",
  38. // PowerOfTwo );
  39. exit(1);
  40. }
  41. for ( i=0; ; i++ )
  42. {
  43. if ( PowerOfTwo & (1 << i) )
  44. return i;
  45. }
  46. }
  47. unsigned ReverseBits ( unsigned index, unsigned NumBits )
  48. {
  49. unsigned i, rev;
  50. for ( i=rev=0; i < NumBits; i++ )
  51. {
  52. rev = (rev << 1) | (index & 1);
  53. index >>= 1;
  54. }
  55. return rev;
  56. }
  57. double Index_to_frequency ( unsigned NumSamples, unsigned Index )
  58. {
  59. if ( Index >= NumSamples )
  60. return 0.0;
  61. else if ( Index <= NumSamples/2 )
  62. return (double)Index / (double)NumSamples;
  63. return -(double)(NumSamples-Index) / (double)NumSamples;
  64. }
  65. /*--- end of file fftmisc.c---*/