forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARtrain.m
More file actions
27 lines (25 loc) · 683 Bytes
/
Copy pathARtrain.m
File metadata and controls
27 lines (25 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function [a residual]=ARtrain(v,L)
%ARTRAIN Fit autoregressive (AR) coefficients of order L to v.
% [a residual]=ARtrain(v,L)
%
% Inputs:
% v: input sequence
% L : order of the AR model
%
% Outputs:
% a : learned AR coefficients
% residual : error in the prediction of each point
% This uses a simple Gaussian Elimination solver
% -- Levinson Durbin is a recommended alternative
vvhat=zeros(L,1); v=v(:);
vhatvhat=zeros(L,L);
for t=L+1:length(v)
vhat = v(t-L:t-1);
vvhat = vvhat + v(t)*vhat;
vhatvhat = vhatvhat + vhat*vhat';
end
a = (vhatvhat+eps*eye(size(vhatvhat)))\vvhat;
for t=L+1:length(v(:))
vhat = v(t-L:t-1);
residual(t)=v(t)-a'*vhat;
end