-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpolynomial.js
183 lines (158 loc) · 5.23 KB
/
polynomial.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
class DifferentialEquation
{
constructor(xCoefficients, yCoefficients)
{
this.x = new Delay();
this.y = new Delay();
this.resetState();
this.coefficients = xCoefficients;
this.recursionCoefficients = yCoefficients;
// Remove the y[n] coefficient
this.recursionCoefficients.shift();
}
normalize(scalar)
{
let sumX = 0;
let sumY = 0;
for(var i = 0; i < this.coefficients.length; i++)
{
sumX += this.coefficients[i];
}
for(var i = 0; i < this.recursionCoefficients.length; i++)
{
sumY += this.recursionCoefficients[i];
}
for(var i = 0; i < this.coefficients.length; i++)
{
this.coefficients[i] *= scalar;
}
}
resetState()
{
this.x.set(0);
this.y.set(0);
}
tick(x)
{
this.x.push(x);
let out = 0;
for(var i = 0; i < this.coefficients.length; i++)
{
out += this.coefficients[i] * this.x.delayed(i);
}
for(var i = 0; i < this.recursionCoefficients.length; i++)
{
out += this.recursionCoefficients[i] * this.y.delayed(i);
}
this.y.push(out);
return out;
}
complexToString(value, precision = 3)
{
if(value.im < 0.000001)
{
return "" + parseFloat(value.re).toFixed(precision);
}
else
{
return "(" + parseFloat(value.re).toFixed(precision) + " + " + parseFloat(value.im).toFixed(precision) + "i)";
}
}
floatToString(value, precision = 3)
{
return "" + parseFloat(value).toFixed(precision);
}
prettyText(precision = 9)
{
let text = "y[n] = ";
if(this.coefficients.length > 0)
{
if(this.coefficients[0] != 0)
{
text += this.floatToString(this.coefficients[0], precision) + "*x[n] + ";
}
}
for(var i = 1; i < this.coefficients.length; i++)
{
if(this.coefficients[i] != 0)
{
text += this.floatToString(this.coefficients[i], precision) + "*x[n - " + i + "] + ";
}
}
for(var i = 0; i < this.recursionCoefficients.length; i++)
{
if(this.recursionCoefficients[i] != 0)
{
text += this.floatToString(this.recursionCoefficients[i], precision) + "*y[n - " + (i + 1) + "] + ";
}
}
return text.substring(0, text.length - 3);
}
};
function sum(array)
{
let sum = Complex(0, 0);
for(var i = 0; i < array.length; i++)
{
sum = sum.add(array[i]);
}
return sum;
}
/** Returns the sum of the multiplication of all possible number combinations in a given array
* Examples:
*
* for [1, 2, 3] and numCombinations set to 2, this will be the sum of (1*2 + 1*3 + 2*3)
* for [2, 3, 4, 5] and numCombinations set to 3, this will be the sum of (2*3*4, 2*3*5, 2*4*5, 3*4*5)
*/
function sumOfAllPossibleCombinations(array, numCombinations, startIndex = 0, currentLevel = 0)
{
// first check whether the indices are still valid
// if the number of combinations has been reached, stop the recursion by simply mulitplying with 1
if(currentLevel >= numCombinations && startIndex <= array.length)
{
return new Complex(1, 0);
}
// if the number of combinations has not been reached, return 0 to stop the recursion and remove the result.
else if(startIndex > array.length)
{
return new Complex(0, 0);
}
let foundMultipliedCombinations = [];
// start looking for the next combination number, starting from the index where the previous call left of
for(var i = startIndex; i < array.length; i++)
{
// recursively find all the next possible combinations, result is 0 if non are found
const newMultiplication = array[i].mul( sumOfAllPossibleCombinations(array, numCombinations, i + 1, currentLevel + 1) );
foundMultipliedCombinations.push(newMultiplication);
}
// return the sum of the multiplications of all possible combinations that were found in this function call
return sum(foundMultipliedCombinations);
}
function fromPoleZerosToDifferentialEquation(poles, zeros)
{
// Invert the sign for all zeros and poles (as thats how they are placed in the transferfunction)
// (z - p)(z - p)
for(var i = 0; i < zeros.length; i++)
{
zeros[i] = zeros[i].neg();
}
for(var i = 0; i < poles.length; i++)
{
poles[i] = poles[i].neg();
}
// Calculate the coefficients for the poles and zeros by expanding the polynomial
let yCoefficients = [];
for(var i = 0; i < poles.length + 1; i++)
{
yCoefficients.push( sumOfAllPossibleCombinations(poles, i).re );
// negate y coefficients, as they are substracted
yCoefficients[i] *= -1;
}
let xCoefficients = [];
for(var i = 0; i < zeros.length + 1; i++)
{
xCoefficients.push( sumOfAllPossibleCombinations(zeros, i).re );
}
// Create the actual differential equation
return new DifferentialEquation(xCoefficients, yCoefficients);
}