[Python] Comparing numerical and analytical convolution

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am calculating the convolution of two functions, exp(-bt^2 + iat) and exp(-c|t| + iat). If try to analytically calculate it in Mathematica with

    Convolve[Exp[-b*t^2 + I*a*t], Exp[-c*Abs[t] + I*a*t], t, \[Tau]]


    I get the solution

    [​IMG]

    Then if I plot this solution in Python, and compare it with the result from numerical convolution scipy.signal.fftconvolve, they don't agree with each other. The x-intercepts seem ok but there is a problem with the vertical scaling.

    [​IMG]

    Context: This is a MWE - I ultimately want to work just in Python after finding an analytical solution in Mathematica. It would be simpler to use an explicit expression instead of having to calculate a numerical convolution.

    import numpy as np
    from scipy.special import erfc
    from scipy.signal import fftconvolve
    import matplotlib.pyplot as plt

    t = np.arange(-100,100,1e6) # time

    a = np.random.rand()
    b = np.random.rand()
    c = np.random.rand()

    x = np.exp(-b*t**2+1j*a*t)
    y = np.exp(-c*abs(t)+1j*a*t)
    z = 2*np.sqrt(np.pi/b)*np.exp(1j*a*t+c**2/(4*b)) * ( \
    np.exp(-c*t)*erfc((c-2*b*t)/2*np.sqrt(b)) + \
    np.exp(c*t)*erfc((c+2*b*t)/2*np.sqrt(b)) )

    zc = fftconvolve(x,y,mode='same')*(t[1]-t[0])

    plt.figure(1,figsize=(12,4))

    plt.subplot(121)
    plt.plot(t,np.real(zc),'--',label='conv')
    plt.plot(t,np.real(z),label='analytic')
    plt.xlabel('time')
    plt.title('real part')
    plt.legend(loc='best')
    plt.grid()
    plt.xlim((-20,20))

    plt.subplot(122)
    plt.plot(t,np.imag(zc),'--',label='conv')
    plt.plot(t,np.imag(z),'-',label='analytic')
    plt.xlabel('time')
    plt.title('imag part')
    plt.legend(loc='best')
    plt.grid()
    plt.xlim((-20,20))

    Continue reading...

Compartilhe esta Página