<!-- DO NOT EDIT. -->
<!-- THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. -->
<!-- TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: -->
<!-- "auto_examples/physics/demo_lidar.py" -->
<!-- LINE NUMBERS ARE GIVEN BELOW. -->

#### NOTE
New to DeepInverse? Get started with the basics with the
[5 minute quickstart tutorial](https://deepinv.org/auto_examples/basics/demo_quickstart.html.md#sphx-glr-auto-examples-basics-demo-quickstart-py)..

<a id="sphx-glr-auto-examples-physics-demo-lidar-py"></a>

# Single photon lidar operator for depth ranging.

In this example we show how to use the [`deepinv.physics.SinglePhotonLidar`](https://deepinv.org/api/stubs/deepinv.physics.SinglePhotonLidar.html.md#deepinv.physics.SinglePhotonLidar) forward model.

<!-- GENERATED FROM PYTHON SOURCE LINES 8-14 -->
```Python
import deepinv as dinv
import torch
import matplotlib.pyplot as plt
```

<!-- GENERATED FROM PYTHON SOURCE LINES 16-33 -->

## Create forward model

We create a lidar model with 100 bins per pixel and a Gaussian impulse response function
with a standard deviation of 2 bins.

The forward model for the case of a single depth per pixel is defined as (e.g. see Rapp *et al.*<sup>[1](#footcite-rapp2020advances)</sup>):

$$
y_{i,j,t} = \mathcal{P}(h(t-d_{i,j}) r_{i,j} + b_{i,j})
$$

where $\mathcal{P}$ is the Poisson noise model, $h(t)$ is a Gaussian impulse response function at
time $t$, $d_{i,j}$ is the depth of the scene at pixel $(i,j)$,
$r_{i,j}$ is the intensity of the scene at pixel $(i,j)$ and $b_{i,j}$ is the background noise
at pixel $(i,j)$.

<!-- GENERATED FROM PYTHON SOURCE LINES 33-39 -->
```Python
device = dinv.utils.get_device()
bins = 100
irf_sigma = 2
physics = dinv.physics.SinglePhotonLidar(bins=bins, sigma=irf_sigma, device=device)
```

```none
Selected GPU 0 with 28779.9375 MiB free memory
```

<!-- GENERATED FROM PYTHON SOURCE LINES 40-51 -->

## Generate toy signal and measurement

We generate a toy signal with a single peak per pixel located around the 50th bin
and a signal-to-background ratio of 10%.

Signals should have size (B, 3, H, W) where the first channel contains the depth of the scene,
the second channel contains the intensity of the scene and the third channel contains the
per pixel background noise levels.

The measurement associated with a signal has size (B, bins, H, W).

<!-- GENERATED FROM PYTHON SOURCE LINES 51-69 -->
```Python
sbr = 0.1
signal = 50
bkg_level = signal / sbr / bins
depth = bins / 2

# depth
d = torch.ones(1, 1, 2, 2, device=device) * depth
# signal
r = torch.ones_like(d) * signal
# background
b = torch.ones_like(d) * bkg_level

x = torch.cat([d, r, b], dim=1)  # signal of size (B, 3, H, W)

y = physics(x)  # measurement of size (B, bins, H, W)
```

<!-- GENERATED FROM PYTHON SOURCE LINES 70-77 -->

## Apply matched filtering to recover the signal and plot the results

We apply matched filtering to recover the signal and plot the results.

The measurements are shown in blue and the depth and intensity of the
recovered signals are shown in red.

<!-- GENERATED FROM PYTHON SOURCE LINES 77-98 -->
```Python
xhat = physics.A_dagger(y)

plt.figure()
for i in range(2):
    for j in range(2):
        plt.subplot(2, 2, i * 2 + j + 1)
        plt.plot(y[0, :, i, j].detach().cpu().numpy())
        plt.stem(
            xhat[0, 0, i, j].detach().cpu().numpy(),
            xhat[0, 1, i, j].detach().cpu().numpy() / 4,
            linefmt="red",
            markerfmt="red",
        )
        plt.title(f"pixel ({i}, {j})")


plt.tight_layout()
plt.show()
```

```none
/local/jtachell/deepinv/deepinv/deepinv/physics/lidar.py:90: UserWarning: Using padding='same' with even kernel lengths and odd dilation may require a zero-padded copy of the input be created (Triggered internally at /home/conda/feedstock_root/build_artifacts/libtorch_1791515682889/work/aten/src/ATen/native/Convolution.cpp:1101.)
  x = torch.nn.functional.conv1d(y, self.irf, padding="same")
```

<!-- GENERATED FROM PYTHON SOURCE LINES 99-102 -->
* **References:**

* <a id='footcite-rapp2020advances'>**[1]**</a> Joshua Rapp, Julián Tachella, Yoann Altmann, Stephen McLaughlin, and Vivek K Goyal. Advances in single-photon lidar for autonomous vehicles: working principles, challenges, and recent advances. *IEEE Signal Processing Magazine*, 37(4):62–71, 2020.

**Total running time of the script:** (0 minutes 0.153 seconds)

🏷 Tags: Low-photon imaging

<a id="sphx-glr-download-auto-examples-physics-demo-lidar-py"></a>

[`Download Jupyter notebook: demo_lidar.ipynb`](https://deepinv.org/auto_examples/physics/_downloads/268ae32df741eeeab9ec9b388d77c13f/demo_lidar.ipynb)

[`Download Python source code: demo_lidar.py`](https://deepinv.org/auto_examples/physics/_downloads/3c55f69355e01ca17263d39ab3d33e0b/demo_lidar.py)

[`Download zipped: demo_lidar.zip`](https://deepinv.org/auto_examples/physics/_downloads/8cdb0ce5bc3ac86928a2507b1f61eb7b/demo_lidar.zip)

[Gallery generated by Sphinx-Gallery](https://sphinx-gallery.github.io)
