import React from 'react'; import { Activity, Camera, BarChart3, Info, Lightbulb, ChevronUp, ChevronDown, ZoomIn } from 'lucide-react'; import { ResponsiveContainer, Tooltip as RechartsTooltip, BarChart, Bar, XAxis, YAxis, CartesianGrid } from 'recharts'; import VerdictBadge from '../ui/VerdictBadge'; const API_BASE = import.meta.env.VITE_API_URL || 'http://127.0.0.1:8000'; const FrequencyTab = ({ result, getScoreColor, setZoomedImage, showFullSpectralInfo, setShowFullSpectralInfo, }) => { return ( <>
{/* Info Callout */}

How Spectral & Frequency Analysis Works

Frequency analysis mathematically decomposes an image into its constituent wave frequencies. Real cameras capture a natural balance of low frequencies (smooth gradients like skin) and high frequencies (sharp edges, pores, and natural sensor noise). AI-generated deepfakes often struggle to reproduce this microscopic high-frequency detail, resulting in an image that is mathematically "too smooth" at the pixel level.

{showFullSpectralInfo && ( <>
  • FFT (Fast Fourier Transform): Converts the image into a 2D spectrum map. The bright center represents low-frequency energy (smooth areas), while the outer rings represent high-frequency detail (sharp edges). A deepfake often lacks energy in the outer rings.
  • DCT (Discrete Cosine Transform): Similar to FFT, but used heavily in JPEG compression. We analyze the grid of DCT coefficients (top-left is low frequency, bottom-right is high frequency) to look for unnatural quantization or missing high-frequency coefficients typical of GAN generation.
  • High-Freq Energy Ratio: A direct mathematical measurement of how much energy exists in the high frequencies compared to the total image energy. Very low numbers (like 0.001%) usually mean the image is extremely compressed or synthetically smoothed by AI.
  • PCA (Principal Component Analysis): Inspired by satellite hyperspectral imaging, we mathematically separate the image's Red, Green, and Blue color channels into their "Principal Components." The 3rd component (PC3) holds the least natural image variance, meaning it is where hidden GAN artifacts and artificial generation residuals are trapped. PC3 acts as a forensic microscope!
  • High-Pass Spatial Filter: Mathematically deletes the low frequencies (smooth skin tones, lighting) via Inverse FFT, completely isolating high-frequency edges and noise. Deepfake blending borders light up brilliantly here.
  • Phase Spectrum: Visualizes the structural phase angle of the FFT. Spliced face-swaps break the continuous phase coherence of the original photograph, causing visible structural anomalies.
  • 8x8 Block DCT High-Frequency Map: Computes the DCT on every 8x8 pixel block independently. Deepfake generation and splicing disrupt the natural JPEG 8x8 grid, causing a glowing mismatch in localized frequency energy.
  • Switching Noise Estimator (SWN): Based on the Ranjbaran et al. filtering algorithm, this technique specifically isolates high-frequency noise by finding zero-crossings in the mathematical gradient of the image. By mathematically suppressing physical edges (like jawlines or glasses), the SWN isolates pure generation noise, lighting up deepfake splicing seams like a neon sign.
  • Cepstrum Echo Analysis: The "spectrum of a spectrum." Resizing or rotating a pasted fake face leaves microscopic periodic interpolation echoes in the image, which show up as bright stars in the Cepstrum domain.
  • Discrete Wavelet Transform (DWT): Decomposes the image into high-frequency local details. We isolate the "Diagonal Details" (HH) channel because AI generators notoriously fail to synthesize consistent diagonal noise compared to real cameras.

Note: Blurry or low-quality laptop webcam photos will naturally have low high-frequency energy due to built-in camera denoising, which is why our XAI engine cross-references multiple sensors!

)}
{/* Quantitative Metrics Summary */}
0.6 ? 'var(--danger)' : result.frequency_analysis.spectral_anomaly_score > 0.4 ? 'var(--warning)' : 'var(--success)') }}>

Spectral & Frequency Metrics

