plot square-wave sine grating

Hello,
I would like to plot several different black and white stripe patterns with different sine-wave frequencies.
So far I have figured out how to make a sine grating, however, I can't seem to determine how to go about changing this frequency grating to a square wave (code below).
Here is waht a get vs. what I want (pic from the internet)
vs
Any advice for how to do this will be greatly appreciated!
frequency = 10;
phase = 90;
amplitude = 1;
[X,Y]=meshgrid(0:0.001:1,0:0.001:1);
% X = square(X);
Z = amplitude*sin((2*3.1415*frequency.*X)+(phase));
surf(X,Y,Z)
shading interp
view(0,90)
colormap gray
axis off
axis square

回答(2 个)

To change a sine wave to a square wave, use the sign function:
t = linspace(0, 5, 250);
f = 2;
sinwav = sin(2*pi*t*f);
sqrwav = sign(sin(2*pi*t*f));
figure
plot(t, sinwav)
hold on
plot(t, sqrwav)
hold off
grid
ylim(ylim*1.1)
EDIT — (10 Nov 2020 at 00:14)
Adapting this to your code:
frequency = 10;
phase = 90;
amplitude = 1;
[X,Y]=meshgrid(0:0.001:1,0:0.001:1);
% X = square(X);
Z = amplitude*sign(sin((2*3.1415*frequency.*X)+(phase)));
surf(X,Y,Z)
shading interp
view(0,90)
colormap gray
axis off
axis square
producing this plot:
.

2 个评论

doh! so simple! I suppose i just don't know what I don't know.
Thank you so much!
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

请先登录,再进行评论。

t = 85:1e-2:100;
[X,Y]=meshgrid(t);
% you can use fourier series to see the change in the function
% components = 1 --> sinusoidal wave (Figure 1)
% components = 5 -- Figure 2
% components ~ 100 --> square wave (Figure 3)
components = 100; sumatoria = 0 ;
for k = 1:1:components
n = 2*k-1;
serie=2/pi*1/n*sin(n*pi*t);
sumatoria = serie+sumatoria;
end
signal = 1/2+sumatoria;
Z = signal.*X;
figure
surf(X,Y,Z)
shading interp
view(0,90)
colormap gray
axis off
axis square
123

类别

帮助中心File Exchange 中查找有关 Data Distribution Plots 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by