clear
clc
%ofdm transmitter & receiver downlink with channel estimation using pilots
%and based on least square then interpolation
%--------------------------------------------------------------------------
%size of ifft =128 .
%pilots per one ofdm symbol =16 pilot
%determin pilot symbol location
pilot_location=1:8:128;
%generate data (binary random 1s 0s)
user_bits=de2bi(randint(1,4*512));%we will test for 4 ofdm symbols
%here we assume that we already put the pilot bits that is known to
%receiver
h=modem.qammod('M',16 ,'InputType','Bit');%generate 16 qam modulator
v=modem.qamdemod('M',16 ,'outputType','Bit');%generate 16 qam demodulator
user_output_bits=zeros(1,4*512);
for i=1:4 %doing for loop for 4 ofdm symbol
user_mod_data=modulate(h,user_bits(i*512-511:i*512));%modulate each ofdm symbol for user1
%this modulation data included 16 pilot symbol that the receiver is known
%it so we can extract it
subcarriers_ofdm_symbol_data=user_mod_data;
pilot_symbols=subcarriers_ofdm_symbol_data(pilot_location);
ifft_data=ifft(subcarriers_ofdm_symbol_data);
%adding cyclic prefix = size ifft /4 to make transmitted data circular
%(periodic ) to cinvert linear convolution into circular convolution
cyclic_prefix=ifft_data(end-31:end);
tx_data=[cyclic_prefix;ifft_data];
%passing data through channel assume rayleigh channel with noise and we can apply then for any channel and make channel estimation
rayleigh_channel=rayleighchan(1/(15000*128),1000); % Rayleigh channel
data_channel_passed=filter(rayleigh_channel,tx_data);
rx_data=awgn(data_channel_passed,20,'measured');%adding noise with certain snr
%removing cyclic prefix
rx_data_without_prefix=rx_data(33:end);
%passing signal through fft block
fft_data=fft(rx_data_without_prefix);
%mapping data of certain user: here we assume that we are interested in
%first user data
user_received_data=fft_data(1:128);
%channel estimation in f-domain using pilots symbols
channel_ls=fft(user_received_data(pilot_location))./fft(pilot_symbols);
%making linear interpolation to get channel impulse response for all received
%symbols
estimated_channel=interp1(pilot_location,channel_ls,1:128,'linear','extrap');%in f-domain
estimated_channel=estimated_channel';
%equalize user1 data after making channel estimation
user_equalized_data_fdomain=fft(user_received_data)./estimated_channel;
user_equalized_data=ifft(user_equalized_data_fdomain);
user_output_bits(i*512-511:i*512)=demodulate(v,user_equalized_data);
end
user_output_bits=user_output_bits';
ber=sum(xor(user_output_bits,user_bits))/(4*512)