{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "6142bda224f4",
   "metadata": {},
   "source": [
    "# Detecting the Gouy phase at a beam waist\n",
    "Compare focused Gaussian propagation with a plane reference carrying the same axial phase. Predict how a larger waist changes the location and total amount of the residual phase shift."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c13e42469b2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "waist_um=40.\n",
    "wavelength_um=.633\n",
    "extent_in_rayleigh=4.\n",
    "reference_offset_rad=.4\n",
    "assert waist_um>3*wavelength_um>0 and extent_in_rayleigh>0"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12d9f49f3fc1",
   "metadata": {},
   "source": [
    "## Recover phase from two interference quadratures\n",
    "Use two calibrated equal-amplitude reference measurements to reconstruct the phase independent of amplitude. The reference sign is defined so the on-axis field phase is kz minus the Gouy angle."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3ddcc9350b1d",
   "metadata": {},
   "outputs": [],
   "source": [
    "zr=np.pi*waist_um**2/wavelength_um\n",
    "s=np.linspace(-extent_in_rayleigh,extent_in_rayleigh,401);z_mm=zr*s/1000\n",
    "envelope=1/(1+1j*s)\n",
    "phase=np.unwrap(np.angle(envelope))\n",
    "c=np.real(envelope*np.exp(1j*reference_offset_rad))\n",
    "q=np.imag(envelope*np.exp(1j*reference_offset_rad))\n",
    "recovered=np.unwrap(np.arctan2(q,c))-reference_offset_rad\n",
    "assert np.allclose(np.exp(1j*recovered),np.exp(1j*phase))\n",
    "print(\"Rayleigh range (mm):\",zr/1000,\"; residual phase from −zR to +zR (rad):\",-np.pi/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0d513e8dfac5",
   "metadata": {},
   "source": [
    "## Separate axial beam width from axial phase\n",
    "Reflecting z about the waist preserves beam size and reverses the phase shift. An intensity-only scan cannot distinguish the two signs. The two readout channels on the right retain sign information after calibration."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e7aa761948f8",
   "metadata": {},
   "outputs": [],
   "source": [
    "fig,ax=plt.subplots(1,2,figsize=(9,3.9),layout=\"constrained\")\n",
    "ax[0].plot(z_mm,phase,color=\"#175868\",label=\"Gaussian residual\")\n",
    "ax[0].axhline(0,ls=\":\",color=\"grey\",label=\"Plane-wave residual\");ax[0].legend(fontsize=8)\n",
    "ax[0].set(xlabel=\"Axial coordinate (mm)\",ylabel=\"Phase after removing kz (rad)\",title=\"On-axis Gouy phase\")\n",
    "ax[1].plot(z_mm,c,color=\"#375b9a\",label=\"In-phase channel\")\n",
    "ax[1].plot(z_mm,q,color=\"#a23d21\",label=\"Quadrature channel\")\n",
    "ax[1].set(xlabel=\"Axial coordinate (mm)\",ylabel=\"Normalised field quadrature\",title=\"Two-phase interferometer\");ax[1].legend(fontsize=8)\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
