MATLAB Script
% The code is written by SalimWireless.Com
clc;
clear all;
close all;
% Generate random bits
numBits = 100;
bits = randi([0, 1], 1, numBits);
% Define parameters
numSubcarriers = 4; % Number of subcarriers
numPilotSymbols = 3; % Number of pilot symbols
cpLength = ceil(numBits / 4); % Length of cyclic prefix (one-fourth of the data length)
% Add cyclic prefix
dataWithCP = [bits(end - cpLength + 1:end), bits];
% Insert pilot symbols
pilotSymbols = ones(1, numPilotSymbols); % Example pilot symbols (could be any pattern)
dataWithPilots = [pilotSymbols, dataWithCP];
% Perform OFDM modulation (IFFT)
dataMatrix = reshape(dataWithPilots, numSubcarriers, []);
ofdmSignal = ifft(dataMatrix, numSubcarriers);
ofdmSignal = reshape(ofdmSignal, 1, []);
% Display the generated data
disp("Original Bits:");
disp(bits);
disp("Data with Cyclic Prefix and Pilots:");
disp(dataWithPilots);
disp("OFDM Signal:");
disp(ofdmSignal);
%%%%%%%%%%%%%%%%%%%%%%%%%%% Demodulation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Perform FFT on the received signal
%ofdmSignal = awgn(ofdmSignal, 1000);
ofdmSignal = reshape(ofdmSignal, numSubcarriers, []);
rxSignal = fft(ofdmSignal, numSubcarriers);
%rxSignal = [rxSignal(1,:) rxSignal(2,:) rxSignal(3,:) rxSignal(4,:)];
% Remove cyclic prefix
rxSignalNoCP = rxSignal(cpLength + 1:end);
% Extract data symbols and discard pilot symbols
dataSymbols = rxSignalNoCP(numPilotSymbols + 1:end);
% Demodulate the symbols using thresholding
threshold = 0;
demodulatedBits = (real(dataSymbols) > threshold);
figure(1)
stem(bits);
legend("Original Information Bits")
figure(2)
stem(demodulatedBits);
legend("Received Bits")
Output