Many years after graduating i thought i would never see light of a communications assignment again...as luck would have it they asked me to assist the group with MATLAB and i took it upon myself to replicate an OFDM concept that i had once learnt on the job...hopefully in the days to come i should get the opportunity to work closely with MATLAB groups within the automotive arm of KPIT...!!!
Please copy the below text and save the file as ofdm.m
%with love from the grasshopper => 4KK
%clearing memory
clear all;close all;clear all;clc;
%creating a time vector
t=0:1/256:512-1/256;
%preparing a random data vector
data_vec_sin=sign(randn(1,512));
data_vec_cos=sign(randn(1,512));
figure(1);
subplot(2,1,1);stem(data_vec_sin);title('sin stream data');
subplot(2,1,2);stem(data_vec_cos);title('cos stream data');
figure(2);
stem(data_vec_sin(1:8));title('sample data in the sin stream zoomed in');
%creating a frequency domain vector
data_freq_domain=data_vec_sin+(i.*data_vec_cos);
figure(3);
polar(angle(data_freq_domain),abs(data_freq_domain),'-ro');
title('polar plot of the complex freq domain vector');
%converting to time domain
time_domain_vec=ifft(data_freq_domain);
figure(4);
subplot(2,1,1);stem(real(time_domain_vec));
title('time domain data - real part');
subplot(2,1,2);stem(imag(time_domain_vec));
title('time domain data - imag part');
figure(5);
polar(angle(time_domain_vec),abs(time_domain_vec),'-ro');
title('polar plot of the complex time domain vector');
%upsampling the time domain vector
upsamp_vector_sin=[];
upsamp_vector_cos=[];
for m=1:512
rel=real(time_domain_vec(m));
img=imag(time_domain_vec(m));
upsamp_vector_sin=[upsamp_vector_sin repmat(rel,1,256)];
upsamp_vector_cos=[upsamp_vector_cos repmat(img,1,256)];
end
figure(6);
subplot(2,2,1);stem(real(time_domain_vec(1:4)));title('4 real bits');
subplot(2,2,3);stem(imag(time_domain_vec(1:4)));title('4 imag bits');
subplot(2,2,2);stem(upsamp_vector_sin(1:1024));title('4 sin bits');
subplot(2,2,4);stem(upsamp_vector_cos(1:1024));title('4 cos bits');
%preparing the sin and cos carriers
sin_str=sin(2*pi*4*t);
cos_str=cos(2*pi*4*t);
figure(7);
subplot(2,1,1);plot(sin_str(1:256),'-ro');
title('sin wave data - modulates the real part');
subplot(2,1,2);plot(cos_str(1:256),'-ro');
title('cos wave data - modulates the imag part');
%creating the transmitted stream
trans_str=upsamp_vector_sin.*sin_str+upsamp_vector_cos.*cos_str;
figure(8);
plot(trans_str(1:1024),'-bo');
title('transmitted vector for 4 symbol lengths');
%recovering the transmitted data
recov_sin_str=trans_str.*sin_str;
recov_cos_str=trans_str.*cos_str;
figure(9);
subplot(2,1,1);plot(recov_sin_str(1:1024),'-ro');
title('recovered time domain data - real part 4 symbols');
subplot(2,1,2);plot(recov_cos_str(1:1024),'-ro');
title('recovered time domain data - imag part 4 symbols');
%create a simple low pass filter to
%remove the 2f term
filt_2f=1/32.*ones(1,32);
figure(10);
stem(filt_2f);
%filtering the recovered streams
filt_out_sin=filter(filt_2f,1,recov_sin_str);
filt_out_cos=filter(filt_2f,1,recov_cos_str);
figure(11);
subplot(2,1,1);plot(recov_sin_str(1:1024));hold on;plot(filt_out_sin(1:1024),'-r');
title('recovered time domain data - real part 4 symbols');
subplot(2,1,2);plot(recov_cos_str(1:1024));hold on;plot(filt_out_cos(1:1024),'-r');
title('recovered time domain data - imag part 4 symbols');
%recovered bits
data_out_sin=[];
data_out_cos=[];
%getting back encoded data
for m=1:512
t=mean(filt_out_sin((m-1)*256+1:m*256));
data_out_sin=[data_out_sin t];
t=mean(filt_out_cos((m-1)*256+1:m*256));
data_out_cos=[data_out_cos t];
end
%recreating the time domain vector
recov_time_dom_data=2*(data_out_sin+(i.*data_out_cos));
figure(12);
subplot(2,2,1);stem(real(recov_time_dom_data(1:4)));title('4 recovered real bits');
subplot(2,2,3);stem(imag(recov_time_dom_data(1:4)));title('4 recovered imag bits');
subplot(2,2,2);stem(recov_time_dom_data);title('recovered sin stream');
subplot(2,2,4);stem(recov_time_dom_data);title('recovered cos stream');
%recreating the freq domain data
recov_freq_domain_data=fft(recov_time_dom_data);
figure(13);
subplot(2,1,1);stem(real(recov_freq_domain_data));
title('time domain data - real part');
subplot(2,1,2);stem(imag(recov_freq_domain_data));
title('time domain data - imag part');
%recovered sin and cos stream data
sin_str_decoded=sign(real(recov_freq_domain_data));
cos_str_decoded=sign(imag(recov_freq_domain_data));
figure(14);
subplot(2,1,1);stem(sin_str_decoded);title('recovered sin stream data');
subplot(2,1,2);stem(cos_str_decoded);title('recovered cos stream data');
figure(15);
stem(sin_str_decoded(1:8));title('recovered sample data in the sin stream zoomed in');
%taking the difference between the bits sent and recieved
find(data_vec_sin-sin_str_decoded)
find(data_vec_cos-cos_str_decoded)
%this proves ofdm recovery