clear all
close all
clc
M = 16;
bits_symbol = log2(M);
fc = 1e9;
fs = 10*fc;
num_symbol = 250;
t = (0:1/fs:num_symbol*(1/fs)-1/fs);
input = randi([0,1], num_symbol, bits_symbol);
constellation = (-sqrt(M)+1:2:sqrt(M)-1);
I = constellation(bi2de(input(:,1:bits_symbol/2),'left-msb')+1);
Q = constellation(bi2de(input(:, bits_symbol/2+1:end),'left-msb')+1);
Tx = I .* cos(2*pi*fc*t) + Q .* sin(2*pi*fc*t);
Rx_I = 2 * Tx .* cos(2*pi*fc*t);
Rx_Q = 2 * Tx .* sin(2*pi*fc*t);
Rx_I_f = lowpass(Rx_I, fc/2, fs);
Rx_Q_f = lowpass(Rx_Q, fc/2, fs);
I_org = zeros(num_symbol, 1);
Q_org = zeros(num_symbol, 1);
for x = 1:num_symbol
[~, I_org(x)] = min(abs(Rx_I_f(x) - constellation'));
[~, Q_org(x)] = min(abs(Rx_Q_f(x) - constellation'));
end
output = [de2bi(I_org-1, bits_symbol/2, 'left-msb'), de2bi(Q_org-1, bits_symbol/2, 'left-msb')];
[~, BER] = biterr(input, output);
fprintf('Bit Error Rate (BER): %f\n', BER);