矩形光栅的matlab代码
时间: 2024-02-12 07:27:25 浏览: 84
光栅的矩阵传输MATLAB程序
5星 · 资源好评率100%
Sure! Here's a simple MATLAB code for generating a rectangular grating:
```matlab
% Parameters
width = 500; % Width of the grating
height = 200; % Height of the grating
period = 20; % Period of the grating (distance between each line)
amplitude = 1; % Amplitude of the grating
% Create a blank image
grating = zeros(height, width);
% Generate the grating pattern
for i = 1:height
for j = 1:width
if mod(j, period) < period/2
grating(i, j) = amplitude;
else
grating(i, j) = -amplitude;
end
end
end
% Display the grating
imshow(grating);
```
This code creates a blank image and then generates a rectangular grating pattern by iterating over each pixel and setting its value based on the position within the period. The resulting grating is then displayed using the `imshow` function.
Feel free to adjust the parameters (width, height, period, amplitude) to customize the grating pattern as per your requirements.
阅读全文