/*      Class ImpedSpecRegression
*
*       Non-linear regression procedures for fitting impedance
*       spectroscopy and electrochemical impedance spectroscopy
*       data to user supplied circuit or one of a range of precompiled
*       circuit models.
*
*       User supplied circuit models require the interface ImpedSpecModel
*
*       WRITTEN BY: Dr Michael Thomas Flanagan
*
*       DATE:    9 June 2007  (Derived from impedance spectroscopy programs, 2004 - 2007)
*       UPDATE:  16 October 2007, 5 July 2008
*
*       DOCUMENTATION:
*       See Michael T Flanagan's Java library on-line web pages:
*       http://www.ee.ucl.ac.uk/~mflanaga/java/
*       http://www.ee.ucl.ac.uk/~mflanaga/java/ImpedSpecRegression.html
*
*       Copyright (c)  June 2007    Michael Thomas Flanagan
*
*       PERMISSION TO COPY:
*       Permission to use, copy and modify this software and its documentation for
*       NON-COMMERCIAL purposes is granted, without fee, provided that an acknowledgement
*       to the author, Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies.
*
*       Dr Michael Thomas Flanagan makes no representations about the suitability
*       or fitness of the software for any or for a particular purpose.
*       Michael Thomas Flanagan shall not be liable for any damages suffered
*       as a result of using, modifying or distributing this software or its derivatives.
*
****************************************************************************************/

package flanagan.circuits;

import flanagan.io.*;
import flanagan.complex.Complex;
import flanagan.complex.ComplexErrorProp;
import flanagan.analysis.ErrorProp;
import flanagan.analysis.Regression;
import flanagan.analysis.RegressionFunction2;
import flanagan.analysis.Stat;
import flanagan.math.Fmath;
import flanagan.math.Conv;
import flanagan.plot.*;

import java.lang.reflect.Array;
import java.text.*;
import java.util.*;
import java.lang.Object;


public class ImpedSpecRegression extends Regression{

    private String regressionTitle = null;              // Title for output graphs and text file
    private boolean fileType = false;                   // = true if 'n' number to be added to file name

    private Complex appliedVoltage = null;              // magnitude of the applied voltage as complex
    private boolean appliedVoltageSet = false;          // = true when applied voltage entered
    private Complex appliedVoltageError = null;         // error of the applied voltage as complex
    private boolean voltageErrorSet = false;            // = true when applied voltage error entered

    private Complex referenceImpedance = null;          // reference impedance
    private boolean referenceSet = false;               // = true when reference impedance entered

    private double[] frequencies = null;                // frequencies [Hz]
    private double[] omegas = null;                     // radial frequencies
    private double[] log10frequencies = null;           // log10[frequencies/Hz]
    private double[] log10omegas = null;                // log10[radial frequencies]
    private int numberOfFrequencies = 0;                // number of points in the simulation
    private boolean frequenciesSet = false;             // = true when frequencies entered

    private Complex[] voltages = null;                  // voltages
    private Complex[] voltageWeights = null;            // voltage weights
    private double[] voltageMagnitudes = null;          // magnitude of the voltages
    private double[] voltageMagnitudeWeights = null;    // voltage magnitude weights
    private double[] voltagePhasesRad = null;           // voltage phases [radians]of the voltages
    private double[] voltagePhaseWeightsRad = null;     // voltage phase weights [radians]
    private double[] voltagePhasesDeg = null;           // voltage phases [degrees]of the voltages
    private double[] voltagePhaseWeightsDeg = null;     // voltage phase weights [degrees]
    private double[] realV = null;                      // real part of the voltage
    private double[] realVweights = null;               // real part of the voltage - weights
    private double[] imagV = null;                      // imaginary part of the voltages
    private double[] imagVweights = null;               // imaginary part of the voltage - weights

    private boolean weightsSet = true;                  // = false if no weights provided

    private int dataEnteredTypePointer = -1;            // = 0; real and imag voltage
                                                        // = 1: complex voltage
                                                        // = 2: voltage magnitude and radians
                                                        // = 3: voltage magnitude and degreees
                                                        // = 4; real and imag impedance
                                                        // = 5: complex mpedance
                                                        // = 6: mpedance magnitude and radians
                                                        // = 7: mpedance magnitude and degreees
    // Entered data type
    private String[] dataEnteredType = {"Complex voltage (as real and imaginary parts)", "Complex voltage (as Complex)",  "Voltage Magnitude and phase (in radians)", "Voltage Magnitude and phase (in degrees)", "Complex impedance (as real and imaginary parts)", "Complex impedance (as Complex)", "Magnitude and phase (in radians)", "Magnitude and phase (in degrees)"};
    private boolean voltageOrImpedance = true;          // = true:  data entered as test circuit voltages
                                                        // = false: data entered as test circuit impedances

