close all
clear all
clc
% permutation matrix test

Ncbps = 52;
Nbpsc = 1;
Nss = 1;
k = 0:Ncbps-1;

% puvodni (48 subcarriers)
s = max([1, Nbpsc/2]);

% interleaving
i = (Ncbps/16)*mod(k,16)+floor(k/16);

j = s*floor(i/s)+mod(i+Ncbps-floor(16*(i/Ncbps)),s);

% deinterleaving
ii = s*floor(j/s)+mod(j+floor(16*(j/Ncbps)),s);

kk = (16*ii)-((Ncbps-1)*floor(16*(ii/Ncbps)));

% nove (52 subcarriers/20 MHz)
bw = 20;
Nbpscs(1:Nss) = Nbpsc;
Ncbpss(1:Nss) = Ncbps;


switch bw
    case 20
        n_col = 13;
        n_row = 4*Nbpscs;
        n_rot = 11;
    case 40
        n_col = 18;
        n_row = 6*Nbpscs;
        n_rot = 29;
end

k = 0:Ncbpss-1;
s = max([Nbpscs/2,1])

% interleaving
for i_ss = 1:Nss

i = n_row*mod(k,n_col)+floor(k/n_col)

j = (s*floor(i/s))+mod(i+Ncbpss-floor((n_col*i)/Ncbpss),s)

r = mod(j-(mod((i_ss-1)*2,3))+3*floor((i_ss-1)/3)*n_rot*Nbpscs,Ncbpss)

jj = mod(r+(mod((i_ss-1)*2,3))+3*floor((i_ss-1)/3)*n_rot*Nbpscs,Ncbpss)

ii = (s*floor(jj/s))+mod(jj+floor(n_col*jj/Ncbpss),s)

kk = (n_col*ii)-((Ncbpss-1)*floor(ii/n_row))

end

% % for 1 ss
% % i = n_row*mod(k,n_col)+floor(k/n_col)
% 
% i = 0:51
% j = (s*floor(i/s))+mod(i+Ncbpss-floor((n_col*i)/Ncbpss),s)
% 
% 
% ii = mod((s*floor(jj/s))+(jj+floor(n_col*jj/Ncbpss)),s)
% 
% % kk = (n_col*ii)-((Ncbpss-1)*floor(ii/n_row))
% 
sum(kk-k)
