/** * * @param features * Matrix with feature vectors as rows * @return A list with all changing points detected in the file */ private LinkedList <Integer> GetAllChangingPoints(Array2DRowRealMatrix features) { LinkedList <Integer> ret = new LinkedList <Integer>(); ret.Add(0); int framesCount = features.getRowDimension(), step = 500; int start = 0, end = step, cp; while (end < framesCount) { cp = GetPoint(start, end - start + 1, step / 10, features); if (cp > 0) { start = cp; end = start + step; ret.Add(cp); } else { end += step; } } ret.Add(framesCount); return(ret); }
private LinkedList getAllChangingPoints(Array2DRowRealMatrix array2DRowRealMatrix) { LinkedList linkedList = new LinkedList(); linkedList.add(Integer.valueOf(0)); int rowDimension = array2DRowRealMatrix.getRowDimension(); int num = 500; int num2 = 0; int i = num; while (i < rowDimension) { int point = this.getPoint(num2, i - num2 + 1, num / 10, array2DRowRealMatrix); if (point > 0) { num2 = point; i = num2 + num; linkedList.add(Integer.valueOf(point)); } else { i += num; } } linkedList.add(Integer.valueOf(rowDimension)); return(linkedList); }
/// <summary> /// Used for computing the actual transformations (A and B matrices). These are stored in As and Bs. /// </summary> /// <param name="regLs">The reg ls.</param> /// <param name="regRs">The reg rs.</param> private void computeMllrTransforms(double[][][][][] regLs, double[][][][] regRs) { int len; for (int c = 0; c < nrOfClusters; c++) { this.As[c] = new float[loader.getNumStreams()][][]; this.Bs[c] = new float[loader.getNumStreams()][]; for (int i = 0; i < loader.getNumStreams(); i++) { len = loader.getVectorLength()[i]; //TODO: CHECK SEMANTICS //this.As[c][i] = new float[len][len]; this.As[c][i] = new float[len][]; this.Bs[c][i] = new float[len]; for (int j = 0; j < len; ++j) { var coef = new Array2DRowRealMatrix(regLs[c][i][j], false); var solver = new LUDecomposition(coef).getSolver(); var vect = new ArrayRealVector(regRs[c][i][j], false); var ABloc = solver.solve(vect); for (int k = 0; k < len; ++k) { this.As[c][i][j][k] = (float)ABloc.getEntry(k); } this.Bs[c][i][j] = (float)ABloc.getEntry(len); } } } }
public SpeakerCluster(Segment s, Array2DRowRealMatrix featureMatrix, double bicValue) { this.segmentSet = new TreeSet(); this.featureMatrix = new Array2DRowRealMatrix(featureMatrix.getData()); this.bicValue = bicValue; this.addSegment(s); }
public virtual ArrayList cluster(ArrayList features) { ArrayList arrayList = new ArrayList(); Array2DRowRealMatrix array2DRowRealMatrix = this.ArrayToRealMatrix(features, features.size()); LinkedList allChangingPoints = this.getAllChangingPoints(array2DRowRealMatrix); Iterator iterator = allChangingPoints.iterator(); int num = ((Integer)iterator.next()).intValue(); Array2DRowRealMatrix array2DRowRealMatrix2; while (iterator.hasNext()) { int num2 = ((Integer)iterator.next()).intValue(); Segment s = new Segment(num * 10, (num2 - num) * 10); array2DRowRealMatrix2 = (Array2DRowRealMatrix)array2DRowRealMatrix.getSubMatrix(num, num2 - 1, 0, 12); arrayList.add(new SpeakerCluster(s, array2DRowRealMatrix2, SpeakerIdentification.getBICValue(array2DRowRealMatrix2))); num = num2; } int num3 = arrayList.size(); new Array2DRowRealMatrix(num3, num3); array2DRowRealMatrix2 = this.updateDistances(arrayList); for (;;) { double num4 = (double)0f; int num5 = -1; int num6 = -1; for (int i = 0; i < num3; i++) { for (int j = 0; j < num3; j++) { if (i != j) { num4 += array2DRowRealMatrix2.getEntry(i, j); } } } num4 /= (double)(num3 * (num3 - 1) * 4); for (int i = 0; i < num3; i++) { for (int j = 0; j < num3; j++) { if (array2DRowRealMatrix2.getEntry(i, j) < num4 && i != j) { num4 = array2DRowRealMatrix2.getEntry(i, j); num5 = i; num6 = j; } } } if (num5 == -1) { break; } ((SpeakerCluster)arrayList.get(num5)).mergeWith((SpeakerCluster)arrayList.get(num6)); this.updateDistances(arrayList, num5, num6, array2DRowRealMatrix2); arrayList.remove(num6); num3 += -1; } return(arrayList); }
/// <summary> /// Used for computing the actual transformations (A and B matrices). These are stored in As and Bs. /// </summary> /// <param name="regLs">The reg ls.</param> /// <param name="regRs">The reg rs.</param> private void ComputeMllrTransforms(double[][][][][] regLs, double[][][][] regRs) { int len; for (int c = 0; c < _nrOfClusters; c++) { As[c] = new float[_loader.NumStreams][][]; Bs[c] = new float[_loader.NumStreams][]; for (int i = 0; i < _loader.NumStreams; i++) { len = _loader.VectorLength[i]; As[c][i] = Java.CreateArray <float[][]>(len, len); //this.As[c][i] = new float[len][len]; Bs[c][i] = new float[len]; for (int j = 0; j < len; ++j) { var coef = new Array2DRowRealMatrix(regLs[c][i][j], false); var solver = new LUDecomposition(coef).getSolver(); var vect = new ArrayRealVector(regRs[c][i][j], false); var aBloc = solver.solve(vect); for (int k = 0; k < len; ++k) { As[c][i][j][k] = (float)aBloc.getEntry(k); } Bs[c][i][j] = (float)aBloc.getEntry(len); } } } }
public static RealMatrix GetCorrelationMatrix(double[] autocorrelations) { RealMatrix result = null; int dim = autocorrelations.Length; double[][] psiValues = new double[dim][]; for (int row = 0; row < dim; row++) { for (int col = row; col < dim; col++) { if (row == col) { psiValues[col][row] = 1.0; } else { psiValues[col][row] = autocorrelations[col - row - 1]; psiValues[row][col] = autocorrelations[col - row - 1]; } } } result = new Array2DRowRealMatrix(psiValues); return(result); }
public void MergeWith(SpeakerCluster target) { if (target == null) { throw new NullReferenceException(); } var it = target._segmentSet.GetEnumerator(); while (it.MoveNext()) { if (!AddSegment(it.Current)) { Console.WriteLine("Something doesn't work in mergeWith method, Cluster class"); } } int rowDim = FeatureMatrix.getRowDimension() + target.FeatureMatrix.getRowDimension(); int colDim = FeatureMatrix.getColumnDimension(); var combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim); combinedFeatures.setSubMatrix(FeatureMatrix.getData(), 0, 0); combinedFeatures .setSubMatrix(target.FeatureMatrix.getData(), FeatureMatrix.getRowDimension(), 0); _bicValue = SpeakerIdentification.GetBICValue(combinedFeatures); FeatureMatrix = new Array2DRowRealMatrix(combinedFeatures.getData()); }
public SpeakerCluster(Segment s, Array2DRowRealMatrix featureMatrix, double bicValue) { _segmentSet = new SortedSet <Segment>(); this.FeatureMatrix = new Array2DRowRealMatrix(featureMatrix.getData()); this._bicValue = bicValue; AddSegment(s); }
/** * @param Clustering * The array of clusters * @param posi * The index of the merged cluster * @param posj * The index of the cluster that will be eliminated from the * clustering * @param distance * The distance matrix that will be updated */ void UpdateDistances(List <SpeakerCluster> clustering, int posi, int posj, Array2DRowRealMatrix distance) { int clusterCount = clustering.Count; for (int i = 0; i < clusterCount; i++) { distance.setEntry(i, posi, ComputeDistance(clustering[i], clustering[posi])); distance.setEntry(posi, i, distance.getEntry(i, posi)); } for (int i = posj; i < clusterCount - 1; i++) { for (int j = 0; j < clusterCount; j++) { distance.setEntry(i, j, distance.getEntry(i + 1, j)); } } for (int i = 0; i < clusterCount; i++) { for (int j = posj; j < clusterCount - 1; j++) { distance.setEntry(i, j, distance.getEntry(i, j + 1)); } } }
private void computeMllrTransforms(double[][][][][] array, double[][][][] array2) { for (int c = 0; c < this.nrOfClusters; c++) { this.As[c] = new float[this.loader.getNumStreams()][][]; this.Bs[c] = new float[this.loader.getNumStreams()][]; for (int i = 0; i < this.loader.getNumStreams(); i++) { int len = this.loader.getVectorLength()[i]; float[][][] array3 = this.As[c]; int num2 = i; int num3 = len; int num4 = len; int[] array4 = new int[2]; int num5 = num4; array4[1] = num5; num5 = num3; array4[0] = num5; array3[num2] = (float[][])ByteCodeHelper.multianewarray(typeof(float[][]).TypeHandle, array4); this.Bs[c][i] = new float[len]; for (int j = 0; j < len; j++) { #if USE_MAPACK //TODO: need to check if the math is correct Matrix m = new Matrix(array[c][i][j]); int rs = array2[c][i][j].Length; Matrix rv = new Matrix(rs, 1); for (int r = 0; r < rs; r++) { rv[r, 0] = array2[c][i][j][r]; } Matrix solved = m.Solve(rv); for (int k = 0; k < len; k++) { this.As[c][i][j][k] = (float)solved[k, 0]; } //TODO: there may be a problem of index out of range? //(index == len)? this.Bs[c][i][j] = (float)solved[len, 0]; #else Array2DRowRealMatrix matrix = new Array2DRowRealMatrix(array[c][i][j], false); DecompositionSolver solver = new LUDecomposition(matrix).getSolver(); ArrayRealVector rv = new ArrayRealVector(array2[c][i][j], false); RealVector realVector = solver.solve(rv); for (int k = 0; k < len; k++) { this.As[c][i][j][k] = (float)realVector.getEntry(k); } this.Bs[c][i][j] = (float)realVector.getEntry(len); #endif } } } }
void PrintMatrix(Array2DRowRealMatrix a) { for (int i = 0; i < a.getRowDimension(); i++) { for (int j = 0; j < a.getColumnDimension(); j++) { Console.Write(a.getEntry(i, j) + " "); } Console.WriteLine(); } }
public SpeakerCluster(SpeakerCluster c) { _segmentSet = new SortedSet <Segment>(); FeatureMatrix = new Array2DRowRealMatrix(c.FeatureMatrix.getData()); var it = c._segmentSet.GetEnumerator(); while (it.MoveNext()) { AddSegment(it.Current); } }
internal virtual void printMatrix(Array2DRowRealMatrix array2DRowRealMatrix) { for (int i = 0; i < array2DRowRealMatrix.getRowDimension(); i++) { for (int j = 0; j < array2DRowRealMatrix.getColumnDimension(); j++) { [email protected](new StringBuilder().append(array2DRowRealMatrix.getEntry(i, j)).append(" ").toString()); } [email protected](); } }
public static double getBICValue(Array2DRowRealMatrix mat) { double num = (double)0f; EigenDecomposition eigenDecomposition = new EigenDecomposition(new Covariance(mat).getCovarianceMatrix()); double[] realEigenvalues = eigenDecomposition.getRealEigenvalues(); for (int i = 0; i < realEigenvalues.Length; i++) { num += java.lang.Math.log(realEigenvalues[i]); } return(num * (double)(mat.getRowDimension() / 2)); }
/** * @param mat * A matrix with feature vectors as rows. * @return Returns the BICValue of the Gaussian model that approximates the * the feature vectors data samples */ public static double GetBICValue(Array2DRowRealMatrix mat) { double ret = 0; EigenDecomposition ed = new EigenDecomposition(new Covariance(mat).getCovarianceMatrix()); double[] re = ed.getRealEigenvalues(); for (int i = 0; i < re.Length; i++) { ret += Math.Log(re[i]); } return(ret * (mat.getRowDimension() / 2)); }
internal virtual double getLikelihoodRatio(double num, int num2, Array2DRowRealMatrix array2DRowRealMatrix) { int num3 = 13; double num4 = 0.5 * ((double)num3 + 0.5 * (double)num3 * (double)(num3 + 1)) * java.lang.Math.log((double)array2DRowRealMatrix.getRowDimension()) * 2.0; int rowDimension = array2DRowRealMatrix.getRowDimension(); int columnDimension = array2DRowRealMatrix.getColumnDimension(); Array2DRowRealMatrix mat = (Array2DRowRealMatrix)array2DRowRealMatrix.getSubMatrix(0, num2 - 1, 0, columnDimension - 1); Array2DRowRealMatrix mat2 = (Array2DRowRealMatrix)array2DRowRealMatrix.getSubMatrix(num2, rowDimension - 1, 0, columnDimension - 1); double num5 = SpeakerIdentification.getBICValue(mat); double num6 = SpeakerIdentification.getBICValue(mat2); return(num - num5 - num6 - num4); }
/** * Determines the coefficients (alpha) for the ARTA-Process. * */ public static double[] ArAutocorrelationsToAlphas(double[] arAutocorrelations) { int dim = arAutocorrelations.Length; double[] alphas = new double[dim]; RealMatrix psi = AutoCorrelation.GetCorrelationMatrix(arAutocorrelations); RealMatrix r = new Array2DRowRealMatrix(arAutocorrelations).transpose(); RealMatrix a = r.multiply(new CholeskyDecomposition(psi).getSolver().getInverse()); alphas = a.getRow(0); return(alphas); }
static double ComputeDistance(SpeakerCluster c1, SpeakerCluster c2) { int rowDim = c1.FeatureMatrix.getRowDimension() + c2.FeatureMatrix.getRowDimension(); int colDim = c1.FeatureMatrix.getColumnDimension(); Array2DRowRealMatrix combinedFeatures = new Array2DRowRealMatrix(rowDim, colDim); combinedFeatures.setSubMatrix(c1.FeatureMatrix.getData(), 0, 0); combinedFeatures.setSubMatrix(c2.FeatureMatrix.getData(), c1.FeatureMatrix.getRowDimension(), 0); double bicValue = GetBICValue(combinedFeatures); double d = Segment.FeaturesSize; double penalty = 0.5 * (d + 0.5 * d * (d + 1)) * Math.Log(combinedFeatures.getRowDimension()) * 2; return(bicValue - c1.GetBicValue() - c2.GetBicValue() - penalty); }
/** * * @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 */ static double GetLikelihoodRatio(double bicValue, int frame, Array2DRowRealMatrix features) { double bicValue1, bicValue2; int d = Segment.FeaturesSize; 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); }
internal virtual double computeDistance(SpeakerCluster speakerCluster, SpeakerCluster speakerCluster2) { int rowDimension = speakerCluster.getFeatureMatrix().getRowDimension() + speakerCluster2.getFeatureMatrix().getRowDimension(); int columnDimension = speakerCluster.getFeatureMatrix().getColumnDimension(); Array2DRowRealMatrix array2DRowRealMatrix = new Array2DRowRealMatrix(rowDimension, columnDimension); array2DRowRealMatrix.setSubMatrix(speakerCluster.getFeatureMatrix().getData(), 0, 0); array2DRowRealMatrix.setSubMatrix(speakerCluster2.getFeatureMatrix().getData(), speakerCluster.getFeatureMatrix().getRowDimension(), 0); double num = SpeakerIdentification.getBICValue(array2DRowRealMatrix); double num2 = 13.0; double num3 = 0.5 * (num2 + 0.5 * num2 * (num2 + (double)1f)) * java.lang.Math.log((double)array2DRowRealMatrix.getRowDimension()) * 2.0; return(num - speakerCluster.getBicValue() - speakerCluster2.getBicValue() - num3); }
internal virtual Array2DRowRealMatrix updateDistances(ArrayList arrayList) { int num = arrayList.size(); Array2DRowRealMatrix array2DRowRealMatrix = new Array2DRowRealMatrix(num, num); for (int i = 0; i < num; i++) { for (int j = 0; j <= i; j++) { array2DRowRealMatrix.setEntry(i, j, this.computeDistance((SpeakerCluster)arrayList.get(i), (SpeakerCluster)arrayList.get(j))); array2DRowRealMatrix.setEntry(j, i, array2DRowRealMatrix.getEntry(i, j)); } } return(array2DRowRealMatrix); }
/** * @param Clustering * The array of clusters */ Array2DRowRealMatrix UpdateDistances(List <SpeakerCluster> clustering) { int clusterCount = clustering.Count; Array2DRowRealMatrix distance = new Array2DRowRealMatrix(clusterCount, clusterCount); for (int i = 0; i < clusterCount; i++) { for (int j = 0; j <= i; j++) { distance.setEntry(i, j, ComputeDistance(clustering[i], clustering[j])); distance.setEntry(j, i, distance.getEntry(i, j)); } } return(distance); }
internal virtual Array2DRowRealMatrix ArrayToRealMatrix(ArrayList arrayList, int num) { int num2 = ((float[])arrayList.get(1)).Length; Array2DRowRealMatrix array2DRowRealMatrix = new Array2DRowRealMatrix(num, num2); for (int i = 0; i < num; i++) { double[] array = new double[num2]; for (int j = 0; j < num2; j++) { array[j] = (double)((float[])arrayList.get(i))[j]; } array2DRowRealMatrix.setRow(i, array); } return(array2DRowRealMatrix); }
/** * @param lst * An ArrayList with all the values being vectors of the same * dimension * @return The RealMatrix with the vectors from the ArrayList on columns */ static Array2DRowRealMatrix ArrayToRealMatrix(List <float[]> lst, int size) { int length = lst[1].Length; var ret = new Array2DRowRealMatrix(size, length); int i = 0; for (i = 0; i < size; i++) { double[] converted = new double[length]; for (int j = 0; j < length; j++) { converted[j] = ((lst[i])[j]); } ret.setRow(i, converted); } return(ret); }
/** * @param start * 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 static int GetPoint(int start, int length, int step, Array2DRowRealMatrix features) { double max = Double.NegativeInfinity; int ncols = features.getColumnDimension(), point = 0; var sub = (Array2DRowRealMatrix)features.getSubMatrix(start, start + length - 1, 0, ncols - 1); double bicValue = GetBICValue(sub); for (int i = Segment.FeaturesSize + 1; i < length - Segment.FeaturesSize; 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); }
private int getPoint(int num, int num2, int num3, Array2DRowRealMatrix array2DRowRealMatrix) { double num4 = double.NegativeInfinity; int columnDimension = array2DRowRealMatrix.getColumnDimension(); int num5 = 0; Array2DRowRealMatrix array2DRowRealMatrix2 = (Array2DRowRealMatrix)array2DRowRealMatrix.getSubMatrix(num, num + num2 - 1, 0, columnDimension - 1); double num6 = SpeakerIdentification.getBICValue(array2DRowRealMatrix2); for (int i = 14; i < num2 - 13; i += num3) { double num7 = this.getLikelihoodRatio(num6, i, array2DRowRealMatrix2); if (num7 > num4) { num4 = num7; num5 = i; } } if (num4 < (double)0f) { num5 = int.MinValue; } return(num5 + num); }
public virtual void mergeWith(SpeakerCluster target) { if (target == null) { throw new NullPointerException(); } Iterator iterator = target.segmentSet.iterator(); while (iterator.hasNext()) { if (!this.addSegment((Segment)iterator.next()).booleanValue()) { [email protected]("Something doesn't work in mergeWith method, Cluster class"); } } int rowDimension = this.featureMatrix.getRowDimension() + target.getFeatureMatrix().getRowDimension(); int columnDimension = this.featureMatrix.getColumnDimension(); Array2DRowRealMatrix array2DRowRealMatrix = new Array2DRowRealMatrix(rowDimension, columnDimension); array2DRowRealMatrix.setSubMatrix(this.featureMatrix.getData(), 0, 0); array2DRowRealMatrix.setSubMatrix(target.getFeatureMatrix().getData(), this.featureMatrix.getRowDimension(), 0); this.bicValue = SpeakerIdentification.getBICValue(array2DRowRealMatrix); this.featureMatrix = new Array2DRowRealMatrix(array2DRowRealMatrix.getData()); }
internal virtual void updateDistances(ArrayList arrayList, int num, int num2, Array2DRowRealMatrix array2DRowRealMatrix) { int num3 = arrayList.size(); for (int i = 0; i < num3; i++) { array2DRowRealMatrix.setEntry(i, num, this.computeDistance((SpeakerCluster)arrayList.get(i), (SpeakerCluster)arrayList.get(num))); array2DRowRealMatrix.setEntry(num, i, array2DRowRealMatrix.getEntry(i, num)); } for (int i = num2; i < num3 - 1; i++) { for (int j = 0; j < num3; j++) { array2DRowRealMatrix.setEntry(i, j, array2DRowRealMatrix.getEntry(i + 1, j)); } } for (int i = 0; i < num3; i++) { for (int j = num2; j < num3 - 1; j++) { array2DRowRealMatrix.setEntry(i, j, array2DRowRealMatrix.getEntry(i, j + 1)); } } }
/** * @param features The feature vectors to be used for clustering * @return A cluster for each speaker detected based on the feature vectors provided */ public List <SpeakerCluster> Cluster(List <float[]> features) { List <SpeakerCluster> ret = new List <SpeakerCluster>(); Array2DRowRealMatrix featuresMatrix = ArrayToRealMatrix(features, features.Count); LinkedList <Integer> l = GetAllChangingPoints(featuresMatrix); var it = l.GetEnumerator(); int curent; it.MoveNext(); int previous = it.Current; while (it.MoveNext()) { curent = it.Current; Segment s = new Segment(previous * Segment.FrameLength, (curent - previous) * (Segment.FrameLength)); Array2DRowRealMatrix featuresSubset = (Array2DRowRealMatrix)featuresMatrix.getSubMatrix( previous, curent - 1, 0, 12); ret.Add(new SpeakerCluster(s, featuresSubset, GetBICValue(featuresSubset))); previous = curent; } int clusterCount = ret.Count; 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[imin].MergeWith(ret[jmin]); UpdateDistances(ret, imin, jmin, distance); ret.Remove(jmin); clusterCount--; } return(ret); }