public Matrix createCrossComparison(String trainingSample, String verifySample) throws IOException, ClassNotFoundException { String dir = "/Users/thomaskaiser/Documents/MCM/MC480_Project_I/SVN/trunk/Implementation/ProjectFiles/mobile phone folder structure"; HashMap<String, String> sampleDirectories = readSampleDirectories(dir); Set<String> keySet = sampleDirectories.keySet(); int count = keySet.size(); Matrix results = new Matrix(count, count); List<String> speakerIdList = new ArrayList<String>(keySet); double[][][] trainingMfccs = new double[count][][]; double[][][] verifyMfccs = new double[count][][]; Codebook[] codebooks = new Codebook[count]; for (int i = 0; i < count; i++) { String currentDir = sampleDirectories.get(speakerIdList.get(i)); double[] samples = readSamples(currentDir, trainingSample); trainingMfccs[i] = calculateMFCCs(samples); codebooks[i] = trainCodebook(trainingMfccs[i]); writeCodebook(currentDir, codebooks[i]); samples = readSamples(currentDir, verifySample); verifyMfccs[i] = calculateMFCCs(samples); // codebooks[i] = readCodebook(currentDir); } double distortion = 0; double sumDistortion = 0; double avgDistortion = 0; for (int i = 0; i < count; i++) { double minDist = Double.MAX_VALUE; int minDistIndex = -1; for (int j = 0; j < count; j++) { distortion = ClusterUtil.calculateAverageDistortion(verifyMfccs[i], codebooks[j]); sumDistortion += distortion; results.set(i, j, distortion); if (distortion < minDist) { minDistIndex = j; minDist = distortion; } } avgDistortion = sumDistortion / count; sumDistortion = 0; Log.d( "Min. Distortion for %d is: %7.2f. Avg. Distortion to all: %7.2f. Ratio: %5.3f", i, minDist, avgDistortion, (minDist / avgDistortion)); } return results; }
public void testSampleCrossComparison(String sampleFileName) throws IOException { String dir = "/Users/thomaskaiser/Documents/MCM/MC480_Project_I/SVN/trunk/Implementation/ProjectFiles/mobile phone folder structure"; HashMap<String, String> sampleDirectories = readSampleDirectories(dir); Set<String> keySet = sampleDirectories.keySet(); int count = keySet.size(); Matrix results = new Matrix(count, count); for (String speakerId : keySet) { double[] sampleValues = readSamples(sampleDirectories.get(speakerId), sampleFileName); double[][] mfcc = calculateMFCCs(sampleValues); String minDistanceSampleId = null; double minAverageDistortion = Double.MAX_VALUE; double sumDistortion = 0; double avgDistortion = 0; int codebookCount = keySet.size(); for (String sampleId : keySet) { Codebook cb = readCodebook(sampleDirectories.get(sampleId)); if (cb != null) { double averageDistortion = ClusterUtil.calculateAverageDistortion(mfcc, cb); sumDistortion += averageDistortion; Log.d("Calculated avg distortion=%f", averageDistortion); if (averageDistortion < minAverageDistortion) { minAverageDistortion = averageDistortion; minDistanceSampleId = sampleId; } } } avgDistortion = (sumDistortion) / (codebookCount); Log.d("******\nFound minimum codebook distance for speaker: %s", speakerId); Log.d( "Least codebook distance for: %s\n Distance: %.2f\n Average Distance %.2f", minDistanceSampleId, minAverageDistortion, avgDistortion); } }