필터 지우기
필터 지우기

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 CenterFile Exchange에서 Colormaps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by