summaryrefslogtreecommitdiff
path: root/macros/arParEst.sci
blob: 971bc45b84d925045aff379656b0ceee3060d4fc (plain)
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
function [model_est, var_est, msg_strg] = arParEst(data_in, order, method)
    
    checkNArgin(3, 3, argn(2))
    
    model_est = [];
    var_est   = [];
    
    if isvector(data_in) then
        data_in = data_in(:);
    end
    
    select method
    case 'covariance' then
        x_min_len = 2*order;
    case 'modified' then
        x_min_len = (3*order)/2;
    else 
        msg_strg = 'Error in function arParEst: Unknown estimation method'
        return;
    end
    
    if size(data_in, 1)< x_min_len   then
        if (strcmp(method, 'covariance') == 0) then
            if isvector(data_in) then
                msg_strg = 'length of input vector should be greater than 2 times of the input order of the model'
                return
            else 
                msg_strg = 'number of rows in input matrix should be greater than 2 times of the input order of the model'
                return
            end
        
        elseif (strcmp(method, 'modified') == 0) then
            if isvector(data_in) then
                msg_strg = 'length of input vector should be greater than 3/2 times of the input order of the model'
                return
            else 
                msg_strg = 'number of rows in input matrix should be greater than 3/2 times of the input order of the model'
                return  
            end
        end
    end
    
    if issparse(data_in) then
        msg_strg = 'Input data should not be sparse';
        return
    end
    
    if isempty(order) | order ~= round(order) then
        msg_strg = 'Model order should be an integer';
        return
    end
    
    msg_strg = [];
    model_est = zeros((order + 1), size(data_in, 2));
    var_est = zeros(1, size(data_in, 2));  
    
    for i = 1: size(data_in, 2)
        data_corrmtx = corrmtx(data_in(:, i), order, method);
        data_allcols = data_corrmtx(:, 2:size(data_corrmtx,2));
        data_sincol = data_corrmtx(:, 1);
    
        model_est(:, i) = [1; -data_allcols\data_sincol];
        
        var_tmp = data_sincol'*data_allcols;
        var_est(:, i) = data_sincol'*data_sincol + var_tmp*model_est(2:size(model_est, 1), i);
        
        var_est(:, i) = abs(real(var_est(:, i)));         
    end
    
    model_est = model_est.';
    
    
endfunction
function checkNArgin(min_argin, max_argin, num_of_argin)
    if num_of_argin < min_argin then
        error('Not enough input arguments')
    end
    
    if num_of_argin > max_argin then
        error('Too many input arguments')
    end
        
endfunction