-
Notifications
You must be signed in to change notification settings - Fork 5
/
NonUniform4thOrderANFFixedBlock.m
147 lines (120 loc) · 4.32 KB
/
NonUniform4thOrderANFFixedBlock.m
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function [X1,X2,M] = NonUniform4thOrderANFFixedBlock (Y, modifiedt, initialFreq, gamma, xi)
%----------Y = 1x N input signal with unknown Frequency---------%
%----------T = uniform sampling interval------------------------%
N = length(Y);
%---State of Agent 1
X1 = zeros(1,N); %Output 1
DX1 = 0;
D2X1 = 0;
D3X1 = 0;
D4X1 = 0;
%---State of Agent 2
X2 = zeros(1,N); %Output 2
DX2 = 0;
D2X2 = 0;
D3X2 = 0;
D4X2 = 0;
%---State of Agent 3
M = zeros(1,N); %Output 3
M(1) = initialFreq;
DM = 0;
D2M = 0;
D3M = 0;
D4M = 0;
X3 = 0;
DX3 = 0;
D2X3 = 0;
D3X3 = 0;
D4X3 = 0;
X4 = 0;
DX4 = 0;
D2X4 = 0;
D3X4 = 0;
D4X4 = 0;
X5 = 0;
DX5 = 0;
D2X5 = 0;
D3X5 = 0;
D4X5 = 0;
X6 = 0;
DX6 = 0;
D2X6 = 0;
D3X6 = 0;
D4X6 = 0;
DY = 0;
Y_Estimated_Derivatives = zeros (1,N);
%-------------------------------Hsu Globally convergent Estimator-------%
for i =2:N
% DY = [Y(i)-Y(i-1)+T^2*M(i-1)^2*Y(i-1)/2 + T^4*M(i-1)^4*Y(i-1)/24]/(T - M(i-1)^2*T^3/6); % Using Backward Differentiation Formula
T = double(modifiedt(i)- modifiedt(i-1));
% T = 0.001;
DY = X1(i-1)*M(i-1)*(-2*xi);
% DY = [Y(i)-Y(i-1)*(1 - T^2*M(i-1)^2/2 + T^4*M(i-1)^4/24) ]/(T-M(i-1)^2*T^3/6);
Y_Estimated_Derivatives(i) = [Y(i)-Y(i-1)*(1 - T^2*M(i-1)^2/2 + T^4*M(i-1)^4/24) ]/(T-M(i-1)^2*T^3/6);
% if (i > 2)
% del_T = double(modifiedt(i-1)- modifiedt(i-2));
% DY_2 = [Y(i-2)-Y(i-1)*(1 - del_T^2*M(i-1)^2/2 + del_T^4*M(i-1)^4/24) ]/(-del_T+M(i-1)^2*del_T^3/6);
% DY = (DY+DY_2)/2;
% end
X3 = M(i-1)^2;
X4 = X3*Y(i-1);
X5 = 2*xi*M(i-1)*X2(i-1);
X6 = X1(i-1)*X3;
%----------------------------1st order--------------------------------%
DX1 = X2(i-1);
DX2 = X4 - X5 - X6;
DM = -gamma*(X4 - X5)*X1(i-1);
DX3 = 2*M(i-1)*DM;
DX4 = X3*DY+Y(i-1)*DX3;
DX5 = 2*xi*(M(i-1)*DX2+ X2(i-1)*DM);
DX6 = X1(i-1)*DX3 + X3*DX1;
%---------------------------------------------------------------------%
%-----------------------2nd Order-------------------------------------%
D2X1 = DX2;
D2X2 = DX4 - DX5 - DX6;
D2M = -gamma*( (X4 - X5)*DX1 + (DX4 - DX5)*X1(i-1));
D2X3 = 2*(M(i-1)*D2M+ DM^2);
D2X4 = -X3^2*Y(i-1) + 2*DX3*DY+Y(i-1)*D2X3;
D2X5 = 2*xi*(M(i-1)*D2X2+ 2*DM*DX2 + X2(i-1)*D2M);
D2X6 = X1(i-1)*D2X3 + 2*DX3*DX1 +X3*D2X1;
%---------------------------------------------------------------------%
%-----------------------3rd Order-------------------------------------%
D3X1 = D2X2;
D3X2 = D2X4 - D2X5 - D2X6;
D3M = -gamma*( (X4 - X5)*D2X1 + 2*(DX4 - DX5)*DX1 + (D2X4 - D2X5)*X1(i-1));
D3X3 = 2*(M(i-1)*D3M+ 3*DM*D2M);
D3X4 = -X3*(M(i-1)^2*DY+2*M(i-1)*DM*Y(i-1)) - 3*X4*DX3+ 3*D2X3*DY+Y(i-1)*D3X3;
D3X5 = 2*xi*(M(i-1)*D3X2+ 3*D2M*DX2 +3*DM*D2X2 + X2(i-1)*D3M);
D3X6 = X1(i-1)*D3X3 + 3*D2X3*DX1+ 3*DX3*D2X1 +X3*D3X1;
%---------------------------------------------------------------------%
%-----------------------4th Order-------------------------------------%
D4X1 = D3X2;
D4X2 = D3X4 -D3X5 - D3X6;
D4M = -gamma*((X4 - X5)*D3X1 + 3*(D2X4 - D2X5)*DX1 + 3*(DX4 - DX5)*D2X1 + (D3X4 - D3X5)*X1(i-1) );
D4X3 = 2*(M(i-1)*D3M+ 3*DM*D2M);
D4X4 = -X3*(M(i-1)^2*DY+2*M(i-1)*DM*Y(i-1)) - 3*X4*DX3+ 3*D2X3*DY+Y(i-1)*D3X3;
D4X5 = 2*xi*(M(i-1)*D3X2+ 3*D2M*DX2 +3*DM*D2X2 + X2(i-1)*D3M);
D4X6 = X1(i-1)*D3X3 + 3*D2X3*DX1+ 3*DX3*D2X1 +X3*D3X1;
%---------------------------------------------------------------------%
%-----------Agents- Solving n Linear Differential Equations------%
X1(i) = X1(i-1) + T*DX1 + T^2/2*D2X1 + T^3/6*D3X1+ T^4/24*D4X1;
X2(i) = X2(i-1) + T*DX2 + T^2/2*D2X2 + T^3/6*D3X2+ T^4/24*D4X2;
M(i) = M(i-1) + T*DM + T^2/2*D2M + T^3/6*D3M+ T^4/24*D4M;
%------------------------------------------------------------------%
end
%--------Plotting the derivative of the signal in ahighly noisy enviornment
% A = 1;
% phi = pi/2;
% f = 60;
%
% Y_derivative = A *2*pi*f* cos (2*pi*f*modifiedt + phi );
%
% figure, plot (modifiedt, Y_derivative, modifiedt, Y_Estimated_Derivatives,'LineWidth' , 1.5)
% Ax = legend ('True 1^{st} Derivative of y', 'Numerical Differentiator, Dy')
% Ax.FontSize = 14;
% ylabel ('Amplitude (units)')
% xlabel ('Time(s)')
% axis([.4 .6 -3000 3000])
% grid on
%----------------------------------------------------------------------%
end