%Illustrative example of channel estimation for OFDM
%with BPSK information symbols.  There is no spreading in 
%this code.  Also, I will add noise later.
clear all
clf
set(0,'defaultaxesfontsize',22);
%Nb:  total no. of symbols processed
%I don't have zero padding here, but that's an easy change
%Nfft=256;
Nfft=64;
N=Nfft;
L=8;
Nb=Nfft;
%Nb=Nfft-L;
%
%I make up a random complex channel of length L.
%My multipath delays occur at integer sample times for simplicity.
%
h=randn(1,L)+j*randn(1,L);
%h=zeros(1,L); %then fill in values for sparse channel
%
%Here's where I characterize the FFT of the channel in terms
%as the product of an Nfft x L matrix with a L x 1 vector containing
%the L values of the channel impulse response
%
%generate random BPSK information symbols
%
% br=ones(1,Nb); temp=rand(1,Nb);
% br(find(temp<.5))=-1;
% br(1,Nb+1:Nfft)=zeros(1,Nfft-Nb);
%
Intr=randi(8,Nfft,1);
br=2*(Intr-5)+1; br=br.';
%
%here's where I take the inverse N-point DFT of the data
%
W=dftmtx(Nfft); WI=W'/N;
xmat=br*WI.';
x=ifft(br,Nfft);
normdiff1=norm(x-xmat)
%
%Here's where I add a cyclic prefix equal to the channel length L
%
z=[x(1,end-L+1:end) x];
%
%Here's where I convolve the transmitted data with the channel
%
yt=conv(z,h);
%
%Here's where I throw away L samples at the beginning and 
%L-1 samples at the end and then take the fft
%
y=yt(L+1:end-L+1);
Yf=fft(y,Nfft);
Yfmat=y*W;
normdiff2=norm(Yf-Yfmat)
%
%BinGns=fft([0; h.'],Nfft);
Hf=fft(h,Nfft);
%Here's where I now multiply by the complex conjugate of the
%the channel gains on a per freq bin basis -- THEN slice
%
Z=Yf./Hf;
%Z=Z/Nfft;
%
%BitEsts=ones(Nfft,1);
%BitEsts(find(real(YfMags)<0))=-1;
%we obtain no bit errors when there is no noise
normdiff3=norm(Z-br)
figure(1)
stem(real(Z))
axis([0 Nfft-1 -7 7])
figure(2)
stem(br,'r')
axis([0 Nfft-1 -7 7])
%
w0=pi/8; n=0:156;
x=cos(w0*n);
h=[2 -1 0.5 -0.25];
y=conv(x,h);
figure(3)
%plot(n,x)
plot([0:length(y)-1],y,'Linewidth',3)





