clc; clear; close all;
% Simulation Parameters
carrierFrequency = 2e6; % Lowered to avoid aliasing (must be < SampleRate/2)
SampleRate = 30.72e6; % 30.72 MHz sample rate
numSamples = 1024; % Number of QPSK symbols
snr = 20; % Signal-to-Noise Ratio in dB
% 1. Generate Random Bits and Map to QPSK
data = randi([0, 1], 2, numSamples);
qpskSymbols = 2 * data - 1; % Maps 0 -> -1, 1 -> +1
qpskSignal = qpskSymbols(1, :) + 1j * qpskSymbols(2, :);
% 2. Upconvert to Carrier Frequency
time = (0:numSamples - 1) / SampleRate;
carrierSignal = exp(1j * 2 * pi * carrierFrequency * time);
transmittedSignal = qpskSignal .* carrierSignal;
% 3. Pass Through AWGN Channel
noisySignal = awgn(transmittedSignal, snr, 'measured');
% 4. Downconvert and Demodulate
receivedSignal = noisySignal .* conj(carrierSignal); % Element-wise downconversion
decodedData = zeros(2, numSamples);
decodedData(1, :) = real(receivedSignal) > 0; % Recover I-branch bits
decodedData(2, :) = imag(receivedSignal) > 0; % Recover Q-branch bits
% 5. Calculate Bit Error Rate (BER)
numErrors = sum(data(:) ~= decodedData(:));
fprintf('Bit Errors: %d out of %d\n', numErrors, numel(data));
% 6. Plot Results (Zoomed in on first 100 samples for visibility)
figure;
subplot(3, 1, 1);
plot(time(1:100), real(transmittedSignal(1:100)));
title('Transmitted Signal (Real Component - First 100 Samples)');
xlabel('Time (s)'); ylabel('Amplitude'); grid on;
subplot(3, 1, 2);
plot(time(1:100), real(noisySignal(1:100)));
title('Received Signal with AWGN Noise (First 100 Samples)');
xlabel('Time (s)'); ylabel('Amplitude'); grid on;
subplot(3, 1, 3);
stem(data(1:50), 'rx', 'LineWidth', 1.5); hold on;
stem(decodedData(1:50), 'bo', 'LineWidth', 1.5);
title('Transmitted vs. Decoded Bits (First 50 Bits)');
xlabel('Bit Index'); ylabel('Bit Value');
legend('Transmitted Data', 'Decoded Data');
ylim([-0.2 1.2]); grid on;
To embed this project on your website, copy the following code and paste it into your website's HTML: