OFDM based transmitter and receiver channel equalization
13 views (last 30 days)
Show older comments
Jose Iglesias
on 20 Apr 2023
Commented: Jose Iglesias
on 28 Apr 2023
I am currently trying to equalize my OFDM based transmitter and receiver with BPSK modulation. Its respective Matlab code is shown below. I tried to equalize the channel by adding zero padding of zeros(1, 64) to the channel since this OFDM system has 64 subcarriers and the channel between the transmitter and receiver has 10 taps; The code below shows the zero padding I added:
% Generate channel
channel = (randn(1, 10) + 1i * randn(1, 10)) / sqrt(2);
channel = [channel zeros(1, 64)];
However, when I ran the code, I get the following error in my "Loop over SNR range" within the for loop which states:
Arrays have incompatible sizes for this operation.
Error in ofdm_bpsk_ji_3 (line 102)
noisy_channel = channel + sqrt(noise_var/2)*(randn(1,10)+1i*randn(1,10));
So, I think the zero padding I added previously to the channel before the for loop is causing an iteration error for the noisy channel.
So I got rid of the zero padding and then added the following code in order to estimate and compensate for the channel response for equalization.
% Generate training sequence
training_seq = ones(1, block_size);
I added this before the channel generation and before the OFDM transmission but the symbols are received in a scattered fashion and my BER vs SNR plot does not look good. I can use some advice on how to equalize my channel correcly. Thanks in advance!
clc;
clear all;
close all;
% TODO> use M for modulation, and use n_subcarriers for blocksize
% 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
% TODO: cp is supposed to be 1\4 of your number of subcarriers. *(to be
% precise, it should be 1\4 of your OFDM symbol duration, which means 1\4
% of the samples in time domain
% Read the video file
video = VideoReader('pexels-pixabay-856027-960x540-25fps.mp4');
%%
% Loop through each frame of the video and convert it to a binary data stream
binary_data = [];
while hasFrame(video)
frame = readFrame(video);
% Convert the image to grayscale
gray_frame = rgb2gray(frame);
% Threshold the grayscale image to create a binary image
binary_frame = imbinarize(gray_frame);
% Reshape the binary image into a 1D array and append it to the binary data
binary_data = [binary_data reshape(binary_frame, 1, [])];
break % TODO: check if this is breaking the loop
end
%%
% Calculate the total number of bits in the video
total_bits = numel(binary_data);
% Use the binary data as the input source data
data = binary_data(1:no_of_data_bits);
figure(1)
stem(data);
grid on;
xlabel('DataPoints');
ylabel('Amplitude');
title('Original Data')
% Perform BPSK modulation on the input source data
psk_modulated_data = pskmod(data,2);
figure(2)
stem(psk_modulated_data);
% scatter(real(rx_fft), imag(rx_fft))
title('BPSK Modulation') % TODO: make the title change based on the modulation
% Converting the series data stream into parallel data stream to form
% subcarriers
S2P = reshape(psk_modulated_data,no_of_data_bits/M,M);
figure(3)
stem(S2P)
title('Subcarriers')
grid on;
% Generate training sequence
training_seq = ones(1, block_size);
% Generate channel
channel = (randn(1, block_size) + 1i * randn(1, block_size)) / sqrt(2);
% channel = (randn(1, 10) + 1i * randn(1, 10)) / sqrt(2);
% channel = [channel zeros(1, 64)];
figure(4)
stem(channel);
grid on;
% IFFT of subcarriers and add cyclic prefix
number_of_subcarriers = M;
ifft_Subcarrier = ifft(S2P, block_size);
figure(5);
plot(real(ifft_Subcarrier),'r');
title('IFFT on all the subcarriers');
%%
% Add cyclic prefix
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
figure(6);
plot(real(Append_prefix),'r');
% Initiate SNR range and BER array
SNR_range = -10:10;
% SNR_range = 5;%-10:1:10;
BER = zeros(size(SNR_range));
% Loop over SNR range
for i = 1:length(SNR_range)
% Calculate noise variance for current SNR
snr = SNR_range(i);
disp(['SNR = ' num2str(snr)]);
% disp(snr) % TODO> display the string 'snr = '
noise_var = 1 / (10^(snr/10));
% Generate noisy channel coefficients
noisy_channel = channel + sqrt(noise_var/2)*(randn(1,block_size)+1i*randn(1,block_size));
% noisy_channel = channel + sqrt(noise_var/2)*(randn(1,10)+1i*randn(1,10));
% Add training sequence to the OFDM
training_seq = ones(1, block_size);
% Convert to serial stream for transmission
ofdm_signal = Append_prefix(:).';
figure(7);
plot(real(ofdm_signal));
xlabel('Time');
ylabel('Amplitude');
title('OFDM Signal');
grid on;
% Apply the channel
rx_signal = conv(ofdm_signal, channel, 'same');
% Remove cyclic prefix at the receiver
% disp(size(rx_signal));
% disp(size(rx_no_cp));
% rx_no_cp = reshape(rx_signal, block_size+cp_len, M); % TODO> check the error in this line
% rx_no_cp(1:cp_len, :) = [];
rx_no_cp = rx_signal(end-block_size+1:end); % TODO> check what this line does, and if it doing what u want
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% Perform FFT on channel
channel_fft = fft(channel(1));
% Equalize channel effect
rx_equalized = rx_fft ./ channel(1); % TODO: channel not equalized.
% Q1: how to equalize the channel
% scatter(real(rx_fft), imag(rx_fft))
% Perform BPSK demodulation
rx_demod = pskdemod(rx_equalized(:), 2);
% Calculate bit error rate
ber = sum(rx_demod ~= data.') / numel(data);
% Update BER value for current SNR value
BER(i) = ber;
end
% Plot BER vs SNR
figure;
semilogy(SNR_range, BER);
grid on;
xlabel('SNR (dB)');
ylabel('Bit Error Rate');
title('Probability of Error vs SNR');
0 Comments
Accepted Answer
Sai Kiran
on 25 Apr 2023
Hi,
As per my understanding you want to equalize the channel in OFDM.
You can use 'ofdmEqualize' function which performs the MMSE equalization.
Please refer to the following documentation for more information.
I hope it helps!
Thanks.
More Answers (0)
See Also
Categories
Find more on WLAN Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!