Skip to content

Instantly share code, notes, and snippets.

@jjzazuet
Last active January 24, 2022 16:41
Show Gist options
  • Save jjzazuet/d34a62903a6373966ffb4e27bee7589d to your computer and use it in GitHub Desktop.
Save jjzazuet/d34a62903a6373966ffb4e27bee7589d to your computer and use it in GitHub Desktop.
Volach - unused code
public void performAsyncAction(Runnable r, int tries) {
if (tries > 0) {
executor.execute(new Runnable() {
int left = tries;
public void run() {
try { r.run(); } catch (Throwable t) {
log.error("Some error");
if(--left > 0) {
executor.schedule(this, sleepSeconds, TimeUnit.SECONDS);
} else {
log.error("No more retries. Stop");
}
}
}
});
}
}
import java.io.Serializable;
public class BxBitSet implements Serializable, Cloneable {
private static final long serialVersionUID = 1L;
public final long[] bits;
public final int numBits;
public BxBitSet(BxBitSet other) {
bits = other.bits.clone();
numBits = other.numBits;
}
public BxBitSet(int numBits) {
int numLongs = numBits >>> 6;
if ((numBits & 0x3F) != 0) {
numLongs++;
}
bits = new long[numLongs];
this.numBits = numBits;
}
private BxBitSet(int numBits, long[] bits) {
this.numBits = numBits;
this.bits = bits;
}
public boolean get(int index) {
return (bits[index >>> 6] & 1L << (index & 0x3F)) != 0L;
}
public void set(int index, boolean b) {
if(b) bits[index >>> 6] |= 1L << (index & 0x3F);
else bits[index >>> 6] &= ~(1L << (index & 0x3F));
}
public void set(int index) {
bits[index >>> 6] |= 1L << (index & 0x3F);
}
public void clear(int index) {
bits[index >>> 6] &= ~(1L << (index & 0x3F));
}
public void clear() {
int length = bits.length;
for (int i = 0; i < length; i++) {
bits[i] = 0L;
}
}
@Override public BxBitSet clone() {
return new BxBitSet(numBits, bits.clone());
}
@Override public String toString() {
StringBuilder result = new StringBuilder(64 * bits.length);
for (long l : bits) {
for (int j = 0; j < 64; j++) {
result.append((l & 1L << j) == 0 ? '0' : '1');
}
result.append(' ');
}
return result.toString();
}
}
package io.vacco.volach.fprint;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;
public class VlBark {
public static final int[] bandEdges = {
0, 100, 200, 300, 400, 510, 630, 770,
920, 1080, 1270, 1480, 1720, 2000, 2320, 2700,
3150, 3700, 4400, 5300, 6400, 7700, 9500, 12000,
15500
};
public static float[] energyGroup(FloatBuffer freqBandEnergy, int sampleRate) {
int nyquistFreq = sampleRate / 2;
List<Float> groups = new ArrayList<>();
int idx0, idx1;
for (int k = 0; k < bandEdges.length - 1; k++) {
idx0 = (bandEdges[k] * freqBandEnergy.capacity()) / nyquistFreq;
idx1 = (bandEdges[k + 1] * freqBandEnergy.capacity()) / nyquistFreq;
if (idx0 < freqBandEnergy.capacity() && idx1 < freqBandEnergy.capacity()) {
float bandEnr = 0;
for (int i = idx0; i < idx1; i++) {
float freqEnr = freqBandEnergy.get(i);
bandEnr += Math.abs(freqEnr);
}
groups.add(bandEnr);
}
}
float[] out = new float[groups.size()];
for (int j = 0; j < groups.size(); j++) {
out[j] = groups.get(j);
}
return out;
}
}
public static void toBits32(int x, double[] dest) {
for (int i = 31; i >= 0 ; i--) {
int mask = 1 << i;
dest[i] = (x & mask) != 0 ? 1 : 0;
}
reverse(dest);
}
public static int fromBits32(double[] bits) {
int n = 0;
for (double bit : bits) {
n = (n << 1) + (bit >= 0.5 ? 1 : 0);
}
return n;
}
public static void reverse(double[] input) {
if(input == null || input.length <= 1){ return; }
double tmp;
for (int i = 0; i < input.length / 2; i++) {
tmp = input[i];
input[i] = input[input.length - 1 - i];
input[input.length - 1 - i] = tmp;
}
}
package io.vacco.volach.dsp;
public class VlButterworthLp {
public double[] yf0, yf1, xf;
public double[] xv, yv;
public double scale = 1;
public VlButterworthLp(int order, int cuttoffHz, int frequencyHz) {
double invert = 1;
double cutoff = -(cuttoffHz / (double) frequencyHz * 2 * Math.PI);
xv = new double[order];
yv = new double[order];
yf0 = new double[order + 1];
yf1 = new double[order + 1];
xf = new double[order + 1];
yf0[0] = -1;
yf1[0] = 0;
xf[0] = 1;
for (int i = 1; i <= order; i++) {
double angle = ((i - 0.5) / order) * Math.PI;
double sinsin = 1 - Math.sin(cutoff) * Math.sin(angle);
double rcof0 = -Math.cos(cutoff) / sinsin;
double rcof1 = Math.sin(cutoff) * Math.cos(angle) / sinsin;
yf0[i] = 0;
yf1[i] = 0;
for (int j = i; j > 0; --j) {
yf0[j] += rcof0 * yf0[j - 1] + rcof1 * yf1[j - 1];
yf1[j] += rcof0 * yf1[j - 1] - rcof1 * yf0[j - 1];
}
scale *= sinsin * 2 / (1 - Math.cos(cutoff) * invert);
xf[i] = xf[i - 1] * invert * (order - i + 1) / i;
}
scale = Math.sqrt(scale);
for (var i = 1; i <= order; ++i) { yf0[i] *= scale; }
}
public double apply(double input) {
if (xv.length - 1 >= 0) System.arraycopy(xv, 1, xv, 0, xv.length - 1);
xv[xv.length - 1] = input / scale;
if (yv.length - 1 >= 0) System.arraycopy(yv, 1, yv, 0, yv.length - 1);
double xvi = xv[0] + xv[xv.length - 1];
/*
yv[yv.length - 1] =
(
(xv[0] + xv[4])
- (dCoefficient1 * xv[2])
+ (dCoefficient2 * yv[0])
+ (dCoefficient3 * yv[1])
+ (dCoefficient4 * yv[2])
+ (dCoefficient5 * yv[3])
);
*/
return (yv[4]);
}
}
package io.vacco.volach.dsp;
public class VlFft {
private final float[] window;
public VlFft(int sampleSize) {
this.window = VlHammingWindow.generateCurve(sampleSize);
}
public VlFftSample fft(float[] inputReal, float[] inputImag, boolean direct) {
int n = inputReal.length;
double ld = Math.log(n) / Math.log(2.0);
if (((int) ld) - ld != 0) {
throw new IllegalArgumentException("The number of elements is not a power of 2.");
}
int nu = (int) ld;
int n2 = n / 2;
int nu1 = nu - 1;
float[] xReal = new float[n];
float[] xImag = new float[n];
float tReal, tImag, p, arg, c, s;
float constant = (float) (direct ? -2 * Math.PI : 2 * Math.PI);
for (int i = 0; i < n; i++) {
xReal[i] = inputReal[i];
xImag[i] = inputImag[i];
}
// First phase - calculation
int k = 0;
for (int l = 1; l <= nu; l++) {
while (k < n) {
for (int i = 1; i <= n2; i++) {
p = bitreverseReference(k >> nu1, nu);
arg = constant * p / n;
c = (float) Math.cos(arg);
s = (float) Math.sin(arg);
tReal = xReal[k + n2] * c + xImag[k + n2] * s;
tImag = xImag[k + n2] * c - xReal[k + n2] * s;
xReal[k + n2] = xReal[k] - tReal;
xImag[k + n2] = xImag[k] - tImag;
xReal[k] += tReal;
xImag[k] += tImag;
k++;
}
k += n2;
}
k = 0;
nu1--;
n2 /= 2;
}
// Second phase - recombination
k = 0;
int r;
while (k < n) {
r = bitreverseReference(k, nu);
if (r > k) {
tReal = xReal[k];
tImag = xImag[k];
xReal[k] = xReal[r];
xImag[k] = xImag[r];
xReal[r] = tReal;
xImag[r] = tImag;
}
k++;
}
return VlFftSample.from(xReal, xImag);
}
public VlFftSample fft(float[] inputReal, boolean direct) {
if (inputReal.length != window.length) {
throw new IllegalArgumentException(
String.format("Invalid sample size: %s", inputReal.length));
}
float[] windowed = new float[inputReal.length];
for (int i = 0; i < inputReal.length; i++) {
windowed[i] = inputReal[i] * this.window[i];
}
return fft(windowed, new float[inputReal.length], direct);
}
private int bitreverseReference(int j, int nu) {
int j2;
int j1 = j;
int k = 0;
for (int i = 1; i <= nu; i++) {
j2 = j1 / 2;
k = 2 * k + j1 - 2 * j2;
j1 = j2;
}
return k;
}
}
package io.vacco.volach.dsp;
public class VlFftSample {
public float[] real;
public float[] imaginary;
public float[] composite;
public static VlFftSample from(float[] real, float[] imaginary) {
VlFftSample sample = new VlFftSample();
sample.real = real;
sample.imaginary = imaginary;
sample.composite = new float[real.length];
for (int i = 0; i < real.length / 2; i++) {
sample.composite[i * 2] = real[i];
sample.composite[i * 2 + 1] = -imaginary[i];
}
return sample;
}
}
package io.vacco.volach;
import static j8spec.J8Spec.it;
import io.vacco.volach.dsp.VlButterworthLp;
import io.vacco.volach.dsp.VlFft;
import io.vacco.volach.dsp.VlFftSample;
import j8spec.annotation.DefinedOrder;
import j8spec.junit.J8SpecRunner;
import java.text.DecimalFormat;
import java.util.Arrays;
import org.junit.runner.RunWith;
@DefinedOrder
@RunWith(J8SpecRunner.class)
public class VlFftSpec {
private static DecimalFormat df = new DecimalFormat("00.000000");
private static float[] copy(double[] input) {
float[] out = new float[input.length];
for (int k = 0; k < input.length; k++) {
out[k] = (float) input[k];
}
return out;
}
private static void printChunked(int chunkSize, float[] a) {
for (int i = 0; i < a.length; i += chunkSize) {
float[] slice = Arrays.copyOfRange(a, i, Math.min(a.length, i + chunkSize));
for (float v : slice) { System.out.print((v < 0 ? "" : " ") + df.format(v) + " " ); }
System.out.println();
}
System.out.println();
}
static {
it("Can perform forward FFT on audio input values", () -> {
double[] inputD = {0.02499466, -0.09680471, -0.061311685, -0.09573656, -0.12631612, -0.1754509, -0.18015076, -0.07513657, 0.00695822, 0.035523545, 0.055604726, 0.0593585, 0.09997864, 0.1897641, 0.14844203, 0.03888058, 0.0026856288, -0.05465865, -0.081148714, -0.10715049, -0.15561388, -0.17105624, -0.16843165, -0.016266365, 0.12631612, 0.107181005, 0.084810935, 0.06918546, 0.17920469, 0.23474838, 0.11111789, -0.05133213, -0.08966338, -0.06985687, -0.09521775, -0.13681448, -0.17535935, -0.15442365, -0.044923246, 0.04260384, 0.048677024, 0.057191685, 0.10586871, 0.12802514, 0.13324381, 0.1223487, 0.043122653, -0.013794366, -0.06259346, -0.12829982, -0.11813715, -0.17975402, -0.17578661, -0.12726219, 0.04278695, 0.11307108, 0.10754722, 0.08719138, 0.108401746, 0.24314097, 0.17337565, 0.051057465, -0.07763909, -0.08432264, -0.087496564, -0.12424085, -0.16608173, -0.17896053, -0.095919676, -0.008484146, 0.055757318, 0.056215093, 0.0819422, 0.14252144, 0.16345714, 0.11825922, 0.051026948, -0.0075075533, -0.029084139, -0.0654622, -0.120365, -0.15408796, -0.13428144, -0.15512559, -0.075106055, 0.09121983, 0.0741905, 0.096591085, 0.094912566, 0.150853, 0.20648824, 0.11951049, -0.03219703, -0.07971434, -0.084017456, -0.10968352, -0.13486129, -0.15808588, -0.14844203, -0.048646502, 0.032258064, 0.046937466, 0.08862575, 0.10278634, 0.15692617, 0.14636678, 0.08728294, 0.018494217, -0.0021057772, -0.052705467, -0.10312204, -0.11941893, -0.12668233, -0.086336866, -0.15805537, -0.0025635548, 0.0806299, 0.0995819, 0.09601123, 0.075807974, 0.15756707, 0.16470839, 0.055726796, -0.0753502, -0.08752708, -0.09598071, -0.100039676, -0.1274453, -0.14697714, -0.09207434, 0.005981628, 0.05035554, 0.0651265, 0.0880459, 0.1073336, 0.14340648, 0.10934782, 0.045716725, -0.013153478, -0.029084139, -0.06424146, -0.07950072, -0.12909329, -0.103946045, -0.078127384, -0.0056459242, 0.056031983, 0.03460799, 0.07907346, 0.06515702, 0.12417982, 0.13071078, 0.049897764, -0.047761466, -0.054414503, -0.087496564, -0.09884945, -0.105380416, -0.11795404, -0.08172857, 1.5259255E-4, 0.016876735, 0.046296578, 0.098391674, 0.082461014, 0.115970336, 0.083254494, 0.049012728, 0.02438429, -0.014831996, -0.069093905, -0.06552324, -0.083895385, -0.101260416, -0.040437024, -0.079989016, 0.03234962, 0.10666219, 0.084353164, 0.016418958, 0.056978058, 0.11169775, 0.11026338, -0.009857479, -0.10324412, -0.08505508, -0.071779534, -0.075655386, -0.09253212, -0.113956116, -0.037049472, 0.05487228, 0.04895169, 0.07351909, 0.06634724, 0.093478195, 0.111087374, 0.055391096, 0.005035554, -0.0168157, -0.03540147, -0.06756798, -0.063844725, -0.111514635, -0.057649463, -0.050447095, -0.034455396, 0.08560442, 0.077242345, 0.06604205, 0.051057465, 0.09222694, 0.107394636, 0.064760275, -0.06927702, -0.10879849, -0.07058931, -0.09601123, -0.0799585, -0.10901211, -0.080782495, 0.014557329, 0.06158635, 0.05221717, 0.08990753, 0.079683825, 0.10547197, 0.10296945, 0.029602954, 4.5777764E-4, -0.032105472, -0.055757318, -0.0819422, -0.077242345, -0.11450545, -0.04135258, -0.061311685, -2.1362957E-4, 0.09643849, 0.07620472, 0.07361065, 0.061494797, 0.10431226, 0.10400708, 0.022583697, -0.08627582, -0.078768276, -0.08844264, -0.092684716, -0.06900235, -0.122562334, -0.073213905, 0.03500473, 0.04275643, 0.069429606, 0.07415998, 0.07348857, 0.120212406, 0.098605305, 0.01843318, 0.0057679983, -0.037720878, -0.05856502, -0.05261391, -0.103732415, -0.104525894, -0.049684133, -0.06637776, 0.026886806, 0.08371227, 0.05267495, 0.07000946, 0.08282723, 0.119205296, 0.103732415, -0.0110477, -0.066866055, -0.05114902, -0.086703084, -0.0896939, -0.10281686, -0.11761834, -0.034669027, 0.017365031, 0.016266365, 0.054109316, 0.065767385, 0.09875789, 0.1167333, 0.06747642, 0.03585925, 0.0135197, -0.020813623, -0.040803246, -0.065981016, -0.112186044, -0.06894131, -0.07528916, -0.06738487, 0.04177984, 0.05121006, 0.05178991, 0.0568865, 0.08819849, 0.13309123, 0.08874783, -0.02887051, -0.03701895, -0.047975097, -0.073427536, -0.07272561, -0.12387463, -0.10266426, -0.020508438, -0.00634785, 0.008911405, 0.038453322, 0.042725913, 0.11001923, 0.104525894, 0.056093022, 0.052278206, 0.023316141, -0.0056764428, -0.011139256, -0.0700705, -0.100619525, -0.04934843, -0.102267526, -0.034546953, 0.033997618, 0.011108737, 0.027314067, 0.052339245, 0.09634694, 0.12723167, 0.04364147, -0.031373028, -7.9348125E-4, -0.036774803, -0.05630665, -0.075807974, -0.11841182, -0.06482132, -0.020813623, -0.031800285, 0.0061952574, 0.02148503, 0.050202947, 0.100161746, 0.06863613, 0.0568865, 0.057985168, 0.031708732, 0.010986663, -0.00286874, -0.07626575, -0.052766502, -0.06366161, -0.09399701, 0.003295999, 0.0062562944, -0.0042725913, 0.0046388134, 0.051728874, 0.10177923, 0.09475997, -0.009765923, -0.010010071, 0.0018616291, -0.02188177, -0.03439436, -0.09399701, -0.09121983, -0.026642658, -0.024842067, -0.023438215, -0.0013733329, 0.00900296, 0.07208472, 0.07773064, 0.041932434, 0.05178991, 0.04977569, 0.029663991, 0.02496414, -0.033326212, -0.06323435, -0.024231697, -0.08908353, -0.041016877, 0.012634663, -0.01043733, -0.007721183, 0.017853327, 0.064851835, 0.10531937, 0.030854214, -0.030640583, 0.001678518, -0.020508438, -0.025940733, -0.05383465, -0.09842219, -0.040223397, -0.003173925, -0.019379254, 0.0024414808, 0.0043336283, 0.04364147, 0.08517716, 0.043702506, 0.034455396, 0.03823969, 0.021210365, 0.008301035, -0.010864589, -0.07235938, -0.036286507, -0.05017243, -0.076021604, 0.021668142, 0.022827845, 0.008575701, 0.016876735, 0.052064575, 0.10855434, 0.08337657, -0.027588733, -0.03070162, -0.026245918, -0.044557024, -0.047608875, -0.09686575, -0.084475234, -0.0073854793, -0.008636738, 0.0064394055, 0.018158514, 0.03158666, 0.08786279, 0.082461014, 0.038392283, 0.040009767, 0.024506364, 0.0053712577, -0.009399701, -0.05728324, -0.0799585, -0.036561176, -0.08395642, -0.041016877, 0.03070162, 0.015411847, 0.026184881, 0.032898955, 0.09018219, 0.12204352, 0.039429914, -0.029694509, -0.01742607, -0.039429914, -0.04242073, -0.07473983, -0.109195225, -0.051301613, -0.009582812, -0.015686514, 0.008941923, 0.012298959, 0.057252724, 0.10031434, 0.058442947, 0.045014802, 0.04458754, 0.02557451, 0.008087405, -0.023590807, -0.07873775, -0.057832576, -0.05389569, -0.090151675, -0.01413007, 0.012604144, 0.009155553, 0.02478103, 0.044404432, 0.10803552, 0.10400708, 0.011200293, -0.010010071, -0.019348735, -0.033600878, -0.03421125, -0.09179968, -0.09436323, -0.031159397, -0.023865474, -0.013245033, -0.0039368877, 0.0069277016, 0.072634056, 0.0826136, 0.044373915, 0.049409468, 0.03863643, 0.032105472, 0.018402662, -0.037598804, -0.06732383, -0.03485214, -0.06610309};
double[] fftOutputD = {-0.025998052, 0.06367779, -0.061211377, -0.0010892637, -0.032186955, 0.013458487, -0.035592765, 0.00766284, -0.03425049, -0.0019414928, -0.041701704, -0.008966502, -0.045855448, 0.0012086481, -0.03941124, 0.01090553, -0.040721923, 0.010832518, -0.037232902, 0.0072392276, -0.04172233, -0.011769835, -0.050473005, 0.024541572, -0.019623198, -0.013850713, -0.07618512, 0.007908927, -0.13930172, 0.04550442, -0.095395744, 0.1283765, 0.38694564, -0.5313025, -0.42918286, 0.95781267, 0.09672834, -0.17015845, 0.017908424, -0.26579103, -0.103713304, 0.15124303, -0.062161237, -0.0028984398, -0.057577327, -0.0024504662, -0.07489918, 0.029624686, -0.055161364, 0.073421046, -0.058168277, 0.061086774, -0.11509833, -0.009060569, -0.08238542, 0.020382766, -0.075820625, -0.048882075, -0.0011589853, 0.17243958, 0.5292605, 0.5882932, -2.235452, -1.2240139, 3.4602468, -0.36108613, -10.714348, 4.308201, 9.346253, -0.27821493, -0.04660511, -2.2871642, -0.43236795, 0.07659079, -0.044130124, 0.16890779, 0.056885477, -0.01163879, 0.120314285, -0.17272389, -8.8864565E-4, -0.03285277, 0.11544687, 0.0050459877, -6.73756E-5, -0.025026657, 0.107432365, -0.012091566, 0.053701602, -0.07341023, 0.0029377658, 0.008114906, 0.11451018, -0.033616528, 0.08197731, -0.11332913, -0.091565184, -0.087808326, 0.19573137, -0.102333404, -0.6804974, 0.50974584, 0.38381046, -0.33900893, 0.005227985, 0.0026655793, 0.051242687, -0.010757513, 0.0198958, -0.029690634, 0.00539957, 0.04502184, 0.0573088, -0.054638233, -0.006194748, -0.0053862035, 0.06116029, -0.02106953, -0.024623558, -0.039997607, 0.0072542187, 0.02572514, 0.049860634, -0.052422013, -0.043137882, -0.020319205, -0.056331143, 0.080378674, 0.1394395, 0.005022198, 0.056781475, -0.026826905, -0.07835051, -0.09666835, 0.24270868, 0.18709765, -0.030756302, -0.165624, -0.057722926, 0.0069376472, 0.037494883, -0.029802866, -0.032472897, -0.068440646, 0.012604982, 0.015015423, -0.0173368, -0.019105867, -0.008211181, 0.021168709, 0.040182255, -0.02814807, -0.017669482, -0.026751725, -0.012706833, -0.0027096448, 0.045317367, 0.032301508, 0.04426267, -0.08933062, -0.0471749, -0.06444376, -0.09654938, 0.043335695, 0.12140052, -0.0015536249, -0.53533417, -0.2585726, 0.70283824, 0.47405657, 0.024502322, -0.16566388, -0.19082585, -0.09583926, 0.09569151, 0.048627105, -0.022939723, -0.06575456, 0.009446224, -0.00975702, 0.0016801767, -0.051722545, -0.018259972, 0.00846668, 0.01362105, 0.005652555, 0.05234885, -0.13752311, -0.15500395, -0.07114535, -0.05468425, 0.0474941, 0.05038706, 0.05343116, 0.091765374, -0.14928326, -0.33963105, -0.12458068, 0.32321116, 0.23422049, -2.0443363, -1.3365602, 1.8712385, 2.0832777, 0.0016608797, -0.33099422, -0.09108348, -0.014150428, 0.09252362, -0.03286069, 0.04940255, 0.026892472, 0.025475737, 0.05046378, 0.1891938, -0.14962234, -0.4303031, -0.08829961, 0.34482086, 0.5039355, 0.13150162, -0.25915033, -0.1265548, -0.2574557, -0.17562047, 0.39240685, 0.44468945, 0.026706275, -0.09155075, -0.38824135, -0.18202443, 0.23859656, 0.61617273, -0.31840855, -2.0276303, 0.9832537, 1.1621271, -0.49726146, 0.19553593, -0.10298899, -0.13275489, 0.07680958, 0.09162817, 0.012201903, -0.05895811, 0.034935266, 0.12762265, 0.021531172, 0.051505975, -0.10772297, -0.051179297, 0.0010228197, -0.019696025, 0.033015855, 0.11294837, 0.050605245, 0.00396112, -0.1411652, -0.09241196, 0.007965477, 0.020382965, 0.06209734, 0.12016374, 0.06725982, -0.04499606, -0.07243794, 0.48658267, 0.010686975, -0.927904, -0.12080544, 0.24520612, 0.04054514, 0.12872104, 0.012987542, -0.04141576, 0.058843452, 0.06732093, -0.02351918, -0.028010286, -0.040717423, -0.04033682, 0.029161325, 0.044705503, 0.08126734, 0.040374402, -0.08979593, -0.08729809, 0.067890525, 0.10397345, 0.032341976, -0.014466969, -0.030680116, 0.02767477, 0.036057908, 0.007890858, -0.047237966, 0.07276655, 0.0150644705, -0.15642533, -0.049319167, 0.36688325, 0.24333864, -0.4546214, -0.1876865, 0.16462927, 0.01283114, 0.057169706, 0.0135015715, -0.03926536, -0.043084286, -0.011431754, 0.10450807, 0.13500866, -0.04209587, -0.056389466, -0.0638015, 0.003755902, 0.05859655, 0.078339055, -0.03838852, -0.03133777, -0.019160083, 0.019707767, -0.013193563, -0.031102251, 0.00411362, 0.05489627, -0.01582741, 7.915022E-4, 0.020830408, 0.035424054, -0.11944745, -0.04161431, 0.02086537, -0.12852997, 0.016785085, -0.119298615, 0.06797627, 0.17949563, 0.05425484, 0.0728825, -0.012138505, -0.039683852, -0.17457704, -0.19760388, 0.12612489, 0.22958198, 0.10854934, -0.02949693, -0.19488299, -0.037523847, 0.12738304, 0.010320611, -0.01430659, 0.15590473, -0.13252062, -0.2657941, 0.024024993, -0.013486354, 0.028359313, 0.08713609, 0.123534985, 0.037597444, -0.04643556, -0.21764582, 0.030002221, 0.8742517, 0.17766356, -1.911296, -0.37747067, 1.4185234, -0.21309435, -0.103334546, 0.27875075, -0.01861829, 0.028567756, -0.06416224, -0.04465594, 0.017477445, 0.016646907, -0.0065277545, 0.079972774, 0.11748728, -0.029967228, -0.04537211, 0.020012528, 0.040443696, 0.027232705, 0.048174288, 0.012794461, 0.035245642, -0.018194638, 0.030594258, 0.0050494163, -0.017473955, -0.049305297, 0.008678811, 0.06511702, 0.0022270272, -0.0027124286, 0.16816673, 0.13248551, -0.12015128, -0.20897466, -0.06645679, -0.08885361, -0.0031329505, 0.15147996, 0.098236576, 0.0031106584, 0.028784275, -0.05132381, -0.038687468, -0.02323255, -0.0030348822, 0.044202797, 0.024158273, -0.006038271, 0.0097960755, -0.026463129, -0.011459071, 0.020905135, 0.033295892, 0.013403716, -0.031469204, -0.021937668, 0.058680054, 0.047239747, -0.03612131, -0.060082287, 0.013783328, 0.15402919, 0.011471435, -0.1233964, -0.13265127, 0.37886295, 0.39494392, -0.43770215, -0.1288102, -0.014112033, -0.07169622, 0.10541843, 0.022117615, -0.028463244, 0.040591955, 0.035330296, 0.008635998, -0.0027968884, -0.0077421665, -0.04322791, -0.03555797, 0.06965935, 0.15217827, 0.064989835, -0.05442105, -0.11906066, 0.11980292, 0.03611692, -0.17112218, -0.15120837, 0.09819949, 0.22586583, -0.0032664686, -0.20525156, -0.064086676, 0.20044562, 0.23042056, -0.1161561, -0.53912115, 0.0017189234, 0.5053157, 0.15220301, 0.14008388, -0.081903405, -0.20243572, -0.13726518, -0.032035954, 0.037741076, 0.08799034, 0.09419395, 0.010643214, -0.10245581, -0.07255009, 0.10765343, 0.13739955, -0.053037778, -0.091254205, -0.0049547236, 0.059570577, 0.0279653, -0.016993567, -0.043493044, 0.06977988, 0.014736056, -0.021887384, 5.490198E-4, 0.0426088, -0.021336414, -0.059073858, 0.034563903, 0.09405149, 0.022538338, -0.11136651, -0.10005132, 0.05930558, 0.06562129, 0.0757892, 0.046151005, -0.0034891022, -0.06225685, -0.021602921, 0.02340943};
float[] inputF = copy(inputD);
float[] fftOutputF = copy(fftOutputD);
VlFft fft = new VlFft(512);
VlFftSample fftSmp = fft.fft(inputF, true);
printChunked(8, fftOutputF);
System.out.println("=======================");
printChunked(8, fftSmp.composite);
});
}
}
package io.vacco.volach.dsp;
public class VlHammingWindow {
public static final float TWO_PI = (float) (2 * Math.PI);
public static float value(int length, int index) {
return 0.54f - 0.46f * (float) Math.cos(TWO_PI * index / (length - 1));
}
public static float[] generateCurve(int length) {
float[] samples = new float[length];
for (int n = 0; n < length; n++) {
samples[n] = value(length, n);
}
return samples;
}
}
public class VlKernel33 {
public int kRows = 3, kCols = 3;
public int kCenterX = 3 / 2, kCenterY = 3 / 2;
public float[][] krn = new float[3][];
public static VlKernel33 from(float nw, float n, float ne,
float w, float c, float e,
float sw, float s, float se) {
VlKernel33 k = new VlKernel33();
k.krn[0] = new float[] {nw, n, ne};
k.krn[1] = new float[] {w, c, e};
k.krn[2] = new float[] {sw, s, se};
return k;
}
/** Data MUST be in row/column order */
public void apply(float[][] in, float[][] out) {
int i, j, ii, jj, n, m, mm, nn;
int rows = in.length, cols = in[0].length;
for (i = 0; i < rows; ++i) {
for (j = 0; j < cols; ++j) {
for (m = 0; m < kRows; ++m) {
mm = kRows - 1 - m;
for (n = 0; n < kCols; ++n) {
nn = kCols - 1 - n;
ii = i + (kCenterY - mm);
jj = j + (kCenterX - nn);
if (ii >= 0 && ii < rows && jj >= 0 && jj < cols) {
out[i][j] += in[ii][jj] * krn[mm][nn];
}
}
}
}
}
}
}
package io.vacco.volach.fprint;
import io.vacco.jtinn.net.JtPredictionSample;
import io.vacco.jtinn.net.JtPredictionSampleSupplier;
import io.vacco.volach.util.VlArrays;
import io.vacco.volach.wavelet.VlWaveletPacketAnalysisExtractor;
import io.vacco.volach.wavelet.dto.VlAnalysisChunk;
import io.vacco.volach.wavelet.dto.VlAnalysisParameters;
import java.util.Iterator;
import java.util.function.Supplier;
public class VlNnFpSampleSupplier implements JtPredictionSampleSupplier {
private Iterator<VlAnalysisChunk> activeChunks;
private final VlAnalysisParameters params;
private final Supplier<Integer> trackIdSupplier;
private JtPredictionSample[] samples;
private final double[]
buff0 = new double[32],
buff1 = new double[32],
buff2 = new double[64];
private double[][] freqBuff;
public VlNnFpSampleSupplier(Supplier<Integer> trackIdSupplier, VlAnalysisParameters params) {
this.params = params;
this.trackIdSupplier = trackIdSupplier;
}
public void setLabel(int trackId, VlAnalysisChunk chunk) {
VlArrays.packPair(trackId, chunk.signal.offset, buff0, buff1, buff2);
}
public void setFeatures(VlAnalysisChunk chunk) {
if (freqBuff == null) {
freqBuff = new double[chunk.samples.length][chunk.samples[0].freqPower.capacity()];
}
for (int k = 0; k < chunk.samples.length; k++) {
for (int j = 0; j < chunk.samples[k].freqPower.capacity(); j++) {
freqBuff[k][j] = chunk.samples[k].freqPower.get(j);
}
}
}
private void checkNext() {
try {
if (!activeChunks.hasNext()) {
this.activeChunks = VlWaveletPacketAnalysisExtractor.from(params).iterator();
}
} catch (Exception e) {
this.activeChunks = VlWaveletPacketAnalysisExtractor.from(params).iterator();
}
}
public int getInputSize() {
checkNext();
return activeChunks.next().samples[0].freqPower.capacity();
}
@Override public JtPredictionSample[] get() {
checkNext();
VlAnalysisChunk c = activeChunks.next();
setFeatures(c);
setLabel(trackIdSupplier.get(), c);
if (samples == null) { samples = new JtPredictionSample[c.samples.length]; }
for (int k = 0; k < samples.length; k++) {
if (samples[k] == null) {
samples[k] = new JtPredictionSample();
}
samples[k].features = freqBuff[k];
samples[k].labels = buff2;
}
return samples;
}
}
package io.vacco.volach.fprint;
public class VlXXHashFingerPrint {
public byte version;
public float eSum;
public byte eSumGtEAvg;
public int hilbertOffset;
@Override public String toString() {
return String.format("fp[ver: %s, offset: %s, eSum > eAvg: %s, eSum: %s]",
version, hilbertOffset, eSumGtEAvg, eSum
);
}
}
package io.vacco.volach.fprint;
import io.vacco.volach.wavelet.VlAnalysisSample;
import io.vacco.volach.wavelet.VlWaveletPacketAnalysisExtractor;
import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
public class VlXXHashFingerPrintExtractor extends Spliterators.AbstractSpliterator<VlXXHashFingerPrint[]> {
public static final byte version = 1;
private final VlWaveletPacketAnalysisExtractor analysisExtractor;
public VlXXHashFingerPrintExtractor(VlWaveletPacketAnalysisExtractor analysisExtractor) {
super(Long.MAX_VALUE, Spliterator.ORDERED | Spliterator.NONNULL);
this.analysisExtractor = Objects.requireNonNull(analysisExtractor);
}
@Override
public boolean tryAdvance(Consumer<? super VlXXHashFingerPrint[]> onFingerPrints) {
return analysisExtractor.tryAdvance(chunk -> {
VlXXHashFingerPrint[] fingerPrints = new VlXXHashFingerPrint[chunk.samples.length];
for (int i = 0; i < chunk.samples.length; i++) {
VlXXHashFingerPrint fingerPrint = new VlXXHashFingerPrint();
fingerPrint.version = version;
fingerPrint.eSum = 0;
VlAnalysisSample sample = chunk.samples[i];
for (int k = 0; k < sample.freqPower.capacity(); k++) {
float val = sample.freqPower.get(k);
fingerPrint.eSum += (float) Math.pow(Math.abs(val), 2);
}
fingerPrint.hilbertOffset = sample.hilbertOffset;
fingerPrints[i] = fingerPrint;
}
float eAvg = (float) (Arrays.stream(fingerPrints).mapToDouble(fp -> fp.eSum).sum() / fingerPrints.length);
for (VlXXHashFingerPrint fingerPrint : fingerPrints) {
float eSum = fingerPrint.eSum;
fingerPrint.eSumGtEAvg = (byte) (eSum >= eAvg ? 1 : 0);
}
onFingerPrints.accept(fingerPrints);
});
}
public static Stream<VlXXHashFingerPrint[]> from(VlWaveletPacketAnalysisExtractor analysisExtractor) {
return StreamSupport.stream(new VlXXHashFingerPrintExtractor(analysisExtractor), false);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment