Created
August 14, 2022 12:23
-
-
Save Kraballa/1846a1aeb42976c17e35a4f2a3e7212c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const NUM_STEPS = 1000; | |
const e24 = [1, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2, 2.2, 2.4, 2.7, 3, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1]; | |
const e48 = [1, 1.05, 1.1, 1.15, 1.21, 1.27, 1.33, 1.4, 1.47, 1.54, 1.62, 1.69, 1.78, 1.87, 1.96, 2.05, 2.15, 2.26, 2.37, 2.49, 2.61, 2.74, 2.87, 3.01, 3.16, 3.32, 3.48, 3.65, 3.83, 4.02, 4.22, 4.42, 4.64, 4.87, 5.11, 5.36, 5.62, 5.9, 6.19, 6.49, 6.81, 7.15, 7.5, 7.87, 8.25, 8.66, 9.09, 9.53]; | |
const e96 = [1, 1.02, 1.05, 1.07, 1.1, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.3, 1.33, 1.37, 1.4, 1.43, 1.47, 1.5, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74, 1.78, 1.82, 1.87, 1.91, 1.96, 2, 2.05, 2.1, 2.15, 2.21, 2.26, 2.32, 2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.8, 2.87, 2.94, 3.01, 3.09, 3.16, 3.24, 3.32, 3.4, 3.48, 3.57, 3.65, 3.74, 3.83, 3.92, 4.02, 4.12, 4.22, 4.32, 4.42, 4.53, 4.64, 4.75, 4.87, 4.99, 5.11, 5.23, 5.36, 5.49, 5.62, 5.76, 5.9, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32, 7.5, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76]; | |
function calcChains(volt, ampMin, ampMax, targets, series) { | |
if (targets === null) | |
return []; | |
var eseries = series === 'e24' ? e24 : series === 'e96' ? e96 : e48; | |
if (ampMax === null || ampMin === ampMax) { | |
return [calcChain(volt, ampMin, targets, eseries)]; | |
} | |
var chains = []; | |
for (var step = 0; step < NUM_STEPS; step++) { | |
var curAmp = ampMin + (ampMax - ampMin) / NUM_STEPS * step; | |
chains.push(calcChain(volt, curAmp, targets, eseries)); | |
} | |
return chains.sort((a, b) => a.accuracy - b.accuracy).slice(0, 20); | |
} | |
function calcChain(volt, amp, targets, eseries) { | |
var chain = { | |
volt: volt, | |
amp: amp, | |
targets: targets, | |
res: [], | |
accuracy: 0, | |
outs: [], | |
}; | |
var remainingRes = volt / amp; | |
var diff = 0.0; | |
for (var i = 0; i < targets.length + 1; i++) { | |
var nextRes; | |
if (i < targets.length) { | |
var ratio; | |
if (i === 0) { | |
ratio = targets[i] / volt; | |
} else { | |
ratio = targets[i] / targets[i - 1]; | |
} | |
var idealRes = (remainingRes + diff) - ratio * (remainingRes + diff); | |
nextRes = bestRes(idealRes, eseries); | |
diff = (remainingRes - ratio * remainingRes) - nextRes; | |
} else { | |
nextRes = bestRes(remainingRes + diff, eseries); | |
} | |
remainingRes -= nextRes; | |
chain.res.push(nextRes); | |
} | |
evaluate(chain); | |
return chain; | |
} | |
function dist(num1, num2) { | |
return Math.abs(num1 - num2); | |
} | |
function bestRes(target, eseries) { | |
//index in eseries array | |
var index = 0; | |
//decade factor. number of digits infront of a comma/dot - 1 | |
var factor = target.toString().split(".")[0].length - 1; | |
var currentBest = eseries[index] * Math.pow(10, factor); | |
for (; index < eseries.length - 1; index++) { | |
var newValue = eseries[index + 1] * Math.pow(10, factor); | |
if (dist(target, currentBest) > dist(target, newValue)) { | |
currentBest = newValue; | |
} else { | |
break; | |
} | |
} | |
//in the case we need to wrap decade to one higher | |
if (index === eseries.length - 1) { | |
var newValue = eseries[0] * Math.pow(10, factor + 1); | |
if (dist(target, currentBest) > dist(target, newValue)) { | |
currentBest = newValue; | |
} | |
} | |
return currentBest; | |
} | |
function evaluate(chain) { | |
for (var i = 0; i < chain.res.length - 1; i++) { | |
//[:i] | |
var p1 = chain.res.slice(0, i + 1).reduce((a, b) => a + b, 0); | |
//[i+1:] | |
var p2 = chain.res.slice(i + 1, chain.res.length).reduce((a, b) => a + b, 0); | |
chain.outs[i] = (chain.volt * p2) / (p1 + p2); | |
} | |
chain.accuracy = 0; | |
for (var i = 0; i < chain.outs.length; i++) { | |
var curDev = dist(chain.targets[i], chain.outs[i]); | |
chain.accuracy += Math.pow(curDev, 2); | |
} | |
chain.accuracy = Math.sqrt(chain.accuracy); | |
} | |
export { calcChains }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
export resChain to LTSpice: gist