
%T=1: modello normale
%T=2: output error, no parte autoregressiva
%T=3: sparsita' casuale
%T=4: come 3 ma c'e' sempre parte autoregressiva

function [den,numG,numH,mord,Ind,V]=genmod(m,mordn,off,SV,T);

mord=max(off+floor((mordn-off)*rand),2);
fl=0;
while fl==0;

    for kk=1:m;
        V(kk+1)=+inf;
        while V(kk+1)>SV^2;% | V(kk+1)<1;
            if kk==1;
                [numG(kk,:),den]=drmodel(mord);
            else
                [numG(kk,:),denn]=drmodel(mord);
            end
            V(kk+1)=sum(dimpulse(numG(kk,:),den,300).^2);
        end
    end
    V(1)=+inf;
    while V(1)>SV^2;% | V(1)<1;;
        [a,numH]=drmodel(mord);
        V(1)=sum(dimpulse(numH,den,300).^2);
    end
    q1=roots(den);
    q2=roots(numH);
    if max(abs(q1))<0.95 &  max(abs(q2))<0.95;%& min(abs(q1))>0.05
        for kk=1:m;
            if numG(kk,1)==0;
                si=size(numG(kk,:),2);
                for i=1:si;
                    if numG(kk,i)==0;
                        sell=i;
                    end;
                end;
                numG(kk,:)=[numG(kk,(sell+1):si) zeros(1,sell)];
            end;
            %numG(kk,:)=[numG(kk,(sell+1):si) zeros(1,sell)];%no delay
        end
        numG(:,end)=[];%1;
        %numG
        %V
        %pause(2)
        fl=1;
    end
end
Ind=ones(1,m+1);%da 2 a m+1: se a zero fdt a zero, prima componente: a zero = output error

if T==2;
    numH=den;
    Ind(1)=0;%output error model
elseif T==3 | T==4;
    %quante da 1 a m-1 sono zero
    %poi con probabilita' 0.5 numH=den;
    U1=7+round((m-7-1)*rand);%ceil((m-1)/2)+round(rand*((m-1)/2-1));%U1=ceil((m-1)*rand);%%%%%%%%%%%%%%%%%%%%%%%%%%%
    for kk=1:U1;
        numG(kk,:)=0*numG(kk,:);
        Ind(kk+1)=0;
    end
    if T==3;
    U2=rand;
    if U2<0.5;
        numH=den;
        Ind(1)=0;%output error model
    end
    end
end

perm=[1 randperm(size(Ind,2)-1)+1];
Ind=Ind(perm);
numG=numG(perm(2:end)-1,:);

  