matlab中光波叠加的可视化

matlab中光波叠加的可视化,matlab,visualization,light,wavefront,Matlab,Visualization,Light,Wavefront,我写了一些快速代码来可视化空间中两个不同振幅的波的叠加,点源几何。这在khanacademy CS平台上起作用。但我无法在matlab中再现精确的现象。我得到的只是一个嘈杂的图像。这与随机数生成中的差异有关吗?我不知道random(0,1)(在JS中)和rand(在matlab中)有多不同 下面是matlab代码 象平面上x,y点的波叠加函数 function S = Super(refamp,objamp,x,y,a,lambda) r1 = sqrt(a*a+x*x+y*y); %a

我写了一些快速代码来可视化空间中两个不同振幅的波的叠加,点源几何。这在khanacademy CS平台上起作用。但我无法在matlab中再现精确的现象。我得到的只是一个嘈杂的图像。这与随机数生成中的差异有关吗?我不知道random(0,1)(在JS中)和rand(在matlab中)有多不同

下面是matlab代码

象平面上x,y点的波叠加函数

function S = Super(refamp,objamp,x,y,a,lambda)
    r1 = sqrt(a*a+x*x+y*y); %a is in z-axis
    S = refamp+(objamp*cos(2*pi*r1/(lambda/(10^6))));
测试脚本

close all;
clear all;
clc;

a=10;       %distance from source to image plane

width = 1024;
height =1024;

im = zeros(width); % the image
x=1;
y=1;
A0 = 3; % amplitude of reference wave
A1 = 1; % amplitude of object wave  A0>>A1: A0/A1>=3
lambda = 632; % wavelength in nanometers
% generate the superposition in space width*height at a along z-axis
for y=1:height
    for x=1:width
        s = Super(A0,A1,x-(width/2),y-(height/2),a, lambda);
        r=rand;
        if(r<(s/(A0+A1)))
            im(x,y) = 1;
    end
end

%display the image
figure
imshow(im,[])
title('test image')
全部关闭;
清除所有;
clc;
a=10;%源到像面距离
宽度=1024;
高度=1024;
im=零(宽度);%形象
x=1;
y=1;
A0=3;%参考波振幅
A1=1;%目标波振幅A0>>A1:A0/A1>=3
λ=632;%波长单位:纳米
%沿z轴在a处生成空间宽度*高度的叠加
y=1时:高度
对于x=1:宽度
s=超级(A0,A1,x-(宽度/2),y-(高度/2),a,λ);
r=兰特;

如果(r主要的问题是你的天平关闭了,所以你看不到干涉模式。如果你仔细考虑每件东西有多大/多远,它就会正确,你就能看到干涉模式

第二个问题是,你的代码将真正受益于矢量化。我在下面展示了这一点——这样做可以大大加快执行速度

function Interference
a=1000 * 10^-9;       #% distance from source to image plane
width = 10000 * 10^-9;
height= 10000 * 10^-9;
size = 700;
A0 = 3; %# amplitude of reference wave
A1 = 1; %# amplitude of object wave  A0>>A1: A0/A1>=3
lambda = 632 * 10^-9; #% wavelength in nanometers

x=linspace(0,width,size); #% vector from 0 to width
y=linspace(0,height,size); #% vector from 0 to height
[X,Y]=meshgrid(x,y); #% matrices of x and y values at each position

s=Super(A0, A1, X-(width/2), Y-(height/2), a, lambda); #% size-by-size (700x700) 
r=rand(size); #% 700x700 matrix of random values on [0 1]

im = zeros(size);
im(r<(s/(A0+A1))) = 1; %# do this all at once instead of pixel-by-pixel

#% display the image
figure
imshow(im,[])
title('test image')
end #% end of function Interference


#% Super is now vectorized, so you can give it a matrix of values for x and y
function S = Super(refamp,objamp,x,y,a,lambda)
    r1 = sqrt(a.*a+x.*x+y.*y); #% dot notation: multiply element-wise
    S = refamp+(objamp*cos(2*pi*r1/(lambda)));
end #% end of function Super
功能干扰
a=1000*10^-9;#%从源到图像平面的距离
宽度=10000*10^-9;
高度=10000*10^-9;
尺寸=700;
A0=3;%#参考波振幅
A1=1;%#目标波振幅A0>>A1:A0/A1>=3
λ=632*10^-9;#%波长(纳米)
x=linspace(0,宽度,大小);#%0到宽度的向量
y=linspace(0,高度,大小);#%从0到高度的向量
[X,Y]=每个位置X和Y值的网格(X,Y)#%矩阵
s=Super(A0,A1,X-(宽度/2),Y-(高度/2),a,λ);#尺寸百分比(700x700)
r=rand(size);#%700x700[0 1]上随机值矩阵
im=零(大小);

im(r+1:我正在写这一切,而你走在我前面:)最近我经常遇到这种情况。非常感谢。我仍在思考这一点…特别是高度和宽度:Dbtw,考虑到我试图模拟的物理系统,缩放仍处于关闭状态。像素的宽度等值应以几微米为单位,因此我发布了正确的工作代码
function i = Interference(width, height, sizeh,sizev,z)
% parameters explained
% width: is the horizontal pixel pitch in microns
% height: is the vertical pixel pitch in microns
% size is the width=height of the CCD in number of pixels
% z is distance from source to image plane
A0 = 3; %# amplitude of reference wave
A1 = 1; %# amplitude of object wave  A0>>A1: A0/A1>=3
lambda = 635 * 10^-9; % wavelength in nanometers

%the linspace was wrong
x=linspace(0,width*sizeh,sizeh); % vector from 0 to width of size 'size'
y=linspace(0,height*sizev,sizev); % vector from 0 to height of size 'size'
[X,Y]=meshgrid(x,y); % matrices of x and y values at each position

s=Super(A0, A1, X-((width*sizeh)/2), Y-((height*sizev)/2), z, lambda); % size-by-size (1024x1024) 
r=rand(size); % 1024x1024 matrix of random values on [0 1]
%i=s;
im = zeros(size);
im(r<(s/(A0+A1))) = 1; %# do this all at once instead of pixel-by-pixel
i=im;
end % end of function Interference


% Super is now vectorized, so you can give it a matrix of values for x and y
function S = Super(refamp,objamp,x,y,a,lambda)
    r1 = sqrt(a.*a+x.*x+y.*y); % dot notation: multiply element-wise
    S = refamp+(objamp*cos(2*pi*r1/(lambda)));
end % end of function Super
width = 2.8 * 10^-6;
height= 2.8 * 10^-6; %pixel size
%  sizeh = 16; %image size in pixels
%  sizev = 16;
 sizeh = 1600; %image size in pixels
 sizev = 1200;
  int_z = 100*10^-3; % z dist in m
%  xes1 = 100;
 %xes2 = ;
 int_im = Interference(width,height,sizeh, sizev,int_z);
 int_im = int_im/max(max(int_im)); % normalize
 int_im = (int_im-0.5)*2; % enhance visualy

% display the image
figure
imshow(int_im,[])