This commit is contained in:
Gianpaolo Coro 2014-02-04 12:36:48 +00:00
parent 7e8b730858
commit 3b071a58f9
1 changed files with 87 additions and 0 deletions

View File

@ -303,4 +303,91 @@ public class SignalConversions {
return (int) (time * sampleRate);
}
static double[] takeMaxFrequenciesInSpectrogram(double[][] spectrogram, int samplingRate,int windowSamples, float minfreq) {
double[] maxs = new double[spectrogram.length];
int j = 0;
for (double[] slice : spectrogram) {
int bestidx = 0;
double max = -Double.MAX_VALUE;
for (int k = 0; k < slice.length; k++) {
double ele = slice[k];
if (ele > max) {
max = ele;
bestidx = k;
}
}
// maxs[j] = spectrogram[j][bestidx];
// maxs[j]=bestidx;
int minFidx = SignalConversions.frequencyIndex(minfreq, windowSamples, samplingRate);
// System.out.println("min f idx: "+minFidx);
maxs[j] = spectrumIdx2Frequency(minFidx+bestidx,samplingRate,windowSamples);
j++;
}
return maxs;
}
static double[] takeLongestStableTract(double[] signal, double valuedifftoleranceperc) {
ArrayList<int[]> pairs = new ArrayList<int[]>();
int idx1 = -1;
int[] pair = null;
//analyze the signal
for (int i = 1; i < signal.length; i++) {
//if there is not current range create it
if (idx1 == -1) {
idx1 = 1;
pair = new int[2];
pair[0] = i - 1;
pair[1] = i - 1;
}
//if the current sample is similar to the previous, enlarge the range
if (Math.abs(signal[i] - signal[i - 1])/Math.max(signal[i],signal[i - 1]) <= valuedifftoleranceperc)
pair[1] = i;
//otherwise add the couple and reset
else {
idx1 = -1;
pairs.add(pair);
}
}
//if the last couple was reset, add the last interval
if (idx1 > -1)
pairs.add(pair);
//find the longest pair
int best = 0;
int maxsize = 0;
int k = 0;
for (int[] setcouple : pairs) {
int diff = setcouple[1] - setcouple[0];
if (diff > maxsize) {
maxsize = diff;
best = k;
}
k++;
}
//take the longest range
int[] bestcouple = pairs.get(best);
//take the related slice of signal
double[] subsignal = new double[bestcouple[1] - bestcouple[0]];
System.out.println("Longest range: from "+bestcouple[0]+" to "+bestcouple[1]);
int l = 0;
for (int i = bestcouple[0]; i < bestcouple[1]; i++) {
subsignal[l] = signal[i];
l++;
}
return subsignal;
}
static float spectrumIdx2Frequency(int idx,int samplingRate,int windowsSizeSamples){
return ((float)idx*samplingRate)/(float)(windowsSizeSamples-1);
}
static int spectrumFreq2Idx(float freq, int samplingRate,int windowsSizeSamples){
return Math.round((windowsSizeSamples-1)*freq/samplingRate);
}
}