-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestLocation.R
196 lines (158 loc) · 6.38 KB
/
estLocation.R
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
# Helper functions
# ---- Function to compute condition number for 2x2 matrix.
# COND2(X) returns the 2-norm condition number (the ratio of the
# largest singular value of X to the smallest). Large condition
# numbers indicate a nearly singular matrix.
#
# Modified to handle only the 2-norm, eliminate some error checking,
# and other condition testing.
cond2 <- function(x, eps=.Machine$double.eps){
sv <- svd(x)$d # singular values of x
if( any( abs(sv) <= eps )){ # not sure need abs
ans <- Inf
} else {
ans <- max(sv) / min(sv)
}
ans
}
## ESTIMATE TARGET LOCATION ####
estLocation <- function(raw, method=method, kappa=k, tuningConstant = tc){
ansList <- list()
pointEstimate <- NULL
for(i in unique(raw$EstimateID)){ # i = 1
station.locs <- cbind(raw[raw$EstimateID == i, colX],
raw[raw$EstimateID == i, colY])
angles <- raw[raw$EstimateID == i, colBearing]
# ---- Main code
cond.num = 1e15 # For test of singularity.
dist1 = 1
x = station.locs[,1]
y = station.locs[,2]
# Convert bearing to standard radian measure
theta = (90-angles)*pi/180
theta = as.numeric(theta < -pi) *2*pi + theta
s = sin(theta)
c = cos(theta)
z = s*x - c*y
sstar = s
cstar = c
n = length(angles)
w = rep(1, n)
M1 = rbind(sstar, -cstar) %*% cbind(s, -c)
converge = FALSE
trace=T
iter = 0
maxiter = 50
robust = method %in% c("a", "h")
if( all(is.na(k)) ){
kest = TRUE
} else {
kest = FALSE
}
failed = c("less than 2 bearings",
"negative variance estimates",
"solution behind stations",
"failed to converge",
"parallel bearings")
if( cond2(M1)<cond.num ){
M2 = rbind(sstar, -cstar) %*% z
xyhat = solve(M1,M2)
# --- Main loop
while (!converge & (iter<maxiter)){
iter = iter+1
xyold = xyhat
d = sqrt( (xyhat[1]-x)^2 + (xyhat[2]-y)^2 )
if( trace ){
cat(paste("--- Iteration:", iter, "Current solution: "))
cat(xyhat)
cat("\n")
}
if( (n>2) & robust){
# Need 3 or more bearings to calculate weights for robust methods
dxy = c(xyhat)-rbind(x, y)
# muhat = cart2pol(dxy[1,],dxy[2,])
muhat = atan2(dxy[2,],dxy[1,]) # compute angle of 2D system
Cd = cos(theta-muhat)
if( kest ){
Cbar = abs(sum(w*Cd)/sum(w))^(n/(n-2)) # Abs is ad hoc but may avoid temporary numeric problems in iteration
k = (2*(1-Cbar)+(1-Cbar)^2*(0.48794-0.82905*Cbar-1.3915*Cbar^2)/Cbar)
k = ifelse( abs(k) < .Machine$double.eps, Inf, 1/k) # k is Inf if bearings all cross at single point. Happens with n=2 bearings.
}
tt = sqrt(2*k*(1-Cd))
if( method == "a" ){
phit = tc*sin(tt/tc)*(abs(tt)<(tc*pi))
} else {
phit = sign(tt)*apply( cbind(abs(tt), tc), 1, min)
}
same = (Cd==1) # Take care when estimated & observed bearings
tt[same] = 1 # are identical avoid division by zero.
phit[same] = 1 # Usually occurs with just 2 intersecting bearings.
w = phit/tt
} # if robust
sstar = w*(xyhat[2]-y)/(d^3)
cstar = w*(xyhat[1]-x)/(d^3)
M1 = rbind(sstar, -cstar) %*% cbind(s, -c)
if( ((n-sum(!as.logical(w)))>1) & (cond2(M1)<cond.num)){
M2 = rbind(sstar, -cstar) %*% z
xyhat = solve(M1, M2)
converge = sum(abs(xyhat-xyold)<dist1)==2
} else {
break # If either condition above occurs, convergence will very
} # likely fail, so break out of while loop.
} # while
if( converge ){
dxy = c(xyhat) - rbind(x, y)
muhat = atan2(dxy[2,],dxy[1,]) # compute angle of 2D system
Cd = cos(theta-muhat)
if( kest & (n>2) ){
Cbar = (sum(w*Cd)/sum(w))^(n/(n-2)) # Exponent is small sample size correction
k = (2*(1-Cbar)+(1-Cbar)^2*(0.48794-0.82905*Cbar-1.3915*Cbar^2)/Cbar)
k = ifelse( abs(k) < .Machine$double.eps, Inf, 1/k ) # k = Inf only if perfect solution w/ n>2. Only in pathological case
}
if( sum(Cd*w) > 0){ # Weighted average of cosine differences (check
VM = c(xyhat) # on bad solutions behind DASARs)
names(VM) = c("xHat","yHat")
if( kest & (n==2) ){ # Cannot estimate Qhat with only 2 bearings
Qhat = matrix( NA, 2, 2)
outcome = "successful 2 bearings no Kappa"
} else if( kest & all(is.infinite(k)) ){ # Perfect solution -> Zero variance
Qhat = matrix( 0, 2, 2)
outcome = "successful perfect solution Zero Kappa estimate"
} else {
cv = -(sum(k*sstar*c) + sum(k*cstar*s))/2
M3 = cbind(c(sum(k*sstar*s), cv), c(cv, sum(k*cstar*c)))
Qhat = solve(M3)
if( all(diag(Qhat)>0) ){
outcome = "successful" # Successful solution
} else {
outcome = failed[2] # Implausible variance estimate(s)
}
}
} else {
outcome = failed[3] # Bad solution behind DASARs
} # if all(Cd>0)
} else {
outcome = failed[4] # No convergence
} # if converge
} else {
outcome = failed[5]
} # if cond2
# } # if n<=1
if(outcome %in% failed){
VM = c(NA,NA)
Qhat = matrix( NA, 2,2)
w = rep(0,n)
}
dimnames(Qhat) = list(names(VM), names(VM))
names(w) = dimnames(station.locs)[[1]]
ansList[[i]] <- list( EstimateID=i, loc.est = VM, qhat=Qhat, angle = angles,
outcome=outcome, w=w, k=k, tc=tc, method=method )
# t <- data.frame(cbind(data.frame(VM)[1,], data.frame(VM)[2,]))
# names(t) <- c("estEasting", "estNorthing")
# t$EstimateID <- i
# t$TrackID <- unique(raw[i, colTrackID])
# triangulatedPoints <- data.frame(rbind(triangulatedPoints, t))
} # End Main loop
# } # End function
return(ansList)
} #end