    private Complex[] impedances = null;                // model impedances
    private Complex[] impedanceWeights = null;          // model impedance weights
    private double[] impedanceMagnitudes = null;        // magnitude of the impedances
    private double[] impedanceMagnitudeWeights = null;  // impedance magnitude weights
    private double[] impedancePhasesRad = null;         // impedance phases [radians]of the impedances
    private double[] impedancePhaseWeightsRad = null;   // impedance phase weights [radians]
    private double[] impedancePhasesDeg = null;         // impedance phases [degrees]of the impedances
    private double[] impedancePhaseWeightsDeg = null;   // impedance phase weights [degrees]
    private double[] realZ = null;                      // real part of the model impedance
    private double[] realZweights = null;               // real part of the model impedance - weights
    private double[] imagZ = null;                      // imaginary part of the model impedance
    private double[] imagZweights = null;               // imaginary part of the model impedance - weights
    private boolean impedancesSet = false;              // = true when impedances calculated


    private double[] xRegression = null;                // regression x-axis data
    private double[][] yRegression = null;              // regression y-axis data
    private double[][] wRegression = null;              // regression weights

    private int modelNumber = 0;                        // model number
    private int numberOfParameters = 0;                 // number of model parameters
    private String[] parameterSymbols = null;           // model parameter symbols
    private boolean modelSet = false;                   // = true when a model number is entered
    private boolean estimatesNeeded = false;            // = true when a no estimates are to be entered and they are yet to be calculated

    private boolean supressDefaultConstraints = false;  // = true when in-built constraints on parameters supressed
    private boolean supressAddedConstraints = false;    // = true when added constraints on parameters supressed
    private boolean supressAllConstraints = false;      // = true when all constraints on parameters supressed

    private ArrayList constraints = null;       // user added constraints
    private int numberOfAddedConstraints = -1;          // number of user added constraints

    private boolean constraintsAdded = false;           // = true when user added constraints on parameters entered

    private double[] initialEstimates = null;           // initial estimates of parameter values
    private double[] initialSteps = null;               // initial steps of parameter values

    private double[] bestEstimates = null;              // best estimates of parameter values
    private double[] standardDeviations = null;         // standard deviations of the best estimates
    private double[] coefficientsOfVariation = null;    // coefficients of variation of the best estimates
    private double[][] correlationCoefficients = null;  // correlation coefficients of the best estimates
    private double[] preMinimumGradients = null;        // gradient before the minimum for each parameter
    private double[] postMinimumGradients = null;       // gradient after the minimum for each parameter

    private int degreesOfFreedom = 0;                   // degrees of freedom
    private double sumOfSquares = 0.0D;                 // sum of squares at minimum
    private double reducedSumOfSquares = 0.0D;          // reduced sum of squares at minimum
    private double chiSquare = Double.NaN;              // chiSquare
    private double reducedChiSquare = Double.NaN;       // reducedChiSquare
    private double[] realZresiduals = null;             // Real[Z] residuals
    private double[] imagZresiduals = null;             // Imag[Z] residuals

    private double[] calculatedRealZ = null;            // calculated Real[Z]
    private double[] calculatedImagZ = null;            // calculated Imag[Z]
    private Complex[] calculatedImpedances = null;      // calculated model impedances
    private double[] calculatedImpedanceMagnitudes = null;      // calculated impedance magnitudes
    private double[] calculatedImpedancePhasesRad = null;       // calculated impedance phases (radians)
    private double[] calculatedImpedancePhasesDeg = null;       // calculated impedance phases (degrees)

    private double[] calculatedRealV = null;            // calculated Real[voltage]
    private double[] calculatedImagV = null;            // calculated Imag[voltage]
    private Complex[] calculatedVoltages = null;        // calculated voltages
    private double[] calculatedVoltageMagnitudes = null;       // calculated voltage magnitudes
    private double[] calculatedVoltagePhasesRad = null;        // calculated voltage phases (radians)
    private double[] calculatedVoltagePhasesDeg = null;        // calculated voltage phases (degrees)

