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

 

Further Reading

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


People are good at skipping over material they already know!

View Related Topics to







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 for a...

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 📚 Resources Definitions Constellation Tool Key Points MATLAB Code 📂 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 ...

DFTs-OFDM vs OFDM: Why DFT-Spread OFDM Reduces PAPR Effectively (with MATLAB Code)

DFT-spread OFDM (DFTs-OFDM) has lower Peak-to-Average Power Ratio (PAPR) because it "spreads" the data in the frequency domain before applying IFFT, making the time-domain signal behave more like a single-carrier signal rather than a multi-carrier one like OFDM. Deeper Explanation: Aspect OFDM DFTs-OFDM Signal Type Multi-carrier Single-carrier-like Process IFFT of QAM directly QAM → DFT → IFFT PAPR Level High (due to many carriers adding up constructively) Low (less fluctuation in amplitude) Why PAPR is High Subcarriers can add in phase, causing spikes DFT "pre-spreads" data, smoothing it Used in Wi-Fi, LTE downlink LTE uplink (as SC-FDMA) In OFDM, all subcarriers can...

Online Simulator for ASK, FSK, and PSK

Try our new Digital Signal Processing Simulator!   •   Interactive ASK, FSK, and BPSK tools updated for 2025. Start Now Interactive Modulation Simulators Visualize binary modulation techniques (ASK, FSK, BPSK) in real-time with adjustable carrier and sampling parameters. 📡 ASK Simulator 📶 FSK Simulator 🎚️ BPSK Simulator 📚 More Topics ASK Modulator FSK Modulator BPSK Modulator More Topics Simulator for Binary ASK Modulation Digital Message Bits Carrier Freq (Hz) Sampling Rate (...

Power Distribution in Amplitude Modulation (AM)

Power Distribution In practice, the AM wave s(t) is a voltage or current signal. In either case, the average power delivered to a 1-ohm load resistor by s(t) is comprised of three components: Carrier power = (1/2) A c 2 Upper side-frequency power = (1/8)μ 2 A c 2 Lower side-frequency power = (1/8)μ 2 A c 2 The ratio of the total sideband power to the total power in the modulated wave is therefore equal to μ 2 / (2 + μ 2 ), which depends only on the modulation factor μ. If μ = 1, that is, 100% modulation is used, the total power in the two side-frequencies of the resulting AM wave is only one-third of the total power in the modulated wave. A major topic in Amplitude Modula...

Filter Bank Multicarrier (FBMC)

Filter Bank Multicarrier (FBMC) Filter Bank Multicarrier (FBMC) is an advanced multicarrier modulation technique designed to overcome the spectral inefficiencies and interference issues of OFDM. Motivation: Limitations of OFDM In an OFDM system , the transmitter uses an Inverse Fast Fourier Transform (IFFT) and the receiver uses a Fast Fourier Transform (FFT) to process multiple subcarriers. Each OFDM symbol occupies a duration denoted by T sym . OFDM is a multicarrier modulation technique where a high data-rate stream is divided into multiple parallel low data-rate streams. To mitigate inter-symbol interference (ISI) caused by multipath fading, the total bandwidth B is divided into N narrow sub-bands. However, a major drawback of OFDM is that the subcarrier filters generated by the IFFT/FFT process have poor spectral cont...

OFDM vs SC-OFDM

  The main difference between OFDM and SC-OFDM is that SC-OFDM transmits the signal using a single carrier, while OFDM uses multiple subcarriers. However, in SC-OFDM, the signal is generated with different sub-bands, but it is transmitted through a single carrier (more technically, through a wideband carrier signal). Block Diagram of OFDM: Data → Modulation → Serial-to-Parallel → IFFT → Add CP → Transmit Received Signal → Remove CP → FFT → Parallel-to-Serial → Demodulation → Data Block Diagram of SC-OFDM: Data → Modulation → DFT → IFFT → Add CP → Transmit Received Signal → Remove CP → FFT → Demodulation → Data    In the case of OFDM, the input modulated data is converted from a serial stream to parallel streams, and different subcarriers are assigned to each chunk. Then, IFFT is applied to these chunks, and a cyclic prefix is added to each one. Each chunk is technically referred to as an OFDM symbol . Unlike OFDM, SC-OFDM does not perform serial-to-parallel conversion o...