-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKnn.java
More file actions
226 lines (191 loc) · 5.37 KB
/
Knn.java
File metadata and controls
226 lines (191 loc) · 5.37 KB
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
package Position;
public class Knn {
public static void main(String[] args) {
// TODO Auto-generated method stub
double[][] RSSI = new double[totaltest][totalbeacon+2];
// double[] RSSI4 = {-74,-64,-89,-84,-72,-55,-88,-97,-96,-60,0.0,1.0};//get
double[] RSSI4 = {-63,-62,-63,0.0,1.0};
double temp[] = new double [totalbeacon+2];
double[] coff = new double[totaltest];
double[] tmpcoff = new double[totaltest];
RSSI = init();
Sort_fliter(RSSI4);
for(int j = 0;j < totalbeacon+2 ; j++){
System.out.print(RSSI4[j]+" ");
}
System.out.println();
for(int i = 0;i < totaltest ; i++){
for(int j = 0;j < totalbeacon+2 ; j++){
temp[j] = RSSI[i][j];
}
Sort_fliter(temp);
for(int j = 0;j < totalbeacon+2 ; j++){
System.out.print(temp[j]+" ");
}
if(likely(temp,RSSI4) > -1)
coff[i] = likely(temp,RSSI4);
else
coff[i] = -10;
}
for(int i = 0;i < totaltest ; i++){
System.out.println(coff[i]+" ");
tmpcoff[i] = coff[i];
}
Sort(coff);
double nowx = 0;
double nowy = 0;
for(int i = 0;i < totaltest ; i++){
for(int k = 1;k <= top ; k++){
if(coff[totaltest-k] == tmpcoff[i]){
nowx += RSSI[i][totalbeacon] ;
nowy += RSSI[i][totalbeacon+1] ;
break;
}
}
}
nowx = nowx/top;
nowy = nowy/top;
System.out.println(nowx +" "+nowy);
// System.out.println(likely(RSSI,RSSI2));
}
private static int totaltest = 6;
private static int totalbeacon = 3;//number of beacon
private static int top = 4;
private static double [][] RSSI = new double [totaltest][totalbeacon+2];
// private static double temp[] = new double [totalbeacon+2];
/* public static double[] settemp(double[] RSSI){
double[] coff = new double[totaltest];
double temp[] = new double [totalbeacon+2];
for(int i = 0;i < totaltest ; i++){
for(int j = 0;j < totalbeacon+2 ; j++){
temp[j] = RSSI[i][j];
}
Sort_fliter(temp);
if(likely(temp,RSSI4) > -1)
coff[i] = likely(temp,RSSI4);
else
coff[i] = -10;
}
return coff;
}*/
public static double[][] init(){
//if cannot read : -150
/* double[][] RSSI = {{-72,-64,-92,-84,-72,-62,-88,-94,-90,-77,4.0,7.0},
{-73,-68,-88,-84,-66,-60,-78,-89,-84,-71,1.0,1.0},
{-80,-80,-64,-84,-92,-88,-68,-64,-71,-71,1.0,0.0}}; //data base
*/
double[][] RSSI = {{-76,-63,-84,7.0,1.0},
{-64,-66,-62,3.0,2.0},
{-75,-61,-65,4.0,4.0},
{-75,-69,-55,4.0,7.0},
{-75,-52,-64,8.0,5.0},
{-67,-55,-59,3.0,6.0},}; //data base
return RSSI;
}
public static void Sort_fliter(double[] rSSI){ //sort and choose top 5
int j;
double n;
//----------------------------------------
int minsave = 1;//choose top 5
double A[] = new double [rSSI.length-2];
int flag[] = new int [rSSI.length-2];
for (int i = 0; i < rSSI.length-2; i++){
A[i] = rSSI[i];
flag[i] =0;
}
for (int i = 1; i < A.length; ++i){
n = A[i];
for (j = i - 1; j >= 0 && A[j] > n; --j)
A[j + 1] = A[j];
A[j + 1] = n;
}
for (int i = 0; i < minsave; i++){
A[i] = 0;
}
for (int i = 0; i < rSSI.length-2; i++){
for (int k = 0; k < rSSI.length-2; k++){
if(rSSI[i] == A[k]) //«O¯d
flag[i] = 1;
}
}
for (int i = 0; i < rSSI.length-2; i++){
if(flag[i] != 1)
rSSI[i] = 0;
}
}
public static void Sort(double[] array)
{
int j;
double n;
for (int i = 1; i < array.length; ++i)
{
n = array[i];
for (j = i - 1; j >= 0 && array[j] > n; --j)
array[j + 1] = array[j];
array[j + 1] = n;
}
}
public static double avg(double[] b){ //couculate average
double average = 0;
double total = 0.0;
for (int i = 0; i < b.length; i++){
total += b[i];
}
average = total / b.length ;
return average;
}
public static double add(double[] array){ //add all
double sum = 0;
for (int i = 0; i < array.length; ++i){
sum = sum + array[i];
}
return sum;
}
public static double likely(double[] RSSI,double[] RSSI2){ //calculate R1 and R2 correlation coefficient
int enough = 0;
double corr = -1;
int c = 0;
double tempup = 0;
for(int i=0;i<totalbeacon;i++){
if(RSSI[i] != 0 && RSSI2[i] != 0)
enough ++;
}
/* for(int i=0;i<totalbeacon;i++){
System.out.print(RSSI[i]+" ");
}
System.out.println();
for(int i=0;i<totalbeacon;i++){
System.out.print(RSSI[i]+" ");
}
System.out.println();*/
// System.out.println(enough);
System.out.println();
if(enough >= 1){//--------------
double A[] = new double [enough];
double B[] = new double [enough];
for(int i=0;i<totalbeacon;i++){
if(RSSI[i] != 0 && RSSI2[i] != 0){
A[c] = RSSI[i];
B[c] = RSSI2[i];
c++;
}
}
double a_avg = avg(A);
double b_avg = avg(B);
// System.out.println("A: "+a_avg+"B: "+b_avg);
double []temX = new double[enough];
double []temY = new double[enough];
for(int j=0;j<enough;j++){
tempup += (A[j] - a_avg)*(B[j] - b_avg);
temX[j] = (A[j] - a_avg)*(A[j] - a_avg);
temY[j] = (B[j] - b_avg)*(B[j] - b_avg);
}
double a = add(temX);
double b = add(temY);
a = Math.sqrt(a);
b = Math.sqrt(b);
corr = tempup / (a*b);
}
return corr;
}
}