function [vys]= gen_sss(Nid,Nid2,ts)
% generator SSS sekvence
%Nid(0-167), Nid2(0-2)
% ts - slot 1 nebo 10
if Nid2>2
     error('Nid2 musí být 0,1, nebo 2!');
end

if Nid>167
     error('Nid1 musí být  z intervalu 0 až 167!');
end

q_carka=fix(Nid/30);
q=fix((Nid+q_carka*(q_carka+1)/2)/30);
m_carka=Nid+q*(q+1)/2;

m0=mod(m_carka,31);
m1=mod((m0+fix(m_carka/31)+1),31);

x(1)=0; x(2)=0; x(3)=0; x(4)=0; x(5)=1;
for i=0:25
    x(i+5+1)=mod((x(i+2+1)+x(i+1)),2);
end

for i=1:length(x)
    s_carka(i)=1-2*x(i);
end

for i=0:30
    s0m0(i+1)=s_carka(mod((i+m0),31)+1);
    s1m1(i+1)=s_carka(mod((i+m1),31)+1);
end

x(1)=0; x(2)=0; x(3)=0; x(4)=0; x(5)=1;
for i=0:25
    x(i+5+1)=mod((x(i+3+1)+x(i+1)),2);
end

for i=1:length(x)
    c_carka(i)=1-2*x(i);
end

for i=0:30
    c0(i+1)=c_carka(mod((i+Nid2),31)+1);
    c1(i+1)=c_carka(mod((i+Nid2+3),31)+1);
end

x(1)=0; x(2)=0; x(3)=0; x(4)=0; x(5)=1;
for i=0:25
    x(i+5+1)=mod((x(i+4+1)+x(i+2+1)+x(i+1+1)+x(i+1)),2);
end

for i=1:length(x)
    z_carka(i)=1-2*x(i);
end
%%
for i=0:30
    z1m0(i+1)=z_carka(mod((i+mod(m0,8)),31)+1);
    z1m1(i+1)=z_carka(mod((i+mod(m1,8)),31)+1);
end


for i=0:30
    SSS_0(2*i+1)=s0m0(i+1)*c0(i+1); % subframe 0
    SSS_0(2*i+2)=s1m1(i+1)*c1(i+1)*z1m0(i+1); 
    SSS_5(2*i+1)=s1m1(i+1)*c0(i+1); % subframe 5
    SSS_5(2*i+2)=s0m0(i+1)*c1(i+1)*z1m1(i+1);
end


if ts==1
    vys=SSS_0;
elseif ts==10
    vys=SSS_5;
else
    error('SSS je pouze ve slotu 1 nebo 10');
end

end