Created
March 13, 2025 00:47
-
-
Save Simon-L/0bfdd6abbc121bbe38270e9fdb151716 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 27, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import math\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 28, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "\n", | |
| "L_L_low = 309\n", | |
| "L_L_high = 437\n", | |
| "\n", | |
| "L_H_low = 400\n", | |
| "L_H_high = 2340\n", | |
| "\n", | |
| "H_L_low = 2500\n", | |
| "H_L_high = 3772\n", | |
| "\n", | |
| "H_H_low = 1000\n", | |
| "H_H_high = 25151" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "| cutoff | envmod | low | high |\n", | |
| "|--------|--------|------|-------|\n", | |
| "| L | L | 309 | 437 |\n", | |
| "| L | H | 400 | 2340 |\n", | |
| "| H | L | 2500 | 3772 |\n", | |
| "| H | H | 1000 | 25151 |" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 296, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "def fton(f):\n", | |
| " return 12.0 * math.log2(f/440.0)\n", | |
| "\n", | |
| "class Coeffs:\n", | |
| " A: float = 4.87\n", | |
| " B: float = 9.22\n", | |
| " C: float = 0.315\n", | |
| " D: float = 0.39\n", | |
| " E: float = 4.020\n", | |
| " exp_bias: float = 3.2\n", | |
| "\n", | |
| " minenvmod_from: float = 2.38\n", | |
| " minenvmod_to: float = 3.9\n", | |
| " maxenvmod_from: float = 1.86\n", | |
| " maxenvmod_to: float = 10\n", | |
| "\n", | |
| " Vco = 11.84\n", | |
| "\n", | |
| "class Run:\n", | |
| " c: Coeffs()\n", | |
| " minlow: float\n", | |
| " minhigh: float\n", | |
| " maxlow: float\n", | |
| " maxhigh: float\n", | |
| "\n", | |
| " def __init__(self, _c):\n", | |
| " self.c = _c\n", | |
| " self.minlow = (self.c.A * (self.c.Vco-self.c.exp_bias) + self.c.B) * math.exp(self.c.C * (self.c.minenvmod_from - self.c.exp_bias) + (self.c.D*0) + self.c.E)\n", | |
| " self.minhigh = (self.c.A * (self.c.Vco-self.c.exp_bias) + self.c.B) * math.exp(self.c.C * (self.c.minenvmod_to - self.c.exp_bias) + (self.c.D*0) + self.c.E)\n", | |
| " self.maxlow = (self.c.A * (self.c.Vco-self.c.exp_bias) + self.c.B) * math.exp(self.c.C * (self.c.maxenvmod_from - self.c.exp_bias) + (self.c.D*0) + self.c.E)\n", | |
| " self.maxhigh = (self.c.A * (self.c.Vco-self.c.exp_bias) + self.c.B) * math.exp(self.c.C * (self.c.maxenvmod_to - self.c.exp_bias) + (self.c.D*0) + self.c.E)\n", | |
| " self.score = self.assess()\n", | |
| "\n", | |
| " def print(self):\n", | |
| " # print(\"Sweep range | A:{:3.3f}|B:{:3.3f}|C:{:3.3f}|D:{:3.3f}|E:{:3.3f} | @Vco={:3.3f}V Min Envmod {:3.3f} -> {:3.3f} | Max Envmod {:3.3f} -> {:3.3f}\"\\\n", | |
| " # .format(c.A, c.B, c.C, c.D, c.E, c.Vco, minlow, minhigh, maxlow, maxhigh))\n", | |
| " print(\"Score:{:3.6f}|A:{:3.6f}|B:{:3.6f}|C:{:3.6f}|D:{:3.6f}|E:{:3.6f}|maxenvmod_from:{:3.6f}|exp_bias:{:3.6f}\".format(self.score, self.c.A, self.c.B, self.c.C, self.c.D, self.c.E, self.c.maxenvmod_from, self.c.exp_bias))\n", | |
| " print(\"\\t\\t{:.2f}->{:.2f}|{:.2f}->{:.2f}|{:.2f}->{:.2f}|{:.2f}->{:.2f}\".format(self.minlow, H_L_low,self.minhigh, H_L_high, self.maxlow, H_H_low, self.maxhigh, H_H_high))\n", | |
| " \n", | |
| " def assess(self):\n", | |
| " self.H_L_low_delta = fton(self.minlow) - fton(H_L_low)\n", | |
| " self.H_L_high_delta = fton(self.minhigh) - fton(H_L_high) \n", | |
| " self.H_H_low_delta = fton(self.maxlow) - fton(H_H_low)\n", | |
| " self.H_H_high_delta = fton(self.maxhigh) - fton(H_H_high)\n", | |
| " score = abs(self.H_L_low_delta) + abs(self.H_L_high_delta) + abs(self.H_H_low_delta) + abs(self.H_H_high_delta)\n", | |
| " # print(\"score:{} H_L_low_delta:{:2f} H_L_high_delta:{:2f} H_H_low_delta:{:2f} H_H_high_delta:{:2f}\".format(score,H_L_low_delta, H_L_high_delta, H_H_low_delta, H_H_high_delta))\n", | |
| " return score\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 301, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "Score:1.596496|A:2.641559|B:12.218335|C:0.305012|D:0.390000|E:4.294796|maxenvmod_from:-0.442149|exp_bias:2.675681\n", | |
| "\t\t2440.37->2500.00|3879.74->3772.00|1031.86->1000.00|24936.75->25151.00\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "import random\n", | |
| "random.seed()\n", | |
| "\n", | |
| "c = Coeffs()\n", | |
| "\n", | |
| "\n", | |
| "def run(c):\n", | |
| " r = Run(c)\n", | |
| " # r.print()\n", | |
| "\n", | |
| "def rrun():\n", | |
| " c = Coeffs()\n", | |
| " c.C += random.uniform(-5, 5)\n", | |
| " c.A += random.uniform(-5, 5)\n", | |
| " c.B += random.uniform(-5, 5)\n", | |
| " c.exp_bias += random.uniform(-1, 1)\n", | |
| " c.maxenvmod_from += random.uniform(-3, 3)\n", | |
| " c.E += random.uniform(-5, 5)\n", | |
| " return Run(c)\n", | |
| "\n", | |
| "runs = []\n", | |
| "for i in range(10000000):\n", | |
| " r = rrun()\n", | |
| " runs.append(r)\n", | |
| "\n", | |
| "runs.sort(key=lambda r: r.score)\n", | |
| "\n", | |
| "runs[0].print()" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": ".venv", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.12.6" | |
| }, | |
| "orig_nbformat": 4, | |
| "vscode": { | |
| "interpreter": { | |
| "hash": "fcabe5e7867ec6bb17dec7ce18a7d4db55bab62a70d42afb12c01f1841ed0749" | |
| } | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment