Source Code
Infrared Signal Coding Schemes
home top contents previous up next

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;


final public class SimpleTrains extends Content{
	public SimpleTrains(Dimension dim, Image img, String problemName){
		super(dim,img,problemName);
    }

	protected void initPreconstructor_Variables(){
		modelDescription=new String[]{
				"Program 11. Model:     Simple Trains - Different Power Spectrum Density Algorithms."
	    };

        DAPPM_L=4;
        DAPPM_A=2;
        PPM_L=8;

        //do1? automate:
        DAPPM_CORR_LIM=1000;
        DAPPM_LIM=40;
		
		dmnStartF=0.0;
		dmnRangeF=10.0;
		dmnRangeFDown=3.0;
        grStartF=500;
        
		dmnStartX=0.01;
		dmnRangeX=2.0;

		functionCOUNT=4;
    	functionColor=new Color[]{
    			new Color(255,0,0),
    			new Color(0,0,255),
    			new Color(0,100,100),
    			new Color(50,200,50)
    	};
        functionTitle=new String[]{
        	"OOK Normalized",
        	"PPM",
        	"PPMFast",
        	"DAPPM Normalized"
        };
        
	}
	
	//-----------------------------------------------
    // User Input Output Prompts
    //- - - - - - - - - - - - - - - - - - - - - - - - 
    int DAPPM_L;
    int DAPPM_A;
    int PPM_L;
    protected int setParsToStrings(){
         int i=0;
         strParsCrr[i][0]=String.valueOf(dmnRangeX);  strParsCrr[i][1]="Range of fTc"; strParsCrr[i++][2]="double"; 
         strParsCrr[i][0]=String.valueOf(dmnRangeF);  strParsCrr[i][1]="Function Range"; strParsCrr[i++][2]="double";
         strParsCrr[i][0]=String.valueOf(dmnStartF);  strParsCrr[i][1]="Function Start"; strParsCrr[i++][2]="double";
         strParsCrr[i][0]=String.valueOf(PPM_L);   strParsCrr[i][1]="PPM L - chips in symbol"; strParsCrr[i++][2]="int"; 
         strParsCrr[i][0]=String.valueOf(DAPPM_L);   strParsCrr[i][1]="DAPPM L"; strParsCrr[i++][2]="int"; 
         strParsCrr[i][0]=String.valueOf(DAPPM_A);   strParsCrr[i][1]="DAPPM A"; strParsCrr[i++][2]="int";
         strParsCrr[i][0]=String.valueOf(DAPPM_LIM);   strParsCrr[i][1]="DAPPM limit of d -  maximum concerned chip correlation distance"; strParsCrr[i++][2]="int";
         return i;
    }
    public String setParsFromStrings(){
         int i=0;
         try{
        	 dmnRangeX =Double.parseDouble(strParsCrr[i][0]);  i++;
        	 dmnRangeF =Double.parseDouble(strParsCrr[i][0]);  i++;
        	 dmnStartF =Double.parseDouble(strParsCrr[i][0]);  i++;
        	 PPM_L =Integer.parseInt(strParsCrr[i][0]);  i++;
        	 DAPPM_L =Integer.parseInt(strParsCrr[i][0]);  i++;
        	 DAPPM_A =Integer.parseInt(strParsCrr[i][0]);  i++;
        	 DAPPM_LIM=Integer.parseInt(strParsCrr[i][0]);  i++;
         }catch(Exception e){
            return "Exception when (re)setting parameters.\n" + e;    
         }
         return "";
    }
    //- - - - - - - - - - - - - - - - - - - - - - - - 
    // User Input Output Prompts
	//-----------------------------------------------
	
	private double functionPPM(double g){
		double q=Math.PI*g;
		double Pw=Math.sin(q)/(q);
		int L=PPM_L;
        //Calculate correlator:
		double WW=0.0;
		for(int d=0; d<L; d++){
			for(int ds=0; ds<L; ds++){
				WW=WW+Math.cos(2.0*q*(d-ds));  
			}
		}
		//return 8.0/(L*L*L)*Pw*Pw*(1-WW/L);
		double w=Pw*Pw*(1-WW/L);
		//con(" PPM="+w);
		return w;
	}

