OFDM

PHOTO EMBED

Mon Nov 18 2024 17:47:54 GMT+0000 (Coordinated Universal Time)

Saved by @lord

OFDM     clc; close all; clear all;

% Parameters
numsubcar = 64;          % Number of subcarriers
numofsym = 100;          % Number of OFDM symbols
cplen = 16;              % Cyclic prefix length
snrange = 0:2:20;        % SNR range in dB
ifft_size = numsubcar;   % IFFT size (equal to number of subcarriers)

% Generate random data bits
databits = randi([0, 1], numsubcar * numofsym, 1); % Generate random bits

% Plot the first 10 bits
figure;
subplot(4, 1, 1);
stem(databits(1:10));
xlabel('Bit Index');
ylabel('Bit Value');
title('Input Data Bits');

% BPSK Modulation (map 0 -> -1, 1 -> +1)
mod_data = 2 * databits - 1;

% Reshape modulated data to match subcarriers and symbols
mod_data = reshape(mod_data, numsubcar, numofsym);

% Perform IFFT
ifft_data = ifft(mod_data, ifft_size);

% Plot the IFFT (real part of the first symbol)
subplot(4, 1, 2);
plot(real(ifft_data(:, 1)));
xlabel('Sample Index');
ylabel('Amplitude');
title('IFFT Output (First Symbol)');

% Add Cyclic Prefix
ofdm_symbols = [ifft_data(end-cplen+1:end, :); ifft_data];

% Serialize OFDM symbols to form the transmit signal
tx_signal = ofdm_symbols(:);

% Plot the real part of the transmitted signal
subplot(4, 1, 3);
plot(real(tx_signal(1:200))); % Plot first 200 samples
xlabel('Sample Index');
ylabel('Amplitude');
title('Transmitted OFDM Signal');

% Initialize BER values
bervalues = zeros(size(snrange));

% Loop over SNR values to compute BER
for idx = 1:length(snrange)
    snr = snrange(idx);
    
    % Calculate signal power
    signal_power = mean(abs(tx_signal).^2);
    
    % Calculate noise power
    noise_power = signal_power / (10^(snr / 10));
    
    % Generate AWGN
    noise = sqrt(noise_power) * randn(size(tx_signal));
    
    % Add noise to the signal
    rx_signal = tx_signal + noise;
    
    % Reshape received signal to matrix form and remove cyclic prefix
    rx_ofdm_symbols = reshape(rx_signal, numsubcar + cplen, []);
    rx_ofdm_symbols = rx_ofdm_symbols(cplen+1:end, :);
    
    % Perform FFT on received symbols
    fft_data = fft(rx_ofdm_symbols, ifft_size);
    
    % BPSK Demodulation (decision rule: > 0 -> 1, <= 0 -> 0)
    demod_data = real(fft_data(:)) > 0;
    
    % Compute BER
    numerrors = sum(databits ~= demod_data);
    ber = numerrors / length(databits);
    bervalues(idx) = ber;
end

% Plot BER vs SNR
subplot(4, 1, 4);
semilogy(snrange, bervalues, '-o');
xlabel('SNR (dB)');
ylabel('BER');
title('BER vs. SNR for OFDM System');
grid on;
content_copyCOPY