-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsp.js
124 lines (107 loc) · 3.12 KB
/
dsp.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
function ampToDb(amplitude)
{
return Math.log10(amplitude) * 20.0;
}
function dbToAmp(db)
{
return Math.pow(10.0, db / 20.0);
}
/** Prewarps the continuous (splane) frequency to match the desired discrete frequency */
function preWarp(frequency)
{
return (sampleRate / Math.PI) * Math.tan((Math.PI * frequency) / sampleRate);
}
/** Inverts frequency prewarping, converts a prewarped continuous frequency to the desired discrete frequency */
function inversePrewarp(warpedFrequency)
{
return sampleRate * Math.atan(warpedFrequency / (sampleRate / Math.PI)) / Math.PI;
}
/** Upscales a continuous point by a desired discrete frequency */
function scaleContinuousNumber(number, frequency)
{
return number.mul( 2 * Math.PI * preWarp(frequency) );
}
/** Downscales a upscaled continuous point */
function normalizeContinuousNumber(scaledNumber, scalingFrequency)
{
return scaledNumber.div( 2 * Math.PI * preWarp(scalingFrequency) );
}
/** Transforms an analog/continuous number to its digital/discrete counterpart (no pre-warping)
* Simply the billinear transform
*/
function toDiscrete(continous)
{
return Complex(1, 0).add( continous.div(2 * sampleRate) ).div( Complex(1, 0).sub( continous.div(2 * sampleRate) ) );
}
/** Transforms a digital/discrete number to its continuous/analog counterpart (no pre-warping correction)
* Inverse of the billinear transform
*/
function toContinuous(discrete)
{
return new Complex(2 * sampleRate, 0).mul( discrete.sub(1, 0).div( discrete.sub(-1, 0) ) );
}
/** Returns a TransferFunction function for a given set of poles and zeros (Position[]) */
function createTransferFunction(poles, zeros)
{
return function(s)
{
nominator = Complex(1);
for(var i = 0; i < zeros.length; i++)
{
nominator = nominator.mul(s.sub(new Complex(zeros[i].valueX, zeros[i].valueY)));
}
denominator = Complex(1);
for(var i = 0; i < poles.length; i++)
{
denominator = denominator.mul(s.sub(new Complex(poles[i].valueX, poles[i].valueY)));
}
return nominator.div(denominator);
};
}
function printArray(numbers, note)
{
console.log('Array:', note);
for(var i = 0; i < numbers.length; i++)
{
console.log('' + i + ' = ' + numbers[i]);
}
console.log('');
}
function printComplexNumbers(numbers, note)
{
console.log('Array:', note);
for(var i = 0; i < numbers.length; i++)
{
console.log('' + i + ' = ' + numbers[i].re + " + " + numbers[i].im + "i" );
}
console.log('');
}
class Delay
{
constructor()
{
this.index = 0;
this.data = [];
for(var i = 0; i < maxOrder + 1; i++)
{
this.data.push(0);
}
}
set(value)
{
for(var i = 0; i < this.data.length; i++)
{
this.data[i] = value;
}
}
delayed(samples)
{
const index = (this.index - samples + this.data.length) % this.data.length;
return this.data[index];
}
push(value)
{
this.index = (this.index + 1) % this.data.length;
this.data[this.index] = value;
}
}