---
# System prepended metadata

title: Code snippets to Dynamic Light Scattering | ViaDean

---

# Code snippets to Dynamic Light Scattering
Dynamic Light Scattering (DLS) is an analytical technique used to measure the size distribution of small particles or molecules in suspension or solution. It works by analyzing fluctuations in the intensity of scattered light caused by the Brownian motion of particles. These fluctuations provide data on particle size by correlating them to diffusion coefficients using the Stokes-Einstein equation. DLS is widely used in fields like nanotechnology, pharmaceuticals, and biology due to its precision in determining particle size, polydispersity, and stability in colloidal systems. It is valued for its non-invasive approach, requiring minimal sample preparation while delivering rapid and accurate measurements.

- [Light scattering of colloids](https://viadean.notion.site/Light-scattering-of-colloids-13b1ae7b9a328016b416d9d271ea55c3)

```mermaid
---
config:
 sankey:
  showValues: false
---
sankey-beta
Code snippets,MATLAB,4
MATLAB,Rayleigh Scattering,1
MATLAB,Mie Scattering,1
MATLAB,Dynamic Light Scattering,1
MATLAB,Static Light Scattering,1

Code snippets,Python,4
Python,Rayleigh Scattering,1
Python,Mie Scattering,1
Python,Dynamic Light Scattering,1
Python,Static Light Scattering,1

Code snippets,R,4
R,Rayleigh Scattering,1
R,Mie Scattering,1
R,Dynamic Light Scattering,1
R,Static Light Scattering,1

Code snippets,Julia,1
Julia,Rayleigh Scattering,1
Julia,Mie Scattering,1
Julia,Dynamic Light Scattering,1

Code snippets,C++,2
C++,Rayleigh Scattering,1
C++,Mie Scattering,1

Code snippets,C#,1
C#,Mie Scattering,1
```
## MATLAB snippet

Dynamic Light Scattering (DLS) is a technique used to measure the size distribution of small particles in suspension by analyzing the fluctuations in light intensity caused by Brownian motion. MATLAB is well-suited for this type of analysis, as it offers extensive mathematical and visualization capabilities.

### Basic Overview of DLS:

1. **Concept**: DLS measures the diffusion of particles in a fluid by observing the time-dependent intensity of scattered light.

2. **Data Analysis**: The time correlation function of the intensity signal is used to extract the diffusion coefficient, which can then be related to particle size using the Stokes-Einstein equation.

3. **Mathematical Background**:

   - **Correlation Function** \( $g_2(\tau)$ \): Used to describe how the intensity changes over time.

   - **Diffusion Coefficient** \( $D$ \): Extracted from the correlation function.

   - **Hydrodynamic Radius** \( $R_H$ \): Calculated using the Stokes-Einstein equation:
     $$
     R_H = \frac{k_B T}{6 \pi \eta D}
     $$

     where \( $k_B$ \) is Boltzmann’s constant, \( $T$ \) is temperature, \( $\eta$ \) is the viscosity of the fluid, and \( $D$ \) is the diffusion coefficient.

### Implementing DLS Analysis in MATLAB:

Here's a simplified approach to analyzing DLS data using MATLAB.

#### Step 1: Import Intensity Data

Ensure you have time-series data representing the intensity of the scattered light. This data is typically obtained from a DLS instrument.

```matlab
% Load the intensity data (assumed to be in a .mat or .csv file)
data = load('intensity_data.mat'); % Replace with your file path or import function
intensity = data.intensity; % Adjust based on your file's structure
time = data.time; % Time vector corresponding to intensity data
```

#### Step 2: Calculate the Correlation Function

The correlation function \( $g_2(\tau)$ \) can be estimated using MATLAB's built-in functions.

```matlab
% Compute the correlation function using autocorrelation
normalized_correlation = xcorr(intensity, 'unbiased');
normalized_correlation = normalized_correlation(normalized_correlation >= 0); % Take only non-negative lags

% Normalize the correlation function
g2_tau = normalized_correlation / max(normalized_correlation);

% Plot the correlation function
figure;
plot(time(1:length(g2_tau)), g2_tau);
xlabel('Time Delay (s)');
ylabel('Correlation Function g_2(\tau)');
title('Intensity Correlation Function');
```

#### Step 3: Extract the Diffusion Coefficient

Fit an exponential decay model to the correlation function to find the diffusion coefficient \( $D$ \).

```matlab
% Fit an exponential decay to estimate D (simplified approach)
fit_func = @(b, x) exp(-2 * b * x); % Simple model for fitting
initial_guess = 1e-4; % Adjust based on expected D
params = lsqcurvefit(fit_func, initial_guess, time(1:length(g2_tau)), g2_tau);

D = params; % Extracted diffusion coefficient
disp(['Estimated Diffusion Coefficient: ', num2str(D)]);
```

#### Step 4: Calculate the Hydrodynamic Radius

Use the Stokes-Einstein equation to calculate the hydrodynamic radius \( $R_H$ \).

```matlab
% Constants
kB = 1.380649e-23; % Boltzmann constant (J/K)
T = 298; % Temperature in Kelvin (e.g., room temperature ~25°C)
eta = 0.001; % Viscosity of water in Pa·s (at ~25°C)

% Calculate the hydrodynamic radius
R_H = (kB * T) / (6 * pi * eta * D);
disp(['Hydrodynamic Radius (nm): ', num2str(R_H * 1e9)]); % Convert to nanometers
```

### Visualization

To better understand the data, visualize the correlation function and the exponential decay fit:

```matlab
% Plot the correlation function with the fitted curve
figure;
plot(time(1:length(g2_tau)), g2_tau, 'b-', 'DisplayName', 'Correlation Function');
hold on;
plot(time(1:length(g2_tau)), fit_func(D, time(1:length(g2_tau))), 'r--', 'DisplayName', 'Exponential Fit');
xlabel('Time Delay (s)');
ylabel('Correlation Function g_2(\tau)');
legend('show');
title('DLS Data Analysis');
```

### Enhancements:

- **Noise Filtering**: Preprocess the data to remove noise before calculating the correlation function.
- **Advanced Fitting**: Use non-linear fitting functions or advanced fitting algorithms to improve the accuracy of \( $D$ \).
- **Temperature and Viscosity**: Input exact experimental values for \( $T$ \) and \( $\eta$ \) for precise results.

### Final Notes:

This MATLAB code provides a starting point for analyzing DLS data. For more advanced or commercial applications, consider using DLS analysis software or more comprehensive MATLAB toolboxes that handle complex fitting and correlation calculations.

## Python snippet

Dynamic Light Scattering (DLS) is a powerful technique used to measure the size distribution of small particles in suspension or polymers in solution. The basic principle relies on the analysis of the time-dependent fluctuations of light scattered by particles undergoing Brownian motion.

Here's an overview and a basic implementation of how to simulate and process DLS data using Python.

### How Dynamic Light Scattering Works:

- **Brownian Motion**: Particles in a suspension move randomly due to thermal energy.

- **Scattered Light**: A laser beam directed at the suspension will scatter light, which fluctuates in intensity due to the movement of particles.

- **Correlation Function**: The time-dependent fluctuations are analyzed using an autocorrelation function to determine the diffusion coefficient \( $D$ \).

- **Particle Size**: The diffusion coefficient is related to the particle size by the Stokes-Einstein equation:
  $$
  D = \frac{k_B T}{6 \pi \eta R}
  where:
  $$

  - \( $k_B$ \) is the Boltzmann constant,
  - \( $T$ \) is the temperature,
  - \($\eta$ \) is the viscosity of the medium,
  - \( $R$ \) is the hydrodynamic radius of the particle.

### Python Implementation:

You can use libraries like `numpy` and `scipy` to generate synthetic DLS data and analyze it.

#### Step-by-Step Code:

1. Generate synthetic intensity data based on Brownian motion.
2. Calculate the autocorrelation function.
3. Estimate the diffusion coefficient and particle size.

Here's a simple example:

```python
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt

# Generate synthetic DLS data
def generate_synthetic_data(num_points, diffusion_coefficient, noise_level=0.01):
    time = np.linspace(0, 1, num_points)
    g2 = np.exp(-2 * diffusion_coefficient * time) + noise_level * np.random.normal(size=num_points)
    return time, g2

# Fit function for the autocorrelation
def fit_func(t, D):
    return np.exp(-2 * D * t)

# Generate data
num_points = 1000
diffusion_coefficient_true = 0.5
time, g2 = generate_synthetic_data(num_points, diffusion_coefficient_true)

# Fit the data
popt, _ = curve_fit(fit_func, time, g2)
diffusion_coefficient_estimated = popt[0]

# Plot the results
plt.plot(time, g2, label='Synthetic Data')
plt.plot(time, fit_func(time, diffusion_coefficient_estimated), label=f'Fit (D={diffusion_coefficient_estimated:.3f})')
plt.xlabel('Time')
plt.ylabel('g2(t)')
plt.title('DLS Autocorrelation Function')
plt.legend()
plt.show()

# Calculate particle size using the Stokes-Einstein equation
T = 298.15  # Temperature in Kelvin (25°C)
k_B = 1.380649e-23  # Boltzmann constant in J/K
eta = 0.001  # Viscosity of water in Pa.s
R = k_B * T / (6 * np.pi * eta * diffusion_coefficient_estimated)

print(f"Estimated particle radius: {R:.2e} m")
```

### Explanation:

- **`generate_synthetic_data`**: Generates synthetic time-dependent intensity data using an exponential decay model.
- **`fit_func`**: Model function for fitting the autocorrelation data.
- **`curve_fit`**: Used to fit the exponential model to the synthetic data and estimate the diffusion coefficient.
- **Particle Size Calculation**: The Stokes-Einstein equation is used to compute the particle radius.

### Output:

- **Plot**: A plot of the synthetic autocorrelation function \( $g_2(t)$ \) and the fitted curve.
- **Estimated Radius**: The calculated particle radius in meters based on the estimated diffusion coefficient.

### Enhancements:

- **Noise Reduction**: Apply filtering techniques to smooth real experimental data.
- **Advanced Models**: Implement multi-exponential or cumulant analysis for more complex data.
- **Real Data Analysis**: Use experimental DLS data files and parse them for analysis.

### Python Libraries to Consider:

- **`numpy`**: For numerical computations.
- **`scipy`**: For fitting models and handling advanced mathematical operations.
- **`matplotlib`**: For plotting data.
- **`pandas`**: For handling data input/output if you are reading experimental data.

This code provides a foundational approach to understanding and implementing DLS analysis in Python.