    private double functionPPMFast(double g){
		double q=Math.PI*g;
		double L=PPM_L;
		double Pw=Math.sin(q)/(q);
        //Calculate correlator:
		double H=0;
		for(int d=1; d<L; d++){
			H=H+(L-d)*Math.cos(2.0*q*d);  
		}
		//Spoil result by multiplying with .95:
		return 0.95  *  Pw*Pw*(1-(L+2.0*H)/L);
	}
    private double functionOOK(double g){
		double q=Math.PI*g;
		double f=Math.sin(q)/(q);
		
		//Normalization:
		// density = 1/2  amean=1.  average=1/2. a2average=1/2=r0. rinf=1/4.
		// rsubtracted = 1/4. rdivided=1:
		
		return f*f;
	}
	
    
    //===========================================================
    //  DAPPM
    //-----------------------------------------------------------
    private static int DAPPM_CORR_LIM;
    private int DAPPM_LIM;
    double[] rDAPPM;
	//Average amplitude:
	double amean;
    //Autocorrelation at infinity:
	double rinf;

	//------------------------------------------------------------
	//  Prepare DAPPM parameters:
	//  Output: normalized r.
	//------------------------------------------------------------
    protected void spawnParametersAfterUserApplied(){
    	if( rDAPPM== null) rDAPPM=new double[DAPPM_CORR_LIM];
		int A=DAPPM_A;
    	int L=DAPPM_L;
		int L1=L+1;
		double amean=(1+A)*0.5;
		double density=2.0/(L+1);
    	double aaverage=amean*density;
		rinf=aaverage*aaverage;
		con(" A="+A+" aaverage="+aaverage+" rinf="+rinf);
        //Amplitude square average:
		rDAPPM[0]=(A+1)*(2*A+1)/3.0/L1-rinf;
		
		rDAPPM[0]=rDAPPM[0]/rinf; //Normalize.
		
		con("d=0 rDAPPM="+rDAPPM[0]);
		int L1Power=1;
		int LPower=1;
		//Calculate chip correlation:
		for(int k=1; k<=DAPPM_L; k++){
			L1Power=L1Power*L1;
			LPower=LPower*L;
			double mu=1.0*L1Power/LPower/L1;
			
			//rDAPPM[k]= rinf  *  mu / density - rinf;
			rDAPPM[k]= mu / density - 1.0;   //Normalized.
			
			//con("d="+k+" rDAPPM="+rDAPPM[k]);
		}
		//Summarize probabilities for correlation at distance k>L:
		for(int k=L1; k<DAPPM_CORR_LIM; k++){
			double sum=0.0;
			for(int j=k-L; j<k; j++){
				sum=sum+rDAPPM[j];
			}
			//Take average:
			rDAPPM[k]=sum/L;
			//con("d="+k+" rDAPPM="+rDAPPM[k]);
		}
    }

    private double functionDAPPM(double g){
		double q=Math.PI*g;
		double Pw=Math.sin(q)/q;
		double sumr=rDAPPM[0];
		int safeLimit=Math.min(DAPPM_CORR_LIM-1,DAPPM_LIM);
		for(int d=1; d<=safeLimit; d++){
			sumr=sumr+rDAPPM[d]*2*Math.cos(2.0*q*d);
		}
		sumr=Pw*Pw*sumr;
		//con("g="+g+" DAPPM="+sumr);
		return sumr;
	}
    //-----------------------------------------------------------
    //  DAPPM
    //===========================================================

    protected double functionSwitchX(int fIx, double t) {return 0;}
	protected double functionSwitch(int fIx, double x){
		    switch(fIx){
		    case 0: return functionOOK(x);
		    case 1: return functionPPM(x);
		    case 2: return functionPPMFast(x);
		    case 3: return functionDAPPM(x);
		    }
		    return 0;
	}
}


Copyright (C) 2009 Konstantin Kirillov