MATLAB Code
This is a simple MATLAB code snippet that computes the Fourier Transform of any signal. I will show you different examples below the main code. Remember, to find the Fourier Transform of any signal using this code snippet, you need a sampled signal and its corresponding sampling frequency.
n = length(signal);
f = (-n/2:n/2-1)*(fs/n); % Frequency axis centered at 0
S_f = abs(fftshift(fft(signal)/n)); % Normalize FFT
S_f_dB = 20*log10(S_f / max(S_f)); % dB scale with normalization
Example for finding FFT of a sine wave
clc;
clear all;
close all;
fm = 10; % Message signal frequency (Hz)
fs = 1000; % Sampling frequency (100 kHz)
t = 0:1/fs:1-1/fs; % Time vector over 1 second
signal = sin(2*pi*fm*t);
% Compute frequency spectrum in dB
n = length(signal);
f = (-n/2:n/2-1)*(fs/n); % Frequency axis centered at 0
S_f = abs(fftshift(fft(signal)/n)); % Normalize FFT
S_f_dB = 20*log10(S_f / max(S_f)); % dB scale with normalization
% Plot normalized spectrum in dB
figure(1);
plot(f, S_f);
title('Normalized Frequency Spectrum of PAM Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
figure(2);
plot(f, S_f_dB);
title('Normalized Frequency Spectrum (dB) of PAM Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
web('https://www.salimwireless.com/search?q=fft%20fourier%20transform', '-browser');