⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainofdm_sc.m

📁 ofdm matlab code
💻 M
字号:
% Program 4-1
% ofdm.m
%
% Simulation program to realize OFDM transmission system
%
% programmed by T.Yamamura and H.Harada
%
%时间函数
%用于 仿真schmidl&cox算法
%   问题:
%   1.该算法中,由于出现了平台效应,导致了符号定时会在一个窗口内随机出现,窗口宽度等于保护间隔减去信道冲击响应.而,在后面的分数频偏估计时,需要用到
%   定时估计的结果,如果定时估计的误差过大的话,对后面的频偏估计的影响非常的大
%function [derr,ferr]=ofdmoffset_scF(ebn0,gilen)
function [derr]=ofdmoffset_scF(ebn0,gilen)
%function [dv,ds]=ofdmoffset_scF(ebn0,gilen)
%********************** preparation part ***************************

para=256;   % Number of parallel channel to transmit (points)
fftlen=256; % FFT length
noc=256;    % Number of carrier
nd=6;       % Number of information OFDM symbol for one loop
ml=2;       % Modulation level : QPSK
sr=250000;  % Symbol rate
br=sr.*ml;  % Bit rate per carrier
gilen=0;   % Length of guard interval (points)
fullen=gilen+para;  %ofdm符号总长
ebn0=10;     %Eb/N0
snr=10^(ebn0/10);

deltad=10;  %时延
deltaf=20.25;  %频偏
deltaf=0;
deltaderr=0;%时延均方误差
deltaferr=0;%频偏均方误差
deltadv=0;
deltads=0;



%************************** main loop part **************************

nloop=100;  % Number of simulation loops

noe = 0;    % Number of error data
nod = 0;    % Number of transmitted data
nop=0;      % Number of transmitted packet

for iii=1:nloop;

%************************** transmitter *********************************

%************************** Data generation **************************** 

%fbconnection=[1 0 0 0 0 0 1 ];
%pn=m_sequence(fbconnection);
%pn=[0 pn];
pn = rand(1,para)>0.5;
pn = reshape(pn,para/2,2);
[ipn0,qpn0] = qpskmod(pn,para/2,1,ml);
kmod=1/sqrt(2); %  sqrt : built in function
ipn0=ipn0.*kmod;
qpn0=qpn0.*kmod;
sym = ipn0 + i*qpn0;
symbuf = zeros(para,1);
symbuf(1:2:para,1) = sym;
train1 = symbuf*7;                                %为什么乘以7
%train1 = symbuf;
clear sym;
clear symbuf;

%fbconnection=[1 0 0 0 1 0 0 ];
%pn1=m_sequence(fbconnection);
%pn1=[0 pn1];
pn1 = rand(1,para)>0.5;
pn1 = reshape(pn1,para/2,2);
[ipn1,qpn1] = qpskmod(pn1,para/2,1,ml);
kmod=1/sqrt(2); %  sqrt : built in function
ipn1=ipn1.*kmod;
qpn1=qpn1.*kmod;
sym = ipn1 + i*qpn1;
symbuf = zeros(para,1);
symbuf(1:2:para,1) = sym;
clear sym;


%fbconnection=[1 0 0 0 1 1 1 ];
%pn2=m_sequence(fbconnection);
%pn2=[0 pn2];
pn2 = rand(1,para)>0.5;
pn2 = reshape(pn2,para/2,2);
[ipn2,qpn2] = qpskmod(pn2,para/2,1,ml);
kmod=1/sqrt(2); %  sqrt : built in function
ipn2=ipn2.*kmod;
qpn2=qpn2.*kmod;
sym = ipn2 + i*qpn2;
symbuf(2:2:para,1) = sym;
train2 = symbuf*5;                                 %为什么乘以5
%train2 = symbuf;
clear sym;
clear symbuf;

v = zeros(1,para); 
kpois=1:2:para;
v(kpois) = sqrt(2)*train2(kpois)./train1(kpois); % 含有 训练序列对应子载波的相位差信息




seldata=rand(1,para*nd*ml)>0.5;  %  rand : built in function

%****************** Serial to parallel conversion ***********************

paradata=reshape(seldata,para,nd*ml); %  reshape : built in function

%************************** QPSK modulation ***************************** 

[ich,qch]=qpskmod(paradata,para,nd,ml);
kmod=1/sqrt(2); %  sqrt : built in function
ich1=ich.*kmod;
qch1=qch.*kmod;

