%code1-----------------------------------------------------------------------------
%the use of cyclic prefixes and preambles in OFDM frequnecy offset
%estimation
%bulent karaoglu,dec 2004,nps
close all
clc
clear
VAR=0.00002; % CASE 1
d=0;
eps=0.13
for w=1:1000 % AVERAGING
S=zeros(1,64);
k=1:6;
S(1+4*k)=[-1-j,-1-j,1+j,1+j,1+j,1+j]; % positive freq
S(1+64-4*k)=[1+j,-1-j,-1-j,1+j,-1-j,1+j]; % negative freq
S=sqrt(13/6)*S;
s=ifft(S);
r_S=[s(1:32),s,s];
Lp=[0,1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,1];
Lm=[1,1,1,1,-1,1,-1,1,1,-1,-1,1,1,1,1,1,1,-1,1,-1,1,1,-1,-1,1,1];
L=[Lp,zeros(1,11),Lm];
ell=ifft(L);
r_L=[ell(33:64),ell,ell]; % Long Preamble
pr=[r_S, r_L];
% MULTIPLY WITH THE OFFSET FREQUENCY
q=0:(length(pr)-1);
profst=pr.*exp(i*eps*q/160)+sqrt(VAR)*randn(1,320); % source
profst1=profst(1:160);
R1=fft(profst1);
profst2=profst(161:320);
R2=fft(profst2);
epsest1(w)=phase(sum(R2.*conj(R1)));
d=1+d;
estimate1(d)=sum(epsest1)/w;
end
VAR=0.0002; % CASE 2
d=0;
eps=0.13
for w=1:1000
S=zeros(1,64);
k=1:6;
S(1+4*k)=[-1-j,-1-j,1+j,1+j,1+j,1+j]; % positive freq
S(1+64-4*k)=[1+j,-1-j,-1-j,1+j,-1-j,1+j]; % negative freq
S=sqrt(13/6)*S
s=ifft(S);
r_S=[s,s,s(1:32)];
Lp=[0,1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,1];
Lm=[1,1,1,1,-1,1,-1,1,1,-1,-1,1,1,1,1,1,1,-1,1,-1,1,1,-1,-1,1,1];
L=[Lp,zeros(1,11),Lm]
ell=ifft(L);
r_L=[ell(33:64),ell,ell]; % Long Preamble
pr=[r_S, r_L];
% MULTIPLY WITH THE OFFSET FREQUENCY
q=0:(length(pr)-1)
profst=pr.*exp(i*eps*q/160)+sqrt(VAR)*randn(1,320); % source
R1=fft(profst1);
profst2=profst(161:320);
R2=fft(profst2);
epsest2(w)=phase(sum(R2.*conj(R1)));
d=1+d;
estimate2(d)=sum(epsest2)/w;
end
VAR=0.0001; % CASE 3
d=0;
eps=0.13
for w=1:1000
S=zeros(1,64);
k=1:6;
S(1+4*k)=[-1-j,-1-j,1+j,1+j,1+j,1+j]; % positive freq
S(1+64-4*k)=[1+j,-1-j,-1-j,1+j,-1-j,1+j]; % negative fre
S=sqrt(13/6)*S;
s=ifft(S);
r_S=[s,s,s(1:32)];
Lp=[0,1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,1];
Lm=[1,1,1,1,-1,1,-1,1,1,-1,-1,1,1,1,1,1,1,-1,1,-1,1,1,-1,-1,1,1];
L=[Lp,zeros(1,11),Lm];
ell=ifft(L);
r_L=[ell(33:64),ell,ell]; % Long Preamble
pr=[r_S, r_L];
% MULTIPLY WITH THE OFFSET FREQUENCY
q=0:(length(pr)-1);
profst=pr.*exp(i*eps*q/160)+sqrt(VAR)*randn(1,320); % source
profst1=profst(1:160);
R1=fft(profst1);
profst2=profst(161:320);
R2=fft(profst2);
epsest3(w)=phase(sum(R2.*conj(R1)));
d=1+d;
estimate3(d)=sum(epsest3)/w;
end
VAR=0.0005; % CASE 4
d=0;
eps=0.13
for w=1:1000
S=zeros(1,64);
k=1:6;
S(1+4*k)=[-1-j,-1-j,1+j,1+j,1+j,1+j]; % positive freq
S(1+64-4*k)=[1+j,-1-j,-1-j,1+j,-1-j,1+j]; % negative freq
S=sqrt(13/6)*S;
s=ifft(S);
r_S=[s,s,s(1:32)];
Lp=[0,1,-1,-1,1,1,-1,1,-1,1,-1,-1,-1,-1,-1,1,1,-1,-1,1,-1,1,-1,1,1,1,1];
Lm=[1,1,1,1,-1,1,-1,1,1,-1,-1,1,1,1,1,1,1,-1,1,-1,1,1,-1,-1,1,1];
L=[Lp,zeros(1,11),Lm];
ell=ifft(L);
r_L=[ell(33:64),ell,ell]; % Long Preamble
pr=[r_S, r_L];
% MULTIPLY WITH THE OFFSET FREQUENCY
q=0:(length(pr)-1);
profst=pr.*exp(i*eps*q/160)+sqrt(VAR)*randn(1,320); % source
profst1=profst(1:160);
R1=fft(profst1);
profst2=profst(161:320);
R2=fft(profst2);
epsest4(w)=phase(sum(R2.*conj(R1)));
d=1+d;
estimate4(d)=sum(epsest4)/w;
end
number_in_avg=1:1000; % AVERAGE
figure(1)
plot(number_in_avg,estimate1,number_in_avg,estimate2,number_in_avg,estimate3,number_in_avg,estimate4)
legend('EB/NO=20 dB','EB/NO=15dB','EB/NO=10dB','EB/NO=5dB')
xlabel('symbol number');ylabel('offset estimations');
grid