genish.js
Version:
87 lines (68 loc) • 2.46 kB
JavaScript
/*
* many windows here adapted from https://github.com/corbanbrook/dsp.js/blob/master/dsp.js
* starting at line 1427
* taken 8/15/16
*/
const windows = module.exports = {
bartlett( length, index ) {
return 2 / (length - 1) * ((length - 1) / 2 - Math.abs(index - (length - 1) / 2))
},
bartlettHann( length, index ) {
return 0.62 - 0.48 * Math.abs(index / (length - 1) - 0.5) - 0.38 * Math.cos( 2 * Math.PI * index / (length - 1))
},
blackman( length, index, alpha ) {
let a0 = (1 - alpha) / 2,
a1 = 0.5,
a2 = alpha / 2
return a0 - a1 * Math.cos(2 * Math.PI * index / (length - 1)) + a2 * Math.cos(4 * Math.PI * index / (length - 1))
},
cosine( length, index ) {
return Math.cos(Math.PI * index / (length - 1) - Math.PI / 2)
},
gauss( length, index, alpha ) {
return Math.pow(Math.E, -0.5 * Math.pow((index - (length - 1) / 2) / (alpha * (length - 1) / 2), 2))
},
hamming( length, index ) {
return 0.54 - 0.46 * Math.cos( Math.PI * 2 * index / (length - 1))
},
hann( length, index ) {
return 0.5 * (1 - Math.cos( Math.PI * 2 * index / (length - 1)) )
},
lanczos( length, index ) {
let x = 2 * index / (length - 1) - 1;
return Math.sin(Math.PI * x) / (Math.PI * x)
},
rectangular( length, index ) {
return 1
},
triangular( length, index ) {
return 2 / length * (length / 2 - Math.abs(index - (length - 1) / 2))
},
// parabola
welch( length, _index, ignore, shift=0 ) {
//w[n] = 1 - Math.pow( ( n - ( (N-1) / 2 ) ) / (( N-1 ) / 2 ), 2 )
const index = shift === 0 ? _index : (_index + Math.floor( shift * length )) % length
const n_1_over2 = (length - 1) / 2
return 1 - Math.pow( ( index - n_1_over2 ) / n_1_over2, 2 )
},
inversewelch( length, _index, ignore, shift=0 ) {
//w[n] = 1 - Math.pow( ( n - ( (N-1) / 2 ) ) / (( N-1 ) / 2 ), 2 )
let index = shift === 0 ? _index : (_index + Math.floor( shift * length )) % length
const n_1_over2 = (length - 1) / 2
return Math.pow( ( index - n_1_over2 ) / n_1_over2, 2 )
},
parabola( length, index ) {
if( index <= length / 2 ) {
return windows.inversewelch( length / 2, index ) - 1
}else{
return 1 - windows.inversewelch( length / 2, index - length / 2 )
}
},
exponential( length, index, alpha ) {
return Math.pow( index / length, alpha )
},
linear( length, index ) {
return index / length
}
}