Image display error when using surf
Show older comments
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
%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
on 9 Jan 2024
See the code output above. rect() is not provided.
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB 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!