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

UGC NET Electronic Science Previous Year Question Papers with Solutions

Download Papers and Solutions Exam Pattern Preparation Tips FAQs More Home / Engineering & Other Exams / UGC NET 2026 PYQ 📊 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 2026 - Question Paper Download PDF June 202...

GMSK BER vs SNR (with Simulation)

  GMSK Performance: Why the BER vs. SNR Curve Matters If you’ve ever wondered why GMSK (Gaussian Minimum Shift Keying) became the backbone of the global GSM revolution, the answer lies in its unique balance of spectral efficiency and power performance. But there is no free lunch in engineering. To get that sleek, narrow spectrum, you have to pay a price in Bit Error Rate (BER) . In this guide, we break down the theoretical BER vs. SNR performance of GMSK, the "magic" of the BT product, and how to optimize your link budget for real-world conditions. What is GMSK and Why Should You Care? GMSK is a derivative of MSK (Minimum Shift Keying) that uses a Gaussian pre-modulation filter. This filter "smooths" the phase transitions, significantly reducing sideband power. The result? A constant envelope signal that allows you to use high-efficiency Class-C amplifiers without distorting the signal. This is the secr...

The output voltage V0 (in Volt) for the network given in the Figure is ___

  Find the Output Voltage V o Solve the given circuit using Kirchhoff's Current Law (KCL), Ohm's law, and the supernode method. 1. Given Circuit Parameters Current source 1 mA Resistors 1 kΩ Each resistor Voltage source 6 V Required V o 2. Understand the Circuit The circuit contains a 1 mA current source, four 1 kΩ resistors, and a 6 V source connected between the two midpoint nodes. The output voltage is measured between the top and bottom rails. Reference node Take the bottom rail as ground: 0 V . Let the top rail voltage be V T , and...

What is a Supernode?

  Circuit Analysis What Is a Supernode? A supernode is formed when an ideal voltage source connects two non-reference nodes. It allows us to apply Kirchhoff's Current Law (KCL) to the combined circuit boundary. 1. Understanding a Supernode In nodal analysis, a voltage source between two unknown-voltage nodes makes the current through that source difficult to express directly using Ohm's law. We combine the two nodes and the voltage source into one extended region called a supernode. Key idea Apply KCL around the outer boundary of the supernode, then use the voltage source to write an additional voltage equation. 2. Supernode Circuit Diagram The voltage source connects nodes V L an...

A binary ripple counter is designed to count (0)₁₀ to (64)₁₀. Which of the following is/are the number of flip-flops required ...

  Q. A binary ripple counter is designed to count  (0)₁₀ to (64)₁₀ . Which of the following is/are the number of flip-flops required to design the counter?   (A) 6 (B) 7 (C) 4 (D) 5 Solution A binary ripple counter must count from (0)₁₀ to (64)₁₀ , including both endpoints. Step 1: Calculate the total number of states Since both 0 and 64 are included, add 1 to the difference: N = Last value − First value + 1 N = 64 − 0 + 1 = 65 states The counter visits every integer from 0 through 64: 0, 1, 2, 3, ..., 63, 64 There are 65 distinct states in total. Step 2: Calculate the number of flip-flops Each flip-flop stores one bit. With n flip-flops, the counter can represent 2ⁿ distinct states. 2⁶ = 64 states (not enough) 2⁷ = 128 states (eno...

Parametric Down-Conversion of Photons

  Nonlinear Optics One incoming photon is converted into two lower-energy photons, typically in a nonlinear optical medium. Energy is conserved: E in = E 1 + E 2 E in ​ = E 1 ​ + E 2 ​ . Parametric Down-Conversion A nonlinear optical process in which one pump photon is converted into two lower-energy photons, known as the signal and idler photons. 1 Energy Conservation The energy of the pump photon equals the sum of the energies of the signal and idler photons. \[ \boxed{\hbar\omega_p=\hbar\omega_s+\hbar\omega_i} \] \[ \boxed{\omega_p=\omega_s+\omega_i} \] ωₚ Pump angular frequency ...

SRAM, DRAM, EPROM, MRAM, and DDRAM Explained

Computer Fundamentals Types of Memory and Their Technologies Understand the fundamental relationship between different types of computer memory and the technologies used to implement them. Fundamental Matching Memory type, technology, and key characteristics Memory Type Technology Key Characteristic Cache Memory SRAM Very fast; stores frequently accessed data Main Memory DRAM Holds programs and data currently in use Au...

The cutoff frequency (in GHz) for the dominant TE10 mode of an air-filled rectangular waveguide of ...

  Q. The cutoff frequency (in GHz) for the dominant TE10 mode of an air-filled rectangular waveguide of inner dimension 0.28 inch×0.14 inch is __. (rounded off to two decimal places). Given Information Waveguide dimensions = 0.28 inch × 0.14 inch Wider dimension (a) = 0.28 inch Smaller dimension (b) = 0.14 inch Medium = Air Dominant mode = TE 10 Speed of light (c) = 3 × 10 8 m/s Step 1: Cutoff Frequency Formula The cutoff frequency of a rectangular waveguide is: f c = (c / 2) √[(m/a) 2 + (n/b) 2 ] For the dominant TE 10 mode: m = 1, n = 0 f c = c / 2a Step 2: Convert Inch into Meter We know that 1 inch = 0.0254 meter. a = 0.28 × 0.0254 a = 0.007112 m ...