![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408670/image.png)
plot square-wave sine grating
60 views (last 30 days)
Show older comments
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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408540/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408545/image.png)
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
0 Comments
Answers (2)
Star Strider
on 9 Nov 2020
Edited: Star Strider
on 10 Nov 2020
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408670/image.png)
2 Comments
Star Strider
on 10 Nov 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.
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
1
2
3![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408620/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408610/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408615/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/408620/image.jpeg)
0 Comments
See Also
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!