clear all
close all
carrier_count=200; %这个程序中OFDM子载波个数为512,其中400为数据符号,其余赋0值
symbols_per_carrier=20;%每个子载波上的符号数,即OFDM符号的个数
bist_per_symbol=4; %OFDM符号的每个子载波上传输的比特数,4bit采用16QAM调制
IFFT_bin_length=512; %FFT长度,即一个OFDM符号的子载波个数
PrefixRatio=1/4; %循环前缀的比值
GI= PrefixRatio*IFFT_bin_length; %保护间隔的长度,128
beta=1/32; %升余弦窗的滚降系数
GIP=beta*(IFFT_bin_length+GI); %循环后缀的长度,20
SNR=30; %考虑到加性高斯白噪声信道,信噪比为30dB
%====================OFDM信号的产生====================
baseband_out_length=carrier_count*symbols_per_carrier*bist_per_symbol;
%传输数据总的比特数,200*20*4bit=16000bit
%20个OFDM符号,每个OFDM符号200个子载波,每个子载波传播4bit信息
carriers=(1:carrier_count)+(floor(IFFT_bin_length/4)-floor(carrier_count/2));
%OFDM符号子载波的序号,carriers中存放的序号是29-228
conjugate_carriers=IFFT_bin_length-carriers+2;
%OFDM符号子载波的序号,conjugate_carriers中存放的序号是282-481
rand('twister',0);
baseband_out=round(rand(1,baseband_out_length));
%产生16000bit待传输的二进制比特流
%===================16QAM调制并绘制星座图=================
complex_carrier_matrix=qam16(baseband_out); %调用子程序qam16进行16QAM调制
%将baseband_out中的二进制比特流,每4bit转换成一个16QAM信号,即-3-3j、-3+3j、
%3-3j、3+3j、-1-3j、-1+3j、1-3j、1+3j、-3-j、-3+j、3-j、3+j、-1-j、-1+j、
%1-j、1+j中一个,转换后complex_carrier_matrix为1*4000矩阵
complex_carrier_matrix=reshape(complex_carrier_matrix',carrier_count,symbols_per_carrier)';
%转换complex_carrier_matrix中的数据为carrier_count*symbols_per_carrier矩阵(20*200矩阵)
figure(1);
plot(complex_carrier_matrix,'*r') %绘制16QAM星座图
axis([-4,4,-4,4]);
title('16QAM调制后星座图');
grid on
%======================IFFT(OFDM调制)=====================
IFFT_modulation=zeros(symbols_per_carrier,IFFT_bin_length);
%将20*512矩阵赋0值,512是IFFT的长度,也是OFDM符号的子载波个数
IFFT_modulation(:,carriers)=complex_carrier_matrix;
%赋值给IFFT_modulation的第29-228列,即给512个子载波中的29-228个子载波赋值
IFFT_modulation(:,conjugate_carriers)=conj(complex_carrier_matrix);
%赋值给512个子载波中的第282-481个子载波
signal_after_IFFT=ifft(IFFT_modulation,IFFT_bin_length,2); %IFFT实现OFDM调制
time_wave_matrix=signal_after_IFFT;
figure(2);
plot(0:IFFT_bin_length-1,time_wave_matrix(2,:)); %画一个OFDM信号的时域表现
axis([0,512,-0.4,0.4]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal,One Symbol Period');
%=====================添加循环前缀与循环后缀==================
XX=zeros(symbols_per_carrier,IFFT_bin_length+GI+GIP); %OFDM、循环前缀、循环后缀长度之和
for k=1:symbols_per_carrier
for i=1:IFFT_bin_length
XX(k,i+GI)=signal_after_IFFT(k,i);
end
for i=1:GI
XX(k,i)=signal_after_IFFT(k,i+IFFT_bin_length-GI); %添加循环前缀
end
for j=1:GIP
XX(k,IFFT_bin_length+GI+j)=signal_after_IFFT(k,j); %添加循环后缀
end
end
time_wave_matrix_cp=XX; %带循环前缀与循环后缀的OFDM符号
figure(3);
plot(0:length(time_wave_matrix_cp)-1,time_wave_matrix_cp(2,:));
%画带循环前缀与循环后缀的OFDM信号的时域波形
axis([0,600,-0.3,0.3]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal with CP,One Symbol Period');
%===============================OFDM符号加窗===================================
windowed_time_wave_matrix_cp=zeros(1,IFFT_bin_length+GI+GIP);
for i=1:symbols_per_carrier
windowed_time_wave_matrix_cp(i,:)=real(time_wave_matrix_cp(i,:)).*rcoswindow(beta,IFFT_bin_length+GI)';
%调用rcoswindow产生升余弦窗,对带循环前缀与循环后缀的OFDM符号加窗
end
figure(4);
plot(0:IFFT_bin_length-1+GI+GIP, windowed_time_wave_matrix_cp(2,:)); %画加窗后的OFDM符号
axis([0,700,-0.2,0.2]);
grid on;
ylabel('Amplitude');
xlabel('Time');
title('OFDM Time Signal Apply a Window,One Symbol Period');
%==============生成发送信号,并串转换==========================
windowed_Tx_data=zeros(1,symbols_per_carrier*(IFFT_bin_length+GI)+GIP);
%并串转换后数据的长度为symbols_per_carrier*(IFFT_bin_length+GI)+GIP,这里考虑到了循环前缀和后缀的重叠相加
windowed_Tx_data(1:IFFT_bin_length+GI+GIP)=windowed_time_wave_matrix_cp(1,:); %发送串行数据
for i=1:symbols_per_carrier-1
windowed_Tx_data((IFFT_bin_length+GI)*i+1:(IFFT_bin_length+GI)*(i+1)+GIP)=windowed_time_wave_matrix_cp(i+1,:);
%并串转换
end
Tx_data_withoutwindow=reshape(windowed_time_wave_matrix_cp',(symbols_per_carrier)*(IFFT_bin_length+GI+GIP),1)';
%不加窗数据并串转换
Tx_data=reshape(windowed_time_wave_matrix_cp',(symbols_per_carrier)*(IFFT_bin_length+GI+GIP),1)';
%加窗数据,按照循环前缀与循环后缀不重叠相加进行并串转换
temp_time1=(symbols_per_carrier)*(IFFT_bin_length+GI+GIP);
%加窗,循环前缀与循环后缀不重叠数据长度,即发送的总数据比特数
figure(5)
subplot(2,1,1);
plot(0:temp_time1-1,Tx_data); %画循环前缀与后缀不重叠相加OFDM信号的时域波形
grid on
ylabel('Amplitude(volts)');
xlabel('Time(samples)');
title('OFDM Time Signal')
temp_time2=symbols_per_carrier*(IFFT_bin_length+GI)+GIP; %加窗,循环前缀与循环后缀重叠相加数据长度
subplot(2,1,2);
plot(0:temp_time2-1,windowed_Tx_data); %画循环前缀与后缀重叠相加OFDM信号的时域波形
grid on
ylabel('Amplitude(volts)');
xlabel('Time(samples)');
title('OFDM Time Signal')
%==============对发送数据分段,分段计算频谱,取平均作为OFDM信号的频谱=========================
symbols_per_average=ceil(symbols_per_carrier/5);
avg_temp_time=(IFFT_bin_length+GI+GIP)*symbols_per_average;
averages=floor(temp_time1/avg_temp_time);%将发送数据分成5段,每段数据长度为avg_temp_time
average_fft(1:avg_temp_time)=0; %存放平均后的OFDM信号的谱,先置零
for a=0:(averages-1)
subset_ofdm=Tx_data_withoutwindow(((a*avg_temp_time)+1):((a+1)*avg_temp_time)); %分段
subset_ofdm_f=abs(fft(subset_ofdm)); %对分段后的OFDM信号计算频谱
average_fft=average_fft+(subset_ofdm_f/averages); %求平均
end
average_fft_log=20*log10(average_fft);%求对数平均谱
figure(6)
subplot(2,1,1)
plot((0:(avg_temp_time-1))/avg_temp_time,average_fft_log);
%画未加窗OFDM符号对数平均谱
hold on
grid on
axis([0 0.5 -20 max(average_fft_log)])
ylabel('Magnitude(dB)')
xlabel('Normalized Frequency(0.5=fs/2)')
title('OFDM Signal Spectrum')
%===================计算加窗OFDM信号频谱=====================
symbols_per_average=ceil(symbols_per_carrier/5);
avg_temp_time=(IFFT_bin_length+GI+GIP)*symbols_per_average;
averages=floor(temp_time1/avg_temp_time);%将发送数据分成5段,每段数据长度为avg_temp_time
average_fft(1:avg_temp_time)=0; %存放平均后的OFDM信号的谱,先置零
for a=0:(averages-1)
subset_ofdm=Tx_data(((a*avg_temp_time)+1):((a+1)*avg_temp_time)); %分段
subset_ofdm_f=abs(fft(subset_ofdm)); %对分段后的OFDM信号计算频谱
average_fft=average_fft+(subset_ofdm_f/averages); %求平均
end
average_fft_log=20*log10(average_fft);%求对数平均谱
subplot(2,1,2);
plot((0:(avg_temp_time-1))/avg_temp_time,average_fft_log)
%画加窗OFDM符号对数平均谱
hold on
grid on
axis([0 0.5 -20 max(average_fft_log)])
ylabel('Magnitude(dB)')
xlabel('Normalized Frequency(0.5=fs/2)')
title('Windowed OFDM Signal Spectrum')
%=======================经过加性高斯白噪声信道=========================
Tx_signal_power=var(windowed_Tx_data); %计算信号功率
linear_SNR=10^(SNR/10); %转换对数信噪比为线性幅度值
noise_sigma=Tx_signal_power/linear_SNR; %计算信噪功率,即方差
noise_scale_factor=sqrt(noise_sigma); %计算标准差
noise=randn(1,((symbols_per_carrier)*(IFFT_bin_length+GI))+GIP)*noise_scale_factor;
%产生功率为noise_scale_factor高斯噪声
Rx_data=windowed_Tx_data+noise;%在发送数据上加噪声,相当于OFDM信号经过加性高斯白噪声信道
%=====================OFDM信号解调========================================
Rx_data_matrix=zeros(symbols_per_carrier,IFFT_bin_length+GI+GIP);
%存放并行的接收数据
for i=1:symbols_per_carrier
Rx_data_matrix(i,:)= Rx_data(1,(i-1)*(IFFT_bin_length+GI)+
评论1