Image display error when using surf

2 次查看(过去 30 天)
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)');

采纳的回答

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 中查找有关 Fourier Analysis and Filtering 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by