Skip to main content

FFT Magnitude and Phase Spectrum using MATLAB


 

MATLAB Code 

% Developed by SalimWireless.Com

clc;
clear;
close all;

% Configuration parameters
fs = 10000; % Sampling rate (Hz)
t = 0:1/fs:1-1/fs; % Time vector creation

% Signal definition
x = sin(2 * pi * 100 * t) + cos(2 * pi * 1000 * t);

% Calculate the Fourier Transform
y = fft(x);
z = fftshift(y);

% Create frequency vector
ly = length(y);
f = (-ly/2:ly/2-1) / ly * fs;

% Calculate phase while avoiding numerical precision issues
tol = 1e-6; % Tolerance threshold for zeroing small values
z(abs(z) < tol) = 0;
phase = angle(z);

% Plot the original Signal
figure;
subplot(3, 1, 1);
plot(t, x, 'b');
xlabel('Time (s)');
ylabel('|y|');
title('Original Messge Signal');
grid on;

% Plot the magnitude of the Fourier Transform
subplot(3, 1, 2);
stem(f, abs(z), 'b');
xlabel('Frequency (Hz)');
ylabel('|y|');
title('Magnitude of the Fourier Transform');
grid on;

% Plot the phase of the Fourier Transform
subplot(3, 1, 3);
stem(f, phase / pi, 'b');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase of the Fourier Transform');
grid on;
web('https://www.salimwireless.com/search?q=fourier%20transform', '-browser');


Output 


 

 

 

Copy the MATLAB Code above from here

 

Another MATLAB Code

clc;
clear;
close all;

% Parameters
fs = 100;           % Sampling frequency
t = 0:1/fs:1-1/fs;  % Time vector

% Signal definition
x = cos(2*pi*15*t - pi/4) - sin(2*pi*40*t);

% Compute Fourier Transform
y = fft(x);
z = fftshift(y);

% Frequency vector
ly = length(y);
f = (-ly/2:ly/2-1)/ly*fs;

% Compute phase

z(abs(z) < 1e-6) = 0;
phase = angle(z);

% Plot magnitude of the Fourier Transform
figure;
subplot(2, 1, 1);
stem(f, abs(z), 'b');
xlabel('Frequency (Hz)');
ylabel('|y|');
title('Magnitude of Fourier Transform');
grid on;

% Plot phase of the Fourier Transform
subplot(2, 1, 2);
stem(f, phase, 'b');
xlabel('Frequency (Hz)');
ylabel('Phase (radians)');
title('Phase of Fourier Transform');
grid on;

web('https://www.salimwireless.com/search?q=fourier%20transform', '-browser');


 

Output 







Copy the MATLAB Code above from here

 

Why use fftshift? In MATLAB, the fft function returns the DC component at the beginning of the array. To visualize a standard double-sided spectrum where 0 Hz is in the center, we use fftshift. This is essential for analyzing signal symmetry in wireless communications and signal processing.

Handling Phase Noise: Notice the tol = 1e-6 line. This is a pro-tip! We zero out very small magnitude values before calculating the phase to avoid "random" phase noise caused by floating-point errors.

Real-World Applications of FFT in MATLAB

  • Wireless Communications: Used in OFDM (5G/Wi-Fi) to split data across multiple sub-carriers.
  • Audio Engineering: Analyzing frequency response for noise cancellation and equalization.
  • Medical Imaging: Processing MRI and Ultrasound data using Fast Fourier Transforms.
  • Radar Systems: Determining the velocity of objects via Doppler shift analysis.


Interactive Online Simulators

Frequently Asked Questions

Q: Why is my magnitude plot showing peaks at the wrong frequency?
A: Ensure your sampling frequency (fs) is at least twice the highest frequency of your signal (Nyquist Theorem).

Q: How do I increase frequency resolution?
A: Increase the number of samples (N) or the time duration of your signal vector.