Overall Spectral Anomaly
0.6 ? 'var(--danger)' : 'var(--success)' }}> {(result.frequency_analysis.spectral_anomaly_score * 100).toFixed(1)}%
Higher = Likely Synthetic
High-Freq Energy Ratio
{result.frequency_analysis.high_freq_energy_ratio.toExponential(2)}
Expected: {'>'} 1e-3 for real photos
RGB Cross-Channel Variance
0.01 ? 'var(--warning)' : 'var(--text-primary)' }}> {result.frequency_analysis.channel_variance < 0.0001 ? result.frequency_analysis.channel_variance.toExponential(2) : result.frequency_analysis.channel_variance.toFixed(4)}
High variance = GAN artifact
{/* Beginner-Friendly Visualizations Grid */}
Switching Noise (SWN) Beginner Friendly
Zero-crossing artifact detector mapped onto original image
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.swn_noise_path}`)} style={{ maxWidth: '600px', margin: '0 auto' }} > Switching Noise SWN { e.target.style.display = 'none'; }} />
The easiest way to spot fakes: pure AI generation noise and deepfake splicing seams light up brightly here, directly superimposed on the original video frame.
🧱
8x8 Block DCT High-Frequency Map Beginner Friendly
Localized JPEG grid anomaly detection mapped onto original image
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.block_dct_path}`)} style={{ maxWidth: '600px', margin: '0 auto' }} > Block DCT Artifacts { e.target.style.display = 'none'; }} />
Visualizes the high-frequency energy of every 8x8 block. Artificial face splicing often disrupts the natural grid, creating a glowing mismatch against the background.

Advanced Mathematical Spectra

{/* Visualizations Grid */}
FFT Magnitude Spectrum
Frequency domain representation
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.fft_magnitude_path}`)} > FFT Magnitude Spectrum { e.target.style.display = 'none'; }} />
Bright center = low-freq energy. Outer ring = high-freq detail.
🌊
DCT Spectrum
Discrete Cosine Transform coefficients
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.dct_spectrum_path}`)} > DCT Spectrum { e.target.style.display = 'none'; }} />
Top-left = lowest freq. Bottom-right = highest freq.
πŸ•ΈοΈ
Spectral Residual Saliency (GAN Checkerboard)
Inverse FFT of frequency residuals to expose Transpose Convolution grids
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.saliency_map_path}`)} style={{ maxWidth: '600px', margin: '0 auto' }} > Spectral Residual Saliency Map { e.target.style.display = 'none'; }} />
Real photos show smooth natural edges. Deepfakes generated by GANs/Diffusion models often exhibit a distinct, microscopic checkerboard grid across the entire face.
⚑
High-Pass Filter
Blending artifacts
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.high_pass_path}`)} > High Pass Filter { e.target.style.display = 'none'; }} />
πŸŒ€
Phase Spectrum
Structural phase discontinuities
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.phase_spectrum_path}`)} > Phase Spectrum { e.target.style.display = 'none'; }} />
{/* PCA & Advanced Visualizations */} {result.frequency_analysis.pca_spectrum_path && (
{/* PCA Panel */}
πŸ”¬
PCA Spectral Component
Hidden periodic artifacts (PC3)
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.pca_spectrum_path}`)} > PCA Spectrum { e.target.style.display = 'none'; }} />
3rd Principal Component β€” reveals GAN residuals
{/* Cepstrum Panel */}
πŸ“‘
Cepstrum Echoes
Detects resizing and rotation
{result.frequency_analysis?.verdicts && }
{ if (result.frequency_analysis.cepstrum_path) { setZoomedImage(`${API_BASE}/${result.frequency_analysis.cepstrum_path}`); } }} > {result.frequency_analysis.cepstrum_path ? ( Cepstrum Analysis { e.target.style.display = 'none'; }} /> ) : (

Visualization unavailable

)}
Bright spikes indicate synthetic resampling
{/* DWT Panel */}
🌊
DWT 4-Band Decomposition
Wavelet (LL, LH, HL, HH) extraction
{result.frequency_analysis?.verdicts && }
setZoomedImage(`${API_BASE}/${result.frequency_analysis.dwt_diagonal_path}`)} > DWT Analysis { e.target.style.display = 'none'; }} />
Analyzes localized HH diagonal high-frequencies
{/* Advanced Metrics Table replaced with Recharts */}
Advanced Frequency Distribution
[`${val.toFixed(2)}%`, 'Variance Ratio']} />
Overall Spectral Anomaly Score: {(result.frequency_analysis.spectral_anomaly_score * 100).toFixed(1)}%
)}
); }; export default React.memo(FrequencyTab);