clear all;
close all;
carrier_count=200;%子载波数
symbols_per_carrier=10;%每子载波含符号数
bits_per_symbol=4;%每符号含比特数,16QAM调制
IFFT_bin_length=512;%FFT点数
PrefixRatio=1/4;%保护间隔与OFDM数据的比例 1/6~1/4
GI=PrefixRatio*IFFT_bin_length ;%每一个OFDM符号添加的循环前缀长度为1/4*IFFT_bin_length 即保护间隔长度为128
beta=1/32;%窗函数滚降系数
GIP=beta*(IFFT_bin_length+GI);%循环后缀的长度20
SNR=15; %信噪比dB
%================信号产生===================================
baseband_out_length = carrier_count * symbols_per_carrier * bits_per_symbol;%所输入的比特数目
carriers = (1:carrier_count) + (floor(IFFT_bin_length/4) - floor(carrier_count/2));%共轭对称子载波映射 复数数据对应的IFFT点坐标
conjugate_carriers = IFFT_bin_length - carriers + 2;%共轭对称子载波映射 共轭复数对应的IFFT点坐标
rand(1,carrier_count*symbols_per_carrier*4);
% rand( 'twister',0);
baseband_out=round(rand(1,baseband_out_length));%输出待调制的二进制比特流
%==============16QAM调制====================================
complex_carrier_matrix=qam16(baseband_out);%列向量
complex_carrier_matrix=reshape(complex_carrier_matrix',carrier_count,symbols_per_carrier)';%symbols_per_carrier*carrier_count 矩阵
%=================IFFT===========================
IFFT_modulation=zeros(symbols_per_carrier,IFFT_bin_length);%添0组成IFFT_bin_length IFFT 运算
IFFT_modulation(:,carriers ) = complex_carrier_matrix ;%未添加导频信号 ,子载波映射在此处
IFFT_modulation(:,conjugate_carriers ) = conj(complex_carrier_matrix);%共轭复数映射
%=================================================================
signal_after_IFFT=ifft(IFFT_modulation,IFFT_bin_length,2);%OFDM调制 即IFFT变换
time_wave_matrix =signal_after_IFFT;%时域波形矩阵,行为每载波所含符号数,列ITTF点数,N个子载波映射在其内,每一行即为个OFDM符号
%=====================添加循环前缀与后缀====================================
XX=zeros(symbols_per_carrier,IFFT_bin_length+GI+GIP);
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符号长度为IFFT_bin_length+GI+GIP=660
%==============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)';%加窗 升余弦窗
end
%========================生成发送信号,并串变换==================================================
windowed_Tx_data=zeros(1,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(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);%加窗后 循环前缀与后缀不叠加 发送总位数
%=================未加窗发送信号频谱==================================
symbols_per_average = ceil(symbols_per_carrier/5);%符号数的1/5,10行%2
avg_temp_time = (IFFT_bin_length+GI+GIP)*symbols_per_average;%点数,10行数据,10个符号%1320
averages = floor(temp_time1/avg_temp_time);%5
average_fft(1:avg_temp_time) = 0;%分成5段
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));%将发送信号分段求频谱
average_fft = average_fft + (subset_ofdm_f/averages);%总共的数据分为5段,分段进行FFT,平均相加
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)%归一化 0/avg_temp_time : (avg_temp_time-1)/avg_temp_time
hold on
grid on
ylabel('Magnitude (dB)')
xlabel('归一化功率')
title('OFDM信号功率谱密度')
%===============加窗的发送信号频谱=================================
symbols_per_average = ceil(symbols_per_carrier/5);%符号数的1/5,10行
avg_temp_time = (IFFT_bin_length+GI+GIP)*symbols_per_average;%点数,10行数据,10个符号
averages = floor(temp_time1/avg_temp_time);
average_fft(1:avg_temp_time) = 0;%分成5段
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));%分段求频谱
average_fft = average_fft + (subset_ofdm_f/averages);%总共的数据分为5段,分段进行FFT,平均相加
end
average_fft_log = 20*log10(average_fft);
subplot(2,1,2)
plot((0:(avg_temp_time-1))/avg_temp_time, average_fft_log)%归一化 0/avg_temp_time : (avg_temp_time-1)/avg_temp_time
hold on
grid on
ylabel('Magnitude (dB)')
xlabel('归一化功率 ')
title('升余弦窗函数对功率谱密度的影响')