%******************* IFFT ************************

x=ich1+qch1.*i;
x(:,1) = train1;
x(:,2) = train2;
y=ifft(x);      %  ifft : built in function
ich2=real(y);   %  real : built in function
qch2=imag(y);   %  imag : built in function

%********* Gurad interval insertion **********

[ich3,qch3]= giins(ich2,qch2,fftlen,gilen,nd);
fftlen2=fftlen+gilen;

%********* Attenuation Calculation *********

spow=sum(ich3.^2+qch3.^2)/nd./para;  %  sum : built in function
attn=0.5*spow*sr/br*10.^(-ebn0/10);
attn=sqrt(attn);

%***************************  Receiver  *****************************

%*************************加频率偏移和时延***************************

[ich3,qch3]=delay(ich3,qch3,length(ich3),deltad);
y=ich3+qch3.*i;
for k=1:length(ich3);
    y(k)=y(k)*exp(i*2*pi*deltaf*k/para);
end
ich3=real(y);
qch3=imag(y);

%***************** AWGN addition ********* 

[ich4,qch4]=comb(ich3,qch3,attn);
y=ich3+qch3.*i;


%*****************频率偏移和时延估计*********************************

for d=1:para
        for m=1:para/2
           z1(m)=conj(y(d+m))*y(d+m+para/2);
        end
      p1(d)=sum(z1);
        for n=1:para/2
           z2(n)=abs(y(d+n+para/2)).^2;
        end
        p2(d)=sum(z2);
 end
p=(abs(p1).^2)./(p2.^2);
%plot(abs(p));
%grid on;
%a1=p(10);
a1=max(p); % 求最大值
%a2=0.9*a1;
a3=find(p==a1);% 求最大值所在位置
a4=sum(a3);
a5=length(a3);
q=a4/a5;
%a3
q
%f=angle(p1(m))/pi;



%********************同步估计均方误差计算***********************
%deltadv=deltadv+(a1-1)^2;
%deltads=deltads+a1;
deltaderr=deltaderr+(q-deltad)^2;
%deltaferr=deltaferr+(f-0.25)^2;
    
end

% derr=sqrt(deltaderr/(nloop*para*nd*ml));
% ferr=sqrt(deltaferr/(nloop*para*nd*ml));
%dv=deltadv/nloop;
%ds=deltads/nloop;
derr=deltaderr/nloop;
%ferr=deltaferr/(nloop);


















% derr=sqrt(deltaderr/(nloop*para*nd*ml));
% ferr=sqrt(deltaferr/(nloop*para*nd*ml));




% %****************** Guard interval removal *********
% 
% [ich5,qch5]= girem(ich4,qch4,fftlen2,gilen,nd);
% 
% %******************  FFT  ******************
% 
% rx=ich5+qch5.*i;
% ry=fft(rx);   	% fft : built in function
% ich6=real(ry);	% real : built in function
% qch6=imag(ry);	% imag : built in function
% 
% %***************** demoduration *******************
% 
% ich7=ich6./kmod;
% qch7=qch6./kmod;
% [demodata]=qpskdemod(ich7,qch7,para,nd,ml);   
% 
% %**************  Parallel to serial conversion  *****************
% 
% demodata1=reshape(demodata,1,para*nd*ml);
% 
% % %************************** Bit Error Rate (BER) ****************************
% 
% % instantaneous number of error and data
% 
% noe2=sum(abs(demodata1-seldata));  %  sum : built in function
% nod2=length(seldata);  %  length : built in function
% 
% % cumulative the number of error and data in noe and nod
% 
% noe=noe+noe2;
% nod=nod+nod2;
% 
% % calculating PER
% 
% if noe2~=0  
%    eop=eop+1;
% else
%    eop=eop;
% end   
%    eop;
%    nop=nop+1;
%    
% 
% fprintf('%d\t%e\t%d\n',iii,noe2/nod2,eop);  %  fprintf : built in function
%    
% end
% 
% %********************** Output result ***************************
% 
% per=eop/nop;
% ber=noe/nod;
% 
% fprintf('%f\t%e\t%e\t%d\t\n',ebn0,ber,per,nloop);
% fid = fopen('BERofdm.dat','a');
% fprintf(fid,'%f\t%e\t%e\t%d\t\n',ebn0,ber,per,nloop);
% fclose(fid);
% 
% %******************** end of file ***************************

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -