function y=idsim(ue,th)
%IDSIM  simulates a given system
%
%	Y = idsim(Z,TH)
%
%	TH: contains the parameters of the model in the format described by 
%	HELP THETA.
%
%	Z: the input-noise data Z=[u e]. For multi-variable systems
%	u=[u1 u2 ... un e1 e2 .. ep], with ui and ei being column vectors. 
%	The number of noise sources should equal the number of outputs. If
%	the e-vector(matrix) is omitted, a noise-free simulation is obtained.
%	The noise contribution is scaled by the variance information con-
%	tained in TH.

%	L. Ljung 10-1-86, 10-2-90
%	Copyright (c) 1986-90 by the MathWorks, Inc.
%	All Rights Reserved.

if isthss(th),eval('y=idsimss(ue,th);'),return,end
[N,nue]=size(ue);
if nue>N, error(' The data should be organized in columns!'),return,end

[a,b,c,d,f]=th2poly(th);
nu=th(1,3);
LAM=th(1,1);
T=gett(th);
if T<0,error('This is a continuous time model of input-output type. Please sample it (using thc2thd) before applying idsim!'),end
y=zeros(N,1);
for k=1:nu
   y=y + filter(b(k,:),f(k,:),ue(:,k));
end

if nue>nu, y=y+sqrt(LAM)*filter(c,d,ue(:,nu+1));end

y=filter([1],a,y);