    ArrayList results = null;                      // ArrayList with elements
                                                        // 0:   number of frequencies
                                                        // 1:   number of parameters
                                                        // 2:   degrees of freedom
                                                        // 3:   initial estimates
                                                        // 4:   initial step sizes
                                                        // 5:   best estimates
                                                        // 6:   standard deviations
                                                        // 7:   coefficients of variation
                                                        // 8:   gradients about the minimum
                                                        // 9:   reduced sum of squares
                                                        // 10:  chi square
                                                        // 11:  reduced chi square


    private boolean estimatesSet = false;               // = true when parameter estimates entered

    private ImpedSpecModel userModel = null;            // supplied user model
    private boolean userModelSet = false;               // = true if user model supplied
    private RegressionFunction2 regressionFunction = null;  // Regression function
    private double tolerance = 1e-9;                    // tolerance in regression exit test
    private int maximumIterations = 10000;              // maximum iterations in regression procedure
    private int numberOfIterations1 = -1;               // number of iterations taken in the first regression
    private int numberOfIterations2 = -1;               // number of iterations taken in the second regression
    private boolean regressionDone = false;             // = true when regression completed

    private int numberOfLineFrequencies = 8000;         // number of points on calculated line plots
    private boolean logOrLinear = true;                 // = true - log plot
                                                        // = false - linear plot
    private double[] lineFrequencies = null;            // frequencies for clculating theoretical lines
    private double[] log10lineFrequencies = null;       // log10  of the frequencies for clculating theoretical lines


    // CONSTRUCTORS

    // Constructor
    public ImpedSpecRegression(){
        this.regressionTitle = "  ";
    }

    // Constructor setting title
    public ImpedSpecRegression(String regressionTitle){
        this.regressionTitle = regressionTitle;
    }

    // ENTER DATA

    // Enter the applied voltage
    public void setAppliedVoltage(double voltage){
        this.appliedVoltage = new Complex(voltage, 0.0D);
        this.appliedVoltageError = new Complex(0.0D, 0.0D);
        this.appliedVoltageSet = true;
        if(this.referenceSet && this.frequenciesSet)this.calculateExperimentalImpedances();
    }

    // Enter the setApplied voltage with error
    public void appliedVoltage(double voltage, double voltageError){
        this.appliedVoltage = new Complex(voltage, 0.0D);
        this.appliedVoltageSet = true;
        this.appliedVoltage = new Complex(voltageError, 0.0D);
        this.voltageErrorSet = true;
        if(this.referenceSet && this.frequenciesSet)this.calculateExperimentalImpedances();
    }

    // Enter the reference impedance  - resistive
    public void setReferenceImpedance(double resistance){
        this.referenceImpedance = new Complex(resistance, 0.0D);
        this.referenceSet = true;
        if(this.appliedVoltageSet && this.frequenciesSet)this.calculateExperimentalImpedances();
    }

    // Enter the reference impedance  - reactive
    public void setReferenceImpedance(double real, double imag){
        this.referenceImpedance = new Complex(real, imag);
        this.referenceSet = true;
        if(this.appliedVoltageSet && this.frequenciesSet)this.calculateExperimentalImpedances();
    }

    // Enter the reference impedance  - reactive
    public void setReferenceImpedance(Complex impedance){
        this.referenceImpedance = impedance;
        this.referenceSet = true;
        if(this.appliedVoltageSet && this.frequenciesSet)this.calculateExperimentalImpedances();
    }

      // Enter data as frequencies and real and imaginary parts of the test circuit voltages - no weights
    public void voltageDataAsComplex(double[] frequencies, double[] real, double[] imag){

        double[] realWeight = new double[frequencies.length];
        double[] imagWeight = new double[frequencies.length];
        this.weightsSet = false;
        this.voltageDataAsComplex(frequencies, real, imag, realWeight, imagWeight);
    }


