# UF-OFDM以及OFDM 传输过程及功率谱密度的仿真

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%----------------------------------------------
%E:\大学课件&科学浏览器\大三下\无线通信\实验\实验一
%----------------------------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

s = rng(211);       % Set RNG state for repeatability
fs = 15000;         %采样频率
dop_fs = 500;       %多普勒频移（Hz）
numFFT = 512;        % FFT点数
subbandSize = 20;    % 子带长度
numSubbands = 10;    % 子载波   numSubbands*subbandSize <= numFFT
subbandOffset = 156; % 频带中心 numFFT/2-subbandSize*numSubbands/2

% 切比雪夫窗
filterLen = 43;      % 滤波器长
slobeAtten = 40;     %  旁瓣衰减（dB）

bitsPerSubCarrier = 4;   % 2代表4QAM, 4代表16QAM, 6代表64QAM, 8代表256QAM
snrdB = 15;              % SNR（dB）

% 设计滤波器（切比雪夫）
prototypeFilter = chebwin(filterLen, slobeAtten);

% QAM调制器
%ModulationOrder星座图中点数，默认为16    BitInput输入，true -- bit ，false -- int
qamMapper = comm.RectangularQAMModulator('ModulationOrder', 2^bitsPerSubCarrier, 'BitInput', true , 'NormalizationMethod', 'Average power');

txSig = complex(zeros(numFFT+filterLen-1, 1));

%%%%%%%%%%%%%%%
%-------------%
% 发送接收模块 %
%-------------%
%%%%%%%%%%%%%%%
% 初始化数组
inpData = zeros(bitsPerSubCarrier*subbandSize, numSubbands);%80*10，10个子带，每个子带80个数据，每一列是一个子带

figure;
axis([-0.5 0.5 -100 20]);
hold on;
grid on

xlabel('归一化频率');
ylabel('功率谱密度(dBW/Hz)')
title(['UFMC, ' num2str(numSubbands) ' Subbands, '  num2str(subbandSize) ' Subcarriers each'])

%  通用滤波多载波调制
for bandIdx = 1:numSubbands
%生成m*n矩阵
bitsIn = randi([0 1], bitsPerSubCarrier*subbandSize, 1);

%阶跃响应，inpData(:, bandIdx)取第bandIdx列
symbolsIn = step(qamMapper,inpData(:, bandIdx));
inpData(:,bandIdx) = bitsIn; % log bits for comparison

%加高斯白噪声，SNR=10
%symbolsIn = awgn(symbolsIn,10);

chan=rayleighchan(1/fs,500);
symbolsIn = filter(chan,symbolsIn);%经过信道后的输出

% 将子带数据打包到ofdm符号中
%计算偏移
offset = subbandOffset+(bandIdx-1)*subbandSize;

%输入信号[0 0 0 ... symbolsIn ... 0 0 0]
symbolsInOFDM1 = [zeros(offset,1); symbolsIn; zeros(numFFT-offset-subbandSize, 1)];

%ifft结果
ifftOut = ifft(ifftshift(symbolsInOFDM1));

% 频移滤波器（对齐中心频率）
bandFilter = prototypeFilter.*exp( 1i*2*pi*(0:filterLen-1)'/numFFT*((bandIdx-1/2)*subbandSize+0.5+subbandOffset+numFFT/2) );

filterOut = conv(bandFilter,ifftOut);

% 画功率谱密度
% periodogram[x,window,nfft,fs]求功率谱密度,2-N点FFT
[psd,f] = periodogram(filterOut, rectwin(length(filterOut)),  numFFT*2, 1, 'centered');

plot(f,10*log10(psd),'Color',[rand(1,1) rand(1,1) rand(1,1)]);

txSig = txSig + filterOut;

end

figure
plot(inpData);
axis([0 40 -0.1 1.1]);
title('发送信号')
hold off;

% 具有相应参数的ofdm调制
figure;
for bandIdx = 1:numSubbands

symbolsIn = step(qamMapper,inpData(:, bandIdx));

symbolsIn = filter(chan,symbolsIn);%经过信道后的输出

offset = subbandOffset;

symbolsInOFDM2 = [zeros(offset, 1); symbolsIn; zeros(numFFT-offset-subbandSize*numSubbands, 1)];

ifftOut = sqrt(numFFT).*ifft(ifftshift(symbolsInOFDM2));

[psd,f] = periodogram(ifftOut, rectwin(length(ifftOut)), numFFT*2, 1, 'centered');

plot(f,10*log10(psd));
end

grid on
axis([-0.3 0.3 -100 20]);
xlabel('归一化频率');
ylabel('功率谱密度(dBW/Hz)')
title(['OFDM, ' num2str(numSubbands*subbandSize) ' Subcarriers'])