This commit is contained in:
Gianpaolo Coro 2013-05-03 16:59:52 +00:00
parent cc4be9ecca
commit 43033513df
4 changed files with 82 additions and 86 deletions

View File

@ -1,22 +1,3 @@
/**
* Copyright 2004-2006 DFKI GmbH.
* All Rights Reserved. Use is subject to license terms.
*
* This file is part of MARY TTS.
*
* MARY TTS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package marytts.signalproc.display;
import java.awt.BorderLayout;
@ -46,13 +27,11 @@ import javax.swing.ScrollPaneConstants;
import marytts.util.string.PrintfFormat;
/**
* @author Marc Schr&ouml;der
*
*/
public class FunctionGraphCustom extends FunctionGraph
{
public static final int DEFAULT_WIDTH=640;
private static final long serialVersionUID = 1L;
public static final int DEFAULT_WIDTH=640;
public static final int DEFAULT_HEIGHT=480;
public static final int DRAW_LINE = 1;
public static final int DRAW_DOTS = 2;

View File

@ -1,26 +1,5 @@
package marytts.signalproc.display;
/**
* Copyright 2004-2010 DFKI GmbH.
* All Rights Reserved. Use is subject to license terms.
*
* This file is part of MARY TTS.
*
* MARY TTS is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
@ -35,7 +14,6 @@ import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.swing.BoxLayout;
@ -53,13 +31,11 @@ import marytts.signalproc.analysis.CepstrumSpeechAnalyser;
import marytts.signalproc.analysis.FrameBasedAnalyser;
import marytts.signalproc.analysis.LpcAnalyser;
import marytts.signalproc.analysis.ShortTermLogSpectrumAnalyser;
import marytts.signalproc.display.FunctionGraph;
import marytts.signalproc.filter.FIRFilter;
import marytts.signalproc.window.HammingWindow;
import marytts.signalproc.window.RectWindow;
import marytts.signalproc.window.Window;
import marytts.util.data.BufferedDoubleDataSource;
import marytts.util.data.audio.AudioDoubleDataSource;
import marytts.util.math.ArrayUtils;
import marytts.util.math.FFT;
import marytts.util.math.MathUtils;
@ -67,13 +43,11 @@ import marytts.util.string.PrintfFormat;
/**
* @author Marc Schr&ouml;der
*
*/
public class SpectrogramCustom extends FunctionGraphCustom
{
public static final int DEFAULT_WINDOWSIZE = 65;
private static final long serialVersionUID = 1L;
public static final int DEFAULT_WINDOWSIZE = 65;
public static final int DEFAULT_WINDOW = Window.HAMMING;
public static final int DEFAULT_WINDOWSHIFT = 32;
public static final int DEFAULT_FFTSIZE = 256;

View File

@ -6,6 +6,9 @@ import java.util.Arrays;
import javax.swing.JPanel;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import marytts.signalproc.display.SpectrogramCustom;
import marytts.signalproc.window.Window;
@ -100,7 +103,7 @@ public class SignalConversions {
return cutSpectrum;
}
public static void exampleSet2Signal(double[] rebuiltSignal, ExampleSet es) {
public static void exampleSet2Signal(double[] rebuiltSignal, ExampleSet es, Double fillerValueFormissingEntries) {
MemoryExampleTable met = (MemoryExampleTable) es.getExampleTable();
int numCol = met.getAttributeCount();
@ -115,24 +118,32 @@ public class SignalConversions {
Example e = es.getExample(i);
// System.out.println(es.getExample(i)+"->"+signal[i]);
for (Attribute a : e.getAttributes()) {
rebuiltSignal[id] = e.getValue(a);
Double value = e.getValue(a);
if (value.equals(Double.NaN) && !fillerValueFormissingEntries.equals(Double.NaN))
value = fillerValueFormissingEntries;
rebuiltSignal[id] = value;
}
}
}
public static void exampleSet2Signal(double[] rebuiltSignal, ExampleSet es) {
exampleSet2Signal(rebuiltSignal, es, null);
}
public static int[] fftBinIndices(double samplingRate, int frameSize, int numMelFilters, int numFequencies, float lowerFilterFreq, float upperFilterFreq) {
int cbin[] = new int[numFequencies+2];
System.out.println("New Filter banks: "+numFequencies);
AnalysisLogger.getLogger().debug("New Filter banks: "+numFequencies);
cbin[0] = (int) Math.round(lowerFilterFreq / samplingRate * frameSize);
cbin[cbin.length - 1] = frequencyIndex(upperFilterFreq, frameSize, (float)samplingRate);
System.out.println("F0: "+lowerFilterFreq);
AnalysisLogger.getLogger().debug("F0: "+lowerFilterFreq);
for (int i = 1; i <= numFequencies; i++) {
double fc = centerFreq(i, samplingRate, lowerFilterFreq, numMelFilters);
System.out.println("F"+(i)+": "+fc);
AnalysisLogger.getLogger().debug("F"+(i)+": "+fc);
cbin[i] = (int) Math.round(fc / samplingRate * frameSize);
}
System.out.println("F"+(cbin.length - 1)+": "+upperFilterFreq);
AnalysisLogger.getLogger().debug("F"+(cbin.length - 1)+": "+upperFilterFreq);
return cbin;
}
@ -213,7 +224,7 @@ public class SignalConversions {
int bestIndex=1;
for (int i = 1; i <= numMelFilters; i++) {
double fc = centerFreq(i, samplingRate, lowerFilterFreq, numMelFilters);
System.out.println("fc "+fc);
AnalysisLogger.getLogger().debug("fc "+fc);
if (fc>maxFilterFreq){
bestIndex=i;
break;
@ -234,7 +245,7 @@ public class SignalConversions {
for (int i = 0; i < signalLength; i++) {
time[i] = (double) i / (double) samplingRate;
}
System.out.println("time " + time[signalLength - 1] * samplingRate + " vs " + signalLength);
AnalysisLogger.getLogger().debug("time " + time[signalLength - 1] * samplingRate + " vs " + signalLength);
return time;
}
@ -242,6 +253,15 @@ public class SignalConversions {
return linearTime / windowShiftTime;
}
public static ExampleSet signal2ExampleSet(double[] signal){
BigSamplesTable samples = new BigSamplesTable();
for (int k = 0; k < signal.length; k++) {
samples.addSampleRow("" + k, signal[k]);
}
AnalysisLogger.getLogger().debug("Example Set Created");
return samples.generateExampleSet();
}
public static double[][] spectrogram(String name, double[] signal, int samplingRate, int windowshift, int frameslength, boolean display) throws Exception {
SpectrogramCustom spec = new SpectrogramCustom(signal, samplingRate, Window.get(Window.HAMMING, frameslength), windowshift, frameslength, 640, 480);
double[][] spectrum = spec.spectra.toArray(new double[spec.spectra.size()][]);

View File

@ -11,12 +11,13 @@ import java.util.List;
import org.gcube.contentmanagement.graphtools.data.BigSamplesTable;
import org.gcube.contentmanagement.graphtools.plotting.graphs.NumericSeriesGraph;
import org.gcube.contentmanagement.graphtools.plotting.graphs.TimeSeriesGraph;
import org.gcube.contentmanagement.lexicalmatcher.utils.AnalysisLogger;
import org.gcube.contentmanagement.lexicalmatcher.utils.MathFunctions;
import org.gcube.dataanalysis.ecoengine.configuration.AlgorithmConfiguration;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYSeriesCollection;
import com.rapidminer.RapidMiner;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.preprocessing.sampling.AbsoluteSampling;
import com.rapidminer.operator.preprocessing.series.filter.SeriesMissingValueReplenishment;
@ -54,20 +55,20 @@ public class SignalProcessing {
public static double[][] calculateSumSpectrum(List<double[]> signals, int windowShiftSamples, int frameLength, int samplingRate) throws Exception {
int signalLenght = signals.get(0).length;
System.out.println("TRIALS LENGHT " + signalLenght);
AnalysisLogger.getLogger().debug("TRIALS LENGHT " + signalLenght);
List<double[][]> spectrograms = new ArrayList<double[][]>();
System.out.println("Getting Spectra");
AnalysisLogger.getLogger().debug("Getting Spectra");
int j = 0;
// get all spectrograms
for (double[] signal : signals) {
double[][] spectro = SignalConversions.spectrogram("Spectrogram", signal, samplingRate, windowShiftSamples, frameLength, false);
System.out.println("Signal Number " + (j + 1) + " spectrum lenght " + ((spectro.length * windowShiftSamples) / samplingRate));
AnalysisLogger.getLogger().debug("Signal Number " + (j + 1) + " spectrum lenght " + ((spectro.length * windowShiftSamples) / samplingRate));
spectrograms.add(spectro);
j++;
}
System.out.println("Summing Spectra");
AnalysisLogger.getLogger().debug("Summing Spectra");
// sum all spectrograms
double[][] sumSpectro = SignalProcessing.sumSpectra(spectrograms);
spectrograms = null;
@ -122,9 +123,8 @@ public class SignalProcessing {
tsg.render(dataset);
}
public static double[] downSample(double[] signal, int numElements) throws Exception {
System.setProperty("rapidminer.init.operators", "operators.xml");
RapidMiner.init();
public static double[] downSample(double[] signal, int numElements, AlgorithmConfiguration config) throws Exception {
config.initRapidMiner();
double[] rebuiltSignal = new double[signal.length];
Arrays.fill(rebuiltSignal, Double.NaN);
BigSamplesTable samples = new BigSamplesTable();
@ -133,13 +133,13 @@ public class SignalProcessing {
}
ExampleSet es = samples.generateExampleSet();
System.out.println("Example Set Created");
AnalysisLogger.getLogger().debug("Example Set Created");
AbsoluteSampling sampler = (AbsoluteSampling) OperatorService.createOperator("AbsoluteSampling");
sampler.setParameter("sample_size", "" + numElements);
sampler.setParameter("local_random_seed", "-1");
es = sampler.apply(es);
System.out.println("Finished");
AnalysisLogger.getLogger().debug("Finished");
SignalConversions.exampleSet2Signal(rebuiltSignal, es);
@ -154,17 +154,17 @@ public class SignalProcessing {
List<double[][]> sumspectrograms = new ArrayList<double[][]>();
List<double[][]> spectrograms = new ArrayList<double[][]>();
System.out.println("Getting Spectra");
AnalysisLogger.getLogger().debug("Getting Spectra");
int j = 0;
// get all spectrograms
for (double[] signal : signals) {
System.out.println("Signal Number " + (j + 1));
AnalysisLogger.getLogger().debug("Signal Number " + (j + 1));
double[][] spectro = SignalConversions.spectrogram("Spectrogram", signal, samplingRate, windowShiftSamples, frameLength, false);
spectrograms.add(spectro);
j++;
}
System.out.println("Summing Spectra");
AnalysisLogger.getLogger().debug("Summing Spectra");
// sum all spectrograms
double[][] sumSpectro = SignalProcessing.sumSpectra(spectrograms);
spectrograms = null;
@ -172,11 +172,11 @@ public class SignalProcessing {
return sumSpectro;
}
public static Date[] fillTimeLine(double[] timeseconds, double samplingRate) throws Exception {
public static Date[] fillTimeLine(double[] timeseconds, double samplingRate, AlgorithmConfiguration config) throws Exception {
double[] values = new double[timeseconds.length];
for (int i = 0; i < timeseconds.length; i++)
values[i] = timeseconds[i] * 1000d;
double[] milliseconds = fillTimeSeries(values, timeseconds, samplingRate);
double[] milliseconds = fillTimeSeries(values, timeseconds, samplingRate, config);
Date[] dates = new Date[milliseconds.length];
for (int i = 0; i < milliseconds.length; i++)
dates[i] = new Date((long) milliseconds[i]);
@ -184,10 +184,21 @@ public class SignalProcessing {
return dates;
}
public static double[] fillTimeSeries(double[] values, double[] timeseconds, double samplingRate) throws Exception {
System.setProperty("rapidminer.init.operators", "operators.xml");
RapidMiner.init();
public static double[] fillSignal(double[] signal) throws Exception{
ExampleSet es = SignalConversions.signal2ExampleSet(signal);
SeriesMissingValueReplenishment sampler = (SeriesMissingValueReplenishment) OperatorService.createOperator("SeriesMissingValueReplenishment");
sampler.setParameter("attribute_name", "att0");
sampler.setParameter("replacement", "3");
es = sampler.apply(es);
AnalysisLogger.getLogger().debug("Finished");
double[] rebuiltSignal = new double[signal.length];
SignalConversions.exampleSet2Signal(rebuiltSignal, es,0d);
return rebuiltSignal;
}
public static double[] fillTimeSeries(double[] values, double[] timeseconds, double samplingRate, AlgorithmConfiguration config) throws Exception {
config.initRapidMiner();
double t0 = timeseconds[0];
double t1 = timeseconds[timeseconds.length - 1];
int signalength = (int) ((t1 - t0) * samplingRate) + 1;
@ -207,13 +218,13 @@ public class SignalProcessing {
}
ExampleSet es = samples.generateExampleSet();
System.out.println("Example Set Created");
AnalysisLogger.getLogger().debug("Example Set Created");
SeriesMissingValueReplenishment sampler = (SeriesMissingValueReplenishment) OperatorService.createOperator("SeriesMissingValueReplenishment");
sampler.setParameter("attribute_name", "att0");
sampler.setParameter("replacement", "3");
es = sampler.apply(es);
System.out.println("Finished");
AnalysisLogger.getLogger().debug("Finished");
SignalConversions.exampleSet2Signal(rebuiltSignal, es);
@ -278,15 +289,15 @@ public class SignalProcessing {
float centralTime = (maxTime / (2f * numOfCentralSeconds));
System.out.println("Max Time in the Spectrum " + maxTime + " Central time " + centralTime);
AnalysisLogger.getLogger().debug("Max Time in the Spectrum " + maxTime + " Central time " + centralTime);
int startIndex = (int) (centralTime / windowShiftTime);
int endIndex = (int) ((centralTime + numOfCentralSeconds) / windowShiftTime);
System.out.println("Recalculated lenght " + maxTime + " sec");
System.out.println("Lenght " + spectrum.length);
AnalysisLogger.getLogger().debug("Recalculated lenght " + maxTime + " sec");
AnalysisLogger.getLogger().debug("Lenght " + spectrum.length);
System.out.println("Start " + startIndex + " End " + endIndex + " max " + spectrum.length + " Cut lenght " + (endIndex - startIndex + 1) * windowShiftTime);
AnalysisLogger.getLogger().debug("Start " + startIndex + " End " + endIndex + " max " + spectrum.length + " Cut lenght " + (endIndex - startIndex + 1) * windowShiftTime);
double[][] cutSpectrum = new double[endIndex - startIndex + 1][spectrum[0].length];
@ -297,4 +308,16 @@ public class SignalProcessing {
return cutSpectrum;
}
public static void main(String[] args) throws Exception{
AlgorithmConfiguration conf = new AlgorithmConfiguration();
conf.setConfigPath("./cfg/");
conf.initRapidMiner();
// double[] signal = new double[] {1,2,Double.NaN,4,5};
double[] signal = new double[] {Double.NaN,1,2,3,4,5,Double.NaN};
// double[] signal = new double[] {Double.NaN,Double.NaN,Double.NaN};
// double[] signal = new double[] {Double.NaN,Double.NaN,0};
double[] resignal = fillSignal(signal);
System.out.println(resignal);
}
}