    // Enter data as frequencies and real and imaginary parts of the test circuit voltages - weights provided
    public void voltageDataAsComplex(double[] frequencies, double[] real, double[] imag, double[] realWeight, double[] imagWeight){

        this.numberOfFrequencies = frequencies.length;
        if(this.numberOfFrequencies!=real.length)throw new IllegalArgumentException("The number of frequencies, " + this.numberOfFrequencies + ", does not equal the number of Real[voltages], " + real.length);
        if(this.numberOfFrequencies!=imag.length)throw new IllegalArgumentException("The number of frequencies, " + this.numberOfFrequencies + ", does not equal the number of Imag[voltages], " + imag.length);
        if(this.numberOfFrequencies!=realWeight.length)throw new IllegalArgumentException("The number of frequencies, " + this.numberOfFrequencies + ", does not equal the number of real weights, " + realWeight.length);
        if(this.numberOfFrequencies!=imagWeight.length)throw new IllegalArgumentException("The number of frequencies, " + this.numberOfFrequencies + ", does not equal the number of imag weights, " + imagWeight.length);

        this.frequencies = Conv.copy(frequencies);
        this.setAllFrequencyArrays();
        this.setCalculatedArrayLengths();

        this.realV = Conv.copy(real);
        this.imagV = Conv.copy(imag);
        this.realVweights = Conv.copy(realWeight);
        this.imagVweights = Conv.copy(imagWeight);
        this.voltageMagnitudes = new double[this.numberOfFrequencies];
        this.voltagePhasesDeg = new double[this.numberOfFrequencies];
        this.voltagePhasesRad = new double[this.numberOfFrequencies];
        this.voltages = Complex.oneDarray(this.numberOfFrequencies);
        for(int i=0; i getRegressionResultsAsArrayList(){
        if(!this.regressionDone)this.regression();
        return this.results;
    }

    // get the regression results as Vector
    public Vector getRegressionResultsAsVector(){
        if(!this.regressionDone)this.regression();
        int n = this.results.size();
        Vector res = new Vector(n);
        for(int i=0; i getRegressionResults(){
        return this.getRegressionResults();
    }

    // Set Regression data arrays
    private void setRegressionArrays(){

        this.xRegression = new double[this.numberOfFrequencies];
        this.yRegression = new double[2][this.numberOfFrequencies];
        if(this.weightsSet)this.wRegression = new double[2][this.numberOfFrequencies];

        for(int i=0; i regression(){

        // check data
        this.degreesOfFreedom = this.numberOfFrequencies - this.numberOfParameters;
        if(this.degreesOfFreedom<=0)throw new IllegalArgumentException("Degrees of freedom, " + this.degreesOfFreedom + ", are less than 1");
        if(!this.impedancesSet)throw new IllegalArgumentException("No impedances or voltages have been entered");

       // check initial estimates have been provided
        if(!this.estimatesSet && !this.userModelSet)this.setInitialEstimates();

        // Set regression arrays
        this.setRegressionArrays();

        // store initial estimates and associated data
        this.results = new ArrayList();
        this.results.add(new Integer(this.numberOfFrequencies));
        this.results.add(new Integer(this.numberOfParameters));
        this.results.add(new Integer(this.degreesOfFreedom));
        this.results.add(this.parameterSymbols);
        this.results.add(Conv.copy(this.initialEstimates));
        this.results.add(Conv.copy(this.initialSteps));

        // Enter regression data
        if(this.weightsSet){
            this.enterData(xRegression, yRegression, wRegression);
        }
        else{
            this.enterData(xRegression, yRegression);
        }

        // Create instance of regression function
        if(this.userModelSet){
            ImpedSpecRegressionFunction2 function = new ImpedSpecRegressionFunction2();
            function.numberOfFrequencies = this.numberOfFrequencies;
            function.isModel = this.userModel;
            this.regressionFunction = function;
        }else{
            ImpedSpecRegressionFunction1 function = new ImpedSpecRegressionFunction1();
            function.numberOfFrequencies = this.numberOfFrequencies;
            function.modelNumber = this.modelNumber;
            this.regressionFunction = function;
        }

        // Enter user added constraints
        int[] param = null;
        int[] direct = null;
        double[] bound = null;
        if(this.constraintsAdded){
            param  = new int[this.numberOfAddedConstraints];
            direct = new int[this.numberOfAddedConstraints];
            bound  = new double[this.numberOfAddedConstraints];
            int index = 0;
            for(int i=0; i plotColeCole(){
        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecRegression program:  Cole - Cole plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.regressionTitle;

        if(!this.regressionDone)this.regression();

        this.calculateLineFrequencies();

        double[][] data = PlotGraph.data(2, this.numberOfLineFrequencies);

        for(int i=0; i plotImpedanceMagnitudes(){

        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecRegression program:  Impedance magnitude versus frequency plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.regressionTitle;

        if(!this.regressionDone)this.regression();

        this.calculateLineFrequencies();

        // Magnitude versus frequency
        double[][] data = PlotGraph.data(2, this.numberOfLineFrequencies);

        if(this.logOrLinear){
            for(int i=0; i plotImpedancePhases(){

        String[] dAndT= this.dateAndTime();
        String graphTitle1 = "ImpedSpecRegression program:  Impedance phase versus frequency plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
        String graphTitle2 = this.regressionTitle;

        if(!this.regressionDone)this.regression();

        this.calculateLineFrequencies();

        // Magnitude versus frequency
        double[][] data = PlotGraph.data(2, this.numberOfLineFrequencies);

        if(this.logOrLinear){
            for(int i=0; i plotVoltageMagnitudes(){

        if(!this.regressionDone)this.regression();

        if(this.referenceSet && this.appliedVoltageSet){
            String[] dAndT= this.dateAndTime();
            String graphTitle1 = "ImpedSpecRegression program:  Voltage magnitude versus frequency plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
            String graphTitle2 = this.regressionTitle;

            this.calculateLineFrequencies();

            double[][] data = PlotGraph.data(2, this.numberOfLineFrequencies);

            if(this.logOrLinear){
                for(int i=0; i plotVoltagePhases(){

        if(!this.regressionDone)this.regression();

        if(this.referenceSet && this.appliedVoltageSet){
            String[] dAndT= this.dateAndTime();
            String graphTitle1 = "ImpedSpecRegression program:  Voltage phase versus frequency plot   [" + dAndT[0] + "    " + dAndT[1] + "]";
            String graphTitle2 = this.regressionTitle;

            this.calculateLineFrequencies();

            double[][] data = PlotGraph.data(2, this.numberOfLineFrequencies);

            if(this.logOrLinear){
                for(int i=0; i printToTextFile(){
        String fileName = "ImpedSpecRegressionOutput.txt";
        this.fileType = true;
        return this.printToTextFile(fileName);
    }

    // Print regression results to text file
    public ArrayList printToTextFile(String fileName){

        if(!this.regressionDone)regression();

        int field = 11;   // output field length
        int trunc = 4;    // truncation length

        // Check extension
        fileName = fileName.trim();
        int dotPosition = fileName.indexOf('.');
        if(dotPosition==-1)fileName += ".txt";

        // instantiate a FileOutput
        FileOutput fout = null;
        if(this.fileType){
            fout = new FileOutput(fileName, 'n');
        }
        else{
            fout = new FileOutput(fileName);
        }

        // print header
        fout.println("ImpedSpecRegression Program Output File:  " + this.regressionTitle);
        fout.dateAndTimeln(fileName);
        fout.println();
        if(this.modelSet){
             fout.println("Circuit - model number " + this.modelNumber);
        }
        else{
             fout.println("Circuit supplied by the user");
        }
        fout.println();


        // print circuit parameters
        fout.println("Circuit Parameters");
        fout.println("Best Estimates");

        fout.print("Parameter", field);
        fout.print("Best", field);
        fout.print("Standard", field);
        fout.print("Coeff. of", field);
        fout.print("Pre-", field);
        fout.println("Post-");

        fout.print("   ", field);
        fout.print("estimate", field);
        fout.print("deviation", field);
        fout.print("variation", field);
        fout.print("gradient", field);
        fout.println("gradient");

        for(int i=0; i printToExcelFile(){
        String fileName = "ImpedSpecRegressionOutput.txt";
        this.fileType = true;
        return this.printToExcelFile(fileName);
    }

    // Print regression results to a .xls (MS Excel) file
    public ArrayList printToExcelFile(String fileName){

        if(!this.regressionDone)regression();

        int field = 11;   // output field length
        int trunc = 4;    // truncation length

        // Check extension
        fileName = fileName.trim();
        int dotPosition = fileName.indexOf('.');
        if(dotPosition==-1){
            fileName += ".xls";
        }
        else{
            fileName = fileName.substring(0, dotPosition) + ".xls";
        }

        // instantiate a FileOutput
        FileOutput fout = null;
        if(this.fileType){
            fout = new FileOutput(fileName, 'n');
        }
        else{
            fout = new FileOutput(fileName);
        }

        // print header
        fout.println("ImpedSpecRegression Program Output File:  " + this.regressionTitle);
        fout.dateAndTimeln(fileName);
        fout.println();
        if(this.modelSet){
             fout.println("Circuit - model number " + this.modelNumber);
        }
        else{
             fout.println("Circuit supplied by the user");
        }
        fout.println();


        // print circuit parameters
        fout.println("Circuit Parameters");
        fout.println("Best Estimates");

        fout.printtab("Parameter", field);
        fout.printtab("Best", field);
        fout.printtab("Standard", field);
        fout.printtab("Coeff. of", field);
        fout.printtab("Pre-", field);
        fout.println("Post-");

        fout.printtab("   ", field);
        fout.printtab("estimate", field);
        fout.printtab("deviation", field);
        fout.printtab("variation", field);
        fout.printtab("gradient", field);
        fout.println("gradient");

        for(int i=0; i 
 

Ads help maintain this website.