我编写了一些快速代码,用于可视化空间中具有不同幅度的两个波的叠加,点源几何。这适用于 khanacademy CS 平台。 http://www.khanacademy.org/cs/superposition/1245709541但我无法在 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')
最佳答案
主要问题是您的天平已关闭,因此您看不到干涉图案。如果你尝试一下所有东西的大小/距离,它就会正确,你可以看到模式。
第二个问题是您的代码确实会从矢量化中受益。我在下面展示了这一点 - 这样做可以显着加快执行速度。
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
关于matlab - matlab中光波叠加的可视化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14030658/