2022-03-08 03:13:53 -05:00
|
|
|
package funkin.audiovis.dsp;
|
2021-09-27 22:30:38 -04:00
|
|
|
|
|
|
|
using Lambda;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Signal processing miscellaneous utilities.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
class Signal
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
/**
|
|
|
|
Returns a smoothed version of the input array using a moving average.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function smooth(y:Array<Float>, n:Int):Array<Float>
|
|
|
|
{
|
|
|
|
if (n <= 0)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
return null;
|
2022-02-10 13:17:46 -05:00
|
|
|
}
|
|
|
|
else if (n == 1)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
return y.copy();
|
2022-02-10 13:17:46 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
var smoothed = new Array<Float>();
|
|
|
|
smoothed.resize(y.length);
|
2022-02-10 13:17:46 -05:00
|
|
|
for (i in 0...y.length)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
var m = i + 1 < n ? i : n - 1;
|
|
|
|
smoothed[i] = sum(y.slice(i - m, i + 1));
|
|
|
|
}
|
|
|
|
return smoothed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Finds indexes of peaks in the order they appear in the input sequence.
|
|
|
|
|
|
|
|
@param threshold Minimal peak height wrt. its neighbours, defaults to 0.
|
|
|
|
@param minHeight Minimal peak height wrt. the whole input, defaults to global minimum.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function findPeaks(y:Array<Float>, ?threshold:Float, ?minHeight:Float):Array<Int>
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
threshold = threshold == null ? 0.0 : Math.abs(threshold);
|
|
|
|
minHeight = minHeight == null ? Signal.min(y) : minHeight;
|
|
|
|
|
|
|
|
var peaks = new Array<Int>();
|
|
|
|
|
2022-02-10 13:17:46 -05:00
|
|
|
final dy = [for (i in 1...y.length) y[i] - y[i - 1]];
|
|
|
|
for (i in 1...dy.length)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
// peak: function growth positive to its left and negative to its right
|
2022-02-10 13:17:46 -05:00
|
|
|
if (dy[i - 1] > threshold && dy[i] < -threshold && y[i] > minHeight)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
peaks.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return peaks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the sum of all the elements of a given array.
|
|
|
|
|
|
|
|
This function tries to minimize floating-point precision errors.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function sum(array:Array<Float>):Float
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
// Neumaier's "improved Kahan-Babuska algorithm":
|
|
|
|
|
|
|
|
var sum = 0.0;
|
|
|
|
var c = 0.0; // running compensation for lost precision
|
|
|
|
|
2022-02-10 13:17:46 -05:00
|
|
|
for (v in array)
|
|
|
|
{
|
2021-09-27 22:30:38 -04:00
|
|
|
var t = sum + v;
|
2022-02-10 13:17:46 -05:00
|
|
|
c += Math.abs(sum) >= Math.abs(v) ? (sum - t) + v // sum is bigger => low-order digits of v are lost
|
|
|
|
: (v - t) + sum; // v is bigger => low-order digits of sum are lost
|
2021-09-27 22:30:38 -04:00
|
|
|
sum = t;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum + c; // correction only applied at the very end
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the average value of an array.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function mean(y:Array<Float>):Float
|
2021-09-27 22:30:38 -04:00
|
|
|
return sum(y) / y.length;
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the global maximum.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function max(y:Array<Float>):Float
|
2021-09-27 22:30:38 -04:00
|
|
|
return y.fold(Math.max, y[0]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the global maximum's index.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function maxi(y:Array<Float>):Int
|
2021-09-27 22:30:38 -04:00
|
|
|
return y.foldi((yi, m, i) -> yi > y[m] ? i : m, 0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the global minimum.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function min(y:Array<Float>):Float
|
2021-09-27 22:30:38 -04:00
|
|
|
return y.fold(Math.min, y[0]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
Returns the global minimum's index.
|
|
|
|
**/
|
2022-02-10 13:17:46 -05:00
|
|
|
public static function mini(y:Array<Float>):Int
|
2021-09-27 22:30:38 -04:00
|
|
|
return y.foldi((yi, m, i) -> yi < y[m] ? i : m, 0);
|
|
|
|
}
|