summaryrefslogtreecommitdiff
path: root/impulseest.sci
blob: 0522a18a289fd9057ade937f19d18cae8394af40 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
function varargout = impulseest(varargin)

// Estimate impulse response and plot of idpoly type model
// 
// Calling Sequence
// impulseest(sys)
// impulseData = impulseest(sys,flag)
//
// Parameters
// sys : idpoly type polynomial 
// flag : boolean type variable
// impulseData : stores impulse response if the flag value is true
//
// Description
// impulseest function estimate and plot the impulse response of idpoly type function.
//
// Examples
//  a = [1 0.2];b = [0 0.2 0.3];
//  sys = idpoly(a,b,'Ts',0.1)
//  impulseest(sys);
// 
// Examples
//  a = [1 0.2];b = [0 0.2 0.3];
//  sys = idpoly(a,b,'Ts',0.1)
//  flag = %T
//  impulseData = impulseest(sys,flag)
//
// Authors
// Ashutosh Kumar Bhargava  

    [lhs,rhs] = argn(0)
    // checking the number of inputs
    if rhs > 2  then
        error(msprintf(gettext("%s: Unexpected number of input arguments "),"impulseest"))
    end
    modelData = varargin(1)
    if typeof(modelData) <> "idpoly" then
        error(msprintf(gettext("%s: Plant model must be ""idpoly"" type. "),"impulseest"))
    end
    // adding noise
    if rhs == 2 then
        noiseFlag = varargin(2)
        if typeof(noiseFlag) <> 'boolean' then
            error(msprintf(gettext("%s: Last input data must be ""boolean"".type "),"impulseest"))
        end
    else
        noiseFlag = %F
    end
    z = poly(0,'z')
    aPoly = poly(modelData.a(length(modelData.a):-1:1),'z','coeff')
    bPoly = poly(modelData.b,'z','coeff')
    fPoly = poly(modelData.f(length(modelData.f):-1:1),'z','coeff')
    afPoly = aPoly*fPoly
    
    bCoeff = modelData.b
    extra = 1
    if ~bCoeff(1,1) then
        afCoeff = coeff(afPoly)
        bLength = length(bCoeff);afLength = length(afCoeff)
        if bLength == afLength then
            extra = 1
        else
            extra = z^-(bLength-afLength)
        end
    end
    
    bPoly = poly(modelData.b(length(modelData.b):-1:1),'z','coeff')
    sys = syslin('d',bPoly,afPoly)*extra
    if size(find(gsort(abs(roots(sys.den)))>1),'*') then
        tempRoot = clean(roots(sys.den))
        [sorted index] = gsort(abs(real(tempRoot)))
        tempRoot = tempRoot(index)
        n = 26/log10(abs(real(tempRoot(1))))
         
    else
        finalValue = horner(sys,1)*1
        n = 10
        tempValue = sum(ldiv(sys.num,sys.den,n))
        if finalValue >= 0 then
            while finalValue*0.99 >= tempValue || n >=1000
                n = n+5
                tempValue = sum(ldiv(sys.num,sys.den,n))
            end
        elseif finalValue < 0 then
            while finalValue*0.99 <tempValue || n >=1000
                n = n+5
                tempValue = sum(ldiv(sys.num,sys.den,n))
            end
            if n > 100 then
                n = 100
            end  
        end
    end
    uData = [1 zeros(1,n)]
    yData = flts(uData,sys)
    timeData = (0:(n))*modelData.Ts
//     pause
    if noiseFlag then
        varargout(1) = yData'
    else
        stem(timeData,yData)
        h = gcf()
        
        h.figure_name= "Plant Impulse Response"
        varargout(1) = []
    end
endfunction