Created
January 25, 2013 02:21
-
-
Save thisMagpie/4631177 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * ********************************************************** * | |
| * ******************* By Magdalen Berns ******************** * | |
| * ********************************************************** * | |
| * CONTAINS: | |
| * a static method to compute the gaussian distribution | |
| * | |
| * a static method to convolve two signals (expected input gaussian distribution and set of data points we wish to smooth | |
| * Static methods to calculate the mean and standard deviation (and square of std dev) of an integer array of data points. | |
| * */ | |
| class StatsUtil { | |
| private static final double period = 2 * Math.PI; | |
| //Arguments: sample number, mean and std deviation | |
| public static double[] gaussian(int sampleNumber, double sigmaSquared, double mean){ | |
| // Create Gaussian | |
| double[] gaussian = new double[sampleNumber]; | |
| double tempGaussian= 0.0; | |
| for (int i=0; i<sampleNumber; i++){ | |
| gaussian[i] = Math.sqrt(1/(period)*sigmaSquared)*(Math.exp(-(i-mean)*(i-mean)/(2*sigmaSquared))); | |
| tempGaussian += gaussian[i]; | |
| } | |
| //Normalize the data array | |
| for (int i=0; i<sampleNumber; i++){ | |
| gaussian[i] /= tempGaussian; | |
| } | |
| return gaussian; | |
| } | |
| public static double[] convolve(int[] data, double[] gaussian, int sampleNumber){ | |
| // The convolution smoothing. | |
| double convolved[] = new double[data.length - (sampleNumber + 1)]; | |
| for (int i=0; i<convolved.length; i++){ | |
| convolved[i] = 0.0; // Set all doubles to 0. | |
| for (int j=i, k=0; j<i+sampleNumber; j++, k++){ | |
| convolved[i] += data[j] * gaussian[k]; | |
| } | |
| } | |
| return convolved; | |
| } | |
| public static double mean(double[] data){ | |
| double sum =0; | |
| for (double aData : data) sum += aData; | |
| return sum/data.length; | |
| } | |
| //std deviation squared | |
| public static double devSq(int[] data, double mean){ | |
| double tot=0; | |
| for (int aData : data) tot += Math.pow((aData - mean), 2); | |
| return tot; | |
| } | |
| //std deviation squared | |
| public static double devSq(double[] data, double mean){ | |
| double tot=0; | |
| for (double aData : data) tot += Math.pow((aData - mean), 2); | |
| return tot; | |
| } | |
| //standard | |
| public static double dev(double stdDev, int dataPoints){ | |
| return Math.sqrt((stdDev)/(dataPoints-1)); | |
| } | |
| public static float dev(float stdDev, int dataPoints){ | |
| return (float) Math.sqrt((stdDev)/(dataPoints-1)); | |
| } | |
| public static int findSampleNo(double largest, double[] data){ | |
| int sample=0; | |
| for(int i=1; i<data.length; i++){ | |
| if(data[i]==largest){ | |
| sample=i; | |
| } | |
| } | |
| return sample; | |
| } | |
| public static double findPeak(double[] data){ | |
| double largest=data[0]; | |
| for (double aData : data) { | |
| if (aData > largest) { | |
| largest = aData; | |
| } | |
| } | |
| return largest; | |
| } | |
| //Calculates y axis for gradient given in argument | |
| public static float[][] yCalc(float[][] data,float gradient){ | |
| float[][] yCalc= new float[data.length][data[1].length]; | |
| for (int i=1;i<yCalc.length;i++){ | |
| for (int j=0;j<yCalc[i].length;j++){ | |
| yCalc[i][j] = gradient* data[i][j]; | |
| System.out.printf("%2.2f ",yCalc[i][j]); | |
| } | |
| } | |
| return yCalc; | |
| } | |
| //Calculates y axis for gradient given in argument | |
| public static float[][] residuals(float[][] y, float[][] yCalc){ | |
| float[][] deltaY= new float[yCalc.length][yCalc[2].length]; | |
| System.out.println("Residuals "); | |
| for (int i=1;i<y.length;i++){ | |
| for (int j=0;j<y[i].length;j++){ | |
| deltaY[i][j] = y[i][j]-yCalc[i][j]; | |
| System.out.printf("%2.2f",deltaY[i][j]); | |
| System.out.println(); | |
| } | |
| } | |
| return deltaY; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment