Hi @Robert Stegeman,
To address your query effectively, you need to incorporate the necessary mathematical principles that govern wave propagation in media with varying refractive indices and implement them into a suitable code block. Given your requirements, you need to create a code snippet that applies phase shifts due to refraction without altering the spatial frequency grid. When light propagates through a medium with a refractive index (n(x,y)) at an angle \( theta), the wave vector mathbf{k} can be described as:
[mathbf{k} = frac{2\pi}{\lambda} \begin{pmatrix} n_x \\ n_y \\ n_z \end{pmatrix}]
Where (n_x) and (n_y) are influenced by the angle of incidence. The phase shift introduced by the refractive index can be expressed as:
[Delta \phi(x,y) = -frac{2\pi}{\lambda} (n(x,y) - 1) d]
Here, (d) is the distance traveled within the medium. The electric field can be modified as follows:
1. Compute the phase shift based on the varying refractive index. 2. Apply this phase shift to the electric field in each propagation step.
Below is an example MATLAB code snippet that demonstrates how to propagate an electric field through a medium with a spatially varying refractive index while maintaining a constant spatial frequency grid.
% Parameters lambda = 500e-9; % Wavelength in meters (e.g., 500 nm) dx = 10e-5; % Grid point size in meters dz = 1e-3; % Propagation distance in meters grid_size = 256; % Size of the grid n0 = 1.0; % Refractive index outside the medium theta = pi/4; % Angle of incidence in radians
% Spatial frequency grid fx = (-grid_size/2:grid_size/2-1)/(grid_size*dx); fy = (-grid_size/2:grid_size/2-1)/(grid_size*dx); [FX, FY] = meshgrid(fx, fy);
% Example synthetic data for electric field E E = rand(grid_size) + 1i*rand(grid_size); % Random complex field
% Define spatially varying refractive index (small perturbations) n_variation = 0.01 * rand(grid_size); % Example variation n = n0 + n_variation;
% Compute wavenumber k = 2*pi/lambda;
% Create transfer function for free space propagation H = exp(-1i * dz * ((n0 * FX * cos(theta)) + (n0 * FY * sin(theta))).^2 / (2*k));
% Apply FFT-based propagation with phase correction E_propagated = fftshift(ifft2(fft2(ifftshift(E)) .* fftshift(H)));
% Apply phase shift due to varying refractive index for ix = 1:grid_size for iy = 1:grid_size phase_shift = exp(-1i * (n(ix, iy) - n0) * k * dz); E_propagated(ix, iy) = E_propagated(ix, iy) * phase_shift; end end
% Final results visualization figure; imagesc(abs(E_propagated)); colorbar; title('Magnitude of Propagated Electric Field'); xlabel('X Position'); ylabel('Y Position');
Please see attached.
Here are some further additional insights to help you further.
Phase Shift Calculation: The code calculates a phase shift based on both the incident angle and the local refractive index at each grid point. This approach preserves the spatial frequency characteristics without requiring extensive changes to your existing framework.
Optimization: For larger grids or more complex variations of \( n(x,y) \), consider vectorizing operations to enhance performance. MATLAB’s built-in functions are generally optimized for matrix operations.
Validation: It is crucial to validate this method against known solutions or experimental data to ensure accuracy, particularly when dealing with small perturbations in refractive indices.
Further Generalization: If you later decide to extend this approach into two principal planes or non-linear variations of n(x,y) similar principles can be applied while adjusting the phase shift calculations accordingly.
This implementation should serve as a robust starting point for your optical simulations while adhering to your constraints regarding spatial frequency grids and minimal modifications to existing code structures.
Hope this helps.