Gaussian Pulse generation using python

103 Views Asked by At

I'm trying to generate a gaussain pulse in my FDTD 2D algorithm but it doesn't seem to generate with the defined spatial time step.

import numpy as np
import matplotlib.pyplot as plt
import math
delta_x = 6e-9
Nx = 500
beam_center = Nx / 2 * delta_x
s=2
epsilon_0 = 8.85e-12
mu_0 = 4*math.pi*1e-7
c = 1/math.sqrt(epsilon_0*mu_0)
eta_0 = math.sqrt(mu_0/epsilon_0)

# Calculate the time step based on spatial and time step
delta_x = 6e-9
delta_z = delta_x
delta_t = delta_z/(s*c)
total_time = 5000 * delta_t

# Generate the time array
t = np.arange(0, total_time, delta_t)

beam_waist = 200e-9

gaussian_pulse = np.exp(-((t-beam_center)**2)/2*beam_waist**2)

# Plot the Gaussian pulse
plt.plot(t, gaussian_pulse)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Gaussian Pulse')
plt.show()

This code above gives a straight horizontal line at 1. I understand its a scaling problem becaus if i use t=linspace(100,100,5000), it gives a gaussian pulse. But I'mbound by the timeseries as defined in code because it's part of the larger FDTD 2D code

2

There are 2 best solutions below

0
Drashti On

try below and check if that works or not: gaussian_pulse = np.exp(-((t - beam_center)2) / (2 * beam_waist2))

0
Martin Brown On

It is doing exactly what you told it to do.

Either add brackets to make the expression correct or factor out the denominator first and retain the multiply. Divides do tend to be slow (although today most compilers will do that strength reduction for you)!

The offending code is

beam_waist = 200e-9

gaussian_pulse = np.exp(-((t-beam_center)**2)/2*beam_waist**2) #wrong

# should be 
gaussian_pulse = np.exp(-((t-beam_center)**2)/(2*beam_waist**2))

Old school I would probably code it as

r2sigma2 = 1/(2*beam_waist**2)
gaussian_pulse = np.exp(-((t-beam_center)**2)*r2sigma2)