clear;
% hold on;
para=768;% number of used subcarriers
fftlen=1024;% fft length
noc=1024;% number of subcarriers
nd=36;% number of information ofdm symbol for one loop
np=4;% number of pilot symbol
nloop=2;% number of simulation loops
%%%%%%%%%%%%%%%%%%导频符号%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
pilot_symbol = [1-j, 1-j, -1-j, 1+j, 1-j, 1-j, -1+j, 1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1-j, 1-j, -1-j, 1+j, 1-j, 1-j, -1+j, 1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1-j, 1-j, -1-j, 1+j, 1-j, 1-j, -1+j, 1-j, 1-j, 1-j, 1+j, -1-j, 1+j, 1+j, -1-j, 1+j, -1-j, -1-j, 1-j, -1+j, 1+j, 1+j, 1-j, -1+j, 1+j, 1+j, -1-j, 1+j, 1+j, 1+j, -1+j, 1-j, -1+j, -1+j, 1-j, -1+j, 1-j, 1-j,1+j, -1-j, -1-j, -1-j, -1+j, 1-j, -1-j, -1-j, 1+j, -1-j, -1-j, -1-j, 1-j, -1+j, 1-j, 1-j, -1+j, 1-j, -1+j,-1+j, -1-j, 1+j, -1-j, 1+j, -1+j, -1+j, -1-j, 1+j, 1+j, 1+j, -1-j, 1+j, 1-j, 1-j, 1-j, -1+j, -1+j, -1+j, -1+j, 1-j, -1-j, -1-j, -1+j, 1-j, 1+j, 1+j, -1+j, 1-j, 1-j, 1-j, -1+j, 1-j, -1-j, -1-j, -1-j, 1+j,1+j, 1+j, 1+j, -1-j, -1+j, -1+j, 1+j, -1-j, 1-j, 1-j, 1+j, -1-j, -1-j, -1-j, 1+j, -1-j, -1+j, -1+j, -1+j, 1-j, 1-j, 1-j, 1-j, -1+j, 1+j, 1+j, -1-j, 1+j, -1+j, -1+j, -1-j, 1+j, 1+j, 1+j, -1-j, 1+j, 1-j, 1-j, 1-j, -1+j, -1+j, -1+j, -1+j, 1-j, -1-j, -1-j, 1-j, -1+j, -1-j, -1-j, 1-j, -1+j, -1+j, -1+j, 1-j, -1+j,1+j, 1+j, 1+j, -1-j, -1-j, -1-j, -1-j, 1+j, 1-j, 1-j];
pilot_symbol = [pilot_symbol, pilot_symbol];
pilot_symbol = pilot_symbol(1:384);
pilot_symbol = [pilot_symbol, pilot_symbol];
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
delay=[0 2 4 6];
amp=[0 -2 -10 -20];
ml=2;% modulation level:QPSK
sr=15625;% ofdm symbol rate
br=sr.*ml;% bit rate per carrier
gilen=128;% length of guard interval
k=0:10;
for l=1:length(k)
ebn0=k(l);
% ebn0 = 10;
noe=0;% number oferror data
nod=0;% number of transmitted data
eop=0;% number of error packet
nop=0;% number of transmitted packet
noe2=0;
for iii=1:nloop
seridata=randint(1,para*(nd-np));
%%%%%%%%%%%%%%%%%%%%channel code%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
conved_bit0=zeros(1,para*(nd-np)*2);
for n=1:(nd-np)
conved_bit0(1,(n-1)*para*2+1:n*para*2)= co_dec(seridata(1,(n-1)*para+1:n*para),1);%%%%%%卷积编码%%%%%%
conved_bit(1,(n-1)*para*2+1:n*para*2)=inter_sc(conved_bit0(1,(n-1)*para*2+1:n*para*2),1);%%%%%%%%%%%%交织%%%%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
paradata=reshape(conved_bit,para,(nd-np)*ml);
a=zeros(para,nd*ml);
a(:,3:18)=paradata(:,1:16);
a(:,21:36)=paradata(:,17:32);
a(:,39:54)=paradata(:,33:48);
a(:,57:72)=paradata(:,49:64);
[ich,qch]=qpskmod(a,para,nd,ml);
%%%%%%%%%%%%%%%%%%%%%插入导频%%%%%%%%%%%%%%%%%
[ich,qch]=add_pilot(pilot_symbol,nd,ich,qch);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
kmod=1/sqrt(2);
ich=ich.*kmod;
qch=qch.*kmod;
%%%%%%%%%%%%%%%映射%%%%%%%%%%%%%%%%%%%%%%%%%%
[ich1,qch1]=crmapping(ich,qch,fftlen,nd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
x=ich1+qch1.*i;
y=ifft(x);
ich2=real(y);
qch2=imag(y);
%%%%%%%%%%%%%%%%%插入保护间隔%%%%%%%%%%%%%%%%%%%%%
fftlen2=fftlen+gilen;
[ich4,qch4]=giins(ich2,qch2,fftlen,gilen,nd);
tx_data=ich4+qch4*i;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%上采样和滤波%%%%%%%%%%%%%%%%%%%%%%%%%
coeff = [ 0 1 0 -1 0 2 1 -3 -1 3 2 -4 -4 5 6 -6 -9 6 13 -6 -18 5 24 -2 -31 -3 41 11 -53 -25 70 53 -99 -122 160 498 498 160 -122 -99 53 70 -25 -53 11 41 -3 -31 -2 24 5 -18 -6 13 6 -9 -6 6 5 -4 -4 2 3 -1 -3 1 2 0 -1 0 1 0];
% data_up2 = upsample(tx_data,2);
data_up2 = zeros(1,2*length(tx_data));
for n=1:length(tx_data)
data_up2(1,2*n-1) = tx_data(1,n);
end
data_filt= conv(data_up2,coeff);
data_filt = data_filt(36:end-36);
%-- CIC
tx_data = cicinterpolator(data_filt,4,1,4);
tx_data = reshape(tx_data,1,length(tx_data));
% tx_data = kron(tx_data,ones(1,4));
% f = [0 0.25*(384/512) 0.35*(384/512) 1];
% amp = [1 1 0 0];
% b = remez(80, f, amp);
% tx_data = conv(b, tx_data);
% tx_data = tx_data(40:end-41);
% % ich4 = real(tx_data);
% % qch4 = imag(tx_data);
%%%%%%%%%%%%%%%%%%%多径信道%%%%%%%%%%%%%%%%%%%%%%%
[ich4,qch4] = multi_path(tx_data, delay, amp);
ich4 = ich4(1:length(tx_data));
qch4 = qch4(1:length(tx_data));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
spow=sum(ich4.^2+qch4.^2)/nd./para;
attn=0.5*spow*sr/br*10.^(-ebn0/10);
attn=sqrt(attn);
%%%%%%%%%%%%%%%加入高斯噪声%%%%%%%%%%%%%%%%%%%%%%%%
[ich5,qch5]=comb(ich4,qch4,attn);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
rx_data = ich5+i*qch5;
%%%%%%%%%%%%%%%%滤波、下采样%%%%%%%%%%%%%%%%%%%%%%%
data_cic = cicdecimation(rx_data,4,1,4);
coeff = [ 0 1 0 -1 0 2 1 -3 -1 3 2 -4 -4 5 6 -6 -9 6 13 -6 -18 5 24 -2 -31 -3 41 11 -53 -25 70 53 -99 -122 160 498 498 160 -122 -99 53 70 -25 -53 11 41 -3 -31 -2 24 5 -18 -6 13 6 -9 -6 6 5 -4 -4 2 3 -1 -3 1 2 0 -1 0 1 0];
data_filt= conv(data_cic,coeff);
data_filt = data_filt(36:end-36);
rx_data = downsample(data_filt,2);
rx_data = reshape(rx_data,1,length(rx_data));
% rx_data = conv(b,rx_data);
% rx_data = rx_data(40:end-41);
% rx_data = rx_data(2:4:end);
ich5 = real(rx_data);
qch5 = imag(rx_data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%消除保护间隔%%%%%%%%%%%%%%%%%%%%%%%%
[ich6,qch6]=girem(ich5,qch5,fftlen2,gilen,nd);
rx=ich6+i*qch6;
ry=fft(rx);
ich7=real(ry);
qch7=imag(ry);
%%%%%%%%%%%%%%%解映射%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[ich8,qch8]=crdemapping(ich7,qch7,fftlen,nd);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
ich9=ich8./kmod;
qch9=qch8./kmod;
%%%%%%%%%%%%%%%信道估计%%%%%%%%%%%%%%%%%%%%%%%%%
[ich10,qch10]=channel_est(ich9,qch9,pilot_symbol);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
[demodata]=qpskdemod(ich10,qch10,para,nd,ml);
%%%%%%%%%%%%%%%%%%%%取出数据%%%%%%%%%%%%%%%%%%%%
c=zeros(para,(nd-np)*ml);
c(:,1:16)=demodata(:,3:18);
c(:,17:32)=demodata(:,21:36);
c(:,33:48)=demodata(:,39:54);
c(:,49:64)=demodata(:,57:72);
demodata0=reshape(c,1,para*(nd-np)*ml);
%%%%%%%%%%%%%%%%%%%%%channel decode%%%%%%%%%%%%%%%
demodatal0=zeros(1,para*(nd-np)*ml);
demodatal=zeros(1,para*(nd-np));
for n=1:(nd-np)
demodatal0(1,(n-1)*para*2+1:n*para*2)=inter_sc(demodata0(1,(n-1)*para*2+1:n*para*2),0);%%%%%%%%%解交织%%%%%%%%%%%%
demodatal(1,para*(n-1)+1:para*n) = co_dec( demodatal0(1,para*(n-1)*2+1:para*n*2),0 );%%%%%%%%%%卷积码解码%%%%%%%
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
noe2=sum(abs(demodatal-seridata));
nod2=length(seridata);
if noe2~=0
eop=eop+1;
else
eop=eop;
end
eop;
nop=nop+1;
noe=noe+noe2;
nod=nod+nod2;
end
per(l)=eop/nop;
ber(l)=noe/nod;
end
semilogy(k,ber,'b-*');
grid on;
xlabel('Eb/N0 (dB)');
ylabel('BER');