%%
% This script allows to choose the frame configuration, and payload data and
% simulates the HDL OFDM Transmitter model. This script compares outputs of
% the model with its floating point equivalent function, 
% 'whdlexamples.OFDMTx' and plots the comparison results.

%% Custom Frame Configuration 

%====================================
% Transmitter parameter configuration
%====================================

% modOrder      = 2, 4, 16, and 64 for BPSK, QPSK, 16QAM, and 64QAM respectively
% codeRateIndex = 0, 1, 2, and 3 for 1/2, 2/3, 3/4, and 5/6 respectively
txParam         = struct('modOrder',2, 'codeRateIndex',3,'numFrames',1);
txParam(2)      = struct('modOrder',4, 'codeRateIndex',2,'numFrames',1);

% Define packetValid
packetValid     = ones(2,1);  % for a total of 2 frames defined in txParam

% Create a function handle to calculate trBlkSize based on the modOrder and
% codeRateIndex configuration of the frame. 
% For more information on how to calculate trBlkSize, refer to the 
% 'whdlexamples.OFDMTx' script.

trBlkSize = @(modOrder,codeRateIdx)...
    ((72-12) * 32 * log2(modOrder) *...
    ((codeRateIdx ~= 3) .* ((codeRateIdx+1)./(codeRateIdx+2)) + ...
    (codeRateIdx == 3) .* ((codeRateIdx+2)./(codeRateIdx+3)))) - ...
    (7-1)-32;

% Generate dataBits
for n = 1 : length(txParam)
    txParam(n).txDataBits = randsrc(...
        trBlkSize(txParam(n).modOrder,txParam(n).codeRateIndex)*txParam(n).numFrames,1,[0 1]);
end
clear n;

%% Do not modify the contents below

%====================================
% Run the Simulink transmitter model
%====================================

model_name = 'whdlOFDMTransmitter';                               % Name of the Simulink transmitter model
load_system(model_name);                                          % Load the Simulink model

% Run the Simulink transmitter model which takes the 
% txParam configuration defined earlier in the script.
sim(model_name);                                                  

simOut = txData.Data;                                             % Collect outputs
simOut = squeeze(double(simOut));                                 % Adjust dimensions and data type


%=====================================
% Run the MATLAB transmitter function 
%=====================================

refOut = whdlexamples.OFDMTx(txParam);                            % Run MATLAB function with txParam configuration 
                                                                  % and collect outputs

                                                                  
%============================================== 
% Plot MATLAB and Simulink transmitter outputs
%==============================================                                                                  
                                                                  
% Plot real part of output
figure;
plot(1:length(refOut),real(refOut),'r--.',1:length(simOut),real(simOut),'b-o');
legend('MATLAB output','Simulink output');
title('Real Part of Tx output');
xlabel('Time-domain Samples count');
ylabel('Amplitude');

% Plot imaginary part of output
figure;
plot(1:length(refOut),imag(refOut),'r--.',1:length(simOut),imag(simOut),'b-o');
legend('MATLAB output','Simulink output');
title('Imaginary Part of Tx output');
xlabel('Time-domain Samples count');
ylabel('Amplitude');
