Clear Filters
Clear Filters

plot square-wave sine grating

36 views (last 30 days)
Kate Feller
Kate Feller on 9 Nov 2020
Commented: Star Strider on 10 Nov 2020
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

Answers (2)

Star Strider
Star Strider on 9 Nov 2020
Edited: Star Strider on 10 Nov 2020
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 Comments
Kate Feller
Kate Feller on 10 Nov 2020
doh! so simple! I suppose i just don't know what I don't know.
Thank you so much!
Star Strider
Star Strider on 10 Nov 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Sign in to comment.


Setsuna Yuuki.
Setsuna Yuuki. on 9 Nov 2020
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

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!