Further Reading

  1. Fourier Transform of Sine or Cosine
  2. Definition of the Fourier Series
  3. Continuous and Discrete Time Fourier Transform
  4. Cooley-Tukey algorithm for Fast Fourier Transform (FFT) in MATLAB
  5. Fourier Spectral Analysis
  6. Power Spectral Density Calculation Using FFT in MATLAB
  7. Autocorrelation and Periodicity of a Signal


Power Spectral Density (PSD)

Analyzing Signal Power in the Frequency Domain

In fading channels, **Power Spectral Density (PSD)** is critical because it describes how the power of your signal (or noise) is distributed across frequencies. Unlike a standard Fourier Magnitude, PSD provides a normalized view of power relative to the sampling rate.

Calculation Steps:

  1. Compute the FFT of the signal \(x(t)\).
  2. Calculate the Absolute Magnitude.
  3. Square the magnitude to get the Power Spectrum.
  4. Divide by \((fs \cdot N)\) to find the Density.

The Mathematical Model

\[ PSD = \frac{|FFT(x)|^2}{fs \cdot N} \]
\(fs\): Sampling Frequency
(Determines frequency range)
\(N\): Total Samples
(Determines resolution)
Frequency Resolution (\(\Delta f\)):
\[ \Delta f = \frac{fs}{N} \]

Note: Increasing \(N\) improves frequency resolution, while increasing \(fs\) expands the frequency range (Nyquist limit).

Read More & Access Interactive PSD Simulator

Contact Us

Name

Email *

Message *

Popular Posts

BER vs SNR for M-ary QAM, M-ary PSK, QPSK, BPSK, ...(MATLAB Code + Simulator)

Bit Error Rate (BER) & SNR Guide Analyze communication system performance with our interactive simulators and MATLAB tools. 📘 Theory 🧮 Simulators 💻 MATLAB Code 📚 Resources BER Definition SNR Formula BER Calculator MATLAB Comparison 📂 Explore M-ary QAM, PSK, and QPSK Topics ▼ 🧮 Constellation Simulator: M-ary QAM 🧮 Constellation Simulator: M-ary PSK 🧮 BER calculation for ASK, FSK, and PSK 🧮 Approaches to BER vs SNR What is Bit Error Rate (BER)? The BER indicates how many corrupted bits are received compared to the total number of bits sent. It is the primary figure of merit f...

Online Simulator for ASK, FSK, and PSK

Interactive Digital Signal Processing (DSP) Tutorial and Simulator for ASK, FSK, and BPSK modulation techniques. Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Digital Modulation Visualizer: ASK, FSK, & BPSK Simulator Learn and visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. Perfect for DSP students and engineers. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator More Topics 1. ASK (Amplitude Shift Keying) Simulat...

UGC NET Electronic Science Previous Year Question Papers with Solutions

Home / Engineering & Other Exams / UGC NET 2026 PYQ ⬇️ Download Papers and Solutions 📋 Exam Pattern 💡 Preparation Tips ❓ FAQs 📊 Exam Highlights: Electronic Science (88) Feature Details Junior Research Fellowship (JRF) ₹37,000 + HRA per month Eligibility M.Sc/M.Tech in Electronics (55%) Validity of Certificate JRF (3 Years) | Lectureship (Lifetime) 📥 Download UGC NET Electronics PDFs Complete collection of previous year question papers, answer keys and explanations for Subject Code 88. Start Downloading 📂 View All Question Papers June 2025 - Question Paper Download PDF June 2025 - Solved Paper + Explanation ...

Constellation Diagrams of ASK, PSK, and FSK (with MATLAB Code + Simulator)

