Funkin/source/funkin/audiovis/dsp/Signal.hx

115 lines
2.6 KiB
Haxe
Raw Normal View History

package funkin.audiovis.dsp;
2021-09-27 22:30:38 -04:00
using Lambda;
/**
Signal processing miscellaneous utilities.
**/
class Signal
{
2021-09-27 22:30:38 -04:00
/**
Returns a smoothed version of the input array using a moving average.
**/
public static function smooth(y:Array<Float>, n:Int):Array<Float>
{
if (n <= 0)
{
2021-09-27 22:30:38 -04:00
return null;
}
else if (n == 1)
{
2021-09-27 22:30:38 -04:00
return y.copy();
}
else
{
2021-09-27 22:30:38 -04:00
var smoothed = new Array<Float>();
smoothed.resize(y.length);
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.
**/
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>();
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
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.
**/
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
for (v in array)
{
2021-09-27 22:30:38 -04:00
var t = sum + v;
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.
**/
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.
**/
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.
**/
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.
**/
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.
**/
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);
}