Image display error when using surf

조회 수: 13 (최근 30일)
Minh Hoang
Minh Hoang 2024년 1월 9일
댓글: Star Strider 2024년 1월 9일
Greetings everyone, I am trying to use 'surf' to display a 2D fourier transform of a 2D rectangular-wave signal. The method was learned from "Computational Fourier Optics: a MATLAB Tutorial" by David G.Voelz (this book is available online: https://www.spiedigitallibrary.org/ebooks/TT/Computational-Fourier-Optics-A-MATLAB-Tutorial/eISBN-9780819482051/10.1117/3.858456?SSO=1#_=_). The problem is, I just copied every code lines from the book and there was no error warning from the editor part, but the error popped out when I ran it, saying that there was an error in using 'surf'. I guess the error comes from the difference between the Matlab version of my computer (R2022b) and the one used in the book (Version 7.1) but I don't know how to fix it. Could anyone help me with that? Million thanks!
Here is my codes:
wx=0.1; %rect x half-width (m)
wy=0.05; %rect y half-width (m)
L=2; %side length x and y (m)
M=200; %samples
dx=L/M; %sample interval (m)
x=-L/2:dx:L/2-dx; %x coordinates
y=x; %y coordinates
[X,Y]=meshgrid(x,y); %X and Y grid coords
g=rect(X/(2*wx)).*rect(Y/(2*wy)); %2D signal
Unrecognized function or variable 'rect'.
%2D FFT of rectangular function
g0=fftshift(g); %shift
G0=fft2(g0)*dx^2; %2D fft and dxdy scaling
G=fftshift(G0); %center
fx=-1/(2*dx):1/L:1/(2*dx)/(1/L); %x freq coords
fy=fx;
figure(3)
surf(fx,fy,real(G)); %display tranform magnitude
camplight left;
lighting phong;
colormap('gray');
shading interp;
ylabel('fy (cyc/m)');
xlabel ('fx (cyc/m)');
  댓글 수: 1
Matt J
Matt J 2024년 1월 9일
See the code output above. rect() is not provided.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2024년 1월 9일
MATLAB 7.1 was introduced in 2006. I cannot find any documentation for the Signal Processing Toolbox for it (that likely explains the rect function). MathWorks would have to supply that information. The oldest version documentation available online is for R2018a.
The best I can do is to fudge it —
wx=0.1; %rect x half-width (m)
wy=0.05; %rect y half-width (m)
L=2; %side length x and y (m)
M=200; %samples
dx=L/M; %sample interval (m)
x=-L/2:dx:L/2-dx; %x coordinates
y=x; %y coordinates
[X,Y]=meshgrid(x,y); %X and Y grid coords
% g=rect(X/(2*wx)).*rect(Y/(2*wy)); %2D signal
g=(X/(2*wx)).*(Y/(2*wy)); %2D signal
% g=rect(X/(2*wx)).*rect(Y/(2*wy))
%2D FFT of rectangular function
g0=fftshift(g); %shift
G0=fft2(g0)*dx^2; %2D fft and dxdy scaling
G=fftshift(G0); %center
% fx=-1/(2*dx):1/L:1/(2*dx)/(1/L); %x freq coords
% fy=fx;
[a,b] = bounds(fx);
fx = linspace(a, b, size(g,2));
fy = fx;
figure(3)
surf(fx,fy,real(G)); %display tranform magnitude
% camplight left;
camlight left
lighting phong;
colormap('gray');
shading interp;
ylabel('fy (cyc/m)');
xlabel ('fx (cyc/m)');
.
  댓글 수: 2
Minh Hoang
Minh Hoang 2024년 1월 9일
Thank you so much! Sorry I forgot providing rect function!
Star Strider
Star Strider 2024년 1월 9일
As always, my pleasure!
No worries, however having rect would have helped.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by