A Visulization of 2D-DFT with 3D displaying
Version 1.0.0 (2.42 KB) by
Chun
A simple visulization function with a beautiful 3D model of Fourier field
function Fourier3D()
try
img = im2double(imread('afile'));
catch
% Fallback to another built-in image if 'peppers' is not found.
img = im2double(imread('afile'));
end
[H, W, C] = size(img);
spectrum_log = zeros(H, W, C);
img_recon = zeros(H, W, C);
for k = 1:C
% Forward FFT
F = fft2(img(:, :, k));
F_shifted = fftshift(F);
% Log-Magnitude Spectrum: S = log(1 + |F|). Normalized for visualization.
S = log(1 + abs(F_shifted));
spectrum_log(:, :, k) = (S - min(S(:))) / (max(S(:)) - min(S(:)));
% Inverse FFT (Reconstruction)
f_inv = ifft2(ifftshift(F_shifted));
f_real = real(f_inv);
img_recon(:, :, k) = max(0, min(1, f_real));
end
%% 3. Visualization I: The 2D Mathematical Triptych (Color Recovery)
% Set global dark theme for aesthetics.
fig2d = figure('Name', '2D Fourier Analysis & Color Recovery', 'Color', [0.1 0.1 0.1], ...
'Position', [50, 300, 1200, 400]);
% A. Original Image (Spatial Domain)
ax1 = subplot(1, 3, 1);
imshow(img);
title(ax1, 'Spatial Domain f(x,y)', 'Color', 'w', 'FontWeight', 'bold');
% B. Frequency Domain (RGB Composite Spectrum) - The "Nebula" Art
ax2 = subplot(1, 3, 2);
imshow(spectrum_log);
title(ax2, 'Frequency Domain log|F(u,v)|', 'Color', 'w', 'FontWeight', 'bold');
% C. Reconstructed Image (Perfect Color Recovery)
ax3 = subplot(1, 3, 3);
imshow(img_recon);
title(ax3, 'Reconstructed f^{-1}(F)', 'Color', 'w', 'FontWeight', 'bold');
%% 4. Visualization II: 3D Spectral Topology (Mathematical Beauty)
% Convert image to grayscale (Luminance) to visualize global energy landscape.
img_gray = rgb2gray(img);
F_gray = fftshift(fft2(img_gray));
Z = log(1 + abs(F_gray)); % Z-axis represents energy magnitude (topology)
% Create 3D Surface Figure
fig3d = figure('Name', '3D Spectral Topology', 'Color', [0 0 0], ...
'Position', [50, 50, 800, 600]);
% Plot surface with aesthetic refinement
hSurf = surf(Z);
% AESTHETIC OPTIMIZATIONS (Metallic, Smooth, High-Contrast)
shading interp; % Smooth out the grid lines
colormap(jet(256)); % High contrast color map
alpha(0.9); % Slight transparency
material metal; % Metallic reflection properties
% Lighting for depth perception
camlight('headlight');
lighting gouraud;
% Axis styling
axis off; % Remove axes for pure art look
view(-45, 45); % Set an effective isometric-like view angle
title('3D Magnitude Spectrum Topology', 'Color', [0.8 0.8 0.8], ...
'FontSize', 14, 'FontWeight', 'light');
% Interactive rotation hint
text(0, 0, max(Z(:)), 'DC Component (Low Freq)', 'Color', 'w');
rotate3d on;
end
Cite As
Chun (2026). A Visulization of 2D-DFT with 3D displaying (https://www.mathworks.com/matlabcentral/fileexchange/182795-a-visulization-of-2d-dft-with-3d-displaying), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2023a
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.
| Version | Published | Release Notes | |
|---|---|---|---|
| 1.0.0 |
