% hCSIscaling performs the scaling of LLRs by CSI
% [cws] = hCSIscaling(pdsch,cws,csi) performs the scaling of LLR values in 
% the codeword CWS by the calculated csi values using the CSI and given 
% PDSCH configuration structure CHS.

% Copyright 2018 The MathWorks, Inc.

function [cws] = hCSIscaling(chs,cws,csi)

% The number of codewords, |ncw|, is the number of entries in the
% |chs.Modulation| field.
ncw = length(string(chs.Modulation));

% Layer demapping of CSI
csi = lteLayerDemap(chs,csi);

% CSI calculation and scaling of LLRs
if (ncw == 1) && (strcmpi(chs.CSI,'On'))
    % Calculate CSI
    qm = size(cws{1},1)/size(csi{1},1); % no of LLR values per modulation symbol
    csi{1} = reshape(repmat(csi{1}.',qm,1),qm*numel(csi{1}),1);
    % Scaling LLRs by CSI
    cws{1} = cws{1}.*csi{1};
elseif (ncw == 2) && (strcmpi(chs.CSI,'On'))
    % Calculate CSI
    qm1 = size(cws{1},1)/size(csi{1},1); % no of LLR values per modulation symbol for codeword 1
    csi{1} = reshape(repmat(csi{1}.',qm1,1),qm1*numel(csi{1}),1);
    qm2 = size(cws{2},1)/size(csi{2},1); % no of LLR values per modulation symbol for codeword 2
    csi{2} = reshape(repmat(csi{2}.',qm2,1),qm2*numel(csi{2}),1);
    % Scaling LLRs by CSI
    cws{1} = cws{1}.*csi{1};
    cws{2} = cws{2}.*csi{2};
end

end