OFDM based transmitter and receiver

38 visualizzazioni (ultimi 30 giorni)
Jose Iglesias
Jose Iglesias il 18 Apr 2023
Commentato: Jose Iglesias il 20 Apr 2023
I am trying to correct an error in my Matlab code. I am debugging my OFDM based transmitter and receiver with BPSK modulation Matlab code shown below. I tried a suggestion to remove an error I was getting when I would run my code. The suggestion was to change the following code instruction:
cp_len=floor(0.1*block_size); %Length of cyclic prefix
into the following code in order to convert 'cp_len' to integer
cp_len=round(0.1*block_size); %Length of cyclic prefix
I am still getting the following error.
Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in ofdm_bpsk_ji_2 (line 74)
cyclic_prefix = ifft_Subcarrier(end-cp_len+1:end, :);
Any suggestions on how I can remove this issue in my Matlab code? I am perusing the Mathworks forum and other sources to find a solution.The complete code is shown below. 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.1*block_size); %Length of cyclic prefix
% 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, [])];
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);
title('PSK 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 channel
channel = (randn(1, 10) + 1i * randn(1, 10)) / sqrt(2)
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: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);
noise_var = 1 / (10^(snr/10));
% Generate noisy channel coefficients
noisy_channel = channel + sqrt(noise_var/2)*(randn(1,10)+1i*randn(1,10));
% 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
rx_no_cp = reshape(rx_signal, block_size+cp_len, M);
rx_no_cp(1:cp_len, :) = [];
% Perform FFT on received data
rx_fft = fft(rx_no_cp, block_size);
% Equalize channel effect
rx_equalized = rx_fft ./ channel(1);
% Perform BPSK demodulation
rx_demod = pskdemod(rx_equalized(:), 2);
% Calculate bit error rate
ber = sum(rx_demod ~= data.') / numel(data);
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');
  2 Commenti
Daniel
Daniel il 19 Apr 2023
It's a little tricky to give good information on what's going wrong, as I can't replicate the error for a few reasons. After pasting your provided code into a temporary script, my line numbers don't match yours; and also, I'm having difficulty replicating the video file parsing stuff without that video file. Would you be able to attach a script that produces the error when run in a clean MATLAB instance in an otherwise empty directory?
Jose Iglesias
Jose Iglesias il 20 Apr 2023
Daniel,
Thanbk you for your response. I was able to resolve the issue by using this code:
cyclic_prefix = ifft_Subcarrier(:,end-cp_len+1:end);
Append_prefix = [cyclic_prefix ifft_Subcarrier];
At the momemnt I am still doing some trouble shooting and am currenlty trying add a zero padding to the channel of length
channel = [channel zeros(1, 64)];
I am doing this to equalize the channel since the OFDM sysytem has 64 subcarriers but I am getting an eror within the for loop for the "Loop over SNR range" section . This error 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));
I am going to post this new error and hopefully someone can give me some guidande on how to resolve this issue.

Accedi per commentare.

Risposte (0)

Prodotti


Release

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by