/** * Returns the data values. * * @return the data values */ public double[] getValues() { return data.getValues(); }
/** Normalizes the list of Data. */ private void normalizeList() { for (Data data : cepstraList) { if (data instanceof DoubleData) { ((DoubleData)data).getValues()[0] -= agc; } } } }
/** Normalizes the list of Data. */ private void normalizeList() { // calculate the variance first for (int i = 0; i < variances.length; i++) { variances[i] = Math.sqrt(numberDataCepstra / variances[i]); } for (Data data : cepstraList) { if (data instanceof DoubleData) { double[] cepstrum = ((DoubleData)data).getValues(); for (int j = 0; j < cepstrum.length; j++) { cepstrum[j] *= variances[j]; } } } } }
public void calculateVULevels(Data data) { if (data instanceof DoubleData) { double[] samples = ((DoubleData) data).getValues(); calculateVULevels(samples); } }
/** Normalizes the list of Data. */ private void normalizeList() { StringBuilder cmn = new StringBuilder(); // calculate the mean first for (int i = 0; i < sums.length; i++) { sums[i] /= numberDataCepstra; cmn.append (formatter.format(sums[i])); cmn.append(' '); } logger.info(cmn.toString()); for (Data data : cepstraList) { if (data instanceof DoubleData) { double[] cepstrum = ((DoubleData)data).getValues(); for (int j = 0; j < cepstrum.length; j++) { cepstrum[j] -= sums[j]; // sums[] is now the means[] } } } }
/** * Plots the energy values of the given Data to System.out. If the Data contains a signal, it prints the signal. * * @param cepstrum the Data to plot */ public void plot(Data cepstrum) { if (cepstrum != null) { if (cepstrum instanceof DoubleData) { int energy = (int) ((DoubleData) cepstrum).getValues()[0]; System.out.println(getPlot(energy)); } else { System.out.println(cepstrum); } } }
/** * Tests whether the samples of all <code>Data</code>s in the list are ordered in increasing order with +1 * increments. */ public static boolean hasIncreasingOrder(List<Data> output, int lastValue) { int dataCounter = 0; for (Data data : output) { if (data instanceof DoubleData) { DoubleData dd = (DoubleData) data; for (int i = 0; i < dd.getValues().length; i++) { if ((dataCounter + 1) == dd.getValues()[i]) dataCounter++; else return false; } } } return dataCounter == lastValue; } }
/** * Lifts the input mel-cepstrum. * * @param input * a mel-cepstrum frame * @throws IllegalArgumentException */ private void liftCepstrum(DoubleData input) throws IllegalArgumentException { double[] melCepstrum = input.getValues(); if (lifterWeights == null) { cepstrumSize = melCepstrum.length; computeLifterWeights(); } else if (melCepstrum.length != cepstrumSize) { throw new IllegalArgumentException( "MelCepstrum size is incorrect: " + "melcepstrum.length == " + melCepstrum.length + ", cepstrumSize == " + cepstrumSize); } for (int i = 0; i < melCepstrum.length; i++) { melCepstrum[i] = melCepstrum[i] * lifterWeights[i]; } }
/** * Converts FloatData object to DoubleData. * @param data data to convert * @return converted data */ public static FloatData DoubleData2FloatData(DoubleData data) { int numSamples = data.getValues().length; float[] floatData = new float[numSamples]; double[] values = data.getValues(); for (int i = 0; i < values.length; i++) { floatData[i] = (float) values[i]; } return new FloatData(floatData, data.getSampleRate(), data.getFirstSampleNumber()); } }
private boolean isAudioStreamContinuous(Data input) { if (input instanceof DoubleData) { DoubleData d = (DoubleData) input; if (lastSampleNum != -1 && lastSampleNum != d.getFirstSampleNumber()) { return false; } lastSampleNum = d.getFirstSampleNumber() + d.getValues().length; } else if (input instanceof DataStartSignal) lastSampleNum = -1; return true; } }
/** * Computes the next feature. Advances the pointers as well. * * @return the feature Data computed */ @Override protected Data computeNextFeature() { DoubleData currentCepstrum = cepstraBuffer[currentPosition]; float[] feature = new float[(window * 2 + 1) * currentCepstrum.getValues().length]; int j = 0; for (int k = -window; k <= window; k++) { int position = (currentPosition + k + cepstraBufferSize) % cepstraBufferSize; double[] buffer = cepstraBuffer[position].getValues(); for (double val : buffer) { feature[j++] = (float)val; } } currentPosition = (currentPosition + 1) % cepstraBufferSize ; return (new FloatData(feature, currentCepstrum.getSampleRate(), currentCepstrum.getFirstSampleNumber())); } }
/** * Process data, adding dither * * @param input a frame * @return processed frame * @throws IllegalArgumentException */ private DoubleData process(Data input) throws IllegalArgumentException { DoubleData output; assert input instanceof DoubleData; double[] inFeatures; DoubleData doubleData = (DoubleData) input; inFeatures = doubleData.getValues(); double[] outFeatures = new double[inFeatures.length]; for (int i = 0; i < inFeatures.length; ++i) { outFeatures[i] = r.nextFloat() * 2 * ditherMax - ditherMax + inFeatures[i]; outFeatures[i] = max(min(outFeatures[i], maxValue), minValue); } output = new DoubleData(outFeatures, doubleData.getSampleRate(), doubleData.getFirstSampleNumber()); return output; } }
/** * Returns the next DoubleData object, skipping frames with zero energy * * @return the next available DoubleData object, or null if no Data is available * @throws edu.cmu.sphinx.frontend.DataProcessingException * if a data processing error occurred */ @Override public Data getData() throws DataProcessingException { float energy = 0; Data input = null; do { input = getPredecessor().getData(); if (input == null || !(input instanceof DoubleData)) return input; energy = 0.0f; for (double d : ((DoubleData) input).getValues()) { energy += d * d; } } while (energy < maxEnergy); return input; } }
/** * Converts a given Data-object into a <code>FloatData</code> if possible. * @param data data to convert * @return converted data */ public static FloatData toFloatData(Data data) { FloatData convertData; if (data instanceof FloatData) convertData = (FloatData) data; else if (data instanceof DoubleData) { DoubleData dd = (DoubleData) data; convertData = new FloatData(MatrixUtils.double2float(dd.getValues()), dd.getSampleRate(), dd.getFirstSampleNumber()); } else throw new IllegalArgumentException("data type '" + data.getClass() + "' is not supported"); return convertData; } }
/** * Returns the next Data object being processed by this Preemphasizer, or if it is a Signal, it is returned without * modification. * * @return the next available Data object, returns null if no Data object is available * @throws DataProcessingException if there is a processing error * @see Data */ @Override public Data getData() throws DataProcessingException { Data input = getPredecessor().getData(); if (input != null) { if (input instanceof DoubleData) { applyPreemphasis(((DoubleData) input).getValues()); } else if (input instanceof DataEndSignal || input instanceof SpeechEndSignal) { prior = 0; } } return input; }
@Override public Data getData() throws DataProcessingException { Data data = getPredecessor().getData(); if (data instanceof FloatData) { float[] values = ((FloatData) data).getValues(); if (gainFactor != 1.0) { // apply the gain-factor for (int i = 0; i < values.length; i++) { values[i] *= gainFactor; } } } else if (data instanceof DoubleData) { double[] values = ((DoubleData) data).getValues(); if (gainFactor != 1.0) { // apply the gain-factor for (int i = 0; i < values.length; i++) { values[i] *= gainFactor; } } } return data; }
@Test public void testInsertAtBlockEnd() { RandomSampleRepeater rsr = ConfigurationManager.getInstance(RandomSampleRepeater.class); rsr.randStreamStart = true; rsr.useRandSeed = false; rsr.initialize(); rsr.maxRepeatedSamples = 3; rsr.numInsertedSamples = 20; rsr.nextInsertionPoint = 9; // create a dummy DoubleData double[] data = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; DoubleData dd = new DoubleData(data, 16000, 0); DoubleData extData = rsr.process(dd); double[] expectedData = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 10}; Assert.assertEquals(extData.getFirstSampleNumber(), 20, 0); Assert.assertEquals(extData.getValues().length, expectedData.length); for (int i = 0; i < expectedData.length; i++) { Assert.assertEquals(extData.getValues()[i], expectedData[i], 0); } }
@Test public void testInsertAtZero() { RandomSampleRepeater rsr = ConfigurationManager.getInstance(RandomSampleRepeater.class); rsr.randStreamStart = true; rsr.useRandSeed = false; rsr.initialize(); rsr.maxRepeatedSamples = 5; rsr.numInsertedSamples = 20; rsr.nextInsertionPoint = 5; // create a dummy DoubleData double[] data = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; DoubleData dd = new DoubleData(data, 16000, 0); DoubleData extData = rsr.process(dd); double[] expectedData = new double[]{1, 2, 3, 4, 5, 1, 2, 6, 7, 8, 9, 10}; Assert.assertEquals(extData.getFirstSampleNumber(), 20, 0); Assert.assertEquals(extData.getValues().length, expectedData.length); for (int i = 0; i < expectedData.length; i++) { Assert.assertEquals(extData.getValues()[i], expectedData[i], 0); } }
@Test public void testInsertAt4() { RandomSampleRepeater rsr = ConfigurationManager.getInstance(RandomSampleRepeater.class); rsr.randStreamStart = true; rsr.useRandSeed = false; rsr.initialize(); rsr.maxRepeatedSamples = 3; rsr.numInsertedSamples = 20; rsr.nextInsertionPoint = 5; // create a dummy DoubleData double[] data = new double[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; DoubleData dd = new DoubleData(data, 16000, 0); DoubleData extData = rsr.process(dd); double[] expectedData = new double[]{1, 2, 3, 4, 5, 1, 2, 6, 7, 8, 9, 10}; Assert.assertEquals(extData.getFirstSampleNumber(), 20, 0); Assert.assertEquals(extData.getValues().length, expectedData.length); for (int i = 0; i < expectedData.length; i++) { Assert.assertEquals(extData.getValues()[i], expectedData[i], 0); } }
@Override public Data getData() throws DataProcessingException { Data d = getPredecessor().getData(); if (d instanceof DoubleData && convMode.equals(CONVERT_D2F)) { DoubleData dd = (DoubleData) d; d = new FloatData(MatrixUtils.double2float(dd.getValues()), dd.getSampleRate(), dd.getFirstSampleNumber()); } else if (d instanceof FloatData && convMode.equals(CONVERT_F2D)) { FloatData fd = (FloatData) d; d = new DoubleData(MatrixUtils.float2double(fd.getValues()), fd.getSampleRate(), fd.getFirstSampleNumber()); } return d; } }