Histogram.java

Go to the documentation of this file.
00001 package edu.rice.cs.hpc.viewer.graph;
00002 
00003 import java.util.Arrays;
00004 
00005 
00006 /************************************************************
00007  * Class to compute histogram data: bins, frequency, ...
00008  * @author laksonoadhianto
00009  *
00010  ************************************************************/
00011 public class Histogram {
00012     
00013     // data for histogram graph
00014     private double freq[];
00015     private double axis_x[];
00016     
00017     // histogram property
00018     private double data_min, data_max, data_width;
00019     
00020     
00021     /***
00022      * create histogram data
00023      * 
00024      * @param n_bins: number of bins
00025      * @param data: array of data
00026      */
00027     public Histogram(int n_bins, double data[]) {
00028 
00029         double sorted_data[] = data.clone();
00030         Arrays.sort(sorted_data);
00031         data_min = sorted_data[0];
00032         data_max = sorted_data[data.length-1];
00033         
00034         data_width = ((data_max - data_min) / n_bins);
00035         
00036         freq = new double[n_bins];
00037         axis_x = new double[n_bins];
00038         
00039         //---------------------------------
00040         // initialize frequency and axis
00041         //---------------------------------
00042         for (int i=0; i<n_bins; i++) {
00043             freq[i] = 0.0;
00044             axis_x[i] = data_min + (i * data_width);
00045         }
00046         
00047         //---------------------------------
00048         // compute the frequency
00049         //---------------------------------
00050         for (int i=0; i<data.length; i++) {
00051             int pos = (int) ( (data[i]-data_min) / data_width);
00052             if (pos >= n_bins)
00053                 pos = n_bins - 1;
00054             freq[pos]++;
00055         }
00056         
00057     }
00058     
00059     
00060     /***
00061      * get the x-axis labels
00062      * @return
00063      */
00064     public double[] getAxisX() {
00065         return this.axis_x;
00066     }
00067     
00068     
00069     /****
00070      * get the frequency values
00071      * @return
00072      */
00073     public double[] getAxisY() {
00074         return this.freq;
00075     }
00076     
00077     
00078     /****
00079      * get the minimum data value
00080      * @return
00081      */
00082     public double min() {
00083         return data_min;
00084     }
00085     
00086     
00087     /****
00088      * get the maximum data value
00089      * @return
00090      */
00091     public double max() {
00092         return data_max;
00093     }
00094     
00095     public double getWidth() {
00096         return data_width;
00097     }
00098 }

Generated on 5 May 2015 for HPCVIEWER by  doxygen 1.6.1