仿真生成闪耀光栅,相位范围为(0,2*pi),可以自由更改光栅尺寸、条纹倾角、像素间隔和周期长度。

matlab代码:

function result=blazed_grating(res,theta,pitch,cycle)
%res:矩阵大小
%theta:条纹与x轴正向夹角(弧度制)
%pitch:像素间隔
%cycle:周期长度
theta=mod(theta,2*pi);
new_theta=mod(theta,pi);
%以矩阵的(1,1)点为原点建立坐标系
A=0;B=-1;
if new_theta==pi/2
    A=1;B=0;
else
    A=tan(new_theta);
end
x=0:res(2)-1;
y=0:res(1)-1;
[x,y]=meshgrid(x,y);
dis=(A*x+B*y)/sqrt(A^2+B^2)*pitch;%点到直线的距离
if theta>=pi
    dis=-dis;
end
phase=mod((dis-min(min(dis)))/cycle,1)*2*pi;
result=phase;
end

python代码:

def blazed_grating(res, theta, pitch, cycle):
    # res:光栅矩阵的尺寸(二维输入,更高维度没有测试)
    # theta:条纹与x轴正向夹角(弧度制)
    # pitch:周期长度
    # cycle:像素间隔
    # return:返回一个二维的numpy矩阵,值为闪耀光栅的相位,变化范围为0~2pi
    theta %= 2 * math.pi
    new_theta = theta % (2 * math.pi)
    # 以光栅矩阵的(1,1)点为原点建立坐标系
    # 条纹与过原点的直线(Ax+By=0)平行
    A = 0
    B = -1
    if new_theta == math.pi / 2:  # 竖直方向
        A = 1
        B = 0
    else:
        A = numpy.tan(new_theta)
    x = numpy.linspace(0, res[1] - 1, res[1])
    y = numpy.linspace(0, res[0] - 1, res[0])
    x, y = numpy.meshgrid(x, y)
    dis = (A * x + B * y) / numpy.sqrt(A * A + B * B) * pitch  # 光栅矩阵矩阵的每个点到(1,1)所在直线的实际距离
    if theta >= math.pi:  # 光栅逆向排列
        dis = -dis
    phase = ((dis - numpy.min(dis)) / cycle) % 1 * 2 * math.pi
    return phase
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