import ChaosDemos.*;
import java.awt.*;
import java.util.*;

//********************************************************************
/**                    
* Diagnostics for Map1D <br>
* Plots power spectrum.<br>
* Three different windowing functions can be used, set by WinNum:
* <UL><LI>0=NONE
* <LI>1=BARTLETT
* <LI>2=WELCH
* <LI>3=HANN</UL>
* @version 10 October 1997
* @author Michael Cross
*/
//********************************************************************  

public class Map1DFourier extends Map1DDiagnostics {
    
/** number of data points for power spectrum (must be power of 2!) and histogram */
      private int dataLength ;
/** 2*dataLength */
      private int dataLength2;
/** index point in accumulating Fourier data */      
      private int fourierIndex;
/** windowing in power spectrum */
      private int winNum=0; 
/** array for plotting power spectrum and histogram */
      private double[] plotData;     
/** floor for Fourier transform, so don't take log(0) */      
      private double FLOOR=1.e-12;            
/** class for performing FFT */
      private powerSpectrum mySpectrum;      
      

//******************************************************************
/**
* @param inParent parent class
* @see Map1D
*/
//******************************************************************
      public Map1DFourier(Map1D inParent) {
            super(inParent);
            parameters = new double[parent.nParameters];                                                                                         
      }
      
//********************************************************************
/**
* Updates parameters from the text controls in parent class 
*/ 
//********************************************************************
      public void updateParameters() {
            int i;           
                                 
            x0=parent.variables.parseTextField(5,x0);
            x=x0;
            ntrans=parent.variables.parseTextField(3,ntrans);
            if(ntrans>0) runTrans=true;                                                

            nf=parent.variables.parseTextField(2,nf,true);
            parent.mapFunction.setCompose(nf);
            
            dataLength=parent.variables.parseTextField(4,256,true);
            dataLength2=2*dataLength;
            // Check for power of 2
            int test=dataLength;
            int power=0;
            while(test>1) {
                  test=test/2;
                  power++;
            }
            test=1;
            for(i=0;i<power;i++) test=test*2;
            if(test!=dataLength) {
                  alertDialog alert = new alertDialog
                      (parent," No. of points must be power of two ");
                  dataLength=test;
                  dataLength2=2*dataLength;
                  parent.variables.setText(4,String.valueOf(dataLength));
            }
            winNum=parent.variables.parseTextField(6,winNum,0,3);            
                                                 
      }      
                
//*********************************************************************
/**
* Restarts
*/
//*********************************************************************      
      public void restart() {

            double data1[] = new double[6];
            double data2[] = new double[6];
            int i,j;
            double xSave;
            double tSave;
            double f;
            int ip=0;

            iterations=0;
            
            // Eliminate transient
            if(runTrans) {
                for(i=0;i<ntrans;i++) 
                    x=parent.mapFunction.iterate(x);                   
                runTrans=false;
                iterations+=ntrans;        
            }
            xSave=x;                        
            
            // Reset graph
            ncurve=parent.resetGraph();
                        
            // Set up power spectrum
            plotData = new double[dataLength2];
            double scale=2./(nf*dataLength);                                    
            mySpectrum = new powerSpectrum(dataLength, winNum, scale);
            mySpectrum.setFloor(FLOOR);   
            
            // Plot first spectrum
            fourierIndex=0;
            parent.status.setText("Calculating spectrum");
            for(i=0;i<dataLength;i++) {
               x=parent.mapFunction.iterate(x);                   
               plotData[fourierIndex]=x;
               fourierIndex++;
               plotData[fourierIndex]=0;
               fourierIndex++;
            }                                  
            iterations+=nf*dataLength;
            parent.status.setText("No. of Iterations "+ iterations);                                                             
            mySpectrum.transform(plotData);
            parent.graph.paintAll=false;
            if(ncurve>=0) ncurve = parent.graph.deleteAllCurves();
            ncurve = parent.graph.addCurve(plotData,1+dataLength/2,Color.blue); 
            ncurve1=ncurve;
            parent.graph.paintAll=true; 
            parent.graph.clearAll=true; 
            parent.graph.repaint(); 
//            parent.graph.repaint(delay/2);                                         

            // Reset spectrum
            mySpectrum = new powerSpectrum(dataLength, winNum, scale);
            mySpectrum.setFloor(FLOOR);
            fourierIndex=0;    

            if(parent.showTime) parent.status.setText("No. of Iterations "+ iterations);            
      }

//*********************************************************************
/**
* Iterates Map equations and updates graph according to value of plot 
*/
//*********************************************************************
      
      public boolean iterate() {
            int i,j;
            double[] moredata = new double[6];
            double[] moredata1 = new double[6];

            x=parent.mapFunction.iterate(x);                   
            plotData[fourierIndex]=x;
            fourierIndex++;
            plotData[fourierIndex]=0;
            fourierIndex++;                               
            iterations+=nf;
            if(parent.showTime)
                  parent.status.setText("No. of Iterations "+ iterations);                                                             
            if(fourierIndex>=dataLength2){
               mySpectrum.transform(plotData);
               parent.graph.paintAll=false;
//               if(ncurve>=0) ncurve = graph.deleteAllCurves();
               parent.graph.appendToCurve(plotData,1+dataLength/2,ncurve1); 
               parent.graph.deleteFromCurve(1+dataLength/2,ncurve1);
               parent.graph.paintAll=true; 
               parent.graph.repaint();
//               parent.graph.repaint(delay/2);                              
//               nplot++;
               fourierIndex=0;
            }                 
            return true;
      }
      
//**********************************************************************
/**
* Sets default values of parameters
*/
//**********************************************************************          
      public void setPlotDefaults() {                                                                                                     

             String[] label={"Transient","Points","Start x","Window"};
             String[] text={"64","256","0.2","0"};
             parent.setPlotTextBoxes(label,text);               
 
             parent.choices.enable(0);           
      }                 
            
//**********************************************************************
}      
