package ChaosDemos; import java.util.*; /** * Calculates power spectrum using FFTs and accumulates spectrum.
* Uses routines from Numerical Recipes * @version 26 July 1997 * @author Michael Cross */ public class powerSpectrum { /* Defines windowing of data */ private static final int BARTLETT=1; private static final int WELCH=2; private static final int HANN=3; private static final double Pi2=2*Math.PI; private double[] x; private double[] spectrum; private int nSpectrum; private int winNum; private int n,nn; private double scale; private double[] data; private double[] winMult; private double floor=0.; //********************************************************************** /** * Constructor * @param in_nn number of data points : must be power of 2! * @param in_winNum window for FFT: 1=BARTLETT 2=WELCH 3=HANN other=NONE * @param in_scale scale factor to convert from index to frequency */ //********************************************************************** public powerSpectrum(int in_nn, int in_winNum, double in_scale) { nn=in_nn; winNum=in_winNum; scale=in_scale; spectrum=new double[nn/2+1]; n=2*nn; data = new double[n+1]; nSpectrum=0; winMult=new double[nn]; for(int i=0,j=1;i * On output x contains spectrum in format suitable for direct plotting in graph2D * in first in_nn positions. */ //********************************************************************** public void transform(double[] x) { int i,j; double sumWindow=nn*nn; System.arraycopy(x,0,data,1,n); if(winNum>0) { for(i=0,j=1;i=0) ncurve = graph.deleteAllCurves(); ncurve = graph.addCurve(x,nn,Color.blue); graph.paintAll=true; graph.repaint(); */ four1(data,nn,1); nSpectrum++; x[0]=0; spectrum[0]+=(data[0]*data[0]+data[1]*data[1])/sumWindow; for(i=1,j=2;i * On return contains transform
* For packing see Numerical Recipes * @param nn number of data points * @param isign 1 for forward; -1 for inverse */ //********************************************************************** private void four1(double data[], int nn, int isign) { int n,mmax,m,j,istep,i; double wtemp,wr,wpr,wpi,wi,theta; double tempr,tempi; double swap; n=2*nn; j=1; for (i=1;i i) { swap=data[j]; data[j]=data[i]; data[i]=swap; swap=data[j+1]; data[j+1]=data[i+1]; data[i+1]=swap; } m=n >> 1; while (m >= 2 && j > m) { j -= m; m >>= 1; } j += m; } mmax=2; while (n > mmax) { istep=2*mmax; theta=isign*(6.28318530717959/mmax); wtemp=Math.sin(0.5*theta); wpr = -2.0*wtemp*wtemp; wpi=Math.sin(theta); wr=1.0; wi=0.0; for (m=1;m