Professional Documents
Culture Documents
Anexe Curs
Anexe Curs
# RTTCCEEVVKKEEGGÉÉP
RGGEEVVGGR P
/Q
/ QF FGGP
FGGNNCCTTGGCCĩĩKK++F 5KKUUVVGGO
PVVKKHHKKEECCTTGGCC5 QTT
OGGNNQ
¾ #WVQTK &CPĨVGHâPQKW+QP/CVGK2GVTG5VQKEC
E-mail: danny@router.indinf.pub.ro,
ion_matei_ro@yahoo.com,
ps@SysCon.uu.se
0QVâKO RQTVCPVâ
Ŗ ¡P XGFGTGC CHKĩâTKK ITCHKEG ÉP HGTGUVTG EW KPFGZ EQPVTQNCV FG WVKNK\CVQT OKPK
UKOWNCVQCTGNG FGLC RTQKGEVCVG QRGTGC\â EW XCTKCDKNC INQDCNâ FIG ECTG
VTGDWKGKPKįKCNK\CVâÉPCKPVGFGTWNCTGFKPOGFKWN/#6.#$CUVHGN
>> global FIG ; % Declaraţie variabilă globală.
>> FIG = n ; % Iniţializare (cu n dorit, e.g. 1).
%WRTKPU
%CTCEVGTK\âTKÉPVKORĩKHTGEXGPįâCNGRTQEGUGNQTUVQECUVKEG
ISLAB_1A
OKPKUKOWNCVQT %&
ISLAB_1B
OKPKUKOWNCVQT %&
NOISE
TWVKPâCWZKNKCTâ %&
D_SPEKTR
TWVKPâCWZKNKCTâ %&
SPEFAC
TWVKPâCWZKNKCTâ %&
+FGPVKHKECTGCOQFGNGNQTPGRCTCOGVTKEG
ISLAB_2A
OKPKUKOWNCVQT %&
ISLAB_2D
OKPKUKOWNCVQT %&
ISLAB_2L
OKPKUKOWNCVQT %&
+FGPVKHKECTGRCTCOGVTKEâRTKP/GVQFC%GNQT/CK/KEK2âVTCVG
ISLAB_3D
OKPKUKOWNCVQT %&
+FGPVKHKECTGRCTCOGVTKEâRTKP/GVQFC8CTKCDKNGNQT+PUVTWOGPVCNG
ISLAB_4A
OKPKUKOWNCVQT %&
F_TEST2
TWVKPâCWZKNKCTâ %&
GAIC_R2
TWVKPâCWZKNKCTâ %&
GENDATA
TWVKPâCWZKNKCTâ %&
VALID_LS
TWVKPâCWZKNKCTâ %&
+FGPVKHKECTGRCTCOGVTKEâ
RTKP/GVQFC/KPKOK\âTKK'TQTKKFG2TGFKEįKG
ISLAB_5A
OKPKUKOWNCVQT %&
GAIC_R3
TWVKPâCWZKNKCTâ %&
GEN_DATA
TWVKPâCWZKNKCTâ %&
+FGPVKHKECTGTGEWTUKXâ
ISLAB_6A
OKPKUKOWNCVQT %&
GDATA_VP
TWVKPâCWZKNKCTâ %&
RIV
TWVKPâCWZKNKCTâ %&
#RNKECįKKFGKFGPVKHKECTGTGEWTUKXâ
ISLAB_7A
OKPKUKOWNCVQT %&
ISLAB_7C
OKPKUKOWNCVQT %&
ISLAB_7E
OKPKUKOWNCVQT %&
GDATA_FP
TWVKPâCWZKNKCTâ %&
GDATA_CP
TWVKPâCWZKNKCTâ %&
4WVKPG/CVNCDFGW\IGPGTCN %&
..KKUUVVKKPPIIWWTTKKCCNNGGWWPPQQTTO
OKKPPKKUUKKO
OWWNNCCVVQQCCTTGG
ĩĩKKTTWWVVKKPPGGCCWWZZKKNNKKCCTTGG/
/##66..##$$ÉÉPPTTGGIIKKUUVVTTCCVVGGRRGG&
&KKUUEEWWNN%
%QQO
ORRCCEEVV
%
%CCRRKKVVQQNNWWNN
% ISLAB_1A Module that computes and displays true and estimated
% covariance functions of an ARMA[1,1] process: Ay=Ce.
%
function islab_1a(C,A,N,tau_max,nr)
%
% Inputs: C # MA polynomial (vector [1 c])
% A # AR polynomial (vector [1 a])
% N # number of data (100, by default)
% tau_max # maximum time shift (50, by default)
% nr # number of realizations (1, by default)
%
% Outputs: ---------
%
% Explanation: The number of data N used in the estimation
% and the maximum time shift tau_max can also
% be used to study the effect of the individual
% realizations by repeating the estimation
% procedure nr times.
%
% Author: Helena Haglund (*)
% Revised: Bjorn Wittenmark (*)
% Dan Stefanoiu (**)
% Last upgrade: (*) January 3, 1997
% (**) December 16, 2003
% Copyright: (*) Lund Institute of Technology, SWEDEN
% Dept. of Automatic Control
% (**) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
else
a=A(2);
end;
if length(C)<2
c=0;
else
c=C(2);
end;
%
% Computing the estimated response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
tau=[1:tau_max]';
tau_vec=[0;tau];
e=randn(N,nr);
sys= filt(C,A,1);
for n=1:nr
yn=lsim(sys,e(:,n));
[tmp,lags]=xcov(yn,tau_max,'unbiased');
r_e(:,n)=tmp(tau_max+1:end);
end;
%
% Computing the true response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~
r_t=lam/(1-a^2)*[(1+c^2-2*a*c); ...
(c-a)*(1-a*c)*(-a).^(tau-1)];
%
% Plotting
% ~~~~~~~~
NN=min(N,50);
m=mean(r_e')';
st=std(r_e')';
subplot(211)
plot(tau_vec,r_t,'r-',tau_vec,r_e,'b--');
hold on;
legend('True','Estimated');
title(['Covariance functions']);
xlabel('k');
subplot(212)
plot(yn(1:NN));
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
grid;
title(['Realization (',num2str(NN),' samples)']);
xlabel('Discrete time');
%
% END
%
% ISLAB_1B Module that simulates how an ARMA[2,2] model
% equivalent to an AR[2] model plus a second noise
% (see Exercise #4) varies with the SNR
% (signal-to-noise ratio).
%
function islab_1b(x,y,SNR)
%
% Inputs: x # real part of poles for AR model
% (0.5, by default)
% y # imaginary part of poles for AR model
% (0.5, by default)
% SNR # given SNR (3, by default)
%
% Outputs: ---------
%
% Explanation: An AR(2) process, with poles specified by
% real part x and imaginary part y, is observed
% in white measurement noise. The AR(2) process
% has poles in x+iy and x-iy. The disturbed output
% signal can be viewed as generated by an
% ARMA(2,2) process. The function shows how
% the poles and zeros of the ARMA process are
% varying with the SNR.
%
% Author: Helena Haglund (*)
% Revised: Bjorn Wittenmark (*)
% Dan Stefanoiu (**)
% Last upgrade: (*) January 3, 1997
% (**) December 16, 2003
% Copyright: (*) Lund Institute of Technology, SWEDEN
% Dept. of Automatic Control
% (**) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
% BEGIN
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
clf
if nargin<3
SNR = 3;
end;
if isempty(SNR)
SNR = 3;
end;
if nargin<2
y = 0.5;
end;
if isempty(y)
y = 0.5;
end;
if nargin<1
x = 0.5;
end;
if isempty(x)
x = 0.5;
end;
%
% Deriving the equivalent MA polynomial
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a1=-2*x;
a2=x^2+y^2;
A=[1 a1 a2];
lv2=1;
%
% Determine le2 (lambda_e^2)
%
Rx=[1 a1 a2;a1 1+a2 0;a2 a1 1]\[lv2;0;0];
le2=Rx(1)/SNR;
%
% Spectral factorization
%
r0=lv2+le2*( 1+a1^2+a2^2 );
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
r1=le2*( a1+a1*a2 );
r2=le2*a2;
R=[r0 r1 r2];
[D,leps2]=spefac(R);
poler=roots(A);
nollst=roots(D);
%
% Plotting the poles and zeros
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
subplot(211)
tt=0:.1:6.3; % Plot the unit circle
plot(sin(tt),cos(tt),'k-');
hold on;
axis('square')
set(gca,'xtick',[-1 -0.5 0 0.5 1]);
set(gca,'ytick',[-1 -0.5 0 0.5 1]);
% Show the poles.
plot(real(poler),imag(poler),'rx');
hold on;
% Show the zeros.
plot(real(nollst),imag(nollst),'ro');
axis('square')
grid on;
title('Poles (x) and zeros (o)')
%
% Evaluating the spectrum
% ~~~~~~~~~~~~~~~~~~~~~~~
% AR spectrum
[w_AR,fi_AR]=d_spektr(A,1,lv2);
%
% Noise spectral estimation
w_NOISE=w_AR;
[slask1,slask2]=size(fi_AR);
fi_NOISE=le2*ones(slask1,slask2)/(2*pi);
%
% ARMA spectrum
[w_ARMA,fi_ARMA]=d_spektr(A,D,leps2);
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
% Plotting spectra
% ~~~~~~~~~~~~~~~~
subplot(212)
loglog(w_AR,fi_AR,w_NOISE,fi_NOISE,w_ARMA,fi_ARMA);
legend('AR','White noise','ARMA')
grid on;
title('Spectral densities');
xlabel('\omega');
%
% END
%
%
% NOISE Module illustrating stochastic processes.
%
function noise(operation)
%
% Inputs: operation
% # character string in range:
% 'close_noise'
% 'close_noise_def'
% 'init_noise'
% 'move_p'
% 'move_z'
% 'moved_p'
% 'moved_z'
% 'moving_p'
% 'moving_z'
% 'noiseclear'
% 'show' (by default)
% 'system'
% 'winit_noise'
%
% Outputs: -----------
%
% Author: Helena Haglund (*)
% Revised: Bjorn Wittenmark (*)
% Dan Stefanoiu (**)
% Last upgrade: (*) January 3, 1997
% (**) December 16, 2003
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if strcmp(operation,'show')
[existFlag,figNumber]=figflag('Noise');
if ~existFlag
noise('winit_noise');
noise('init_noise');
[existFlag,figNumber]=figflag('Noise');
else
clf;
noise('init_noise');
end;
%
% Operation: SYSTEM
% ~~~~~~~~~~~~~~~~~
%
% - otherwise, draw the window
%
elseif strcmp(operation,'system')
watchon;
figure(fig_noise);
set(error_noise,'Visible','off');
h = 1;
%
% - make plots go clear after next updating
%
set(cov_handle,'EraseMode','XOR');
set(spectr_handle,'EraseMode','XOR');
set(realiz_handle,'EraseMode','XOR');
if get(system_noise,'value')==1
axes(disc_axes_noise);
cla;
%
% - plot unit circle
%
t=0:.1:6.3;
plot(sin(t),cos(t),'k-');
axes(cov_axes);
cla;
axes(spectrum_axes);
cla;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
axes(realization_axes);
cla;
elseif get(system_noise,'value')==2
Ad = [1 -0.5];
a1 = -0.5;
%
% - gives var(y)=1
%
Bd_test = 0;
Bd1 = 1;
Bd = sqrt(1-a1^2);
[phi,gam,C,D] = tf2ss(Bd,Ad);
elseif get(system_noise,'Value')==3,
Ad = [1 -0.3 0.1];
Bd1 = [1 -0.5];
a1 = -0.3;
a2 = 0.1;
b1 = -0.5;
%
% - gives var(y)=1
%
Bd_test = 0;
vary = ((1+b1^2)*(1+a2)-2*b1*a1)/...
((1-a2^2)*(1+a2)-(a1-a1*a2)*a1);
K = 1/sqrt(vary);
Bd = K*Bd1;
[phi,gam,C,D] = tf2ss(Bd,Ad);
end;
if get(system_noise,'Value')==2 | ...
get(system_noise,'Value')==3 | ...
get(system_noise,'Value')==4
axes(disc_axes_noise);
cla;
%
% - plot unit circle
%
t=0:.1:6.3;
plot(sin(t),cos(t),'k-');
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% - calculate spectrum
%
[w_ab,fi_ab]=d_spektr(Ad,Bd,1);
%
% - plot spectrum
%
axes(spectrum_axes);
set(spectr_handle,'XData',w_ab,'YData',fi_ab);
set(spectr_handle,'LineWidth',2,'EraseMode','None');
%
% - calculate realization
%
axes(realization_axes);
%
% - white noise
%
e = randn(1,50);
y = filter(Bd,Ad,e);
t = 0:49;
%
% - plot realization
%
set(realiz_handle,'XData',t,'YData',y);
set(realiz_handle,'LineWidth',2,'EraseMode','None');
end;
axes(disc_axes_noise);
figure(fig_noise);
watchoff;
%
% Operation: MOVE_P, MOVED_P, MOVING_P (poles)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation,'move_p')
set(fig_noise,'WindowButtonMotionFcn', ...
'noise(''moving_p'');', ...
'WindowButtonUpFcn', ...
'noise(''moved_p'');');
elseif strcmp(operation,'moving_p')
currpoint = get(disc_axes_noise,'CurrentPoint');
x = currpoint(1,1);
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
y = currpoint(1,2);
if get(system_noise,'Value')==2
if ccs_col==1
set(pole,'XData',x,'YData',0,'Color','r');
else
set(pole,'XData',x,'YData',0,'Color','k');
end;
elseif get(system_noise,'Value')==3
if ccs_col==1
set(pole,'XData',[x x],'YData',[y -y],'Color','r');
else
set(pole,'XData',[x x],'YData',[y -y],'Color','k');
end;
end;
elseif strcmp(operation,'moved_p')
%
% - update plots after completed move
%
if get(system_noise,'Value')==2
if ccs_col==1
set(pole,'XData',x,'YData',0,'Color','r');
else
set(pole,'XData',x,'YData',0,'Color','k');
end;
Ad = [1 -x];
a1 = -x;
%
% - test stability
%
if (abs(x)>=1)
Bd_test = 1;
Bd = Bd1;
else
%
% - gives var(y)=1
%
Bd_test = 0;
Bd=sqrt(3)/2;
end;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
elseif get(system_noise,'Value')==3
if ccs_col==1
set(pole,'XData',[x x],'YData',[y -y],'Color','r');
else
set(pole,'XData',[x x],'YData',[y -y],'Color','k');
end;
Ad = [1 -2*x x^2+y^2];
a1 = -2*x;
a2 = x^2+y^2;
%
% - test stability
%
if (max(abs(roots(Ad)))>=1)
Bd_test = 1;
Bd = Bd1;
else
Bd_test = 0;
%
% - gives var(y)=1
%
vary = ((1+b1^2)*(1+a2)-2*b1*a1)/ ...
((1-a2^2)*(1+a2)-(a1-a1*a2)*a1);
K = 1/sqrt(vary);
Bd = K*Bd1;
end;
[phi,gam,C,D] = tf2ss(Bd,Ad);
end;
%
% - warning message if system is unstable
%
if (Bd_test==1 & get(system_noise,'Value')==2) | ...
(Bd_test==1 & get(system_noise,'Value')==3)
set(error_noise,'Visible','on');
else
set(error_noise,'Visible','off');
end;
[phi,gam,C,D] = tf2ss(Bd,Ad);
set(fig_noise,'WindowButtonMotionFcn','', ...
'WindowButtonUpFcn','');
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Operation: NOISECLEAR
% ~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation,'noiseclear')
if get(system_noise,'Value')==2 | ...
get(system_noise,'Value')==3 | ...
get(system_noise,'Value')==4
%
% - plot the most recent covariance function
%
axes(cov_axes);
cla;
if ccs_col==1
cov_handle = plot(0:tau, ry,'r');
else
cov_handle = plot(0:tau, ry,'k');
end;
set(cov_handle,'LineWidth',2,'EraseMode','None');
%
% - plot the most recent spectrum
%
axes(spectrum_axes);
cla;
if ccs_col==1
spectr_handle = loglog(w_ab,fi_ab,'r');
else
spectr_handle = loglog(w_ab,fi_ab,'k');
end;
set(spectr_handle,'LineWidth',2,'EraseMode','None');
%
% - plot the most recent realization
%
axes(realization_axes);
cla;
if ccs_col==1
realiz_handle = plot(t,y,'r');
else
realiz_handle = plot(t,y,'k');
end;
set(realiz_handle,'LineWidth',2,'EraseMode','None');
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
figure(fig_noise);
axes(disc_axes_noise);
end;
%
% Operation: WINIT_NOISE
% ~~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation,'winit_noise')
%
% - create main window
%
fig_noise = figure('Name','Noise','NumberTitle','off', ...
'Units','Normalized','Position', ...
[0.2561 0.4400 0.4861 0.4667 ], ...
'BackingStore','Off',...
'DefaultUicontrolFontSize',11);
set(fig_noise,'Color',[0.8 0.8 0.8]);
%
% Operation: INIT_NOISE
% ~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation,'init_noise')
watchon;
figure(fig_noise);
%
% - frame left
%
close_noise = ...
uicontrol(fig_noise,'Style','Push', ...
'String','Quit', ...
'Units','Normalized', ...
'Position', ...
[0.0339 0.0690 0.1429 0.0595 ], ...
'BackgroundColor',[1 0.4 0.4], ...
'Callback', ...
'noise(''close_noise_def'');');
%
% - frame middle
%
frame_middle = ...
uicontrol(fig_noise,'Style','Frame', ...
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
'Units','Normalized', ...
'Position', ...
[0.2036 0.7119 0.3214 0.2619 ]);
system_noise = ...
uicontrol(fig_noise,'Style','popup', ...
'Units','Normalized', ...
'Position', ...
[0.2214 0.8667 0.2857 0.0595 ], ...
'string',...
['Select system | ' ...
'b/(z+a) | ' ...
'(b0z+b1)/(z^2+a1*z+a2)']);
set(system_noise,'Callback','noise(''system'');');
%
% - create pole/zero axes
%
disc_axes_noise = axes('position',[0.23 0.15 0.28 0.28]);
%
% - plot unit circle
%
t=0:.1:6.3;
plot(sin(t),cos(t),'k-');
grid on;
axis('equal');
title('Poles/Zeros','Color','k', ...
'FontName','Times','Fontsize',11);
set(disc_axes_noise,'XLim',[-1.05 1.05], ...
'YLim',[-1.05 1.05], ...
'Clipping','Off', ...
'XLimMode','Manual', ...
'YLimMode','Manual', ...
'YTick',[-1 0 1], ...
'DrawMode','Fast','Xcolor','k', ...
'Ycolor','k', ...
'FontName','Times','Fontsize',11);
hold on;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% - create handles
%
axes(cov_axes);
if ccs_col==1
cov_handle = plot(NaN,NaN,'r');
axes(spectrum_axes);
spectr_handle = loglog(NaN,NaN,'r');
axes(realization_axes);
realiz_handle = plot(NaN,NaN,'r');
else
cov_handle = plot(NaN,NaN,'k');
axes(spectrum_axes);
spectr_handle = loglog(NaN,NaN,'k');
axes(realization_axes);
realiz_handle = plot(NaN,NaN,'k');
end;
watchoff;
%
% - error mesage
%
error_noise = ...
uicontrol(fig_noise,'Style','text',...
'Units', 'Normalized', ...
'Position', ...
[0.2304 0.5202 0.2679 0.0950 ], ...
'String','Unstable system!', ...
'Fontsize',12, ...
'BackgroundColor','r');
set(error_noise,'Visible','off');
%
% Operation: CLOSE_NOISE
% ~~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation, 'close_noise')
[existFlag,figNumber]=figflag('Noise');
if existFlag
close(fig_noise);
end;
[existFlag,figNumber]= ...
figflag('Sampled Data Systems Help');
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if existFlag
close;
end;
[existFlag,figNumber]= ...
figflag('Welcome to Sampled Data Systems Demo!');
if existFlag
set(fig_ccs,'Visible','on');
end;
%
% Operation: CLOSE_NOISE_DEF
% ~~~~~~~~~~~~~~~~~~~~~~~~~~
elseif strcmp(operation, 'close_noise_def')
[existFlag,figNumber]=figflag('Noise');
if existFlag
close(fig_noise);
end;
[existFlag,figNumber]= ...
figflag('Sampled Data Systems Help');
if existFlag
close;
end;
[existFlag,figNumber]= ...
figflag('Welcome to Sampled Data Systems Demo!');
if existFlag
close(fig_ccs);
end;
%
% Operation: unknown
% ~~~~~~~~~~~~~~~~~~
else
error(['### Unknown argument <<' ...
operation '>>. Nothing to do.']) ;
end;
%
% END
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
a=r'/sqrt(r(1));
da=1;
k=0;
while da>1e-14
k=k+1;
for i=1:n
aa(i,:)=[a(i:n),zeros(1,i-1)]+ ...
[zeros(1,i-1),a(1:n+1-i)];
end;
x=2*(aa\r);
a1=(a+x')/2;
da=norm(a1-a);
a=a1;
if k==50
error('### Convergence too slow.');
return;
end;
end;
l2=a(1)^2;
a = a/a(1);
%
% END
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
%CCRRKKVVQQNNWWNN
% ISLAB_2A Module that estimates the step response of an
% ARX[1,1] model.
%
function [yi,y] = islab_2a(a,b,delay,N,nr)
%
% Inputs: a # coefficient of AR part (-0.8, by default)
% b # coefficent of X part (1, by default)
% delay # delay of unit step (9, by default)
% N # simulation period (100, by default)
% nr # number of realizations (100, by default)
%
% Outputs: yi # ideal output of model (noise free)
% (N-length column vector)
% y # collection of all nr realisations
% (N-by-nr matrix)
%
% Explanation: The step response of an ARX[1,1] model is
% corrupted by a Gaussian white noise with unit
% variance. This simulator shows the ideal
% step response (noise free) and the averaged
% step response over nr realisations in order to
% perform the transient analysis.
%
% Author: Helena Haglund (*)
% Bjorn Wittenmark (*)
% Revised: Dan Stefanoiu (**)
% Last upgrade: (*) January 5, 1997
% (**) February 24, 2004
% Copyright: (*) Lund Institute of Technology, SWEDEN
% Department of Automatic Control
% (**) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
global FIG ; % Figure number handler
% (to be set before running the routine).
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Constants
% ~~~~~~~~~
lambda = 1 ; % Variance of white noise (lambda).
u0 = 1 ; % Ampitude of input step.
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 5)
nr = 100 ;
end ;
if (isempty(nr))
nr = 100 ;
end ;
nr = abs(fix(nr(1))) ;
if (~nr)
nr = 100 ;
end ;
if (nargin < 4)
N = 100 ;
end ;
if (isempty(N))
N = 100 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 100 ;
end ;
if (nargin < 3)
delay = 9 ;
end ;
if (isempty(delay))
delay = 9 ;
end ;
delay = abs(fix(delay(1))) ;
if (~delay)
delay = 9 ;
end ;
if (N < (delay+1))
N = delay + 91 ;
end ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (nargin < 2)
b = 1 ;
end ;
if (isempty(b))
b = 1 ;
end ;
b = b(1) ;
if (nargin < 1)
a = -0.8 ;
end;
if (isempty(a))
a = -0.8 ;
end;
a = a(1) ;
if (abs(a)>=1)
war_err(['<ISLAB_2A>: Model unstable. ' ...
'Reciprocal model considered instead.']) ;
a = 1/a ;
end ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e = lambda*randn(N,nr) ;
e(1:delay,:) = e(1:delay,:)/10 ;
%
% Generating the step input
% ~~~~~~~~~~~~~~~~~~~~~~~~~
u = u0*[zeros(delay,1) ; ones(N-delay,1)] ;
%
% Generating the ideal step response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
yi = filter([0 b],[1 a],u) ;
%
% Generating the nr realizations
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
y = yi*ones(1,N) + filter(1,[1 a],e) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 5)
nr = 100 ;
end ;
if (isempty(nr))
nr = 100 ;
end ;
nr = abs(fix(nr(1))) ;
if (~nr)
nr = 100 ;
end ;
if (nargin < 4)
N = 100 ;
end ;
if (isempty(N))
N = 100 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 100 ;
end ;
if (nargin < 3)
M = 50 ;
end ;
if (isempty(M))
M = 50 ;
end ;
M = abs(fix(M(1))) ;
if (~M)
M = 50 ;
end ;
if (nargin < 2)
b = 1 ;
end ;
if (isempty(b))
b = 1 ;
end ;
b = b(1) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (nargin < 1)
a = -0.8 ;
end;
if (isempty(a))
a = -0.8 ;
end;
a = a(1) ;
if (abs(a)>=1)
war_err(['<ISLAB_2A>: Model unstable. ' ...
'Reciprocal model considered instead.']) ;
a = 1/a ;
end ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e = lambda*randn(N,nr) ;
%
% Generating the Pseudo-Random Binary input
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
u = sign(randn(N,nr)) ;
%
% Generating the ideal impulse response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
hi = 0:(M-2) ;
hi = [0 b*(-a).^hi]' ;
%
% Generating the nr realizations
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
y = filter([0 b],[1 a],u) + filter(1,[1 a],e) ;
%
% Estimating the impulse response i.e.
% evaluating the input-output cross-covariance
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
h = [] ;
for k=1:M
h = [h ; sum(y(k:N,:).*u(1:(N-k+1),:))] ;
end ;
h = h/N ; % Note: the variance of input is unit.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% BEGIN
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (isempty(K))
K = 50 ;
end ;
K = abs(fix(K(1))) ;
if (~K)
K = 50 ;
end ;
if (nargin < 3)
M = 30 ;
end ;
if (isempty(M))
M = 30 ;
end ;
M = max([abs(fix(M(1))) round(N/10) 30]) ;
if (nargin < 2)
b = 1 ;
end ;
if (isempty(b))
b = 1 ;
end ;
b = b(1) ;
if (nargin < 1)
a = -0.8 ;
end;
if (isempty(a))
a = -0.8 ;
end;
a = a(1) ;
if (abs(a)>=1)
war_err(['<ISLAB_2A>: Model unstable. ' ...
'Reciprocal model considered instead.']) ;
a = 1/a ;
end ;
%
% Generating the ideal frequency response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
f = logspace(-2,pi,K) ; % The frequency axis.
[magi,phii] = dbode([0 b],[1 a],Ts,f) ;
% phii measured in degrees.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%
%CCRRKKVVQQNNWWNN
% ISLAB_3D Module that estimates the parameters of an
% ARX[2,2] model, with the help of
% Least Squares (LS) Method.
%
function [a,b,lambda,magi,phii,mag,phi,f] = ...
islab_3d(at,bt,K,N,nr)
%
% Inputs: at # true coefficients of AR part
% ([-0.4 -0.32], by default)
% bt # true coefficents of X part
% ([0.5 0.03], by default)
% K # number of frequency nodes (50, by default)
% N # simulation period (100, by default)
% nr # number of realizations (100, by default)
%
% Outputs: a # LS estimates of AR coefficients
% (nr-by-2 matrix)
% b # LS estimates of X coefficents
% (nr-by-2 matrix)
% lambda # LS estimates of noise standard deviation
% (nr-length vector)
% magi # magnitude of ideal frequency response
% (noise free) (K-length vector)
% phii # phase of ideal frequency response
% (noise free) (K-length vector)
% mag # magnitude of all nr estimations of
% frequency response (K-by-nr matrix)
% phi # phase of all nr estimations of
% frequency response (K-by-nr matrix)
% f # frequency axis (semi-logarithmic)
% between 10^(-2) and pi (K-length vector)
%
% Explanation: The parameters of an ARX[2,2] model are
% estimated by means of LS Method. This simulator
% shows the estimation errors produced on
% Bode diagram of model, as well as the estimation
% of noise variance.
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (~N)
N = 100 ;
end ;
if (nargin < 3)
K = 50 ;
end ;
if (isempty(K))
K = 50 ;
end ;
K = abs(fix(K(1))) ;
if (~K)
K = 50 ;
end ;
if (nargin < 2)
bt = [0.5 0.03] ;
end ;
if (isempty(bt))
bt = [0.5 0.03] ;
end ;
bt = [0 bt(1:2)] ;
if (nargin < 1)
at = [-0.4 -0.32] ;
end;
if (isempty(at))
at = [-0.4 -0.32] ; ;
end;
at = [1 at(1:2)] ;
a = roots(at) ;
if (abs(a(1))>=1)
war_err(['<ISLAB_3D>: Model unstable. ' ...
'Reciprocal model considered instead.']) ;
a(1) = 1/a(1) ;
end ;
if (abs(a(2))>=1)
war_err(['<ISLAB_3D>: Model unstable. ' ...
'Reciprocal model considered instead.']) ;
a(2) = 1/a(2) ;
end ;
at = poly(a) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
[ryu,K] = xcov(y(:,p),u(:,p),'biased') ;
ryu = ryu(K>=-1) ;
ryu = ryu(1:4) ;
R = -[ryu(2) ryu(3) ; ...
ryu(1) ryu(2)] ;
R = [toeplitz(ry(1:2)) R ; ...
R' toeplitz(ru)] ; % Main matrix.
r = [-ry(2) ; -ry(3) ; ryu(3) ; ryu(4)] ; % Free vector.
r = R\r ; % Coefficients.
a = [a ; r(1:2)'] ;
b = [b ; r(3:4)'] ;
e = [y(:,p) ... % Noise estimation.
[0 ; y(1:(N-1),p)] ...
[0 ; 0 ; y(1:(N-2),p)] ...
-[0 ; u(1:(N-1),p)] ...
-[0 ; 0 ; u(1:(N-2),p)]] * [1 ; r] ;
lambda = [lambda ; norm(e)/sqrt(N-4)]; % Standard deviation.
%
% Estimating the frequency response
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[R,r] = dbode([0 b(end,:)],[1 a(end,:)],Ts,f) ;
mag = [mag R] ;
phi = [phi r] ;
end ;
%
% Evaluating the average of parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
am = mean(a) ;
bm = mean(b) ;
%
% Evaluating the average of estimation errors
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
magstd = (magi*ones(1,nr)-mag)' ;
phistd = (phii*ones(1,nr)-phi)' ;
magm = mean(magstd)' ;
phim = mean(phistd)' ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
%CCRRKKVVQQNNWWNN
%
% ISLAB_4A Module that estimates an ARX model by using LSM.
%
function [Mid,Did,Dva] = islab_4a(A,B,C,nk,N,sigma,lambda)
%
% Inputs: A # true coefficients of AR part
% ([1 -1.5 0.7], by default)
% B # true coefficients of X part
% ([1 0.5], by default)
% C # true coefficients of MA part (noise filter)
% ([1 -1 0.2], by default)
% nk # intrinsic delay of process (1, by default)
% N # simulation period (250, by default)
% sigma # standard deviation of PRB input
% (1, by default); if null, ARMA or AR
% processes are considered
% lambda # standard deviation of white noise
% (1, by default)
%
% Outputs: Mid # IDMODEL object representing the
% estimated model
% Did # IDDATA object representing the
% data generated for identification
% Dva # IDDATA object representing the
% data generated for validation
%
% Explanation: Data generated by an ARMAX process are employed
% to identify an ARX model with the help of
% Least Squares Method. The estimation is
% consistent, but not the most efficient
% (i.e. the convergence speed is not the best).
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
% Last upgrade: March 16, 2004
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% BEGIN
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
if (nargin < 4)
nk = 1 ;
end ;
if (isempty(nk))
nk = 1 ;
end ;
nk = abs(fix(nk(1))) ;
if (~nk)
nk = 1 ;
end ;
if (nargin < 3)
C = [1 -1 0.2] ;
end ;
if (isempty(C))
C = [1 -1 0.2] ;
end ;
if (nargin < 2)
B = [1 0.5] ;
end ;
if (isempty(B))
B = [1 0.5] ;
end ;
if (nargin < 1)
A = [1 -1.5 0.7] ;
end;
if (isempty(A))
A = [1 -1.5 0.7] ; ;
end;
A = roots(A) ;
A(abs(A)>=1) = 1./A(abs(A)>=1) ; % Correct the stability.
A = poly(A) ;
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
for na=1:Na
for nb=1:Nb
if ((na>1) | (nb>1))
Mid = arx(Did,[na-1 nb-1 nk]) ;% Model estimation.
M{na,nb} = Mid ; % Save model & variance.
Lambda(na,nb) = Mid.NoiseVariance ;
PEid{na,nb} = resid(Mid,Did) ; % Save prediction err.
PEva{na,nb} = resid(Mid,Dva) ;
ys = sim(Mid,Did) ; % Save simulated y
Yid{na,nb} = ys.y ; % (noise free).
ys = sim(Mid,Dva) ;
Yva{na,nb} = ys.y ;
% Save fitness values.
Eid(na,nb) = 100*(1-norm(PEid{na,nb}.y)/norm(yNid)) ;
Eva(na,nb) = 100*(1-norm(PEva{na,nb}.y)/norm(yNva)) ;
% Save validation idx.
Viid(na,nb) = valid_LS(Mid,Did) ;
Viva(na,nb) = valid_LS(Mid,Dva) ;
if(pf)
figure(FIG),clf ;
fig_look(FIG,1.5) ;
subplot(321)
plot(1:N,Did.y,'-b',1:N,Yid{na,nb},'-r') ;
title('Identification data') ;
ylabel('Outputs') ;
ys = [min(min(Did.y),min(Yid{na,nb})) ...
max(max(Did.y),max(Yid{na,nb}))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(1.22*N,ys(2)+0.45*dy, ...
['na = ' int2str(na-1) ...
' | nb = ' int2str(nb-1)]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' ...
sprintf('%g',Eid(na,nb)) ' %']) ;
subplot(322)
plot(1:N,Dva.y,'-b',1:N,Yva{na,nb},'-r') ;
title('Validation data') ;
ylabel('Outputs') ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
ys = [min(min(Dva.y),min(Yva{na,nb})) ...
max(max(Dva.y),max(Yva{na,nb}))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' ...
sprintf('%g',Eva(na,nb)) ' %']) ;
set(FIG, ...
'DefaultTextHorizontalAlignment','left');
legend('y','ym',0) ;
set(FIG, ...
'DefaultTextHorizontalAlignment', ...
'center') ;
subplot(323)
plot(1:N,PEid{na,nb}.y,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEid{na,nb}.y) ...
max(PEid{na,nb}.y)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' ...
sprintf('%g',std(PEid{na,nb}.y,1)^2)]);
subplot(324)
plot(1:N,PEva{na,nb}.y,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEva{na,nb}.y) ...
max(PEva{na,nb}.y)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' ...
sprintf('%g',std(PEva{na,nb}.y,1)^2)]);
subplot(325)
set(FIG,'DefaultLineLineWidth',0.5) ;
set(FIG,'DefaultLineMarkerSize',2) ;
[r,K] = xcov(PEid{na,nb}.y,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' ...
num2str(Viid(na,nb))]) ;
subplot(326)
[r,K] = xcov(PEva{na,nb}.y,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' ...
num2str(Viva(na,nb))]) ;
set(FIG, ...
'DefaultTextHorizontalAlignment', ...
'right') ;
text(1.3*K,ys(1)-0.4*dy,'<Press a key>') ;
FIG = FIG+1 ;
pause ;
figure(FIG),clf ;
fig_look(FIG,2) ;
pzmap(Mid,'SD',alpha) ;
title('Poles-Zeros representation') ;
xlabel('Real axis') ;
ylabel('Imaginary axis') ;
ys = axis ;
r = 's' ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (na==2)
r = [] ;
end ;
text(0.8*ys(1),0.9*ys(4), ...
[int2str(na-1) ' pole' r]) ;
r = 's' ;
if (nb==2)
r = [] ;
end ;
text(0.8*ys(2),0.9*ys(4), ...
[int2str(nb-1) ' zero' r]) ;
set(FIG, ...
'DefaultTextHorizontalAlignment','left') ;
text(1.1*ys(2),ys(3),'<Press a key>') ;
FIG = FIG-1 ;
pause ;
end ; % [if (pf)]
end ; % [if ((na>1) | (nb>1))]
end ; % [for nb=1:Nb]
end ; % [for na=1:Na]
if (~pf)
war_err(' ... Done.') ;
end ;
%
% Drawing useful surfaces for selecting the optimal structure
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Na = Na-1 ;
Nb = Nb-1 ;
figure(FIG),clf ; % Noise variance.
fig_look(FIG,2) ;
[na,nb] = F_test2(Lambda,N) ;
Lambda(Lambda<eps) = eps ;
Mid = 10*log10(Lambda) ;
surf(0:Nb,0:Na,Mid) ;
ys = [min(min(Mid)) max(max(Mid))] ;
dy = ys(2) - ys(1) ;
axis([0 Nb 0 Na ys(1) ys(2)+0.2*dy]) ;
view([125,30]) ;
title(['Estimated noise variance surface. ' ...
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
xlabel('nb') ;
ylabel('na') ;
zlabel('E_N [%]') ;
text(nb-1,na-1,Eva(na,nb)+0.02*dy,'o') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
text(Nb,1.5*Na,ys(1),'<Press a key>') ;
FIG = FIG+1 ;
pause
figure(FIG),clf ; % GAIC-Rissanen.
fig_look(FIG,2) ;
[na,nb,Mid] = GAIC_R2(Lambda,N) ;
surf(0:Nb,0:Na,Mid) ;
ys = [min(min(Mid)) max(max(Mid))] ;
dy = ys(2) - ys(1) ;
axis([0 Nb 0 Na ys(1) ys(2)+0.2*dy]) ;
view([125,30]) ;
title(['Akaike-Rissanen criterion. ' ...
'Optimum: na = ' int2str(na-1) ...
', nb = ' int2str(nb-1) '.']) ;
xlabel('nb') ;
ylabel('na') ;
zlabel('GAIC-R') ;
text(nb-1,na-1,Mid(na,nb)+0.02*dy,'o') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
text(Nb,1.5*Na,ys(1),'<Press a key>') ;
FIG = FIG+1 ;
pause
%
% Selecting the optimal structure
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
na = input(' # Insert optimal indices [na nb]: ') ;
na = abs(round(na(1:2))) ;
nb = min(Nb,na(2))+1 ;
na = min(Na,na(1))+1 ;
Mid = M{na,nb} ;
Yid = Yid{na,nb} ;
Yva = Yva{na,nb} ;
PEid = PEid{na,nb}.y ;
PEva = PEva{na,nb}.y ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
Eid = Eid(na,nb) ;
Eva = Eva(na,nb) ;
Viid = Viid(na,nb) ;
Viva = Viva(na,nb) ;
war_err(' o Optimum model: ') ;
Mid
war_err([blanks(25) '<Press a key>']) ;
pause
%
% Plotting model performances
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~
figure(FIG),clf ;
fig_look(FIG,1.5) ;
subplot(321)
plot(1:N,Did.y,'-b',1:N,Yid,'-r') ;
title('Identification data') ;
ylabel('Outputs') ;
ys = [min(min(Did.y),min(Yid)) ...
max(max(Did.y),max(Yid))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(1.22*N,ys(2)+0.45*dy, ...
['na = ' int2str(na-1) ...
' | nb = ' int2str(nb-1)]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' sprintf('%g',Eid) ' %']) ;
subplot(322)
plot(1:N,Dva.y,'-b',1:N,Yva,'-r') ;
title('Validation data') ;
ylabel('Outputs') ;
ys = [min(min(Dva.y),min(Yva)) ...
max(max(Dva.y),max(Yva))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' sprintf('%g',Eva) ' %']) ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
legend('y','ym',0) ;
set(FIG,'DefaultTextHorizontalAlignment','center') ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
subplot(323)
plot(1:N,PEid,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEid) max(PEid)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' sprintf('%g',std(PEid,1)^2)]) ;
subplot(324)
plot(1:N,PEva,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEva) max(PEva)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' sprintf('%g',std(PEva,1)^2)]) ;
subplot(325)
set(FIG,'DefaultLineLineWidth',0.5) ;
set(FIG,'DefaultLineMarkerSize',2) ;
[r,K] = xcov(PEid,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' num2str(Viid)]) ;
subplot(326)
[r,K] = xcov(PEva,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' num2str(Viva)]) ;
set(FIG,'DefaultTextHorizontalAlignment','right') ;
text(1.3*K,ys(1)-0.4*dy,'<Press a key>') ;
FIG = FIG+1 ;
pause ;
figure(FIG),clf ;
fig_look(FIG,2) ;
pzmap(Mid,'SD',alpha) ;
title('Poles-Zeros representation') ;
xlabel('Real axis') ;
ylabel('Imaginary axis') ;
ys = axis ;
r = 's' ;
if (na==2)
r = [] ;
end ;
text(0.8*ys(1),0.9*ys(4), ...
[int2str(na-1) ' pole' r]) ;
r = 's' ;
if (nb==2)
r = [] ;
end ;
text(0.8*ys(2),0.9*ys(4), ...
[int2str(nb-1) ' zero' r]) ;
%
% END
%
% F_TEST2 Module that evaluates the optimum structural
% orders through the F-test, when identifying
% a model with 2 structural indexes (ARX, ARMA).
%
function [na,nb] = F_test2(Lambda,N)
%
% Inputs: Lambda # estimated noise variance matrix
% N # size of measured data set
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (isempty(N))
war_err(E1) ;
return ;
end ;
N = abs(fix(N(1))) ;
if (~N)
war_err(E1) ;
return ;
end ;
N = 4/N ;
if (nargin < 1)
war_err(E2) ;
return ;
end ;
if (isempty(Lambda))
war_err(E2) ;
return ;
end ;
Lambda = abs(Lambda) ;
Lambda(~Lambda) = eps ;
%
% Evaluating the F-test
% ~~~~~~~~~~~~~~~~~~~~~
[na,nb] = size(Lambda) ;
if ((na<2) & (nb<2))
war_err(W1) ;
elseif ((na<2) | (nb<2))
if (na<2)
Lambda = (Lambda(1:(nb-1))-Lambda(2:nb))./Lambda(2:nb) ;
else
Lambda = (Lambda(1:(na-1))-Lambda(2:na))./Lambda(2:na) ;
end ;
Lambda = Lambda - N ;
Lambda(Lambda>=0) = -Inf ;
if (na<2)
[Lambda,nb] = max(Lambda) ;
else
[Lambda,na] = max(Lambda) ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isinf(Lambda))
na = [] ;
nb = [] ;
war_err(E3) ;
end ;
else
Na = na ;
Nb = nb ;
Fr = (Lambda(1:(na-1),:)-Lambda(2:na,:))./Lambda(2:na,:)-N ;
Fc = (Lambda(:,1:(nb-1))-Lambda(:,2:nb))./Lambda(:,2:nb)-N ;
Fr(Fr>=0) = -Inf ;
Fc(Fc>=0) = -Inf ;
[Fr,i] = max(Fr) ;
if (Na<3)
j = i ;
i = 1 ;
else
[Fr,j] = max(Fr) ;
i = i(j) ;
end ;
[Fc,na] = max(Fc) ;
if (Nb<3)
nb = 1 ;
else
[Fc,nb] = max(Fc) ;
na = na(nb) ;
end ;
if (isinf(Fr) & isinf(Fc))
na = [] ;
nb = [] ;
war_err(E3) ;
elseif (Fr>Fc)
na = i ;
nb = j ;
end ;
end ;
%
% END
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Faults preventing
% ~~~~~~~~~~~~~~~~~
na = [] ;
nb = [] ;
GAICR = [] ;
if (nargin < 2)
war_err(E1) ;
return ;
end ;
if (isempty(N))
war_err(E1) ;
return ;
end ;
N = abs(fix(N(1))) ;
if (~N)
war_err(E1) ;
return ;
end ;
N = sqrt(N)/N ;
if (nargin < 1)
war_err(E2) ;
return ;
end ;
if (isempty(Lambda))
war_err(E2) ;
return ;
end ;
Lambda = abs(Lambda) ;
Lambda(~Lambda) = eps ;
%
% Evaluating the GAIC_R criterion
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[na,nb] = size(Lambda) ;
GAICR = log(Lambda) + ...
N*((1:na)'*ones(1,nb) + ones(na,1)*(1:nb)-2) ;
if ((na<2) & (nb<2))
war_err(W1) ;
elseif (na<2)
[Lambda,nb] = min(GAICR) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
elseif (nb<2)
[Lambda,na] = min(GAICR) ;
else
[Lambda,na] = min(GAICR) ;
[Lambda,nb] = min(Lambda) ;
na = na(nb) ;
end ;
%
% END
%
% GENDATA Module that generates data from an ARX model with
% colored noise (or ARMAX model).
%
function [D,V,P] = gendata(A,B,C,nk,N,sigma,lambda)
%
% Inputs: A # true coefficients of AR part
% ([1 -1.5 0.7], by default)
% B # true coefficients of X part
% ([1 0.5], by default)
% C # true coefficients of MA part (noise filter)
% ([1 -1 0.2], by default)
% nk # intrinsic delay of process (1, by default)
% N # simulation period (250, by default)
% sigma # standard deviation of PRB input
% (1, by default); if null, ARMA or AR
% processes are considered
% lambda # standard deviation of white noise
% (1, by default)
% if null, noise free ARX or AR processes
% are considered
%
% Outputs: D # IDDATA model representing the
% I/O generated data
% V # IDDATA model representing the
% I/O noise generated data
% (white noise as input, colored noise
% as output)
% P # IDMODEL object representing the
% process that provided the data
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
if (nargin < 4)
nk = 1 ;
end ;
if (isempty(nk))
nk = 1 ;
end ;
nk = abs(fix(nk(1))) ;
if (~nk)
nk = 1 ;
end ;
if (nargin < 3)
C = [1 -1 0.2] ;
end ;
if (isempty(C))
C = [1 -1 0.2] ;
end ;
if (nargin < 2)
B = [1 0.5] ;
end ;
if (isempty(B))
B = [1 0.5] ;
end ;
if (nargin < 1)
A = [1 -1.5 0.7] ;
end;
if (isempty(A))
A = [1 -1.5 0.7] ; ;
end;
A = roots(A) ;
A(abs(A)>=1) = 1./A(abs(A)>=1) ; % Correct the stability.
A = poly(A) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
%CCRRKKVVQQNNWWNN
%
% ISLAB_5A Module that estimates an ARMAX model by using MPEM.
%
function [Mid,Did,Dva] = islab_5a(A,B,C,nk,N,sigma,lambda)
%
% Inputs: A # true coefficients of AR part
% ([1 -1.5 0.7], by default)
% B # true coefficients of X part
% ([1 0.5], by default)
% C # true coefficients of MA part (noise filter)
% ([1 -1 0.2], by default)
% nk # intrinsic delay of process (1, by default)
% N # simulation period (250, by default)
% sigma # standard deviation of PRB input
% (1, by default); if null, ARMA or AR
% processes are considered
% lambda # standard deviation of white noise
% (1, by default)
%
% Outputs: Mid # IDMODEL object representing the
% estimated model
% Did # IDDATA object representing the
% data generated for identification
% Dva # IDDATA object representing the
% data generated for validation
%
% Explanation: Data generated by an ARMAX process are employed
% to identify an ARMAX model with the help of
% Minimum Prediction Error Method (MPEM).
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
% Last upgrade: March 16, 2004
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
if (nargin < 4)
nk = 1 ;
end ;
if (isempty(nk))
nk = 1 ;
end ;
nk = abs(fix(nk(1))) ;
if (~nk)
nk = 1 ;
end ;
if (nargin < 3)
C = [1 -1 0.2] ;
end ;
if (isempty(C))
C = [1 -1 0.2] ;
end ;
if (nargin < 2)
B = [1 0.5] ;
end ;
if (isempty(B))
B = [1 0.5] ;
end ;
if (nargin < 1)
A = [1 -1.5 0.7] ;
end;
if (isempty(A))
A = [1 -1.5 0.7] ; ;
end;
A = roots(A) ;
A(abs(A)>=1) = 1./A(abs(A)>=1) ; % Correct the stability.
A = poly(A) ;
%
% Constructing the data provider process
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
P = idpoly(A,[zeros(1,nk) B],C,1,1,Ts) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%
% Generating the identification data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Did = gen_data(P,N,sigma,lambda) ;
%
% Generating the validation data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dva = gen_data(P,N,sigma,lambda) ;
%
% Estimating all models via MPEM
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Na = Na+1 ;
Nb = Nb+1 ;
Nc = Nc+1 ;
M = cell(Na,Nb,Nc) ; % Cell array of all models.
% Matrix of noise variances.
Lambda = 1000*lambda*ones(Na,Nb,Nc) ;
Yid = cell(Na,Nb,Nc) ; % Cell array of simulated
% outputs when using
% the identification data.
Yva = cell(Na,Nb,Nc) ; % Cell array of simulated
% outputs when using
% the validation data.
PEid = cell(Na,Nb,Nc) ; % Cell array of prediction
% errors on ident. data.
PEva = cell(Na,Nb,Nc) ; % Cell array of prediction
% errors on validation data.
Eid = zeros(Na,Nb,Nc) ; % Matrix of fitness values
% evaluated on ident. data.
Eva = zeros(Na,Nb,Nc) ; % Matrix of fitness values
% evaluated on validation
% data.
yNid = Did.y-mean(Did.y) ; % Centered y (ident.).
yNva = Dva.y-mean(Dva.y) ; % Centered y (validation).
Viid = zeros(Na,Nb,Nc) ; % Validation index matrix
% (identification data).
Viva = zeros(Na,Nb,Nc) ; % Validation index matrix
% (validation data).
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
PEva{na,nb,nc} = resid(Mid,Dva) ;
ys = compare(Mid,Did) ; % Save simulated outputs
Yid{na,nb,nc} = ys{1,1}.y ;% (noise free, initial
ys = compare(Mid,Dva) ; % conditions set for the
Yva{na,nb,nc} = ys{1,1}.y ;% best fit).
% Save fitness values.
Eid(na,nb,nc) = ...
100*(1-norm(PEid{na,nb,nc}.y)/norm(yNid)) ;
Eva(na,nb,nc) = ...
100*(1-norm(PEva{na,nb,nc}.y)/norm(yNva)) ;
% Save validation indices.
Viid(na,nb,nc) = valid_LS(Mid,Did) ;
Viva(na,nb,nc) = valid_LS(Mid,Dva) ;
% Upgrade optimal indices.
if ((na>1) & (nb>1) & (nc>1))
% F-test on prediction error.
if (~sum(optidx(1,:)))
ys = [Lambda(na-1,nb,nc) ...
Lambda(na,nb-1,nc) ...
Lambda(na,nb,nc-1)] ;
ys = ys/Lambda(na,nb,nc) - 1 ;
if (sum(ys<(4/N))==3) | ...
((na==Na) & (nb==Nb) & (nc==Nc))
optidx(1,:) = [na nb nc] ;
end ;
end ;
if (~sum(optidx(2,:))) % F-test on fitness
% (identification).
ys = [Eid(na-1,nb,nc) ...
Eid(na,nb-1,nc) ...
Eid(na,nb,nc-1)] ;
ys = 1-ys/Eid(na,nb,nc) ;
if (sum(ys<(4/N))==3) | ...
((na==Na) & (nb==Nb) & (nc==Nc))
optidx(2,:) = [na nb nc] ;
end ;
end ;
if (~sum(optidx(3,:))) % F-test on fitness
% (validation).
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
ys = [Eva(na-1,nb,nc) ...
Eva(na,nb-1,nc) ...
Eva(na,nb,nc-1)] ;
ys = 1-ys/Eva(na,nb,nc) ;
if (sum(ys<(4/N))==3) | ...
((na==Na) & (nb==Nb) & (nc==Nc))
optidx(3,:) = [na nb nc] ;
end ;
end ;
end ; % [if ((na>1) & (nb>1) & (nc>1))]
if (pf) % Show model performances.
figure(FIG),clf ;
fig_look(FIG,1.5) ;
subplot(321)
plot(1:N,Did.y,'-b',1:N,Yid{na,nb,nc},'-r') ;
title('Identification data') ;
ylabel('Outputs') ;
ys = [min(min(Did.y),min(Yid{na,nb,nc})) ...
max(max(Did.y),max(Yid{na,nb,nc}))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(1.22*N,ys(2)+0.45*dy, ...
['na = ' int2str(na-1) ...
' | nb = ' int2str(nb-1) ...
' | nc = ' int2str(nc-1)]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' ...
sprintf('%g',Eid(na,nb,nc)) ' %']) ;
subplot(322)
plot(1:N,Dva.y,'-b',1:N,Yva{na,nb,nc},'-r') ;
title('Validation data') ;
ylabel('Outputs') ;
ys = [min(min(Dva.y),min(Yva{na,nb,nc})) ...
max(max(Dva.y),max(Yva{na,nb,nc}))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' ...
sprintf('%g',Eva(na,nb,nc)) ' %']) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
set(FIG, ...
'DefaultTextHorizontalAlignment','left');
legend('y','ym',0) ;
set(FIG, ...
'DefaultTextHorizontalAlignment', ...
'center') ;
subplot(323)
plot(1:N,PEid{na,nb,nc}.y,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEid{na,nb,nc}.y) ...
max(PEid{na,nb,nc}.y)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' ...
sprintf('%g', ...
std(PEid{na,nb,nc}.y,1)^2)]) ;
subplot(324)
plot(1:N,PEva{na,nb,nc}.y,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEva{na,nb,nc}.y) ...
max(PEva{na,nb,nc}.y)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' ...
sprintf('%g', ...
std(PEva{na,nb,nc}.y,1)^2)]) ;
subplot(325)
set(FIG,'DefaultLineLineWidth',0.5) ;
set(FIG,'DefaultLineMarkerSize',2) ;
[r,K] = xcov(PEid{na,nb,nc}.y,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' ...
num2str(Viid(na,nb,nc))]) ;
subplot(326)
[r,K] = xcov(PEva{na,nb,nc}.y,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' ...
num2str(Viva(na,nb,nc))]) ;
set(FIG, ...
'DefaultTextHorizontalAlignment', ...
'right') ;
text(1.3*K,ys(1)-0.4*dy,'<Press a key>') ;
FIG = FIG+1 ;
pause ;
figure(FIG),clf ;
fig_look(FIG,2) ;
pzmap(Mid,'b','SD',alpha) ;
title('Poles-Zeros representation (system)') ;
xlabel('Real axis') ;
ylabel('Imaginary axis') ;
ys = axis ;
r = 's' ;
if (na==2)
r = [] ;
end ;
text(0.8*ys(1),0.9*ys(4), ...
[int2str(na-1) ' pole' r]) ;
r = 's' ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (nb==2)
r = [] ;
end ;
text(0.8*ys(2),0.9*ys(4), ...
[int2str(nb-1) ' zero' r]) ;
set(FIG, ...
'DefaultTextHorizontalAlignment','left') ;
text(1.1*ys(2),ys(3),'<Press a key>') ;
FIG = FIG+1 ;
pause ;
if ((na>1) | (nc>1))
Mid.b = Mid.c ;
Mid.nk = 0 ;
figure(FIG),clf ;
fig_look(FIG,2) ;
pzmap(Mid,'r','SD',alpha) ;
title('Poles-Zeros representation (noise)') ;
xlabel('Real axis') ;
ylabel('Imaginary axis') ;
ys = axis ;
r = 's' ;
if (na==2)
r = [] ;
end ;
text(0.8*ys(1),0.9*ys(4), ...
[int2str(na-1) ' pole' r]) ;
r = 's' ;
if (nc==2)
r = [] ;
end ;
text(0.8*ys(2),0.9*ys(4), ...
[int2str(nc-1) ' zero' r]) ;
set(FIG, ...
'DefaultTextHorizontalAlignment','left');
text(1.1*ys(2),ys(3),'<Press a key>') ;
pause ;
end ;
FIG = FIG-2 ;
end ; % [if (pf)]
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
PEva = PEva{na,nb,nc}.y ;
Eid = Eid(na,nb,nc) ;
Eva = Eva(na,nb,nc) ;
Viid = Viid(na,nb,nc) ;
Viva = Viva(na,nb,nc) ;
war_err(' o Optimum model: ') ;
Mid
war_err([blanks(25) '<Press a key>']) ;
pause
%
% Plotting model performances
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(321)
plot(1:N,Did.y,'-b',1:N,Yid,'-r') ;
title('Identification data') ;
ylabel('Outputs') ;
ys = [min(min(Did.y),min(Yid)) ...
max(max(Did.y),max(Yid))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(1.22*N,ys(2)+0.45*dy, ...
['na = ' int2str(na-1) ...
' | nb = ' int2str(nb-1) ...
' | nc = ' int2str(nc-1)]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' sprintf('%g',Eid) ' %']) ;
subplot(322)
plot(1:N,Dva.y,'-b',1:N,Yva,'-r') ;
title('Validation data') ;
ylabel('Outputs') ;
ys = [min(min(Dva.y),min(Yva)) ...
max(max(Dva.y),max(Yva))] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.05*dy, ...
['Fitness E_N = ' sprintf('%g',Eva) ' %']) ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
legend('y','ym',0) ;
set(FIG,'DefaultTextHorizontalAlignment','center') ;
subplot(323)
plot(1:N,PEid,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEid) max(PEid)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' sprintf('%g',std(PEid,1)^2)]) ;
subplot(324)
plot(1:N,PEva,'-m') ;
ylabel('Prediction error') ;
ys = [min(PEva) max(PEva)] ;
dy = ys(2)-ys(1) ;
axis([0 N+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(N/2,ys(2)+0.07*dy, ...
['\lambda^2 = ' sprintf('%g',std(PEva,1)^2)]) ;
subplot(325)
set(FIG,'DefaultLineLineWidth',0.5) ;
set(FIG,'DefaultLineMarkerSize',2) ;
[r,K] = xcov(PEid,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' num2str(Viid)]) ;
subplot(326)
[r,K] = xcov(PEva,'unbiased') ;
r = r(K>=0) ;
K = ceil(length(r)/2) ;
r = r(1:K) ;
stem(1:K,r,'-g','filled') ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
xlabel('Normalized time') ;
ylabel('Auto-covariance') ;
ys = [min(r) max(r)] ;
dy = ys(2)-ys(1) ;
axis([0 K+1 ys(1)-0.05*dy ys(2)+0.2*dy]) ;
text(K/2,ys(2)+0.05*dy, ...
['Validation index = ' num2str(Viva)]) ;
set(FIG,'DefaultTextHorizontalAlignment','right') ;
text(1.3*K,ys(1)-0.4*dy,'<Press a key>') ;
FIG = FIG+1 ;
pause ;
figure(FIG),clf
fig_look(FIG,2) ;
pzmap(Mid,'SD',alpha) ;
title('Poles-Zeros representation (system)') ;
xlabel('Real axis') ;
ylabel('Imaginary axis') ;
ys = axis ;
r = 's' ;
if (na==2)
r = [] ;
end ;
text(0.8*ys(1),0.9*ys(4), ...
[int2str(na-1) ' pole' r]) ;
r = 's' ;
if (nb==2)
r = [] ;
end ;
text(0.8*ys(2),0.9*ys(4), ...
[int2str(nb-1) ' zero' r]) ;
FIG = FIG+1 ;
if ((na>1) | (nc>1))
pause ;
P = Mid ;
P.b = P.c ;
P.nk = 0 ;
figure(FIG),clf ;
fig_look(FIG,2) ;
pzmap(P,'r','SD',alpha) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
M = abs(fix(N(1))) ;
if (~N)
war_err(E1) ;
return ;
end ;
N = sqrt(N)/N ;
if (nargin < 1)
war_err(E2) ;
return ;
end ;
if (isempty(Lambda))
war_err(E2) ;
return ;
end ;
Lambda = abs(Lambda) ;
Lambda(~Lambda) = eps ;
%
% Evaluating the GAIC_R criterion
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[Na,Nb,Nc] = size(Lambda) ;
GAICR = [] ;
Gmin = [] ;
for nc=1:Nc
[Na,Nb,G] = GAIC_R2(Lambda(:,:,nc)*exp(M*(nc-1)),N) ;
na = [na Na] ;
nb = [nb Nb] ;
GAICR = cat(3,GAICR,G) ;
Gmin = [Gmin G(Na,Nb)] ;
end ;
[Gmin,nc] = min(Gmin) ;
na = na(nc) ;
nb = nb(nc) ;
%
% END
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
% BEGIN
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 5)
bin = 1 ;
end ;
if (isempty(bin))
bin = 1 ;
end ;
bin = abs(sign(bin(1))) ;
if (nargin < 4)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
lambda = abs(lambda(1)) ;
if (nargin < 3)
sigma = 1 ;
end ;
if (isempty(sigma))
sigma = 1 ;
end ;
sigma = abs(sigma(1)) ;
if (nargin < 2)
N = 250 ;
end ;
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
if (nargin < 1)
DP = idpoly([1 -1.5 0.7],[0 1 0.5],[1 -1 0.2]) ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isempty(DP))
DP = idpoly([1 -1.5 0.7],[0 1 0.5],[1 -1 0.2]) ;
end ;
if (~isa(DP,'IDMODEL'))
DP = idpoly([1 -1.5 0.7],[0 1 0.5],[1 -1 0.2]) ;
end ;
P = roots(DP.a) ;
P(abs(P)>=1) = 1./P(abs(P)>=1) ; % Correct the stability.
DP.a = poly(P) ;
P = DP ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e = lambda*randn(N,1) ;
%
% Generating the Gaussian colored noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
V = filter(P.c,1,e) ;
V = iddata(V,e) ; % White noise: V.u
% Colored noise: V.y
%
% Generating the PRB input
% ~~~~~~~~~~~~~~~~~~~~~~~~
u = randn(N,1) ;
u = sigma*(sign(u).^bin).*(u.^(1-bin)) ;
%
% Generating the data
% ~~~~~~~~~~~~~~~~~~~
D = iddata(sim(P,[u e]),u) ;
%
% END
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
%CCRRKKVVQQNNWWNN
%
% ISLAB_6A Module that performs comparison between recursive
% identification procedures on an ARMAX process with
% constant parameters.
%
function islab_6a(N,sigma,lambda)
%
% Inputs: N # simulation period (250, by default)
% sigma # standard deviation of PRB input
% (1, by default);
% lambda # standard deviation of white noise
% (1, by default)
%
% Outputs: -------
%
% Explanation: Data generated by an ARMAX process with constant
% parameters are employed to identify its
% parameters through 4 recursive identification
% procedures: RLS, RIV, RPEM, RPLR.
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
% Last upgrade: April 10, 2004
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
% BEGIN
%
global FIG ; % Figure number handler
% (to be set before running the routine).
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 3)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
lambda = abs(lambda(1)) ;
if (~lambda)
lambda = 1 ;
end ;
if (nargin < 2)
sigma = 1 ;
end ;
if (isempty(sigma))
sigma = 1 ;
end ;
sigma = abs(sigma(1)) ;
if (nargin < 1)
N = 250 ;
end ;
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
%
% Generating the identification data (ARMAX)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[D,V,P] = gdata_vp(0,N,sigma,lambda,0) ;
%
% Estimating parameters via RLS
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[theta,ypred] = rarx(D,[1 1 1],'ff',1) ;
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(311)
plot(1:N,P.a(2)*ones(N,1),'--r', ...
1:N,theta(:,1),'-b') ;
title('Performances of Recursive Least Squares') ;
ylabel('AR (a)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
subplot(312)
plot(1:N,P.b(2)*ones(N,1),'--r', ...
1:N,theta(:,2),'-b') ;
ylabel('X (b)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(313)
plot(1:N,D.y-ypred,'-m') ;
xlabel('Normalized time') ;
ylabel('Pred. err.') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
FIG=FIG+1 ;
% Estimating parameters via RIV
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[theta,ypred] = riv(D,[1 1 1]) ;
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(311)
plot(1:N,P.a(2)*ones(N,1),'--r', ...
1:N,theta(:,1),'-b') ;
title(['Performances of Recursive Instrumental ’ ...
’Variables']) ;
ylabel('AR (a)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(312)
plot(1:N,P.b(2)*ones(N,1),'--r', ...
1:N,theta(:,2),'-b') ;
ylabel('X (b)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(313)
plot(1:N,D.y-ypred,'-m') ;
xlabel('Normalized time') ;
ylabel('Pred. err.') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
FIG=FIG+1 ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
subplot(411)
plot(1:N,P.a(2)*ones(N,1),'--r', ...
1:N,theta(:,1),'-b') ;
title(['Performances of Recursive ’ ...
’Pseudo-Linear Regression']) ;
ylabel('AR (a)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(412)
plot(1:N,P.b(2)*ones(N,1),'--r', ...
1:N,theta(:,2),'-b') ;
ylabel('X (b)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(413)
plot(1:N,P.c(2)*ones(N,1),'--r', ...
1:N,theta(:,3),'-b') ;
ylabel('MA (c)') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
subplot(414)
plot(1:N,D.y-ypred,'-m') ;
xlabel('Normalized time') ;
ylabel('Pred. err.') ;
V = axis ;
axis([0 N+1 V(3:4)]) ;
%
% END
%
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 5)
bin = 1 ;
end ;
if (isempty(bin))
bin = 1 ;
end ;
bin = abs(sign(bin(1))) ;
if (nargin < 4)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
lambda = abs(lambda(1)) ;
if (nargin < 3)
sigma = 1 ;
end ;
if (isempty(sigma))
sigma = 1 ;
end ;
sigma = abs(sigma(1)) ;
if (nargin < 2)
N = 250 ;
end ;
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
if (nargin < 1)
cv = 0 ;
end ;
if (isempty(cv))
cv = 0 ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
cv = cv(1) ;
%
% Building the process with constant parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
P = idpoly([1 a],[0 b],[1 c]) ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e = lambda*randn(N,1) ;
%
% Generating the PRB input
% ~~~~~~~~~~~~~~~~~~~~~~~~
u = randn(N,1) ;
u = sigma*(sign(u).^bin).*(u.^(1-bin)) ;
%
% Generating the data
% ~~~~~~~~~~~~~~~~~~~
if (cv) % Case: variable parameters.
n = (1:N)/N ; % Define the variable parameters.
a = a*cos(oa*n) ;
b = b*sign(cos(ob*n)) ;
n = n*oc ;
c = c*sin(n)./n ;
V = e(1) ;
D = V ;
for n=2:N % Generate the noise & output.
V = [V ; e(n)+c(n)*e(n-1)] ;
D = [D ; -a(n)*D(n-1)+b(n)*u(n-1)+e(n)+c(n)*e(n-1)] ;
end ;
D = iddata(D,u) ;
P.a = [1 a] ;
P.b = [0 b] ;
P.c = [1 c] ;
Else % Case: constant parameters.
V = filter(P.c,1,e) ; % Generatng the colored noise.
D = iddata(sim(P,[u e]),u) ; % Output data.
end ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (isempty(D))
war_err(E1) ;
return ;
end ;
if (~isa(D,'IDDATA'))
war_err(E2) ;
return ;
end ;
if (isempty(D.u))
war_err(E3) ;
return ;
end ;
if (nargin < 2)
war_err(E4) ;
return ;
end ;
if (isempty(si))
war_err(E4) ;
return ;
end ;
si = vectoriz(si) ;
if (length(si) < 3)
war_err(E5) ;
return ;
end ;
si = abs(round(si(1:3))) ;
si(3) = max(1,si(3)) ;
na = si(1) ;
nb = si(2) ;
nk = si(3) ;
if (nargin < 3)
f = D.u ;
end ;
if (isempty(f))
f = D.u ;
end ;
f = vectoriz(f)' ;
n = length(f) ;
N = length(D.u) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
n = min(n,N) ;
f = [f(1:n) ; zeros(N-n,1)];
if (nargin < 4)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
lambda = abs(lambda(1)) ;
if (~lambda)
war_err(E6) ;
return ;
end ;
if (lambda>1)
lambda = 1/lambda ;
end ;
k = na+nb+nk-1 ;
if (nargin < 5)
theta0 = zeros(k,1) ;
end ;
if (isempty(theta0))
theta0 = zeros(k,1) ;
end ;
theta0 = vectoriz(theta0)' ;
n = min(length(theta0),k) ;
theta0 = [theta0(1:n) zeros(k-n,1)] ;
if (nargin < 6)
P0 = eye(k) ;
end ;
if (isempty(P0))
P0 = eye(k) ;
end ;
P0 = P0(:,:) ;
if ((size(P0,1)-k) | (size(P0,2)-k))
war_err(E7) ;
return ;
end ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (nargin < 7)
phi0 = zeros(k,1) ;
end ;
if (isempty(phi0))
phi0 = zeros(k,1) ;
end ;
phi0 = vectoriz(phi0)' ;
n = min(length(phi0),k) ;
phi0 = [phi0(1:n) zeros(k-n,1)] ;
if (nargin < 8)
z0 = zeros(k,1) ;
end ;
if (isempty(z0))
z0 = zeros(k,1) ;
end ;
z0 = vectoriz(z0)' ;
n = min(length(z0),k) ;
z0 = [z0(1:n) zeros(k-n,1)] ;
%
% Centering the data
% ~~~~~~~~~~~~~~~~~~
D.y = D.y - mean(D.y) ;
D.u = D.u - mean(D.u) ;
f = f - mean(f) ;
%
% Recursive identification and simulation
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
nb = nb+nk-1 ; % Initialization.
D = iddata([zeros(nb,1) ; -phi0(na:-1:1) ; D.y], ...
[zeros(na,1) ; phi0(k:-1:(na+1)) ; D.u]) ;
f = [z0(k:-1:1) ; f] ;
phi = [-D.y((k-1):-1:(nb+1)) ; 0 ; D.u((k-1):-1:(na+1)) ; 0] ;
z = [f((k-1):-1:1) ; 0] ;
P = P0 ;
si = theta0 ;
for n=(k+1):(k+N) % Estimation and simulation.
phi = [-D.y(n-1) ; phi(1:(na-1)) ; ...
D.u(n-1) ; phi((na+1):(k-1))] ;
z = [f(n-1) ; z(1:(k-1))] ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
nk = P*z ;
nk = nk/(lambda+phi'*nk) ; % Gain vector.
P = (P-nk*phi'*P)/lambda ; % Next matrix.
ypred = [ypred ; phi'*si] ; % Predicted output.
si = si + nk*(D.y(n)-ypred(end)) ; % Upgrade parameters.
theta = [theta ; si'] ; % Save parameters.
end ;
%
% END
%
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
%CCRRKKVVQQNNWWNN
%
% ISLAB_7A Module that performs off-line identification of
% an ARMAX model approximating a general purpose
% model around a specified frequency.
%
function [D,F,M] = islab_7a(N,sigma,lambda)
%
% Inputs: N # simulation period (250, by default)
% sigma # standard deviation of PRB input
% (1, by default);
% lambda # standard deviation of white noise
% (1, by default)
%
% Outputs: D # IDDATA object representing the I/O data
% to be employed within an on-line
% identification procedure
% F # structure representing the Butterworth
% filter applied on the PRB input signal
% in order to provide the data D
% F.A -> filter denominator
% F.B -> filter numerator
% F.fc -> cut-off frequency index
% (scalar or 2-length vector
% in range (0,1) - see MATLAB
% function BUTTER)
% M # IDMODEL object representing the ARMAX
% identified using the off-line PEM
%
% Explanation: Data generated by a general purpose model are
% employed to identify an approximative ARMAX
% model around a specified frequency, through
% the off-line PEM. The method involves designing
% of a Butterworth filter that has to be applied
% on the PRB input signal. The data resulted when
% stimulating the general model with the filtered
% data are returned, as well as the filter
% characteristics and the structural indices of
% the off-line identified ARMAX model.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (nargin < 2)
sigma = 1 ;
end ;
if (isempty(sigma))
sigma = 1 ;
end ;
sigma = abs(sigma(1)) ;
if (nargin < 1)
N = 250 ;
end ;
if (isempty(N))
N = 250 ;
end ;
N = abs(fix(N(1))) ;
if (~N)
N = 250 ;
end ;
%
% Generate the raw identification data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fu = [] ; % Initialization.
Fy = [] ;
u = sigma*randn(N,nr) ; % Gaussian PRB inputs.
for n=1:nr
% Power spectral density of input.
Fu = [Fu abs(fft(xcov(u(:,n))))] ;
D = gdata_cp(u(:,n),lambda) ; % Output data.
% Power spectral density of output.
Fy = [Fy abs(fft(xcov(D.y)))] ;
end ;
%
% Evaluate the process spectrum
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fu = Fu(1:N,:) ; % Use symmetry to reduce size.
Fy = Fy(1:N,:) ;
Fu(Fu<eps) = eps ; % Try to avoid division by zero.
F = Fy./Fu ; % Noised spectrum.
F = mean(F') ; % Averaged spectrum.
F(F<eps) = eps ; % Conversion to deciBells.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
F = dB(F)/2 ;
omega = pi*(0:(N-1))/N ; % Normalized pulsation axis.
%
% Plot the process spectrum
% ~~~~~~~~~~~~~~~~~~~~~~~~~
figure(FIG),clf
fig_look(FIG,1.5) ;
plot(omega,F,'-b') ;
n = scaling(F,0.05) ; % Re-scale the axes.
axis([0 pi n]) ;
grid ;
title('Raw spectrum of data provider process') ;
xlabel('Normalized pulsation [rad]') ;
ylabel('Spectral power [dB]') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
text(2.25,n(1)-0.08*(n(2)-n(1)),'<Go to command window>') ;
FIG = FIG+1 ;
%
% Select the cut-off pulsation(s) of filter
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n = input(M1) ; % Ask for filter type.
if (isempty(n))
n = 2 ; % Band-pass filter by default.
end ;
n = abs(round(n(1))) ;
if (n<2)
n = 1 ;
M1 = 'low' ;
else
n = 2 ;
M1 = 'band' ;
end ;
war_err(sprintf(M2,M1)) ; % Confirm filter type.
war_err(M3) ;
disp(PK) ;
pause ;
[omega,Fy] = ginput(n) ; % Ask for cut-off pulsation(s).
omega = sort(omega) ;
M1 = [] ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (n>1)
M1 = 's' ;
end ;
war_err(sprintf(M4,M1,omega)) ; % Display cut-off pulsation(s).
%
% Design the filter
% ~~~~~~~~~~~~~~~~~
F.fc = omega/pi ; % Record cut-off index.
[F.B F.A] = butter(K,F.fc) ;% Design filter.
%
% Plot filter characteristics
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~
omega = pi*(0:(L-1))/L ; % Set normalized pulsation axis.
% Evaluate magnitude Fy and phase Fu.
[Fy,Fu,omega] = dbode(F.B,F.A,1,omega) ;
Fu = pi*Fu/180 ; % Conversion of phase to radians.
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(211)
plot(omega,Fy,'-b') ;
grid ;
axis([0 pi -0.05 1.05]) ;
M1 = 'Frequency characteristics of ' ;
M3 = '-pass Butterworth filter of order K = ' ;
if (n<2)
M2 = 'low' ;
else
M2 = 'band' ;
end ;
title([M1 M2 M3 int2str(K) '.']) ;
ylabel('Magnitude') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
text(1.05*pi*F.fc(n),0.5, ...
['\omega_c = ' sprintf('%4.2f',pi*F.fc(n))]) ;
if (n>1)
text(pi*F.fc(1),0.5, ...
['\omega_c = ' sprintf('%4.2f',pi*F.fc(1))]) ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
subplot(212)
set(FIG,'DefaultTextHorizontalAlignment','center') ;
plot(omega,Fu,'-m') ;
grid ;
n = scaling(Fu,0.05) ;
axis([0 pi n]) ;
xlabel('Normalized pulsation') ;
ylabel('Phase [rad]') ;
FIG = FIG+1 ;
%
% Provide the identification data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
u = sigma*randn(N,1) ; % Unfiltered input.
u = filter(F.B,F.A,u) ; % Filtered input.
[D,V,P] = gdata_cp(u,lambda) ; % Corresponding I/O data.
%
% Off-line identification of ARMAX model
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
M = pem(D) ; % Using PEM to provide a
% general state-space model.
[M1,M2,M3] = polydata(M) ; % Convert the model to
% polynomials.
M = idpoly(M1,M2,M3) ; % Convert polynomials to
% identification model object.
%
% Simulate the ARMAX model
% ~~~~~~~~~~~~~~~~~~~~~~~~
Fy = sim(M,[D.u V.u]) ;
%
% Compute output power spectral densities
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Fy = abs(fft(xcov(Fy))) ; % Simulated output.
Fy = Fy(1:N) ; % Use symmetry to reduce size.
Fy(Fy<eps) = eps ; % Try to avoid division by zero.
Fy = dB(Fy) ; % Conversion to deciBells.
Fu = abs(fft(xcov(D.y))) ; % Measured output.
Fu = Fu(1:N) ; % Use symmetry to reduce size.
Fu(Fu<eps) = eps ; % Try to avoid division by zero.
Fu = dB(Fu) ; % Conversion to deciBells.
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%
% Compare power spectral densities (full bandwidth)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
omega = pi*(0:(N-1))/N ; % Set normalized pulsation axis.
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(311)
plot(omega,Fu,'-r') ;
u = scaling(Fu,0.05) ;
axis([0 pi u]) ;
title(['Performances of ARMAX model: ' ...
'output psd \phi_y (full bandwidth).']) ;
ylabel('Measured [dB]') ;
subplot(312)
plot(omega,Fy,'-b') ;
u = scaling(Fy,0.05) ;
axis([0 pi u]) ;
ylabel('Simulated [dB]') ;
subplot(313)
plot(omega,Fu-Fy,'-m') ;
u = scaling(Fu-Fy,0.05) ;
axis([0 pi u]) ;
xlabel('Normalized pulsation [rad]') ;
ylabel('Error [dB]') ;
FIG = FIG+1 ;
%
% Compare power spectral densities (filter bandwidth)
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
n = (omega>=(F.fc(1)*pi)) ; % Isolate the filter bandwidth.
if (length(F.fc>1))
n = n & (omega<=(F.fc(2)*pi)) ;
end ;
omega = omega(n) ;
figure(FIG),clf
fig_look(FIG,1.5) ;
subplot(311)
plot(omega,Fu(n),'-r') ;
u = scaling(Fu(n),0.05) ;
axis([omega(1) omega(end) u]) ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 7)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
lambda = abs(lambda(1)) ;
if (~lambda)
lambda = 1 ;
end ;
if (nargin < 6)
U = 0.5 ;
end ;
if (isempty(U))
U = 0.5 ;
end ;
U = U(1) ;
if (abs(U)<eps)
U = 0.5 ;
end ;
if (nargin < 5)
Ts = 0.1 ;
end ;
if (isempty(Ts))
Ts = 0.1 ;
end ;
Ts = abs(Ts(1)) ;
if (Ts<eps)
Ts = 0.1 ;
end ;
if (nargin < 4)
Tmax = 80 ;
end ;
if (isempty(Ts))
Tmax = 80 ;
end ;
Tmax = abs(Tmax(1)) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (Tmax<eps)
Tmax = 80 ;
end ;
Tmax = max(250*Ts,Tmax) ;
if (nargin < 3)
T0 = 0.5 ;
end ;
if (isempty(T0))
T0 = 0.5 ;
end ;
T0 = T0(1) ;
if (abs(T0)<eps)
T0 = 0.5 ;
end ;
if (nargin < 2)
K0 = 4 ;
end ;
if (isempty(K0))
K0 = 4 ;
end ;
K0 = K0(1) ;
if (abs(K0)<eps)
K0 = 4 ;
end ;
if (nargin < 1)
mt = 0 ;
end ;
if (isempty(mt))
mt = 0 ;
end ;
mt = abs(round(mt(1))) ;
%
% Generate identification data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[D,V] = gdata_fp(0,K0,T0,Tmax,Ts,U,lambda) ;
%
% Estimate discrete time parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (~mt)
M = oe(D,[2 2 1]) ; % Model of tipe OE.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
else
M = arx(D,[2 2 1]) ; % Model of type ARX.
end ;
%
% Estimate physical parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (~mt) % OE model.
F.K = sum(M.b)/(1-M.f(3))/Ts ;
F.T = Ts*(M.f(3)*M.b(2)+M.b(3))/sum(M.b)/(1-M.f(3)) ;
% F.T = -Ts/log(M.f(3)) ;
else % ARX model.
F.K = sum(M.b)/(1-M.a(3))/Ts ;
F.T = Ts*(M.a(3)*M.b(2)+M.b(3))/sum(M.b)/(1-M.a(3)) ;
% F.T = -Ts/log(M.a(3)) ;
end ;
%
% Display physical parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~
war_err(M1) ;
disp(M2) ;
disp(sprintf(M3,K0,F.K)) ;
disp(sprintf(M4,T0,F.T)) ;
war_err(PK) ;
pause ;
%
% Plot I/O data
% ~~~~~~~~~~~~~
Tmax = Ts*round(Tmax/Ts) ; % Correct Tmax.
t = 0:Ts:Tmax ; % Set time axis.
figure(FIG),clf
fig_look(FIG,1.5) ;
plot(t,D.u,'-b',t,D.y,'-r') ;
FN = scaling([D.u D.y]) ; % Re-scale the axes.
axis([0 Tmax FN]) ;
title('Input-output data provided by a DC engine.') ;
xlabel('Time [s]') ;
ylabel('Magnitude') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
legend('input (square wave)','output',0) ;
FIG = FIG+1 ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isempty(Ts))
Tmax = 80 ;
end ;
Tmax = abs(Tmax(1)) ;
if (Tmax<eps)
Tmax = 80 ;
end ;
Tmax = max(250*Ts,Tmax) ;
if (nargin < 3)
T0 = 0.5 ;
end ;
if (isempty(T0))
T0 = 0.5 ;
end ;
T0 = T0(1) ;
if (abs(T0)<eps)
T0 = 0.5 ;
end ;
if (nargin < 2)
K0 = 4 ;
end ;
if (isempty(K0))
K0 = 4 ;
end ;
K0 = K0(1) ;
if (abs(K0)<eps)
K0 = 4 ;
end ;
if (nargin < 1)
mt = 0 ;
end ;
if (isempty(mt))
mt = 0 ;
end ;
mt = abs(round(mt(1))) ;
%
% Generate identification data
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
war_err(WB) ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
[ID,V,P] = gdata_fp(cv,K0,T0,Tmax,Ts,U,lambda) ;
if (~cv)
FN = ones(1,length(ID.y)) ;
P.num{1} = K0*FN ;
P.den{1} = T0*FN ;
end ;
%
% Estimate and pursue discrete time parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
if (~mt) % Model of tipe OE.
[theta,SD] = roe(ID,[2 2 1],'ff',0.999) ;
theta = theta(:,[3 4 1 2]) ; % theta = [B|F]->[F|B].
else % Model of type ARX.
[theta,SD] = rarx(ID,[2 2 1],'ff',0.999) ;
% theta = [A|B].
end ;
war_err(WE) ;
SD = iddata(SD,V.u,Ts) ; % Pack simulation data.
%
% Estimate physical parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
F.K = (theta(:,3)+theta(:,4))./(1-theta(:,2))/Ts ;
F.T = Ts*(theta(:,2).*theta(:,3)+theta(:,4))./ ...
(theta(:,3)+theta(:,4))./(1-theta(:,2)) ;
% F.T = -Ts./log(theta(:,2)) ;
%
% Plot I/O data
% ~~~~~~~~~~~~~
Tmax = Ts*round(Tmax/Ts) ; % Correct Tmax.
t = 0:Ts:Tmax ; % Set time axis.
figure(FIG),clf
fig_look(FIG,1.5) ;
plot(t,ID.u,'-b',t,ID.y,'-r') ;
FN = scaling([ID.u ID.y]) ; % Re-scale the axes.
axis([0 Tmax FN]) ;
title('Input-output data provided by a DC engine.') ;
xlabel('Time [s]') ;
ylabel('Magnitude') ;
set(FIG,'DefaultTextHorizontalAlignment','left') ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
% BEGIN
%
% Constants
% ~~~~~~~~~
nu = 1/7 ; % Fraction representing the ratio between
% the period of input square wave and
% the simulation period.
a = [3 -3 1] ; % Coefficients of parabola that
% defines the variable gain K:
% K(t) = K0*a*
% [t^2 ; t*Tmax ; Tmax^2]/(Tmax^2).
b = [0.5 10*pi] ; % Relative amplitude (b(1)) and
% pulsation of harmonic wave (b(2))
% that defines the variable
% time constant T:
% T(t) = T0*(1+b(1)*sin(b(2)*t/Tmax)).
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 7)
lambda = 1 ;
end ;
if (isempty(lambda))
lambda = 1 ;
end ;
lambda = abs(lambda(1)) ;
if (nargin < 6)
U = 0.5 ;
end ;
if (isempty(U))
U = 0.5 ;
end ;
U = U(1) ;
if (abs(U)<eps)
U = 0.5 ;
end ;
if (nargin < 5)
Ts = 0.1 ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isempty(Ts))
Ts = 0.1 ;
end ;
Ts = abs(Ts(1)) ;
if (Ts<eps)
Ts = 0.1 ;
end ;
if (nargin < 4)
Tmax = 80 ;
end ;
if (isempty(Ts))
Tmax = 80 ;
end ;
Tmax = abs(Tmax(1)) ;
if (Tmax<eps)
Tmax = 80 ;
end ;
Tmax = max(250*Ts,Tmax) ;
if (nargin < 3)
T0 = 0.5 ;
end ;
if (isempty(T0))
T0 = 0.5 ;
end ;
T0 = T0(1) ;
if (abs(T0)<eps)
T0 = 0.5 ;
end ;
if (nargin < 2)
K0 = 4 ;
end ;
if (isempty(K0))
K0 = 4 ;
end ;
K0 = K0(1) ;
if (abs(K0)<eps)
K0 = 4 ;
end ;
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
if (nargin < 1)
cv = 0 ;
end ;
if (isempty(cv))
cv = 0 ;
end ;
cv = cv(1) ;
%
% Building the continuous model with constant parameters
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
P = tf(K0,[T0 1 0],0) ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
N = round(Tmax/Ts) ; % Set the number of samples.
Tmax = N*Ts ; % Correct Tmax.
N = N+1 ; % Correct number of samples.
e = lambda*randn(N,1) ;
%
% Generating the square wave input
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D = ones(round(N*nu/2),1) ; % Set half period wave.
U = U*[D ; -D] ; % Set input on whole period.
u = kron(ones(fix(1/nu),1),U) ; % Set almost all input.
u = [u ; U] ; % Complete the input.
u = u(1:N) ; % Initial null values.
%
% Generating the data
% ~~~~~~~~~~~~~~~~~~~
t = 0:Ts:Tmax ; % Set time axis.
if (cv) % Case: variable parameters.
n = t/Tmax ; % Set the normalized time.
% Variable gain.
K = K0*(a(1)*(n.^2)+a(2)*n+a(3)) ;
T = T0*(1+b(1)*sin(b(2)*n)) ;% Variable time constant.
u = [0 ; 0 ; u] ; % Initial null input.
D = [0 ; 0] ; % Initial null output.
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
/KPKUKOWNCVQCTGĩKTWVKPG/#6.#$RGPVTW%CRKVQNWN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isempty(u))
u = randn(250,1) ;
end ;
u = vectoriz(u)' ;
N = length(u) ;
%
% Building the process
% ~~~~~~~~~~~~~~~~~~~~
P = idpoly(a,b,c,d,f) ;
%
% Generating the Gaussian white noise
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
e = lambda*randn(N,1) ;
%
% Generating the data
% ~~~~~~~~~~~~~~~~~~~
V = filter(P.c,P.d,e) ; % Generate the colored noise.
V = filter(1,P.a,V) ;
V = iddata(V,e) ; % Record the noise.
% White noise: V.u
% Colored noise: V.y
D = iddata(sim(P,[u e]),u) ; % Output data.
%
% END
%
%&
4WVKPG/#6.#$FGW\IGPGTCN
4
4WWVVKKPPGG/
/###666...###$$$FFGGWW\\IIGGPPGGTTCCNN
%
% FIG_LOOK Controls the appearance of a MATLAB figure.
%
function fig_look(H,lw)
%
% Inputs: H # number of figure handle
% (current figure number by default)
% lw # line width (pt) of graphics
% 0.5 pt, by default);
%
% Outputs: -------
%
% Explanation: Some parameters that change the general aspect
% (look) of the figure with handle H may be
% changed. If H is missing or empty, the current
% figure is affected. The second parameter, if
% specified and non-empty stands for the desired
% line width of graphics.
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
% Last upgrade: February 5, 2003
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
global FIG ; % Figure number handler
% (to be set before running the routine).
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
if (nargin < 1)
H = gcf ;
end ;
if (isempty(H))
H = gcf ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (nargin < 2)
lw = 0.5 ;
end ;
if (isempty(lw))
lw = 0.5 ;
end ;
lw = abs(lw) ;
if (~lw)
lw = 0.5 ;
end ;
%
% Fixing the look
% ~~~~~~~~~~~~~~~
% Axes:
set(H,'DefaultAxesFontName','Helvetica') ;
set(H,'DefaultAxesFontSize',14) ;
set(H,'DefaultAxesFontWeight','demi') ;
% or 'light', 'normal', 'bold'
set(H,'DefaultAxesFontAngle','normal') ;
% or 'italic', 'oblique'
set(H,'DefaultAxesLineWidth',1) ;
% 2D graphic line:
set(H,'DefaultLineLineStyle','-') ; % or '-', ':', etc.
set(H,'DefaultLineLineWidth',lw) ;
set(H,'DefaultLineMarkerSize',8) ;
%&
4WVKPG/#6.#$FGW\IGPGTCN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
4WVKPG/#6.#$FGW\IGPGTCN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
%&
4WVKPG/#6.#$FGW\IGPGTCN
if (nargin < 2)
fc = 0.5 ;
end ;
if (isempty(fc))
fc = 0.5 ;
end ;
fc = vectoriz(fc) ;
fc(3:end) = [] ;
fc(fc>1) = 1./fc(fc>1) ;
fc(fc==0) = 0.5 ;
if (nargin < 1)
K = 5 ;
end ;
if (isempty(K))
K = 5 ;
end ;
N = abs(round(K(1))) ;
if (~K)
K = 5 ;
end ;
%
% Design of filters
% ~~~~~~~~~~~~~~~~~
F = [] ; % Magnitude.
phi = [] ; % Phase.
omega = pi*(0:(M-1))'/M ; % Normalized pulsation axis.
for k=1:K
[B,A] = butter(k,fc) ; % Design filter.
[A,B] = dbode(B,A,1,omega) ; % Extract magnitude and phase.
F = [F A] ; % Save magnitude.
phi = [phi B] ; % Save phase [deg].
end ;
phi = pi*phi/180 ; % Convert phase to [rad].
%
% Plotting frequency characteristics
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
figure, clf ;
fig_look(gcf,1.5) ;
subplot(211)
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
plot(omega,F) ;
grid ;
axis([0 pi -0.05 1.05]) ;
A = 'Frequency characteristics of ' ;
B = '-pass Butterworth filters. [K=1...' ;
if (length(fc)<2)
k = 'low' ;
else
k = 'band' ;
end ;
% set(gcf,'DefaultTextHorizontalAlignment','center') ;
title([A k B int2str(K) '].']) ;
ylabel('Magnitude') ;
subplot(212)
plot(omega,phi) ;
grid ;
k = axis ;
axis([0 pi k(3) k(4)+0.05*(k(4)-k(3))]) ;
xlabel('Normalized pulsation') ;
ylabel('Phase [rad]') ;
%
% END
%
% SHOW_TS Displays time series.
%
function show_ts(FIG,y,d,tit)
%
% Inputs: FIG # figure number
% (current figure number, by default)
% y # time series data (row vector)
% d # sampling instants
% (uniform sampling, by default)
% tit # figure title
% (‘A time series’, by default)
%
% Outputs: -------
%
% Explanation: Shows variation of a time series y sampled
% at instants given in d (or evenly spaced, when d
%&
4WVKPG/#6.#$FGW\IGPGTCN
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
if (isempty(d))
d = 1:N ;
end ;
d = vectoriz(d) ;
d = sort(d) ;
M = length(d) ;
if (M>N)
d = d(1:N) ;
elseif (M<N)
m = min(d(2:end)-d(1:(end-1))) ;
d = [d d(end)+m*(1:(N-M))] ;
end ;
if (nargin<4)
tit = 'A time series' ;
end ;
if (isempty(tit))
tit = 'A time series' ;
end ;
m = findstr(tit,'[') ;
if (~isempty(m))
M = findstr(tit,']') ;
if (isempty(M))
M = length(tit) ;
end ;
N = tit((m-1):M) ;
tit((m-1):M) = [] ;
else
N = [] ;
end ;
%
% Show time series
% ~~~~~~~~~~~~~~~~
figure(FIG),clf
fig_look(FIG,1.5) ;
plot(d,y,'-b') ;
m = scaling(y,0.05) ;
axis([d(1) d(end) m]) ;
grid ;
title(tit) ;
%&
4WVKPG/#6.#$FGW\IGPGTCN
xlabel('Momente de esantionare') ;
ylabel(['Amplitudine' N]) ;
%
% END
%
% VECTORIZ Reshapes a vector/matrix into a row vector.
%
function v = vectoriz(A)
%
% Input: A # Vector or matrix
%
% Output: v # Row vector
%
% Explanation: Reshapes the matrix or vector A into a
% row vector v. In case of matrices, the columns
% are transposed and successively enumerated
% on the row vector.
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
%
% Last upgrade: March 24, 2003
%
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
% Messages
% ~~~~~~~~
FN = '<VECTORIZ>: ' ;
E1 = [FN 'Missing input. Empty output. Exit.'] ;
%
% Faults preventing
% ~~~~~~~~~~~~~~~~~
v = [] ;
if (nargin < 1)
war_err(E1) ;
return ;
end ;
%&
#URGEVGRTCEVKEGÉP/QFGNCTGCĩK+FGPVKHKECTGC5KUVGOGNQT
% Vectorizing
% ~~~~~~~~~~~
v = A ;
[m,n] = size(v) ;
if ((m>1) & (n>1))
v = reshape(v,1,m*n) ;
elseif (m>1)
v = v' ;
end ;
%
% END
%
% WAR_ERR Displays a message of warning or error.
%
function war_err(msg)
%
% Input: msg # Message to be displayed
% Outputs: -------
%
% Explanation: If the message msg is specified and non void,
% then it is displayed. Otherwise (if it is
% missing or void), a standard error message
% is displyed. In any case, unlike MATLAB function
% ERROR, the program that calls this routine
% is not automatically stopped unless
% an error occurred.
%
% Author: Dan Stefanoiu (*)
% Revised: Dan Stefanoiu (*)
%
% Last upgrade: April 13, 2004
%
% Copyright: (*) "Politehnica" University of Bucharest, ROMANIA
% Dept. of Automatic Control & Computer Science
%
% BEGIN
%
if ((nargin > 0) & (length(msg) > 0))
disp(' ') ;
%&
4WVKPG/#6.#$FGW\IGPGTCN
disp(msg) ;
disp(' ') ;
else
disp(' ') ;
disp('<WAR_ERR>: An error occurred.') ;
disp(' ') ;
end ;
%
% END
%
%&
Aspecte practice în Modelarea şi Identificarea Sistemelor
S
Seerriiii ddee ttiim
mpp ddiissppoonniibbiillee ppee C
CDD
Fişier Semnificaţie
ST01.M Rata lunară a numărului de şomeri din SUA între Ianuarie 1973 şi
iulie 1985 [%].
ST02.M Circulaţia monedei belgiene măsurată lunar, timp de 10 ani, între
1980 şi 1990 [miliarde BFr].
ST03.M Media lunară a numărului de pete solare observate între 1976 şi
1989.
ST04.M Distanţa lunară parcursa la U.K. Airlines pe cursele interne între
1982 şi 1989 [mii km].
ST05.M Rata lunară a şomajului în Marea Britanie între 1978 şi 1989 [%].
CD-144