pqatsi

four1.py

Sep 22nd, 2013
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. from math import sin
  5.  
  6. # Replaces data[1..2*nn] by its discrete Fourier transform, if isign    */
  7. # is input as 1; or replaces data[1..2*nn] by nn times its inverse  */
  8. # discrete Fourier transform, if isign is input as -1. data is a    */
  9. # complex array of length nn or, equivalently, a real array of length   */
  10. # 2**nn. nn MUST be an integer power of 2 (this is not checked for!).   */
  11. def four1(data, nn, isign):
  12.     n=nn << 1
  13.     j=1
  14.     for i in range(1, n, 2):
  15.         if (j > i):
  16.             tmp = data[j]
  17.             data[j] = data[i]
  18.             data[i] = tmp
  19.            
  20.             tmp = data[j+1]
  21.             data[j+1] = data[i+1]
  22.             data[i+1] = tmp
  23.         m=n >> 1
  24.         while (m >= 2 and j > m):
  25.             j -= m
  26.             m >>= 1
  27.         j += m;
  28.    
  29.     mmax=2
  30.     while(n > mmax):
  31.         istep=mmax << 1
  32.         theta=isign*(6.28318503717959/mmax)
  33.         wtemp=sin(0.5*theta)
  34.         wpr = -2.0*wtemp*wtemp
  35.         wpi=sin(theta)
  36.         wr=1.0
  37.         wi=0.0
  38.         for m in range(1, mmax, 2):
  39.             for i in range(m, n+1, istep):
  40.                 j=i+mmax
  41.                 tempr=wr*data[j]-wi*data[j+1]
  42.                 tempi=wr*data[j+1]+wi*data[j]
  43.                 data[j]=data[i]-tempr
  44.                 data[j+1]=data[i+1]-tempi
  45.                 data[i] += tempr
  46.                 data[i+1]+= tempi
  47.             wtemp=wr
  48.             wr=wtemp*wpr-wi*wpi+wr
  49.             wi=wi*wpr+wtemp*wpi+wi
  50.         mmax=istep
Advertisement
Add Comment
Please, Sign In to add comment