Constellation Diagrams: ASK, FSK, and PSK Comprehensive guide to signal space representation, including interactive simulators and MATLAB implementations. 📘 Overview 🧮 Simulator ⚖️ Theory Q-function 📚 Resources 📂 Other Topics: M-ary PSK & QAM Diagrams ▼ 🧮 Simulator for M-ary PSK Constellation 🧮 Simulator for M-ary QAM Constellation BASK (Binary ASK) Modulation Transmits one of two signals: 0 or -√Eb, where Eb​ is the energy per bit. These signals represent binary 0 and 1. BFSK (Binary FSK) Modulation Transmits one of two signals: +√Eb​ (On the y-axis, the phas...

UGC NET Electronic Science June 2025 Question Paper with Answer Key & Detailed Solutions

Home / UGC NET PYQ / June 2025 Solved UGC NET Electronic Science June 2025 Question Paper with Answer Key and Full Explanations 📥 Download Question Paper (PDF) 2025 2024 2023 2022 2021 2020 Explanations 1. For forming a p-type semiconductor, the dopant must be a trivalent impurity (three valence electrons) so that it creates acceptor levels and holes become the majority carriers. Among the given elements, boron (B) is a group-III element (trivalent). Arsenic (As) and phosphorus (P) are group-V (pentavalent) donors that produce n-type material, and germanium (Ge) is a group-IV element usually used as the semiconductor, not as an acceptor dopant. Hence, doping an intrinsic semiconductor with B produces a p-type semiconductor. 2. The ohmic resistance of a JFET at zero gate bias is given by the standard relation: R DS(on) = V P / I DSS because in the ohmic (linear) region with V GS =...

BER performance of QPSK with BPSK, 4-QAM, 16-QAM, 64-QAM, 256-QAM, etc (MATLAB + Simulator)

📘 Overview 📚 QPSK vs BPSK and QAM: A Comparison of Modulation Schemes in Wireless Communication 📚 Real-World Example 🧮 MATLAB Code 📚 Further Reading   QPSK provides twice the data rate compared to BPSK. However, the bit error rate (BER) is approximately the same as BPSK at low SNR values when gray coding is used. On the other hand, QPSK exhibits similar spectral efficiency to 4-QAM and 16-QAM under low SNR conditions. In very noisy channels, QPSK can sometimes achieve better spectral efficiency than 4-QAM or 16-QAM. In practical wireless communication scenarios, QPSK is commonly used along with QAM techniques, especially where adaptive modulation is applied. Modulation Bits/Symbol Points in Constellation Usage Notes BPSK 1 2 Very robust, used in weak signals QPSK 2 4 Balanced speed & reliability 4-QAM ...

1G to 5G Technology - Evolution of Wireless Generations

Cellular wireless evolution Generation Frequency band PHY features Data rate Spectral Eff. (bps/Hz) 1G 850 MHz FDMA, FM N/A N/A 2G 900 MHz, 1.8 GHz TDMA/CDMA, GMSK/QPSK, FEC, PC 10 Kbps < 1 3G 1.8–2.5 GHz CDMA, QAM 1–40 Mbps 1–8 4G 2–8 GHz OFDMA, SC-FDMA, QAM, MIMO-OFDM 100–600 Mbps 15 5G 1–6 GHz mm wave (26–28 GHz) < 1 GHz (massive IoT) visible light? massive MIMO, beamforming D2D, Full duplex, NOMA LDPC and Polar codes OFDM & variants (adapted to extremes?) multi-Gbps several tens Waveform design is the major change between the generations Mobile Wireless Generations Specifications  1G  Voice, Analog traffic, FDMA  2G  Voice, SMS, CS data ...

MATLAB Code for ASK, FSK, and PSK (with Online Simulator)

MATLAB Code for ASK, FSK, and PSK Comprehensive implementation of digital modulation and demodulation techniques with simulation results. 📘 Theory 📡 ASK Code 📶 FSK Code 🎚️ PSK Code 🕹️ Simulator 📚 Further Reading Amplitude Shift Frequency Shift Phase Shift Live Simulator ASK, FSK & PSK HomePage MATLAB Code MATLAB Code for ASK Modulation and Demodulation COPY % The code is written by SalimWireless.Com clc; clear all; close all; % Parameters Tb = 1; fc = 10; N_bits = 10; Fs = 100 * fc; Ts = 1/Fs; samples_per_bit = Fs * Tb; rng(10); binar...