# Gaussian Beam Spot Size and Rayleigh Range Python Script Generator

> Source: https://netray.co/tools/gaussian-beam-spot-size-and-rayleigh-range-python-generator
> Publisher: Netray (netray.co), AI and integration engineering for ERP and manufacturing systems.
> Section: Free Tools

## Short answer

Fill in laser wavelength, M-squared, focal length and input beam diameter to generate a ready-to-run Python script for focused spot size and depth of focus.

## Category

developer

## Industry

Photonics and Optics

## Audience

- Optical engineer
- Laser process engineer
- Test engineer

## Tool

**Kind:** template

**Output:** code

**Language:** python

**Fields:**

#### Wavelength (nm)

**Key:** wavelength_nm

**Type:** number

**Default:** 1064

**Help:** Laser wavelength in nanometers

#### Beam quality M-squared

**Key:** m2

**Type:** number

**Default:** 1.3

**Help:** 1.0 is a perfect Gaussian beam

#### Focal length (mm)

**Key:** focal_mm

**Type:** number

**Default:** 160

**Help:** Focusing lens or scan lens focal length

#### Input beam diameter at lens, 1/e2 (mm)

**Key:** beam_d_mm

**Type:** number

**Default:** 8

**Help:** Diameter at the 1/e squared intensity points

#### Average power (W)

**Key:** power_w

**Type:** number

**Default:** 50

#### Pulse repetition rate (kHz)

**Key:** rep_khz

**Type:** number

**Default:** 100

**Help:** Use 0 for a continuous wave laser

**Blocks:**

#### gaussian_focus.py

**Body:** import math

# Inputs
wavelength_m = {{wavelength_nm}} * 1e-9
m2 = {{m2}}
focal_m = {{focal_mm}} * 1e-3
beam_d_m = {{beam_d_mm}} * 1e-3
power_w = {{power_w}}
rep_khz = {{rep_khz}}

# Input beam radius (1/e^2) at the lens
w_in = beam_d_m / 2.0

# Focused waist radius (far-field approximation, lens at the beam waist)
w0 = m2 * wavelength_m * focal_m / (math.pi * w_in)

# Rayleigh range and depth of focus (2 x Rayleigh range)
z_r = math.pi * w0 ** 2 / (m2 * wavelength_m)
dof = 2.0 * z_r

# Far-field half-angle divergence in the focused beam
theta = m2 * wavelength_m / (math.pi * w0)

# Peak intensity of a CW Gaussian beam: 2P / (pi w0^2)
peak_intensity = 2.0 * power_w / (math.pi * w0 ** 2)

print('Focused spot diameter (1/e^2): %.1f um' % (2 * w0 * 1e6))
print('Rayleigh range: %.2f mm' % (z_r * 1e3))
print('Depth of focus (2 x zR): %.2f mm' % (dof * 1e3))
print('Divergence half-angle: %.2f mrad' % (theta * 1e3))
print('Peak intensity (CW): %.3e W/cm^2' % (peak_intensity / 1e4))

if rep_khz > 0:
    pulse_energy_j = power_w / (rep_khz * 1e3)
    fluence_j_cm2 = 2.0 * pulse_energy_j / (math.pi * w0 ** 2) / 1e4
    print('Pulse energy: %.1f uJ' % (pulse_energy_j * 1e6))
    print('Peak fluence per pulse: %.2f J/cm^2' % fluence_j_cm2)

**Notes:** Uses the standard Gaussian beam focusing relation w0 = M2 x lambda x f / (pi x w_in), valid when the input beam is collimated and the lens is not aberration limited. Strongly focused or truncated beams, large scan angles and thick optics need a full ray trace or measured spot data. Verify with a beam profiler per ISO 11146.

## Content

**Intro:** This generator writes a short Python script that turns your laser wavelength, beam quality, focal length and input beam size into a focused spot size, Rayleigh range, depth of focus, peak intensity and pulse fluence. You can paste it into any Python 3 environment, change the inputs and rerun it for process tuning.

**Sections:**

#### How to use it

**Body:** Fill in the six fields, copy the generated code, and run it with Python 3. The only import is the standard math library, so it runs anywhere. Enter the beam diameter at the 1/e squared points, not the full width at half maximum, since the formulas assume 1/e squared radius. For a CW laser set repetition rate to zero and the fluence lines are skipped. Edit the numbers to explore what-if cases such as a different lens.

**Bullets:**

- Use the 1/e squared diameter at the lens
- Enter M-squared from the laser datasheet or beam profiler
- Set repetition rate to 0 for CW lasers
- Compare results against a measured burn or profiler image

#### The formulas

**Body:** For a collimated Gaussian beam of radius w_in on a lens of focal length f, the focused waist is w0 = M2 x lambda x f / (pi x w_in). The Rayleigh range is zR = pi x w0 squared / (M2 x lambda), the distance over which the beam area doubles, and the depth of focus is commonly taken as twice that. Peak intensity for a CW beam is 2P / (pi x w0 squared). Pulse fluence uses the pulse energy P / f_rep in place of power.

#### Worked example

**Body:** A 1064 nm laser with M-squared of 1.3, an 8 mm input beam and a 160 mm lens gives a waist radius of about 22 micrometers, so a spot diameter near 44 micrometers. The Rayleigh range is about 1.4 mm and the depth of focus about 2.8 mm. Doubling the input beam diameter halves the spot, but the Rayleigh range falls with the square of the waist, so the depth of focus shrinks to about a quarter. That is the core trade-off in laser processing.

#### Common mistakes

**Body:** People often enter the full-width half-maximum beam diameter and get a spot that is too small. Another error is applying the formula to a beam clipped by an aperture, which adds diffraction rings and enlarges the spot. Scan lenses and f-theta optics add aberrations at the edge of the field. Finally, the formulas give an idealised spot, so real thresholds, such as ablation fluence, should be confirmed on sample material.

**Cta:** If your laser process parameters and results are logged in an MES or ERP, we can help build a private assistant that links spot size and fluence to yield and scrap data. Get in touch to see how that could work for you.

**Faq:**

#### What is the Rayleigh range?

**Answer:** It is the distance from the beam waist over which a Gaussian beam radius grows by a factor of the square root of two, so the beam area doubles. It is calculated as pi x w0 squared divided by M-squared times wavelength, and twice that value is a common definition of depth of focus.

#### Does a better M-squared always give a smaller spot?

**Answer:** For the same wavelength, lens and input beam size, a lower M-squared gives a proportionally smaller waist and a longer Rayleigh range relative to it. A perfect Gaussian beam has M-squared of 1, and real lasers range from about 1.05 for good single-mode systems to 20 or more for multimode ones.

#### Why does the script ask for the 1/e squared diameter?

**Answer:** The Gaussian beam formulas are defined using the radius where intensity falls to 1/e squared, about 13.5 percent of peak. Using other width definitions, such as FWHM or D4 sigma without conversion, changes the result, so check which one your datasheet or profiler reports.

#### Can I use this for a fiber-delivered beam?

**Answer:** Yes, if you enter the collimated beam diameter after the collimator and the beam quality of the fiber output. For multimode process fibers the spot is closer to the fiber core image scaled by the focal length ratio, so use the imaging relation instead for those.

## Search terms

gaussian beam spot size python, rayleigh range calculator python, focused laser spot size formula, m squared beam waist script, depth of focus laser python, beam parameter product script, laser spot size calculation, gaussian beam focusing formula

---
Talk to an engineer: https://netray.co/contact?intent=erp_ai | Machine catalog: https://netray.co/agent/catalog.json
