Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.54 KB | None | 0 0
  1. //<![CDATA[
  2.  
  3. /* Copyright (C) 2001-2010 Claudio Girardi
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or (at
  8. * your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19.  
  20. /* well, this is not the simplest way to do it but, as they say, */
  21. /* If ain't broke, don't fix it!" */
  22.  
  23.  
  24. function select_series() {
  25. var sel_val = document.getElementById('rslist').options[document.getElementById('rslist').selectedIndex].value;
  26. switch(sel_val) {
  27. case "E12":
  28. Rbase = new Array(1, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6, 6.8, 8.2);
  29. break;
  30. case "E24":
  31. Rbase = new Array(1, 1.1, 1.2, 1.3, 1.5, 1.6, 1.8, 2.0, 2.2, 2.4, 2.7, 3.0, 3.3, 3.6, 3.9, 4.3, 4.7, 5.1, 5.6, 6.2, 6.8, 7.5, 8.2, 9.1);
  32. break;
  33. case "E96":
  34. Rbase = new Array(1.00, 1.02, 1.05, 1.07, 1.10, 1.13, 1.15, 1.18, 1.21, 1.24, 1.27, 1.30, 1.33, 1.37, 1.40, 1.43, 1.47, 1.50, 1.54, 1.58, 1.62, 1.65, 1.69, 1.74, 1.78, 1.82, 1.87, 1.91, 1.96, 2.00, 2.05, 2.10, 2.15, 2.21, 2.26, 2.32, 2.37, 2.43, 2.49, 2.55, 2.61, 2.67, 2.74, 2.80, 2.87, 2.94, 3.01, 3.09, 3.16, 3.24, 3.32, 3.40, 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.90, 6.04, 6.19, 6.34, 6.49, 6.65, 6.81, 6.98, 7.15, 7.32, 7.50, 7.68, 7.87, 8.06, 8.25, 8.45, 8.66, 8.87, 9.09, 9.31, 9.53, 9.76);
  35. break;
  36. default:
  37. }
  38.  
  39.  
  40. R = new Array;
  41. for (var mult = 0; mult <= 6; mult++) {
  42. for (var idx = 0; idx <Rbase.length; idx++) {
  43. /* need to round to compensate for pow() errors; allow max two decimals, needed for E96 */
  44. R[idx + mult * Rbase.length] = Math.round(Rbase[idx] * Math.pow(10, mult) * 100) / 100;
  45. }
  46. }
  47. n_max = R.length - 1; /* maximum valid index */
  48.  
  49. /* compute the conductances array, lowest conductance first to have an */
  50. /* array sorted in ascending order */
  51. G = new Array;
  52. for (idx = 0; idx <= n_max; idx++) {
  53. G[idx] = 1.0 / R[n_max - idx];
  54. }
  55.  
  56. out_r1 = new Array;
  57. out_r2 = new Array;
  58. out_op = new Array;
  59. out_rres = new Array;
  60. out_tol = new Array;
  61.  
  62. CalcRes();
  63. }
  64.  
  65.  
  66. function FindIndex(vect, value) {
  67. var index_min = 0;
  68. var index_max = n_max + 1;
  69. var index = Math.floor( (index_min + index_max) / 2);
  70. i = 0;
  71.  
  72. while (((index_max - index_min) > 1) && (i < 500)) {
  73. if (vect[index] == value) { break; }
  74. else if (vect[index] > value) { index_max = index }
  75. else if (vect[index] < value) { index_min = index }
  76.  
  77. index = Math.floor( (index_min + index_max) / 2);
  78. i++;
  79. }
  80. if (index < n_max) {
  81. tol1 = Math.abs(vect[index] / value - 1.0);
  82. tol2 = Math.abs(vect[index + 1] / value - 1.0);
  83. if (tol1 < tol2)
  84. return index;
  85. else
  86. return (index + 1);
  87. } else
  88. return index;
  89. }
  90.  
  91. function CalcRes() {
  92. var rd = document.getElementById('rd').value;
  93. var r1, r2, r1_idx, rres, rres_tol, best_tol, out_idx, op;
  94. var out_prres, out_vrres;
  95. var i, j, iter = 0; /* number of iterations */
  96.  
  97. document.getElementById('texta').value = ""
  98. <!-- document.getElementById('debuga').value = "" -->
  99.  
  100. /* compute assuming resistors in series */
  101. /* locate nearest approximation with standard resistor values */
  102. r1_idx = FindIndex(R, rd);
  103. r1 = R[r1_idx];
  104. /* other resistor */
  105. /* r2 = Number.POSITIVE_INFINITY */
  106. r2 = 0;
  107. rres = r1;
  108. rres_tol = (rres - rd) / rd; /* relative tolerance */
  109. best_tol = rres_tol;
  110.  
  111. out_idx = 0;
  112. out_r1[out_idx] = r1;
  113. out_r2[out_idx] = r2;
  114. out_op[out_idx] = "+";
  115. out_rres[out_idx] = rres;
  116. out_tol[out_idx++] = rres_tol;
  117.  
  118. for (; R[r1_idx] >= rd / 2.0; r1_idx--) {
  119. iter++;
  120. r1 = R[r1_idx];
  121.  
  122. r2d = rd - r1; // this is the value needed
  123. <!-- document.getElementById('debuga').value += '+ ' + rd + ' ' + r1 + ' ' + r2d + ' ' + 1/rd + ' ' + 1/r1 + ' ' + 1/r2d + '\n' -->
  124. if (r2d < 0) { continue } // might happen...
  125.  
  126. r2_idx = FindIndex(R, r2d);
  127. r2 = R[r2_idx]; // get the nearest standard value
  128. rres = r1 + r2; // compute the resulting composition
  129. rres_tol = rres / rd - 1.0; // and its tolerance
  130.  
  131.  
  132. if (Math.abs(rres_tol) < Math.abs(best_tol)) {
  133. //best_tol = rres_tol;
  134. out_r1[out_idx] = r1;
  135. out_r2[out_idx] = r2;
  136. out_op[out_idx] = "+";
  137. out_rres[out_idx] = rres;
  138. out_tol[out_idx++] = rres_tol;
  139. }
  140.  
  141. }
  142.  
  143. rd = 1.0 / rd;
  144. /* compute assuming resistors in parallel */
  145. r1_idx = FindIndex(G, rd);
  146. for (; G[r1_idx] >= rd / 2.1; r1_idx--) {
  147. iter++;
  148. r1 = G[r1_idx];
  149.  
  150. r2d = rd - r1; // this is the value needed
  151. <!-- document.getElementById('debuga').value += '|| ' + rd + ' ' + r1 + ' ' + r2d + ' ' + 1/rd + ' ' + 1/r1 + ' ' + 1/r2d + '\n' -->
  152. if (r2d < 0) { continue } // might happen...
  153.  
  154. r2_idx = FindIndex(G, r2d);
  155. r2 = G[r2_idx]; // get the nearest standard value
  156. rres = r1 + r2; // compute the resulting composition
  157. rres_tol = rd / rres - 1.0; // and its tolerance
  158.  
  159. if (Math.abs(rres_tol) < Math.abs(best_tol)) {
  160. //best_tol = rres_tol;
  161. // use values from R array to avoid rounding errors
  162. // which will lead to something like 6800.0000001...
  163. out_r1[out_idx] = R[n_max - r1_idx] // 1.0 / r1;
  164. out_r2[out_idx] = R[n_max - r2_idx] // 1.0 / r2;
  165. out_op[out_idx] = "||";
  166. out_rres[out_idx] = 1.0 / rres;
  167. out_tol[out_idx++] = rres_tol;
  168. }
  169. }
  170.  
  171. // sort the results
  172. for (i = 1; i < out_idx; i++) {
  173. r1 = out_r1[i];
  174. r2 = out_r2[i];
  175. op = out_op[i];
  176. rres = out_rres[i];
  177. rres_tol = out_tol[i];
  178. for (j = i - 1; (j >= 0) &&
  179. Math.abs(out_tol[j]) > Math.abs(rres_tol); j--) {
  180. out_r1[j + 1] = out_r1[j];
  181. out_r2[j + 1] = out_r2[j];
  182. out_op[j + 1] = out_op[j];
  183. out_rres[j + 1] = out_rres[j];
  184. out_tol[j + 1] = out_tol[j];
  185. }
  186. out_r1[j + 1] = r1;
  187. out_r2[j + 1] = r2;
  188. out_op[j + 1] = op;
  189. out_rres[j + 1] = rres;
  190. out_tol[j + 1] = rres_tol;
  191. }
  192.  
  193. for (r1_idx = 0; r1_idx < out_idx; r1_idx++) {
  194. out_prres = (Math.round(out_rres[r1_idx] * 1000)) / 1000 ;
  195. out_vrres = out_prres.toString();
  196. if(out_vrres.length < 8) out_vrres = out_vrres + "\t"
  197. document.getElementById('texta').value +=
  198. out_r1[r1_idx] + "\t" +
  199. out_op[r1_idx] + "\t" +
  200. out_r2[r1_idx] + "\t=\t" +
  201. /* leave three decimal digits maximum */
  202. out_vrres + "\t(" +
  203. (Math.round(out_tol[r1_idx] * 100000)) / 1000 + " %)\n"
  204. }
  205. // put the focus on the input field
  206. //document.getElementById('rd').focus();
  207.  
  208. }
  209.  
  210. //]]>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement