Skip to content

Commit e967ddb

Browse files
committed
Reloading the core functions
1 parent fdc2314 commit e967ddb

39 files changed

+17957
-0
lines changed

core/A_to_f.m

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
%% A_to_f
2+
% Calculates A(f), A in the frequency domain.
3+
%
4+
%% Syntax:
5+
% AL = A_to_f(A, nf)
6+
%
7+
%% Input arguments:
8+
% A - (nChannels x nChannels x p) Recurrence matrix,
9+
% where nChannels is the number of signals, and p the model
10+
% order.
11+
% nf - frequency resolution
12+
%
13+
%% Output argument:
14+
% AL - (nf, nChannels, nChannels) A(f)
15+
16+
% (C) Koichi Sameshima & Luiz A. Baccalá, 2022.
17+
% See file license.txt in installation directory for licensing terms.
18+
19+
function AL = A_to_f(A, nf)
20+
21+
[nChannels, ~, p] = size(A); % Dummy variable ~ is equivalent to nChannels
22+
Jimag = sqrt(-1);
23+
24+
% Variable 'exponents' is an array of FFT exponents, on all frequency range for
25+
% each lag.
26+
exponents = reshape((-Jimag*pi*kron(0:(nf-1),(1:p))/nf),p,nf).';
27+
28+
% Af multiplies the exp(ar) by the matrix A, for all frequencies, the reshape
29+
% and transpose functions are tricks to make the vector calculation possible.
30+
31+
Areshaped = reshape(A, nChannels,nChannels,1,p);
32+
33+
Af = zeros(nChannels,nChannels,nf,p);
34+
for kk = 1:nf
35+
Af(:,:,kk,:) = Areshaped;
36+
end
37+
38+
for i = 1:nChannels
39+
for k = 1:nChannels
40+
Af(i,k,:,:) = reshape(Af(i,k,:,:),nf,p).*exp(exponents);
41+
end
42+
end
43+
44+
Af = permute(Af, [3,1,2,4]);
45+
46+
AL=zeros(nf,nChannels,nChannels);
47+
48+
for kk = 1:nf
49+
temp = zeros(nChannels,nChannels);
50+
for k = 1:p
51+
temp = temp+reshape(Af(kk,:,:,k),nChannels,nChannels);
52+
end
53+
temp = eye(nChannels)-temp;
54+
AL(kk,:,:) = reshape(temp,1,nChannels,nChannels);
55+
end

core/arfitcaps.m

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
%% ARFITCAPS
2+
% Capsule function to call the arfit.m routine, part of
3+
% "ARfit: Multivariate Autoregressive Model Fitting" package.
4+
%
5+
%% Syntax:
6+
% [pf,A,ef] = ARFITCAPS(u,IP)
7+
%
8+
%% Input Arguments
9+
% u: time series
10+
% IP: VAR model order
11+
%
12+
%% Output Arguments
13+
% pf: covariance matrix provided by ARFIT routine
14+
% A: AR estimate matrix provided by ARFIT routine
15+
% ef: forward residuals provided by ARRES routine
16+
%
17+
%% Description:
18+
% ARFITCAPS is capsule that calls arfit.m and arres.m routines, part of
19+
% Autoregressive Model Fitting" package, which implements algorithms
20+
% as described in the following articles:
21+
%
22+
% [1] Neumaier A & Schneider T, 2001. Estimation of parameters and
23+
% eigenmodes of multivariate autoregressive models. ACM Trans Math
24+
% Softw, 27:27-57.
25+
% [2] Schneider T & Neumaier A, 2001. Algorithm 808: ARfit - A Matlab
26+
% package for the estimation of parameters and eigenmodes of multivariate
27+
% autoregressive models. ACM Trans Math Softw, 27:58-65.
28+
%
29+
% If you are interested in using ARfit algorithm for VAR model estimation, we
30+
% advise you to get the software from Tapio Schneider's website at
31+
% https://climate-dynamics.org/software/#arfit
32+
% or from Mathworks.com File Exchange site (verified on August 27, 2021, but
33+
% not tested)
34+
% https://www.mathworks.com/matlabcentral/fileexchange/174-arfit,
35+
%
36+
% and, before using it, verify the license terms, it seems to be a copyrighted
37+
% material by the Association for Computing Machinery, Inc.
38+
%
39+
%% Note: As described by the authors, acf.m in ARfit needs Signal Processing
40+
% Toolbox (TM), as it requires XCORR, a cross-correlation function estimator.
41+
%
42+
% ARfit availability was checked on August 13, 2015, and August 27, 2021. KS
43+
%
44+
% The version we have tested and included was obtained on February 24, 2011 from
45+
% www.gps.caltech.edu/~tapio/arfit/index.html,
46+
% which is now obsolete.
47+
%
48+
%% See also: ARFIT, MVAR, MCARNS, MCARVM, CMLSM
49+
50+
% (C) Koichi Sameshima & Luiz A. Baccalá, 2022.
51+
% See file license.txt in installation directory for licensing terms.
52+
53+
%%
54+
55+
function [pf,A,ef] = arfitcaps(u,IP)
56+
57+
if ~exist('arfit.m','file')
58+
help arfitcaps
59+
error('ARfit.m not found. Get the ARfit package from Tapio Schneider''s web site.')
60+
end;
61+
62+
v = u';
63+
[w, Au, C, sbc, fpe, th] = arfit(v,IP,IP);
64+
pf = C;
65+
66+
if IP >= 20
67+
[siglev,res] = arres(w,Au,v,IP+1);
68+
else
69+
[siglev,res] = arres(w,Au,v);
70+
end;
71+
72+
% Variable 'siglev' is not used.
73+
74+
ef = res';
75+
A = zeros(length(w),length(w),IP);
76+
for i = 1:IP
77+
A(:,:,i) = Au(:,(i-1)*length(w)+1:i*length(w));
78+
wu = ceil(length(ef)*rand(size(w)));
79+
if length(ef)<length(v)
80+
ef = [ef ef(:,wu(1))];
81+
else
82+
ef = ef(:,1:length(v));
83+
end
84+
end

0 commit comments

Comments
 (0)