diff --git a/iPDC/src/Attack_detect.c b/iPDC/src/Attack_detect.c index 51c6699..7dfd2ca 100644 --- a/iPDC/src/Attack_detect.c +++ b/iPDC/src/Attack_detect.c @@ -34,6 +34,7 @@ gboolean attack_detect_freq(struct data_frame *df) head = (struct freqlist *)malloc(sizeof(struct freqlist)); head->AVERAGE_OF_FREQUENCY = 50; head->COUNT = 500; + head->idcode=to_intconvertor(df->idcode); return TRUE; } else @@ -83,6 +84,7 @@ gboolean attack_detect_freq(struct data_frame *df) bring = (struct freqlist *)malloc(sizeof(struct freqlist)); bring->AVERAGE_OF_FREQUENCY = 50; bring->COUNT = 500; + bring->idcode = to_intconvertor(df->idcode); previous->next=bring; return TRUE; } @@ -117,6 +119,7 @@ gboolean attack_detect_vol(struct data_frame *df) headvol = (struct vollist *)malloc(sizeof(struct vollist)); headvol->AVERAGE_OF_VOLTAGE = CURR_vol; headvol->COUNT = 500; + headvol->idcode=to_intconvertor(df->idcode); return TRUE; } else @@ -160,6 +163,7 @@ gboolean attack_detect_vol(struct data_frame *df) bring->AVERAGE_OF_VOLTAGE = CURR_vol; bring->COUNT = 500; previous->next = bring; + bring->idcode = to_intconvertor(df->idcode); return TRUE; } } diff --git a/iPDC/src/Kmeans.c b/iPDC/src/Kmeans.c index 11e5605..1fd95b7 100644 --- a/iPDC/src/Kmeans.c +++ b/iPDC/src/Kmeans.c @@ -6,53 +6,96 @@ #include #include -FILE *fp; -/*intial weights weights for each points */ -unsigned long long int count_A = 1000; -unsigned long long int count_B = 1000; -unsigned long long int count_C = 1000; +// FILE *fp; -/*In India frequency ranges from 49.5 to 50.2 Hz*/ -long double std_dev = 0.6454972244; -long double A = 0; // for point A=50 -long double B = -1.5491933394; // for point B=49 -long double C = 0.7745966692; // for point C=50.5 -long double mean = 50; -unsigned long long int count = 3000; +struct kmeans1 +{ + int idcode; + unsigned long long int count_A = 1000; + unsigned long long int count_B = 1000; + unsigned long long int count_C = 1000; + long double A = 50; + long double B = 49; + long double C = 51; + struct kmeans1 *next; +} + +struct kmeans1 *headk = NULL; gboolean kmeans(struct data_frame *df) { - long double CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3; - long double CURR_FREQ_temp = CURR_FREQ; - CURR_FREQ = (CURR_FREQ - mean) / std_dev; - long double diff_A = fabs(A - CURR_FREQ); - long double diff_B = fabs(B - CURR_FREQ); - long double diff_C = fabs(C - CURR_FREQ); - // fp = fopen("points.csv","a"); - // printf("A: %Lf, B: %Lf, C: %Lf\n",A,B,C); - // fprintf(fp,"%Lf, %Lf, %Lf\n",A,B,C); - // fclose(fp); - - /*Updating std_dev and mean*/ - // printf("std_dev: %Lf, mean: %Lf\n",std_dev,mean); - mean = ((mean * count) + CURR_FREQ_temp) / (++count); - std_dev = (((std_dev * std_dev) * (count - 1)) + (CURR_FREQ * std_dev * CURR_FREQ * std_dev)) / count; - std_dev = sqrt(std_dev); - - if (diff_A <= diff_B && diff_A <= diff_C) + if (headk == NULL) { - A = ((count_A * (A)) + CURR_FREQ) / (++count_A); + headk->A = 50; + headk->B = 49; + headk->C = 51; + headk->count_A = 1000; + headk->count_B = 1000; + headk->count_C = 1000; + headk->next = NULL; + headk->idcode = to_intconvertor(df->idcode); return TRUE; } - else if (diff_B < diff_C) - { - B = ((count_B * (B)) + CURR_FREQ) / (++count_B); - return FALSE; - } else { - C = ((count_C * (C)) + CURR_FREQ) / (++count_C); - return FALSE; + struct kmeans1 *temp == headk; + struct kmeans1 *previous == NULL; + while (temp != NULL) + { + if (temp->idcode == to_intconvertor(df->idcode)) + { + 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); + } + + long double diff_A = fabs(temp->A - CURR_FREQ); + long double diff_B = fabs(temp->B - CURR_FREQ); + long double diff_C = fabs(temp->C - CURR_FREQ); + // fp = fopen("points.csv","a"); + // printf("A: %Lf, B: %Lf, C: %Lf\n",A,B,C); + // fprintf(fp,"%Lf, %Lf, %Lf\n",A,B,C); + // fclose(fp); + + if (diff_A <= diff_B && diff_A <= diff_C) + { + temp->A = ((temp->count_A * (temp->A)) + CURR_FREQ) / (++temp->count_A); + return TRUE; + } + else if (diff_B < diff_C) + { + temp->B = ((temp->count_B * (temp->B)) + CURR_FREQ) / (++temp->count_B); + return FALSE; + } + else + { + temp->C = ((temp->count_C * (temp->C)) + CURR_FREQ) / (++temp->count_C); + return FALSE; + } + break; + } + previous=temp; + temp = temp->next; + } + if(temp==NULL) + { + struct kmeans1 *bring = (struct kmeans1 *)malloc(sizeof(struct kmeans1)); + bring->A = 50; + bring->B = 49; + bring->C = 51; + bring->count_A = 1000; + bring->count_B = 1000; + bring->count_C = 1000; + bring->next = NULL; + bring->idcode = to_intconvertor(df->idcode); + previous->next=bring; + return TRUE; + } } }