Last active
May 26, 2020 09:07
-
-
Save altavir/96b6ff1521551f10bbc6914aaced2745 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
| import math | |
| import scipy.signal | |
| import numpy as np | |
| import plotly.graph_objects as go # I prefer ployly over matplotlib | |
| from scipy.ndimage import gaussian_filter1d | |
| step = 0.01 # Defining the grid step for all further operations | |
| # gaussian kernel on its own grid with the same step | |
| def gauss(pos, sigma): | |
| guass_x = np.arange(-5*sigma, 5*sigma, step) | |
| return np.exp(-(guass_x/sigma)**2.0/2.0)/sigma/np.sqrt(2.0*math.pi) | |
| # convolution operation | |
| def convolve(spectrum, sigma): | |
| # Not an obvious point. We need to convert from grid cell number to actual x value. | |
| sigma_steps = sigma/step | |
| # We can also try to use numpy.convolve or scipy.signal.convolve, but result is more or less the same | |
| return gaussian_filter1d(spectrum, sigma_steps) | |
| # peak definitions | |
| k_base_prob = 0.864 | |
| k_base_energy = 112.0 | |
| k_ext_prob = 0.096 | |
| k_ext_energy = 83.0 | |
| l_base_prob = 0.036 | |
| l_base_energy = 55.0 | |
| l_ext_prob = 0.004 | |
| ext_width = 28.0 | |
| # custom peak definition | |
| def ext_value(x, pos, prob): | |
| if abs(x - pos) > ext_width: | |
| return 0.0 # PITFALL! | |
| else: | |
| return math.cos( (x-pos)/ext_width * math.pi/2.0) /2.0 *prob | |
| # need to vectorize the function to make it work properly with numpy | |
| def vector_ext_func(pos, prob): | |
| return np.vectorize(lambda x: ext_value(x, pos,prob)) | |
| # define peaks | |
| curve_x = np.arange(0, 130, step) # x values for the curve | |
| curve = np.zeros(curve_x.size) # empty array initialization | |
| # add two delta-peaks | |
| curve[(int)(k_base_energy/step)] = k_base_prob | |
| curve[(int)(l_base_energy/step)] = l_base_prob | |
| # add a custom peak | |
| curve = curve + vector_ext_func(k_ext_energy,k_ext_prob)(curve_x) | |
| # generating curve and plotting with Plotly | |
| convoluted = convolve(curve, 0.5) | |
| fig = go.Figure( | |
| data = [ | |
| go.Scatter(x=curve_x, y = curve, name = "initial"), | |
| go.Scatter(x=curve_x, y = convoluted, name = "convolved") | |
| ] | |
| ) | |
| fig.update_yaxes(type="log") | |
| fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment