Clear Filters
Clear Filters

OFDM probability of error vs SNR

39 views (last 30 days)
Need some assistance is possibly troubleshooting my OFDM transmitter and receiver Matlab code shown below. I removed all possible errors and issues I was having when I was initially developing the code. When I run the code there are no errors, but the code was running for many hours and after a while I decided to stop it from running. Does it usually take many hours to produce a BER vs SNR plot for an OFDM with BPSK modulation transmitter and receiver? Any advice is welcome. Again, there are no errors when I run the code, but it never finishes and completes the program. Thank you in advance!
clc;
clear all;
close all;
% Initiation
no_of_data_bits = 64;
M = 64; %Number of subcarrier channel
n = 256; %Total number of bits to be transmitted at transmitter
block_size = 64; %Size of each OFDM block to add cyclic prefix
cp_len = round(0.25*block_size); %Length of cyclic prefix
% Initiate SNR range and error count array
SNR_range = -10:1:10;
% BER = zeros(size(SNR_range));
errors = zeros(size(SNR_range));
symbols = 0;
% Read the video file
video = VideoReader('pexels-pixabay-856027-960x540-25fps.mp4');
% Calculate the total number of bits in the video
total_bits = 0;
% Initialize error count for all frames
error_count = 0;
% Loop through each frame of the video and convert it to a binary data stream
while hasFrame(video)
frame = readFrame(video);
decimal_data = reshape(frame,numel(frame),1);
binary_data = reshape(de2bi(decimal_data,8)',[],1);
% Increment total_bits
total_bits = total_bits + numel(binary_data);
% Loop over SNR range
for i = 1:length(SNR_range)
% Loop through each symbol
for j = 1:numel(binary_data)/no_of_data_bits
% Use the binary data as the input source data
data = binary_data((j-1)*no_of_data_bits+1:j*no_of_data_bits);
% Perform BPSK modulation on the input source data
bpsk_modulated_data = pskmod(data,2);
% Converting the series data stream into parallel data stream to form subcarriers
S2P = reshape(bpsk_modulated_data,no_of_data_bits/M,M);
% Generate channel
channel_len = 10;
channel = (randn(1, channel_len) + 1i * randn(1, channel_len)) / sqrt(2);
% Zero-padding
channel_padded = [channel, zeros(1, block_size - length(channel))];
% IFFT of subcarriers and add cyclic prefix
number_of_subcarriers = M;
% IFFT does parallel to serial conversion
ifft_Subcarrier = ifft(S2P, block_size);
% Add cyclic prefix
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
% Multipath channel
rcvd_symbol = filter(channel,1,Append_prefix);
% Calculate noise variance for current SNR
snr = SNR_range(i);
noise_var = 1 / (10^(snr/10));
% Generate noisy channel coefficients
noisy_symbol = rcvd_symbol + sqrt(noise_var/2)*(randn(1,80)+1i*randn(1,80));
% Remove cyclic prefix at the receiver
rx_no_cp = noisy_symbol(cp_len+1:end);
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% FFT on the channel
channel_fft = fft(channel_padded);
% Equalize channel effect
rx_equalized = rx_fft ./ channel_fft;
% Perform BPSK demodulation
rx_demod = pskdemod(rx_equalized(:), 2); % (:) used to create vectors
% Calculate bit error rate
% ber = sum(rx_demod ~= data.') / numel(data);
% errors(i) = errors(i) + sum(rx_demod ~= data.');
errors(i) = errors(i) + sum(rx_demod.' ~= data.');
symbols = symbols + numel(data);
% Update BER value for current SNR value
% BER(i) = BER(i) + ber; % accumulate BER over all symbols
end
end
end
% Divide the accumulated BER by the total number of symbols to get the average BER for this SNR
% BER = BER / (total_bits/no_of_data_bits);
% Calculate BER for each SNR
BER = errors ./ symbols
% Plot BER vs SNR
figure(8);
semilogy(SNR_range, BER, 'o-');
grid on;
xlabel('SNR (dB)');
ylabel('Bit Error Rate');
title('Probability of Error vs SNR');

Accepted Answer

Pratham Shah
Pratham Shah on 27 Apr 2023
Hi Jose!
No it should not take long time to simulate unless the video you are using is large.
However if you want to plot BER vs SNR for OFDM scheme, this is the code I wrote long time back. I hope it will give you some idea.
clc;
clear all
format long;
nFFT = 64; % fft size
nDSC = 64; % number of data subcarriers
nBitPerSym = 64; % number of bits per OFDM symbol (same as the number of subcarriers for BPSK)
nSym = 10^4; % number of OFDM symbols
EbN0dB =[0:10]; % bit to noise ratio
theoryBer = (1/2)*erfc(sqrt(10.^(EbN0dB/10)));
for k = 1:length(EbN0dB)
% Transmitter
ipBit=randi([0 1],1,nBitPerSym*nSym);
ipMod = 2*ipBit-1; % BPSK modulation 0 --> -1, 1 --> +1
ipMod = reshape(ipMod,nBitPerSym,nSym).'; % grouping into multiple symbols
% Assigning modulated symbols to subcarriers
xF =ipMod;
% Taking FFT, the term (nFFT/sqrt(nDSC)) is for normalizing the
power of transmit symbol to 1
xt = (nFFT/sqrt(nDSC))*ifft(fftshift(xF.')).';
% Parallel to serial
xt = reshape(xt.',1,nSym*nFFT);
% Gaussian noise of unit variance, 0 mean
nt = 1/sqrt(2)*[randn(1,nSym* nFFT) + j*randn(1,nSym* nFFT)];
% Adding noise
yt = xt + 10^(-EbN0dB(k)/20)*nt;
% Receiver serial to parallel
yt = reshape(yt.', nFFT,nSym).';
% converting to frequency domain
yF = fftshift(fft(yt.')).';
%Parallel to serial
yMod = reshape(yF.',1,nSym* nFFT);
%Hard decision decoding for BPSK demodulation
ipBitHat = (sign(real(yMod))+1)/2;
% counting the errors
nErr = sum(xor(ipBitHat,ipBit));
simBer(k) = nErr/(nSym*nBitPerSym);
[EbN0dB(k) theoryBer(k) simBer(k)];
end
figure(1);
semilogy(EbN0dB,theoryBer,EbN0dB,simBer);
legend('Analytical','Simulation');
xlabel('SNR(Eb/No)dB')
ylabel('Bit Error Rate (BER)')
grid on
In the mean time I'll go thorugh your code and revert you back! :)
  1 Comment
Jose Iglesias
Jose Iglesias on 28 Apr 2023
Pratham,
Thank you for your reponse. After doing some troubleshooting, it turns out that my code was stuck on an iteration that was not letting my i index go past -10dB. It was corrected and I was able to get my plot. I will also try the code you were kind enough to share!

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!