Compare commits
No commits in common. "f7339217d67817346bf9155d31dd7d119a174b53" and "c3f46cb6a8dd27fc1cfc40e1c233d5ade9aaa1b3" have entirely different histories.
f7339217d6
...
c3f46cb6a8
|
@ -3,5 +3,3 @@
|
||||||
int DTWfreqDistance(struct data_frame *df);
|
int DTWfreqDistance(struct data_frame *df);
|
||||||
|
|
||||||
int DTWvolDistance(struct data_frame *df);
|
int DTWvolDistance(struct data_frame *df);
|
||||||
|
|
||||||
int DTWfreqvolDistance(struct data_frame *df);
|
|
|
@ -1,4 +1,3 @@
|
||||||
int *getRandoms(int lower, int upper, int count);
|
int *getRandoms(int lower, int upper, int count);
|
||||||
long double distance(struct Point* A, struct Point* B);
|
long double distance(struct Point* A, struct Point* B);
|
||||||
bool Kmeans2(struct data_frame *df);
|
bool Kmeans2(struct data_frame *df);
|
||||||
long double distance2(struct centroid *A, struct Point *B);
|
|
|
@ -76,7 +76,6 @@ gboolean attack_detect_freq(struct data_frame *df)
|
||||||
printf("avg freq: %Lf\n", temp->AVERAGE_OF_FREQUENCY);
|
printf("avg freq: %Lf\n", temp->AVERAGE_OF_FREQUENCY);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
previous = temp;
|
previous = temp;
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
|
@ -175,7 +174,6 @@ gboolean attack_detect_vol(struct data_frame *df)
|
||||||
printf("avg vol: %Lf\n", temp->AVERAGE_OF_VOLTAGE);
|
printf("avg vol: %Lf\n", temp->AVERAGE_OF_VOLTAGE);
|
||||||
return TRUE;
|
return TRUE;
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
previous = temp;
|
previous = temp;
|
||||||
temp = temp->next;
|
temp = temp->next;
|
||||||
|
|
|
@ -137,7 +137,6 @@ int DTWfreqDistance(struct data_frame *df)
|
||||||
free(DTW);
|
free(DTW);
|
||||||
temp->count_track1 = 1;
|
temp->count_track1 = 1;
|
||||||
}
|
}
|
||||||
return temp->result;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
previous = temp;
|
previous = temp;
|
||||||
|
@ -155,6 +154,7 @@ int DTWfreqDistance(struct data_frame *df)
|
||||||
previous->next = bring;
|
previous->next = bring;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
return temp->result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -293,7 +293,6 @@ int DTWvolDistance(struct data_frame *df)
|
||||||
free(DTW);
|
free(DTW);
|
||||||
temp->count_track1 = 1;
|
temp->count_track1 = 1;
|
||||||
}
|
}
|
||||||
return temp->result;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
previous = temp;
|
previous = temp;
|
||||||
|
@ -311,6 +310,7 @@ int DTWvolDistance(struct data_frame *df)
|
||||||
previous->next = bring;
|
previous->next = bring;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
return temp->result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,23 +13,12 @@ struct Point
|
||||||
long double minDist; // default infinite dist to nearest cluster
|
long double minDist; // default infinite dist to nearest cluster
|
||||||
};
|
};
|
||||||
|
|
||||||
struct centroid
|
|
||||||
{
|
|
||||||
long double x, y;
|
|
||||||
long double max_radius;
|
|
||||||
unsigned long long int count;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Kmeans2
|
struct Kmeans2
|
||||||
{
|
{
|
||||||
int idcode;
|
int idcode;
|
||||||
int count;
|
int count;
|
||||||
struct Point *P;
|
struct Point *P;
|
||||||
struct Kmeans2 *next;
|
struct Kmeans2 *next;
|
||||||
struct centroid *C;
|
|
||||||
int no_of_clusters;
|
|
||||||
int isIntialized;
|
|
||||||
bool result;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Kmeans2 *head_of_kmeans2 = NULL;
|
struct Kmeans2 *head_of_kmeans2 = NULL;
|
||||||
|
@ -39,11 +28,6 @@ long double distance(struct Point *A, struct Point *B)
|
||||||
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
|
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
|
||||||
}
|
}
|
||||||
|
|
||||||
long double distance2(struct centroid *A, struct Point *B)
|
|
||||||
{
|
|
||||||
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
|
|
||||||
}
|
|
||||||
|
|
||||||
int *getRandoms(int lower, int upper, int count)
|
int *getRandoms(int lower, int upper, int count)
|
||||||
{
|
{
|
||||||
srand(time(0));
|
srand(time(0));
|
||||||
|
@ -86,10 +70,8 @@ bool Kmeans2(struct data_frame *df)
|
||||||
head_of_kmeans2 = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2));
|
head_of_kmeans2 = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2));
|
||||||
head_of_kmeans2->idcode = to_intconvertor(df->idcode);
|
head_of_kmeans2->idcode = to_intconvertor(df->idcode);
|
||||||
head_of_kmeans2->count = 0;
|
head_of_kmeans2->count = 0;
|
||||||
head_of_kmeans2->isIntialized = 0;
|
|
||||||
head_of_kmeans2->next = NULL;
|
head_of_kmeans2->next = NULL;
|
||||||
head_of_kmeans2->P = NULL;
|
head_of_kmeans2->P = NULL;
|
||||||
head_of_kmeans2->result = true;
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -103,132 +85,9 @@ bool Kmeans2(struct data_frame *df)
|
||||||
printf("count: %d\n",temp->count);
|
printf("count: %d\n",temp->count);
|
||||||
if (temp->count == 0)
|
if (temp->count == 0)
|
||||||
{
|
{
|
||||||
if (temp->isIntialized == 0)
|
|
||||||
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500);
|
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500);
|
||||||
else
|
|
||||||
temp->P = (struct Point *)malloc(sizeof(struct Point) * 100);
|
|
||||||
}
|
}
|
||||||
if (temp->isIntialized == 1 && temp->count != 100)
|
if (temp->count != 500)
|
||||||
{
|
|
||||||
float CURR_FREQ;
|
|
||||||
if (df->dpmu[0]->fmt->freq == '0')
|
|
||||||
{
|
|
||||||
CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
CURR_FREQ = decode_ieee_single(df->dpmu[0]->freq);
|
|
||||||
}
|
|
||||||
float CURR_vol;
|
|
||||||
if (df->dpmu[0]->fmt->phasor == '0')
|
|
||||||
{
|
|
||||||
if (df->dpmu[0]->fmt->polar == '0')
|
|
||||||
{
|
|
||||||
unsigned char s1[2];
|
|
||||||
unsigned char s2[2];
|
|
||||||
strncpy(s1, df->dpmu[0]->phasors[0], 2);
|
|
||||||
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 2);
|
|
||||||
long double v1 = to_intconvertor(s1);
|
|
||||||
long double v2 = to_intconvertor(s2);
|
|
||||||
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
unsigned char s1[2];
|
|
||||||
strncpy(s1, df->dpmu[0]->phasors[0], 2);
|
|
||||||
CURR_vol = to_intconvertor(s1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (df->dpmu[0]->fmt->polar == '0')
|
|
||||||
{
|
|
||||||
unsigned char s1[4];
|
|
||||||
unsigned char s2[4];
|
|
||||||
strncpy(s1, df->dpmu[0]->phasors[0], 4);
|
|
||||||
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 4);
|
|
||||||
long double v1 = decode_ieee_single(s1);
|
|
||||||
long double v2 = decode_ieee_single(s2);
|
|
||||||
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
unsigned char s1[4];
|
|
||||||
strncpy(s1, df->dpmu[0]->phasors[0], 4);
|
|
||||||
CURR_vol = decode_ieee_single(s1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
temp->P[temp->count].x = CURR_FREQ;
|
|
||||||
temp->P[temp->count].y = CURR_vol;
|
|
||||||
temp->P[temp->count].cluster = -1;
|
|
||||||
temp->P[temp->count].minDist = __DBL_MAX__;
|
|
||||||
temp->count++;
|
|
||||||
|
|
||||||
bool result = false;
|
|
||||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
long double dist = distance2(&temp->C[i], &temp->P[temp->count - 1]);
|
|
||||||
if (dist <= temp->C[i].max_radius && dist < temp->P[temp->count - 1].minDist)
|
|
||||||
{
|
|
||||||
temp->P[temp->count - 1].cluster = i;
|
|
||||||
temp->P[temp->count - 1].minDist = dist;
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (result == false)
|
|
||||||
temp->P[temp->count - 1].cluster = -1;
|
|
||||||
temp->result = result;
|
|
||||||
}
|
|
||||||
else if (temp->isIntialized == 1)
|
|
||||||
{
|
|
||||||
int *nPoints = (int *)malloc(sizeof(int) * temp->no_of_clusters);
|
|
||||||
long double *Sumx = (long double *)malloc(sizeof(long double) * temp->no_of_clusters);
|
|
||||||
long double *Sumy = (long double *)malloc(sizeof(long double) * temp->no_of_clusters);
|
|
||||||
|
|
||||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
nPoints[i] = 0;
|
|
||||||
Sumx[i] = 0;
|
|
||||||
Sumy[i] = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 100; i++)
|
|
||||||
{
|
|
||||||
if (temp->P[i].cluster != -1)
|
|
||||||
{
|
|
||||||
nPoints[temp->P[i].cluster]++;
|
|
||||||
Sumx[temp->P[i].cluster] += temp->P[i].x;
|
|
||||||
Sumy[temp->P[i].cluster] += temp->P[i].y;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
temp->C[i].x = ((temp->C[i].count * temp->C[i].x) + Sumx[i]) / (temp->C[i].count + nPoints[i]);
|
|
||||||
temp->C[i].y = ((temp->C[i].count * temp->C[i].y) + Sumx[i]) / (temp->C[i].count + nPoints[i]);
|
|
||||||
temp->C[i].count += nPoints[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < temp->no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
for (int j = 0; j < 100; j++)
|
|
||||||
{
|
|
||||||
if (temp->P[j].cluster == i)
|
|
||||||
{
|
|
||||||
long double dist = distance2(&temp->C[i], &temp->P[j]);
|
|
||||||
if (temp->C[i].max_radius < dist)
|
|
||||||
temp->C[i].max_radius = dist;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
temp->count = 0;
|
|
||||||
free(temp->P);
|
|
||||||
free(Sumx);
|
|
||||||
free(Sumy);
|
|
||||||
free(nPoints);
|
|
||||||
}
|
|
||||||
if (temp->count != 500 && temp->isIntialized == 0)
|
|
||||||
{
|
{
|
||||||
float CURR_FREQ;
|
float CURR_FREQ;
|
||||||
if (df->dpmu[0]->fmt->freq == '0')
|
if (df->dpmu[0]->fmt->freq == '0')
|
||||||
|
@ -284,7 +143,7 @@ bool Kmeans2(struct data_frame *df)
|
||||||
temp->P[temp->count].minDist = __DBL_MAX__;
|
temp->P[temp->count].minDist = __DBL_MAX__;
|
||||||
temp->count++;
|
temp->count++;
|
||||||
}
|
}
|
||||||
else if (temp->isIntialized == 0)
|
else
|
||||||
{
|
{
|
||||||
int no_of_clusters = 5;
|
int no_of_clusters = 5;
|
||||||
int epochs = 20;
|
int epochs = 20;
|
||||||
|
@ -340,68 +199,33 @@ bool Kmeans2(struct data_frame *df)
|
||||||
Centroids[i].y = Sumy[i] / nPoints[i];
|
Centroids[i].y = Sumy[i] / nPoints[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (epochs == 0)
|
|
||||||
{
|
|
||||||
int count_centroids = 0;
|
|
||||||
for (int i = 0; i < no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
if (!isnanl(Centroids[i].x))
|
|
||||||
count_centroids++;
|
|
||||||
}
|
|
||||||
|
|
||||||
temp->no_of_clusters = count_centroids;
|
|
||||||
temp->C = (struct centroid *)malloc(sizeof(struct centroid) * count_centroids);
|
|
||||||
int track = 0;
|
|
||||||
for (int i = 0; i < no_of_clusters; i++)
|
|
||||||
{
|
|
||||||
if (!isnanl(Centroids[i].x))
|
|
||||||
{
|
|
||||||
temp->C[track].x = Centroids[i].x;
|
|
||||||
temp->C[track].y = Centroids[i].y;
|
|
||||||
temp->C[track].max_radius = 0;
|
|
||||||
temp->C[track].count = nPoints[i];
|
|
||||||
for (int j = 0; j < 500; j++)
|
|
||||||
{
|
|
||||||
if (temp->P[j].cluster == i)
|
|
||||||
{
|
|
||||||
long double dist = distance2(&temp->C[track], &temp->P[j]);
|
|
||||||
if (temp->C[track].max_radius < dist)
|
|
||||||
temp->C[track].max_radius = dist;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
track++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
free(nPoints);
|
free(nPoints);
|
||||||
free(Sumx);
|
free(Sumx);
|
||||||
free(Sumy);
|
free(Sumy);
|
||||||
}
|
}
|
||||||
temp->count = 0;
|
temp->count = 0;
|
||||||
// FILE *fp;
|
FILE *fp;
|
||||||
// fp = fopen("kmeans.txt", "a");
|
fp = fopen("kmeans.txt", "a");
|
||||||
|
|
||||||
// for (int i = 0; i < 500; i++)
|
for (int i = 0; i < 500; i++)
|
||||||
// {
|
{
|
||||||
// fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
|
fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
|
||||||
// }
|
}
|
||||||
// fprintf(fp, "\n\n");
|
fprintf(fp, "\n\n");
|
||||||
|
|
||||||
// for (int i = 0; i < no_of_clusters; i++)
|
for (int i = 0; i < no_of_clusters; i++)
|
||||||
// {
|
{
|
||||||
// fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
|
fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
|
||||||
// }
|
}
|
||||||
|
|
||||||
// fprintf(fp, "\n\n");
|
fprintf(fp, "\n\n");
|
||||||
|
|
||||||
// fclose(fp);
|
fclose(fp);
|
||||||
|
|
||||||
free(Centroids);
|
free(Centroids);
|
||||||
free(temp->P);
|
free(temp->P);
|
||||||
temp->isIntialized = 1;
|
|
||||||
}
|
}
|
||||||
return temp->result;
|
return true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
previous = temp;
|
previous = temp;
|
||||||
|
@ -413,9 +237,7 @@ bool Kmeans2(struct data_frame *df)
|
||||||
bring->idcode = to_intconvertor(df->idcode);
|
bring->idcode = to_intconvertor(df->idcode);
|
||||||
bring->count = 0;
|
bring->count = 0;
|
||||||
bring->next = NULL;
|
bring->next = NULL;
|
||||||
bring->isIntialized = 0;
|
|
||||||
bring->P = NULL;
|
bring->P = NULL;
|
||||||
bring->result = true;
|
|
||||||
previous->next = bring;
|
previous->next = bring;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -129,13 +129,13 @@ gboolean update_images(gpointer* pars){
|
||||||
}
|
}
|
||||||
}else if(curr_measurement == 3){
|
}else if(curr_measurement == 3){
|
||||||
if(algorithm==0 && dimmension == 0){
|
if(algorithm==0 && dimmension == 0){
|
||||||
if (!attack_detect_freq(df)){
|
if (!attack_detect_vol(df)){
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||||
}else{
|
}else{
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||||
}
|
}
|
||||||
}else if (algorithm==0 && dimmension == 1){
|
}else if (algorithm==0 && dimmension == 1){
|
||||||
if (!attack_detect_vol(df)){
|
if (!attack_detect_freq(df)){
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||||
}else{
|
}else{
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||||
|
@ -161,23 +161,19 @@ gboolean update_images(gpointer* pars){
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||||
}
|
}
|
||||||
}else if (algorithm==2 && dimmension == 0){
|
}else if (algorithm==2 && dimmension == 0){
|
||||||
if(!DTWfreqDistance(df)){
|
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
|
||||||
}else{
|
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
|
||||||
}
|
|
||||||
}else if (algorithm==2 && dimmension == 1){
|
|
||||||
if(!DTWvolDistance(df)){
|
if(!DTWvolDistance(df)){
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||||
}else{
|
}else{
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||||
}
|
}
|
||||||
}else if (algorithm==2 && dimmension == 2){
|
}else if (algorithm==2 && dimmension == 1){
|
||||||
if(!DTWfreqvolDistance(df)){
|
if(!DTWfreqDistance(df)){
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
|
||||||
}else{
|
}else{
|
||||||
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
|
||||||
}
|
}
|
||||||
|
}else if (algorithm==2 && dimmension == 2){
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue