天天看點

ofdm 誤碼率 matlab,OFDM不同信噪比下的誤碼率matlab源碼

clear;

clc;

s = rng(211);            % 設定RNG狀态的可重複性

numFFT = 1024;           % FFT點的數量

numRBs = 50;             % 資源塊的數量

rbSize = 12;             % 每個資源塊的副載波數量

cpLen = 72;              % 樣本中的循環字首長度

bitsPerSubCarrier = 6;   % 2: QPSK, 4: 16QAM, 6: 64QAM, 8: 256QAM

snrdB =30;              % 以dB為機關的SNR

toneOffset = 2.5;        % 音調偏移或超額帶寬(在副載波中)

L = 513;                 % 濾波器長度(=濾波器次序+ 1),奇數

numDataCarriers = numRBs*rbSize;    % 子帶中資料子載波的數量

halfFilt = floor(L/2);

n = -halfFilt:halfFilt;

% Sinc函數原型濾波器

pb = sinc((numDataCarriers+2*toneOffset).*n./numFFT);

% Sinc截斷視窗

w = (0.5*(1+cos(2*pi.*n/(L-1)))).^0.6;

% 歸一化的低通濾波器系數

fnum = (pb.*w)/sum(pb.*w);

% 過濾脈沖響應

h = fvtool(fnum, 'Analysis', 'impulse', ...

'NormalizedFrequency', 'off', 'Fs', 15.36e6);

h.CurrentAxes.XLabel.String = 'Time (\mus)';

h.FigureToolbar = 'off';

% 使用dsp過濾器對象進行過濾

filtTx = dsp.FIRFilter('Structure', 'Direct form symmetric', ...

'Numerator', fnum);

filtRx = clone(filtTx); % Rx的比對過濾器

% QAM符号映射器

qamMapper = comm.RectangularQAMModulator( ...

'ModulationOrder', 2^bitsPerSubCarrier, 'BitInput', true, ...

'NormalizationMethod', 'Average power');

% 為譜圖設定一個數字

hFig = figure('Position', figposition([46 50 30 30]), 'MenuBar', 'none');

axis([-0.5 0.5 -200 -20]);

hold on;

grid on

xlabel('Normalized frequency');

ylabel('PSD (dBW/Hz)')

title(['F-OFDM, ' num2str(numRBs) ' Resource blocks, '  ...

num2str(rbSize) ' Subcarriers each'])

% 生成資料符号

bitsIn = randi([0 1], bitsPerSubCarrier*numDataCarriers, 1);

symbolsIn = qamMapper(bitsIn);

% 将資料打包成OFDM符号

offset = (numFFT-numDataCarriers)/2; % 為通帶中心

symbolsInOFDM = [zeros(offset,1); symbolsIn; ...

zeros(numFFT-offset-numDataCarriers,1)];

ifftOut = ifft(ifftshift(symbolsInOFDM));

%預先加上循環字首

txSigOFDM = [ifftOut(end-cpLen+1:end); ifftOut];

% 使用零填充過濾器來重新整理尾部。 擷取發射信号

txSigFOFDM = filtTx([txSigOFDM; zeros(L-1,1)]);

%繪制功率譜密度(PSD)

[psd,f] = periodogram(txSigFOFDM, rectwin(length(txSigFOFDM)), ...

numFFT*2, 1, 'centered');

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

% 計算峰均功率比(PAPR)

PAPR = comm.CCDF('PAPROutputPort', true, 'PowerUnits', 'dBW');

[~,~,paprFOFDM] = PAPR(txSigFOFDM);

disp(['Peak-to-Average-Power-Ratio for F-OFDM = ' num2str(paprFOFDM) ' dB']);

% 繪制OFDM信号的功率譜密度(PSD)

[psd,f] = periodogram(txSigOFDM, rectwin(length(txSigOFDM)), numFFT*2, ...

1, 'centered');

hFig1 = figure('Position', figposition([46 15 30 30]));

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

grid on

axis([-0.5 0.5 -200 -20]);

xlabel('Normalized frequency');

ylabel('PSD (dBW/Hz)')

title(['OFDM, ' num2str(numRBs*rbSize) ' Subcarriers'])

%計算峰均功率比(PAPR)

PAPR2 = comm.CCDF('PAPROutputPort', true, 'PowerUnits', 'dBW');

[~,~,paprOFDM] = PAPR2(txSigOFDM);

disp(['Peak-to-Average-Power-Ratio for OFDM = ' num2str(paprOFDM) ' dB']);

%添加AWGN和Rayleigh

chan = rayleighchan(0.00001,100);

rxSig = filter(chan,txSigFOFDM);

rxtSig = awgn(rxSig, snrdB, 'measured');

% 接收比對的過濾器,

rxSigFilt = filtRx(rxtSig);

% 考慮過濾器延遲

rxSigFiltSync = rxSigFilt(L:end);

% 删除循環字首

rxSymbol = rxSigFiltSync(cpLen+1:end);

%執行FFT

RxSymbols = fftshift(fft(rxSymbol));

%選擇資料副載波

dataRxSymbols = RxSymbols(offset+(1:numDataCarriers));

% 繪制接收符号星座圖

switch bitsPerSubCarrier

case 2  % QPSK

refConst = qammod((0:3).', 4, 'UnitAveragePower', true);

case 4  % 16QAM

refConst = qammod((0:15).', 16,'UnitAveragePower', true);

case 6  % 64QAM

refConst = qammod((0:63).', 64,'UnitAveragePower', true);

case 8  % 256QAM

refConst = qammod((0:255).', 256,'UnitAveragePower', true);

end

constDiagRx = comm.ConstellationDiagram( ...

'ShowReferenceConstellation', true, ...

'ReferenceConstellation', refConst, ...

'Position', figposition([20 15 30 40]), ...

'MeasurementInterval', length(dataRxSymbols), ...

'Title', 'F-OFDM Demodulated Symbols', ...

'Name', 'F-OFDM Reception', ...

'XLimits', [-1.5 1.5], 'YLimits', [-1.5 1.5]);

constDiagRx(dataRxSymbols);

% 由于沒有通道模組化,是以通道均衡不是必要的

%解映射和BER計算

qamDemod = comm.RectangularQAMDemodulator('ModulationOrder', ...

2^bitsPerSubCarrier, 'BitOutput', true, ...

'NormalizationMethod', 'Average power');

BER = comm.ErrorRate;

% 執行艱難的決策并測量錯誤

rxBits = qamDemod(dataRxSymbols);

ber = BER(bitsIn, rxBits);

disp(['F-OFDM Reception, BER = ' num2str(ber(1)) ' at SNR = ' ...

num2str(snrdB) ' dB']);

%恢複RNG狀态

rng(s);