-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproblemII_assignment.m
255 lines (221 loc) · 7.27 KB
/
problemII_assignment.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
%-------------------------------(Problem I (Part B)-----------------------%
%Extend the 1-nearest neighbours algorithm developed in Lab A.II to create
%k-nearest neighbours soltuions
% K = 1, 3 and 5
% Performed with only four FEATURES obtained during Lab A.I
% Load Faults from Lab A.I (After Applying PCA to 4 Features)
load Fault_1 %Bearing Fault
load Fault_2 % Gearmesh Fault
load Fault_3 % Imbalance Fault
load Fault_4 % Misalignment Fault
load Fault_5 % Resonance Fault
% load bearing_features.mat % f_b
% load gearmesh_features.mat % f_g
% load imbalance_features.mat %f_i
% load misalignment_features.mat %f_m
% load resonance_features.mat %f_r
%Training Data - used in construction of the classifier
%Test Data - used to evaluate the goodness or performance of the classifier
NoOfTrainingCases = 35;
NoOfTestingCases = length(Fault_1) - NoOfTrainingCases; % 15 vectors
% Trainging Data
trainingSet = [Fault_1(1:NoOfTrainingCases,:);
Fault_2(1:NoOfTrainingCases,:);
Fault_3(1:NoOfTrainingCases,:);
Fault_4(1:NoOfTrainingCases,:);
Fault_5(1:NoOfTrainingCases,:)]; %Training Data
% Testing set by last 15 vectors
testingSet = [Fault_1(NoOfTrainingCases+1:end,:);
Fault_2(NoOfTrainingCases+1:end,:);
Fault_3(NoOfTrainingCases+1:end,:);
Fault_4(NoOfTrainingCases+1:end,:);
Fault_5(NoOfTrainingCases+1:end,:)]; %Testing Data
%----------Initial Varaiables for k-nearest neighbour search------------%
% Label sets
trainingTarget = [ones(1,NoOfTrainingCases),...
ones(1,NoOfTrainingCases)*2,...
ones(1,NoOfTrainingCases)*3,...
ones(1,NoOfTrainingCases)*4,...
ones(1,NoOfTrainingCases)*5];
testingTarget = [ones(1,NoOfTestingCases),...
ones(1,NoOfTestingCases)*2,...
ones(1,NoOfTestingCases)*3,...
ones(1,NoOfTestingCases)*4,...
ones(1,NoOfTestingCases)*5];
%----------------------------k-nearest neighbour search-----------------%
% Total number of cases
totalNoOfTestingCases = NoOfTestingCases * 5;
totalNoOfTrainingCases = NoOfTrainingCases * 5;
inferredlabels = zeros(1,totalNoOfTestingCases);
% This loop cycles through each unlabelled item:
for unlabelledCaseIdx = 1:totalNoOfTestingCases
unlabelledCase = testingSet(unlabelledCaseIdx,:);
% As any distance is shorter than infinity
shortestDistance = inf;
% Assign a temporary label
shortestDistanceLabel = 0;
currentDist = 0;
% This loop cycles through each labelled item:
for labelledCaseIdx = 1:totalNoOfTrainingCases
labelledCase = trainingSet(labelledCaseIdx, :);
% Calculate the Euclidean distance:
currentDist(labelledCaseIdx) = euc(unlabelledCase, labelledCase);
end
% find the 3 shortest distances
[shortestDistance,I] = mink(currentDist,5); % 3 for k =3
% match the 3 shortest distances with the corresponding labels
shortestDistanceLabel_temp = trainingTarget(I);
% Most frequent label of k (k = 3 in this case) shortest distances
% Mode used for most frequenct values
[shortestDistanceLabel,F] = mode(shortestDistanceLabel_temp);
% if all 3 shortest distances fall into different fault types,
% label the testing data as the type with shortest distance
if F == 1
shortestDistanceLabel = shortestDistanceLabel_temp(1);
end
% Assign the found label to the vector of inferred labels:
inferredLabels(unlabelledCaseIdx) = shortestDistanceLabel;
end
% No. of correctly classified samples
Nc = length(find(inferredLabels == testingTarget));
% No. of all samples
Na = length(testingTarget);
%Accuracy of Classification ACC
Acc = 100*(Nc/Na); % Accuracy of classification (ACC)
disp(Acc)
%%
%-------------------------------Problem II (Assignment)-------------------%
% ---------PART A-----------%
%Description ; Wind Turbine manufacturing company & tasked to design a
% multisensor signal estimation & health monitoring system for the blade
% pitching mechanism of the wind turbine.
%Observation model y(t) = x(t) + v(t) (Part A)
% x(t) = ax(t-1) + w(t)
%Prior Knowledge
% sensor noise v ~ N(0,9)
% X - Uniformly distributed (0,30)
load encoder.mat
T = 100; % Data (Encoder) rows / measurement
var_noise = 9; %variance
max = 30;
min = 0;
k = 1;
for T = [1: 100] % Case 1 = [1:100] & Case 2 = [1:5]
t = var_noise/T;
t1 = sqrt(2*pi*t);
x_mean = mean(encoder(1:T));
numerator = @(x) ((x/t1) .* exp(-((x-x_mean).^2 / (2*t))));
denominator = @(x) ((1/t1) .* exp(-(x-x_mean).^2 / (2*t)));
num_int = integral(numerator,0,30); % 0 & 30 - limits
den_int = integral(denominator,0,30); % 0 & 30 - limits
x_MMSE(k) = num_int / den_int;
k = k+1;
plot (x_MMSE); hold on
xlabel ('Measurement')
ylabel ('MMSE')
title ('MMSE wrt Measurement - Case 2 = [1:5] ')
plot(x_mean,'o');
legend('MMSE','Mean');hold off
end
%%
%----------PART B--------------------%
% Prior knowledge X is Gaussian distributed N(15,4)
% Bayesian Estimation with gaussian Priors
load encoder.mat
var_x=4;
mean_x=15;
i=1;
x_MMSE_Gaus = zeros(1,2); %Pre-allocation for speed
for T=[1: 100] % Case 1 = [1:100] & Case 2 = [1:5]
x_mean=mean(encoder(1:T));
x_MMSE_Gaus(i)=(x_mean/var_noise + mean_x/var_x)/(T/var_noise + 1/var_x);
i=i+1;
plot (x_MMSE_Gaus); hold on
xlabel ('MMSE')
ylabel ('Measurement')
title ('MMSE wrt Measurement - Case 2 - [1:5] ')
plot(x_mean,'--'); hold off
end
%%
%--------------------PART C-----------------%
load straingauge.mat;
theta0 = 3000;
sigma0 = 1;
threshold=20;
straingauge = [0,straingauge];
T = length(straingauge);
g(1)=0; % Initialization / Starts from zero value
%NOTE: Possible drift(gamma) is ignored
gamma = 0; % Therefore gamma = 0
g(1) = 0;
for j=1:T-1
g(j+1)=g(j)+(straingauge(j+1)-theta0)/sigma0 + gamma;
if abs(g(j+1)) > threshold % Crosses the threshold
fault_on_set_time = j+1; %Fault occurs / Note the fault on set time
disp(['Fault occured at ',num2str(fault_on_set_time)])
end
end
%%
%% Problem 2 (c)
% two-sided CUSUM test
% load data
load straingauge.mat
straingauge = [0,straingauge];
% normal operation condition
theta0 = 3000;
sigma0 = 1;
% threshold
delta = 20;
delta_neg = -20;
% data length
T = length(straingauge);
% no model error/drift
gamma = 0;
% 2 one-sided CUSUM test
% decision statistic
g(1) = 0;
G(1) = 0;
%%
% Positive CUSUM
disp('One-Sided Positive CUSUM Test:');
for i = 2:T
g(i) = g(i-1) + (straingauge(i)-theta0)/sigma0 - gamma;
% check if positive
if g(i) < 0
g(i) = 0;
end
% decision rule
if g(i) > delta
fprintf('Alert! Fault occurs at index: %i (T = %i).\n\n',i,i-1);
break
end
end
%%
% Negative CUSUM
disp('One-Sided Negative CUSUM Test:');
for i = 2:T
G(i) = G(i-1) + (straingauge(i)-theta0)/sigma0 - gamma;
% check if negative
if G(i) > 0
G(i) = 0;
end
% decision rule
if G(i) < delta_neg
fprintf('Alert! Fault occurs at index: %i (T = %i).\n\n',i,i-1);
break
end
end
%% Two sided test
disp('Two-Sided CUSUM Test:');
for i = 2:T
g(i) = g(i-1) + (straingauge(i)-theta0)/sigma0 - gamma;
% check if positive
%if g(i) < 0
%g(i) = 0;
%end
% decision rule
if abs(g(i)) > delta
fprintf('Alert! Fault occurs at index: %i (T = %i).\n\n',i,i-1);
%break
end
end