%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%                                                                        %
%                               WiMAX_Int                                %
%                                                                        %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%ZABEZPECUJE INTERLEAVNUTIE A DEINTERLEAVNUTIE DAT
%Vstupne premenne:
%data - ktore budu de/interleavnute [double,int]
%x - oznacuje interleaver/deinterleaver [bool][1-int,0-deint]
%Vystupna premenna:
%out - interleavnute/deinterleavnute data [double,int]
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function out = WiMAX_Int(data,x)

inf = 9e9;
[r, s] = size(data);
vys_int = repmat(inf,[r,s]);
vys_dei = repmat(inf,[r,s]);
table = [12,24,48,72,96,144,192,288,384,576,768,1152];                      %velkosti blokov
[a, b] = find(table(1,:)==s);
z = ceil(1/2);

if (~isempty(a)&&~isempty(b))
    %%INTERLEAVER
    if x == 1
        for i=1:s                                                           %vypocet podla standardu WiMAX
            mk = (s/12)*(mod(i-1,12))+(floor((i-1)/12));
            perm1(1, mk+1) = data(1, i);
        end
        for j=1:s
            jk = z*floor((j-1)/z)+(mod(((j-1)+s-floor((12*(j-1))/s)),z));
            vys_int(1, jk+1) = perm1(1, j);
        end
        out = vys_int;
    %%DEINTERLEAVER
    elseif x == 0
        for i=1:s                                                           %vypocet podla standardu WiMAX
            mj = z*floor((i-1)/z)+(mod(((i-1)+floor(12*(i-1)/s)),z));
            perm1(1, mj+1) = data(1, i);
        end
        for j=1:s
            kj = 12*(j-1)-(s-1)*floor(12*(j-1)/s);
            vys_dei(1, kj+1) = perm1(1, j); 
        end
        out = vys_dei;
else
    error('CHYBA');
end
end