*4.4. Simulations*

To validate the particle detection and classification scheme used on confocal images, a program was written in Python (using the NumPy and Pandas libraries) to simulate particle diffusion in a plane and generate confocal images of this plane (with dimensions *nx* × *ny* = 100 × 100 pxl, pixel size *d* = 100 nm and pixel dwell time *δ* = 1 ms). To emulate the behaviour of particles which may move during the recording of a pixel intensity, the time step for the simulation *τ* was chosen to be a fraction of the exposure time, *τ* = *δ*/*<sup>m</sup>*, with *m* = 10. Initial positions for *N* different particles,*r*0, were randomly selected inside the image area. Tracks for mobile particles were simulated with the constant diffusion coefficient *D*, by generating at each time step *n* a displacement *<sup>δ</sup>r*(*n*) = (*<sup>δ</sup><sup>x</sup>*, *<sup>δ</sup>y*) for each particle, whose components were drawn from a Gaussian distribution:

$$\Psi[\delta x] = \frac{1}{\sqrt{4\pi D\tau}} \exp\left\{-\frac{\delta x^2}{4D\tau}\right\}.\tag{1}$$

With the knowledge of all particle positions at all times (*rp*(*t*)), the confocal image was generated on a pixel-by-pixel basis, were the intensity at pixel (*i*, *j*) is recorded starting at time *tij* = (*nx j* + *<sup>i</sup>*)*<sup>δ</sup>*. Assuming a Gaussian beam profile with 1/*e*<sup>2</sup> radius *w*0, a particle found at a distance Δ*r* from the center of the pixel contributes on average the following number of photons during the time interval *τ*:

$$\lg[\Delta \vec{r}] = B\tau \exp\left\{-\frac{2(\Delta \vec{r})^2}{w\_0^2}\right\},\tag{2}$$

where *B* is the molecular brightness of the particle. For the simulations we used *w*0 = 0.3 μm and *B* = 5 to 20 kHz. The average pixel photon intensity recorded at pixel (*i*, *j*) between *t* and *t* + *τ* can then be calculated by summing over all particles and over the whole dwell time:

$$I(i,j) = \sum\_{n=0}^{m-1} \sum\_{p=1}^{N} \lg\left[\vec{r}\_{ij} - \vec{r}\_p(t\_{ij} + n\tau)\right] + i\_B \delta\_\prime \tag{3}$$

where the constant background intensity of *iB* = 1.3 kHz was added at each pixel, to match the typical background intensity measured in the experimental confocal images. Finally, photon noise was added to the image, by replacing the average photon count calculated using Equation (3) by a photon count drawn from a Poisson distribution with the average pixel intensity as the expectation value.

The Python code used to simulate confocal images is available in the publicly accessible repository: https://github.com/cecilefradin/BidBax\_Simulation\_and\_Analysis (accessed on 30 July 2021).
