Image display error when using surf

3 views (last 30 days)
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 Comment
Matt J
Matt J on 9 Jan 2024
See the code output above. rect() is not provided.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 9 Jan 2024
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 Comments
Minh Hoang
Minh Hoang on 9 Jan 2024
Thank you so much! Sorry I forgot providing rect function!
Star Strider
Star Strider on 9 Jan 2024
As always, my pleasure!
No worries, however having rect would have helped.

Sign in to comment.

More Answers (0)

Categories

Find more on Fourier Analysis and Filtering 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!