Created
April 21, 2022 18:54
-
-
Save jradavenport/25e5c536ab2bb90ede9bc2332a3a272c to your computer and use it in GitHub Desktop.
how to create some linear functions
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"id": "62c6ee2b", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import matplotlib.pyplot as plt\n", | |
"from scipy.optimize import curve_fit" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "bf926a3e", | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"def linear_gaus1(x, a0, aslope, b, x0, sigma, per):\n", | |
" '''\n", | |
" here is a simple Gaussian function, but where the\n", | |
" amplitude changes linearly with time.\n", | |
" \n", | |
" note: curve fit will try to fit period in this case!\n", | |
" use very tight boundaries!!\n", | |
" '''\n", | |
" phase = (x % per) / per\n", | |
" ampl = a0 + (x * aslope)\n", | |
" return ampl * np.exp(-(phase - x0)**2 / (2 * sigma**2)) + b\n", | |
"\n", | |
"\n", | |
"def linear_gaus2(x, a0, aslope, b, x0, xslope, sigma, per):\n", | |
" '''\n", | |
" here is a simple Gaussian function, but where the\n", | |
" amplitude AND phase center changes linearly with time\n", | |
" \n", | |
" note: curve fit will try to fit period in this case!\n", | |
" use very tight boundaries!!\n", | |
" '''\n", | |
" phase = (x % per) / per\n", | |
" ampl = a0 + (x * aslope)\n", | |
" xcent = x0 + (phase * xslope)\n", | |
" return ampl * np.exp(-(phase - xcent)**2 / (2 * sigma**2)) + b\n", | |
"\n", | |
"\n", | |
"def linear_gaus_P(x, a0, aslope, b, x0, xslope, sigma, per = 1.234):\n", | |
" '''\n", | |
" here is a simple Gaussian function, but where the\n", | |
" amplitude AND phase center changes linearly with time\n", | |
" \n", | |
" here period is hard coded and does not need to be passed\n", | |
" note: you'd need to make this function for every system.\n", | |
" '''\n", | |
" phase = (x % per) / per\n", | |
" ampl = a0 + (x * aslope)\n", | |
" xcent = x0 + (phase * xslope)\n", | |
" return ampl * np.exp(-(phase - xcent)**2 / (2 * sigma**2)) + b\n", | |
"\n", | |
"\n", | |
"# here's one possible hack... which *might* work. I dont know! :D\n", | |
"for k in range(len(systems)):\n", | |
" # define a function INSIDE the loop!\n", | |
" def myfunc(x, a0, aslope, b, x0, xslope, sigma):\n", | |
" return linear_gaus_P(x, a0, aslope, b, x0, xslope, sigma, per=periods[k])\n", | |
" \n", | |
" popt,pcov = curve_fit(myfunc xdata, ydata, p0=[some initial parameters])" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"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.8.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment