Example usage for org.apache.commons.math3.linear Array2DRowRealMatrix getSubMatrix

List of usage examples for org.apache.commons.math3.linear Array2DRowRealMatrix getSubMatrix

Introduction

In this page you can find the example usage for org.apache.commons.math3.linear Array2DRowRealMatrix getSubMatrix.

Prototype

public RealMatrix getSubMatrix(final int startRow, final int endRow, final int startColumn, final int endColumn)
        throws OutOfRangeException, NumberIsTooSmallException 

Source Link

Usage

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * @param start/*from   w  w w .  ja  v  a  2s .c o m*/
 *            The starting frame
 * @param length
 *            The length of the interval, as numbers of frames
 * @param features
 *            The matrix build with feature vectors as rows
 * @return Returns the changing point in the input represented by features
 * 
 */

private int getPoint(int start, int length, int step, Array2DRowRealMatrix features) {
    double max = Double.NEGATIVE_INFINITY;
    int ncols = features.getColumnDimension(), point = 0;
    Array2DRowRealMatrix sub = (Array2DRowRealMatrix) features.getSubMatrix(start, start + length - 1, 0,
            ncols - 1);
    double bicValue = getBICValue(sub);
    for (int i = Segment.FEATURES_SIZE + 1; i < length - Segment.FEATURES_SIZE; i += step) {
        double aux = getLikelihoodRatio(bicValue, i, sub);
        if (aux > max) {
            max = aux;
            point = i;
        }
    }
    if (max < 0)
        point = Integer.MIN_VALUE;
    return point + start;
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * //  w w w .j a v a 2s  .  c  o  m
 * @param bicValue
 *            The bicValue of the model represented by only one Gaussian.
 *            This parameter it's useful when this function is called
 *            repeatedly for different frame values and the same features
 *            parameter
 * @param frame
 *            the frame which is tested for being a change point
 * @param features
 *            the feature vectors matrix
 * @return the likelihood ratio
 */
double getLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) {
    double bicValue1, bicValue2;
    int d = Segment.FEATURES_SIZE;
    double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.log(features.getRowDimension()) * 2;
    int nrows = features.getRowDimension(), ncols = features.getColumnDimension();
    Array2DRowRealMatrix sub1, sub2;
    sub1 = (Array2DRowRealMatrix) features.getSubMatrix(0, frame - 1, 0, ncols - 1);
    sub2 = (Array2DRowRealMatrix) features.getSubMatrix(frame, nrows - 1, 0, ncols - 1);
    bicValue1 = getBICValue(sub1);
    bicValue2 = getBICValue(sub2);
    return (bicValue - bicValue1 - bicValue2 - penalty);
}

From source file:edu.cmu.sphinx.speakerid.SpeakerIdentification.java

/**
 * @param features The feature vectors to be used for clustering
 * @return A cluster for each speaker detected based on the feature vectors provided
 *///w  ww.  j a va2s  . c om
public ArrayList<SpeakerCluster> cluster(ArrayList<float[]> features) {
    ArrayList<SpeakerCluster> ret = new ArrayList<SpeakerCluster>();
    Array2DRowRealMatrix featuresMatrix = ArrayToRealMatrix(features, features.size());
    LinkedList<Integer> l = getAllChangingPoints(featuresMatrix);
    Iterator<Integer> it = l.iterator();
    int curent, previous = it.next();
    while (it.hasNext()) {
        curent = it.next();
        Segment s = new Segment(previous * Segment.FRAME_LENGTH, (curent - previous) * (Segment.FRAME_LENGTH));
        Array2DRowRealMatrix featuresSubset = (Array2DRowRealMatrix) featuresMatrix.getSubMatrix(previous,
                curent - 1, 0, 12);
        ret.add(new SpeakerCluster(s, featuresSubset, getBICValue(featuresSubset)));
        previous = curent;
    }
    int clusterCount = ret.size();

    Array2DRowRealMatrix distance;
    distance = new Array2DRowRealMatrix(clusterCount, clusterCount);
    distance = updateDistances(ret);
    while (true) {
        double distmin = 0;
        int imin = -1, jmin = -1;

        for (int i = 0; i < clusterCount; i++)
            for (int j = 0; j < clusterCount; j++)
                if (i != j)
                    distmin += distance.getEntry(i, j);
        distmin /= (clusterCount * (clusterCount - 1) * 4);

        for (int i = 0; i < clusterCount; i++) {
            for (int j = 0; j < clusterCount; j++) {
                if (distance.getEntry(i, j) < distmin && i != j) {
                    distmin = distance.getEntry(i, j);
                    imin = i;
                    jmin = j;
                }
            }
        }
        if (imin == -1) {
            break;
        }
        ret.get(imin).mergeWith(ret.get(jmin));
        updateDistances(ret, imin, jmin, distance);
        ret.remove(jmin);
        clusterCount--;
    }
    return ret;
}