Skip to content

Instantly share code, notes, and snippets.

@ajossorioarana
Last active July 18, 2023 19:25
Show Gist options
  • Save ajossorioarana/047202db9c2990b43cf7ba2e02735faf to your computer and use it in GitHub Desktop.
Save ajossorioarana/047202db9c2990b43cf7ba2e02735faf to your computer and use it in GitHub Desktop.
PDFs for Convolution with FFTW
import numpy as np
from scipy.signal import fftconvolve
def get_pdf(pdf_params, x):
loc = pdf_params[0]
unc = pdf_params[1]
pd_loc = pdf_params[2]
lpb = pdf_params[3]
upb = pdf_params[4]
pd_lpb = pdf_params[5]
pd_upb = pdf_params[6]
lub = pdf_params[7]
uub = pdf_params[8]
pd_lub = pdf_params[9]
pd_uub = pdf_params[10]
locs = [lpb, lub, loc, uub, upb]
pd_locs = [pd_lpb, pd_lub, pd_loc, pd_uub, pd_upb]
# Actual implementation of PDF
p_x = np.where((x >= lpb) & (x <= upb),
np.interp(x, locs, pd_locs), 0)
# Normalize
pdf = p_x / np.sum(p_x)
return pdf
pdf_x_size = 100000
# Values of X where to calculate PDF
x = np.linspace(-0.05, 1, pdf_x_size, dtype='float16')
# Define parameters of PDFs
pdf1_params = [0.85887937, 0.14592418, 12.3069, 0.75, 1., 0.57650279,
0.57650279, 0.8078059, 0.95373008, 0.57650279, 0.57650279]
pdf2_params = [ 4.00e-02, 2.00e-02, 9.25e+01, -1.00e-02, 5.00e-02, 1.50e+00,
1.50e+00, 3.00e-02, 500e-02, 1.50e+00, 1.50e+00]
# Creating the pdfs, based on parameters
pdf1 = get_pdf(pdf1_params, x)
pdf2 = get_pdf(pdf2_params, x)
# Do the convovlution with scipy.signal.fftconvolve
convolution_pdf = scipy.signal.fftconvolve(pdf1, pdf2, 'full')
convolution_pdf = convolution_pdf[0:len(pdf1)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment