フィルターのクリア

plot square-wave sine grating

36 ビュー (過去 30 日間)
Kate Feller
Kate Feller 2020 年 11 月 9 日
コメント済み: Star Strider 2020 年 11 月 10 日
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 件)

Star Strider
Star Strider 2020 年 11 月 9 日
編集済み: Star Strider 2020 年 11 月 10 日
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 件のコメント
Kate Feller
Kate Feller 2020 年 11 月 10 日
doh! so simple! I suppose i just don't know what I don't know.
Thank you so much!
Star Strider
Star Strider 2020 年 11 月 10 日
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

サインインしてコメントする。


Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 9 日
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

カテゴリ

Help Center および File ExchangeColormaps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by