Notes
Notes - notes.io |
clear;
close all;
%% PARAMETERS
N = 8;
cp_len = 2;
numSymbols = 5000;
M = 16;
bits_per_sym = log2(M);
SNR_dB_range = 0:2:20;
BER_cp = zeros(size(SNR_dB_range));
BER_no_cp = zeros(size(SNR_dB_range));
%% LOOP OVER SNR
for snr_idx = 1:length(SNR_dB_range)
SNR_dB = SNR_dB_range(snr_idx);
%% BIT GENERATION
numBits = N * numSymbols * bits_per_sym;
bits = randi([0 1], 1, numBits);
%% 16-QAM MODULATION (MANUAL)
symbols = zeros(1, numBits/4);
k = 1;
for i = 1:4:numBits
b = bits(i:i+3);
% I mapping
if isequal(b(1:2), [0 0])
I = -3;
elseif isequal(b(1:2), [0 1])
I = -1;
elseif isequal(b(1:2), [1 1])
I = 1;
else
I = 3;
end
% Q mapping
if isequal(b(3:4), [0 0])
Q = -3;
elseif isequal(b(3:4), [0 1])
Q = -1;
elseif isequal(b(3:4), [1 1])
Q = 1;
else
Q = 3;
end
symbols(k) = I + 1j*Q;
k = k + 1;
end
symbols = symbols / sqrt(10);
%% SERIAL TO PARALLEL
symbols_parallel = reshape(symbols, N, numSymbols);
%% MANUAL IFFT
ofdm_time = zeros(N, numSymbols);
for sym = 1:numSymbols
for n = 1:N
sum_val = 0;
for k2 = 1:N
sum_val = sum_val + symbols_parallel(k2, sym) * ...
exp(1j*2*pi*(n-1)*(k2-1)/N);
end
ofdm_time(n, sym) = sum_val / N;
end
end
%% ADD CYCLIC PREFIX
ofdm_cp = [ofdm_time(end-cp_len+1:end, :); ofdm_time];
%% SERIALIZE
tx_cp = ofdm_cp(:).';
tx_no_cp = ofdm_time(:).';
%% ADD AWGN NOISE
snr_linear = 10^(SNR_dB/10);
noise_cp = (randn(size(tx_cp)) + 1j*randn(size(tx_cp))) / sqrt(2*snr_linear);
noise_no_cp = (randn(size(tx_no_cp)) + 1j*randn(size(tx_no_cp))) / sqrt(2*snr_linear);
rx_cp = tx_cp + noise_cp;
rx_no_cp = tx_no_cp + noise_no_cp;
%% RECEIVER (WITH CP)
% Reshape
rx_cp_matrix = reshape(rx_cp, N+cp_len, numSymbols);
% Remove CP
rx_cp_matrix = rx_cp_matrix(cp_len+1:end, :);
%% MANUAL FFT
rx_symbols_cp = zeros(N, numSymbols);
for sym = 1:numSymbols
for k2 = 1:N
sum_val = 0;
for n = 1:N
sum_val = sum_val + rx_cp_matrix(n, sym) * ...
exp(-1j*2*pi*(n-1)*(k2-1)/N);
end
rx_symbols_cp(k2, sym) = sum_val;
end
end
%% RECEIVER (WITHOUT CP)
rx_no_cp_matrix = reshape(rx_no_cp, N, numSymbols);
rx_symbols_no_cp = zeros(N, numSymbols);
for sym = 1:numSymbols
for k2 = 1:N
sum_val = 0;
for n = 1:N
sum_val = sum_val + rx_no_cp_matrix(n, sym) * ...
exp(-1j*2*pi*(n-1)*(k2-1)/N);
end
rx_symbols_no_cp(k2, sym) = sum_val;
end
end
%% PARALLEL TO SERIAL
rx_sym_cp = rx_symbols_cp(:).' * sqrt(10);
rx_sym_no_cp = rx_symbols_no_cp(:).' * sqrt(10);
%% 16-QAM DEMODULATION
bits_rx_cp = zeros(1, numBits);
bits_rx_no_cp = zeros(1, numBits);
k = 1;
for i = 1:length(rx_sym_cp)
% ----- WITH CP -----
I = real(rx_sym_cp(i));
Q = imag(rx_sym_cp(i));
% I decision
if I < -2
bI = [0 0];
elseif I < 0
bI = [0 1];
elseif I < 2
bI = [1 1];
else
bI = [1 0];
end
% Q decision
if Q < -2
bQ = [0 0];
elseif Q < 0
bQ = [0 1];
elseif Q < 2
bQ = [1 1];
else
bQ = [1 0];
end
bits_rx_cp(k:k+3) = [bI bQ];
% ----- WITHOUT CP -----
I = real(rx_sym_no_cp(i));
Q = imag(rx_sym_no_cp(i));
if I < -2
bI = [0 0];
elseif I < 0
bI = [0 1];
elseif I < 2
bI = [1 1];
else
bI = [1 0];
end
if Q < -2
bQ = [0 0];
elseif Q < 0
bQ = [0 1];
elseif Q < 2
bQ = [1 1];
else
bQ = [1 0];
end
bits_rx_no_cp(k:k+3) = [bI bQ];
k = k + 4;
end
%% BER CALCULATION
BER_cp(snr_idx) = sum(bits ~= bits_rx_cp) / numBits;
BER_no_cp(snr_idx) = sum(bits ~= bits_rx_no_cp) / numBits;
end
%% PLOT
figure;
semilogy(SNR_dB_range, BER_cp, '-o', 'LineWidth', 2); hold on;
semilogy(SNR_dB_range, BER_no_cp, '-s', 'LineWidth', 2);
grid on;
xlabel('SNR (dB)');
ylabel('BER');
legend('With CP', 'Without CP');
title('OFDM BER Performance (16-QAM)');
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
