Merge pull request #7 from karthikmurakonda/kmeans

Kmeans
This commit is contained in:
karthik mv 2022-10-28 19:15:08 +05:30 committed by GitHub
commit 801b369a1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 36235 additions and 60 deletions

View File

@ -57,7 +57,7 @@ CLEAN_OBJECTS = $(wildcard bin/*.o) $(PROGRAM)
all: $(PROGRAM) all: $(PROGRAM)
$(PROGRAM): $(OBJECT_FILES) $(PROGRAM): $(OBJECT_FILES)
$(CC) $(CFLAGS) $(OBJECT_FILES) -o $(PROGRAM) $(LDLIBS) $(CC) $(CFLAGS) $(OBJECT_FILES) -o $(PROGRAM) $(LDLIBS) -lm
bin/%.o: src/%.c bin/%.o: src/%.c
$(CC) $(CFLAGS) -c $< -o $@ $(CC) $(CFLAGS) -c $< -o $@

View File

@ -2,10 +2,7 @@
#include <time.h> #include <time.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
/* variables declared of attack_detect function */ /* variables declared of attack_detect function */
float SUM_OF_FREQUENCY=0;
float COUNT=0;
time_t START;
/* function declared */ /* function declared */
gboolean attack_detect(struct data_frame *df,time_t* START,float* COUNT,float* SUM_OF_FREQUENCY); gboolean attack_detect(struct data_frame *df);
/* pavan changes */ /* pavan changes */

View File

@ -0,0 +1,5 @@
/*pavan changes*/
int DTWfreqDistance(struct data_frame *df);
int DTWvolDistance(struct data_frame *df);

10
iPDC/inc/Kmeans.h Normal file
View File

@ -0,0 +1,10 @@
/*Pavan Changes*/
#include <gtk/gtk.h>
// intial variables
//function declaration
gboolean kmeans(struct data_frame *df);
/*Pavan Changes*/

29
iPDC/plot_kmeans1.py Normal file
View File

@ -0,0 +1,29 @@
import csv
import pandas as pd
import matplotlib.pyplot as plt
x=[]
y=[]
x1=[]
y1=[]
x2=[]
y2=[]
with open('points.csv', mode ='r') as file:
csvFile = csv.reader(file)
for lines in csvFile:
print(lines)
x.append(float(lines[0]))
y.append(0)
x1.append(float(lines[1]))
y1.append(0)
x2.append(float(lines[2]))
y2.append(0)
plt.scatter(x, y)
plt.scatter(x1, y1)
plt.scatter(x2,y2)
plt.show()

35785
iPDC/points.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -1,60 +1,39 @@
/* pavan changes */ /* pavan changes */
#include<time.h> #include <time.h>
#include<stdio.h> #include <stdio.h>
#include<stdlib.h> #include <stdlib.h>
#include<gtk/gtk.h> #include <gtk/gtk.h>
#include <math.h>
#include "parser.h" #include "parser.h"
gboolean attack_detect(struct data_frame *df,time_t* START,float* COUNT,float* SUM_OF_FREQUENCY) long double AVERAGE_OF_FREQUENCY = 50;
{ unsigned long long int COUNT = 500;
// printf("freq: %d\n",to_intconvertor(&(df->dpmu[0]->fmt->freq)));
// printf("analog: %d\n",to_intconvertor(&(df->dpmu[0]->fmt->analog)));
// printf("phasor %d\n",to_intconvertor(&(df->dpmu[0]->fmt->phasor)));
// printf("polar: %d\n",to_intconvertor(&(df->dpmu[0]->fmt->polar)));
*COUNT = *COUNT+1; gboolean attack_detect(struct data_frame *df)
float CURR_FREQ=to_intconvertor(df->dpmu[0]->freq); {
*SUM_OF_FREQUENCY+=CURR_FREQ; float CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3;
float FREQ_AVG=*SUM_OF_FREQUENCY/(*COUNT*1.0f); printf("Current freq: %f\n", CURR_FREQ);
float DETECT_PERCENT=(abs(FREQ_AVG-CURR_FREQ)/(FREQ_AVG*1.0f))*100; float DETECT_PERCENT = (fabs(AVERAGE_OF_FREQUENCY - CURR_FREQ) / (AVERAGE_OF_FREQUENCY * 1.0f)) * 100;
gboolean detect;
/* detecting based on thershold */ /* detecting based on thershold frequency can vary only around 0.2 hz*/
float THRESHOLD=60; if (DETECT_PERCENT > 0.9)
if(DETECT_PERCENT>THRESHOLD)
{ {
detect=FALSE;
printf("\033[0;31m"); printf("\033[0;31m");
printf("ATTACK DETECTED!"); printf("ATTACK DETECTED!");
printf("\033[0m"); printf("\033[0m");
// intf(" Detect_percent: %f\n",DETECT_PERCENT); return FALSE;
} }
else else
{ {
detect=TRUE;
printf("\033[0;32m"); printf("\033[0;32m");
printf("NO PROBLEM :)\n"); printf("NO PROBLEM :)\n");
printf("\033[0m"); printf("\033[0m");
}
/* calcculating time */ /* updating mean of frequency */
if(*COUNT==1) AVERAGE_OF_FREQUENCY = ((AVERAGE_OF_FREQUENCY * COUNT) + CURR_FREQ) / ++COUNT;
{ printf("avg freq: %Lf\n", AVERAGE_OF_FREQUENCY);
time(START); return TRUE;
printf("entered\n");
} }
time_t END;
time(&END);
double time_used = difftime(END,*START);
printf("time used %lf\n",time_used);
/* resetting after i minute */
if(time_used > 60)
{
time(START);
*SUM_OF_FREQUENCY=CURR_FREQ;
*COUNT=1;
}
return detect;
} }
/* pavan changes */ /* pavan changes */

View File

@ -0,0 +1,298 @@
#include "parser.h"
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <gtk/gtk.h>
#include <stdlib.h>
struct DTWfreqlist
{
int idcode;
long double *freq1;
long double *freq2;
int count_track1;
int count_track2;
int flag;
int result;
struct DTWfreqlist *next;
};
struct DTWvollist
{
int idcode;
long double *vol1;
long double *vol2;
int count_track1;
int count_track2;
int flag;
int result;
struct DTWvollist *next;
};
struct DTWfreqlist *head = NULL;
struct DTWvollist *headvol = NULL;
int DTWfreqDistance(struct data_frame *df)
{
if (head == NULL)
{
head = (struct DTWfreqlist *)malloc(sizeof(struct DTWfreqlist));
head->count_track1 = 0;
head->count_track2 = 0;
head->flag = 0;
head->idcode = to_intconvertor(df->idcode);
head->next = NULL;
head->result=1;
return 1;
}
else
{
struct DTWfreqlist *temp = head;
struct DTWfreqlist *previous;
while (temp != NULL)
{
if (to_intconvertor(df->idcode) == temp->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);
}
// printf("count1: %d\ncount2: %d\n",count_track1,count_track2);
// printf("curr_freq: %f\n",CURR_FREQ);
if (temp->count_track1 == 0)
{
if (temp->flag == 0)
{
temp->flag = 1;
temp->freq1 = (long double *)malloc(500 * sizeof(long double));
}
temp->freq1[temp->count_track2] = CURR_FREQ;
temp->count_track2++;
if (temp->count_track2 == 500)
{
temp->count_track2 = 0;
temp->flag = 0;
temp->count_track1++;
}
}
else if (temp->count_track1 == 1)
{
if (temp->flag == 0)
{
temp->flag = 1;
temp->freq2 = (long double *)malloc(500 * sizeof(long double));
}
temp->freq2[temp->count_track2] = CURR_FREQ;
temp->count_track2++;
if (temp->count_track2 == 500)
{
temp->count_track2 = 0;
temp->flag = 0;
temp->count_track1++;
}
}
else
{
float **DTW = (float **)malloc(501 * sizeof(float *));
for (int i = 0; i < 501; i++)
DTW[i] = (float *)malloc(501 * sizeof(float));
for (int i = 0; i < 501; i++)
{
for (int j = 0; j < 501; j++)
DTW[i][j] = 1000;
}
DTW[0][0] = 0;
for (int i = 1; i < 501; i++)
{
for (int j = 1; j < 501; j++)
{
float cost = fabs(temp->freq1[i - 1] - temp->freq2[j - 1]);
// printf("cost: %f",cost);
DTW[i][j] = cost + fmin(DTW[i][j - 1], fmin(DTW[i - 1][j], DTW[i - 1][j - 1]));
// printf(" DTW[%d][%d]: %f\n",i,j,DTW[i][j]);
}
}
printf("DTWdistance: %f\n", DTW[500][500]);
if (DTW[500][500] >= 35)
{
free(temp->freq2);
temp->result = 0;
}
else
{
long double *f = temp->freq1;
temp->freq1 = temp->freq2;
free(f);
temp->result = 1;
}
free(DTW);
temp->count_track1 = 1;
}
break;
}
previous = temp;
temp = temp->next;
}
if (temp == NULL)
{
struct DTWfreqlist *bring = (struct DTWfreqlist *)malloc(sizeof(struct DTWfreqlist));
bring->count_track1 = 0;
bring->count_track2 = 0;
bring->flag = 0;
bring->idcode = to_intconvertor(df->idcode);
bring->next = NULL;
bring->result=1;
previous->next = bring;
return 1;
}
return temp->result;
}
}
int DTWvolDistance(struct data_frame *df)
{
if (headvol == NULL)
{
headvol = (struct DTWvollist *)malloc(sizeof(struct DTWvollist));
headvol->count_track1 = 0;
headvol->count_track2 = 0;
headvol->flag = 0;
headvol->idcode = to_intconvertor(df->idcode);
headvol->next = NULL;
headvol->result=1;
return 1;
}
else
{
struct DTWvollist *temp = headvol;
struct DTWvollist *previous;
while (temp != NULL)
{
if (to_intconvertor(df->idcode) == temp->idcode)
{
float CURR_vol;
if (df->dpmu[0]->fmt->phasor == '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[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));
}
// printf("count1: %d\ncount2: %d\n",count_track1,count_track2);
// printf("curr_vol: %f\n",CURR_vol);
if (temp->count_track1 == 0)
{
if (temp->flag == 0)
{
temp->flag = 1;
temp->vol1 = (long double *)malloc(500 * sizeof(long double));
}
temp->vol1[temp->count_track2] = CURR_vol;
temp->count_track2++;
if (temp->count_track2 == 500)
{
temp->count_track2 = 0;
temp->flag = 0;
temp->count_track1++;
}
}
else if (temp->count_track1 == 1)
{
if (temp->flag == 0)
{
temp->flag = 1;
temp->vol2 = (long double *)malloc(500 * sizeof(long double));
}
temp->vol2[temp->count_track2] = CURR_vol;
temp->count_track2++;
if (temp->count_track2 == 500)
{
temp->count_track2 = 0;
temp->flag = 0;
temp->count_track1++;
}
}
else
{
float **DTW = (float **)malloc(501 * sizeof(float *));
for (int i = 0; i < 501; i++)
DTW[i] = (float *)malloc(501 * sizeof(float));
for (int i = 0; i < 501; i++)
{
for (int j = 0; j < 501; j++)
DTW[i][j] = 1000;
}
DTW[0][0] = 0;
for (int i = 1; i < 501; i++)
{
for (int j = 1; j < 501; j++)
{
float cost = fabs(temp->vol1[i - 1] - temp->vol2[j - 1]);
// printf("cost: %f",cost);
DTW[i][j] = cost + fmin(DTW[i][j - 1], fmin(DTW[i - 1][j], DTW[i - 1][j - 1]));
// printf(" DTW[%d][%d]: %f\n",i,j,DTW[i][j]);
}
}
printf("DTWdistance: %f\n", DTW[500][500]);
if (DTW[500][500] >= 35)
{
free(temp->vol2);
temp->result = 0;
}
else
{
long double *f = temp->vol1;
temp->vol1 = temp->vol2;
free(f);
temp->result = 1;
}
free(DTW);
temp->count_track1 = 1;
}
break;
}
previous = temp;
temp = temp->next;
}
if (temp == NULL)
{
struct DTWvollist *bring = (struct DTWvollist *)malloc(sizeof(struct DTWvollist));
bring->count_track1 = 0;
bring->count_track2 = 0;
bring->flag = 0;
bring->idcode = to_intconvertor(df->idcode);
bring->next = NULL;
bring->result=1;
previous->next = bring;
return 1;
}
return temp->result;
}
}

59
iPDC/src/Kmeans.c Normal file
View File

@ -0,0 +1,59 @@
/*Pavan Changes*/
#include "parser.h"
#include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <math.h>
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;
/*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;
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)
{
A = ((count_A * (A)) + CURR_FREQ) / (++count_A);
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;
}
}
/*Pavan Changes*/

View File

@ -1,4 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <gtk/gtk.h>
#include <pthread.h> #include <pthread.h>
#include "global.h" #include "global.h"
#include "align_sort.h" #include "align_sort.h"
@ -6,6 +8,8 @@
#include "parser.h" #include "parser.h"
#include "osm-gps-map.h" #include "osm-gps-map.h"
#include "Attack_detect.h" #include "Attack_detect.h"
#include "Kmeans.h"
#include "Dynamic_time_warping.h"
#include "connections.h" #include "connections.h"
#include "livechart.h" #include "livechart.h"
#include "utility_tools.h" #include "utility_tools.h"
@ -26,6 +30,11 @@ gboolean update_images(gpointer* pars){
if (df == NULL){ if (df == NULL){
return TRUE; return TRUE;
} }
//int freq = to_intconvertor(df->dpmu[0]->freq);
//gboolean green =attack_detect(df);
//printf("map_vis A: %Lf, B: %Lf,C: %Lf\n",A,B,C);
//gboolean green = kmeans(df);
if (curr_measurement==0) if (curr_measurement==0)
{ {
int i = 0, k = 0; int i = 0, k = 0;
@ -35,17 +44,17 @@ gboolean update_images(gpointer* pars){
float lat; float lat;
float lon; float lon;
loops++; loops++;
printf("loops: %d\n", loops); // printf("loops: %d\n", loops);
id = to_intconvertor(df->idcode); id = to_intconvertor(df->idcode);
printf("id = %d\n",id); // printf("id = %d\n",id);
pthread_mutex_lock(&mutex_cfg); pthread_mutex_lock(&mutex_cfg);
temp_cfg = cfgfirst; temp_cfg = cfgfirst;
// Check for the IDCODE in Configuration Frame // Check for the IDCODE in Configuration Frame
while(temp_cfg != NULL){ while(temp_cfg != NULL){
if(id == to_intconvertor(temp_cfg->idcode)){ if(id == to_intconvertor(temp_cfg->idcode)){
cfg_match = 1; cfg_match = 1;
printf("Matched - id : %d\n",id); // printf("Matched - id : %d\n",id);
freq_fmt = temp_cfg->pmu[0]->fmt->freq; freq_fmt = temp_cfg->pmu[0]->fmt->freq;
anal_fmt = temp_cfg->pmu[0]->fmt->analog; anal_fmt = temp_cfg->pmu[0]->fmt->analog;
phas_fmt = temp_cfg->pmu[0]->fmt->phasor; phas_fmt = temp_cfg->pmu[0]->fmt->phasor;
@ -60,7 +69,7 @@ gboolean update_images(gpointer* pars){
// get data from df. // get data from df.
if(freq_fmt == '1'){ if(freq_fmt == '1'){
freq = decode_ieee_single(df->dpmu[i]->freq); freq = decode_ieee_single(df->dpmu[i]->freq);
printf("freq = %f\n",freq); // printf("freq = %f\n",freq);
}else{ }else{
freq = to_intconvertor(df->dpmu[i]->freq)*1e-6+50; freq = to_intconvertor(df->dpmu[i]->freq)*1e-6+50;
} }
@ -71,7 +80,7 @@ gboolean update_images(gpointer* pars){
strncpy(last2bytes, df->dpmu[i]->phasors[0]+2, 2); strncpy(last2bytes, df->dpmu[i]->phasors[0]+2, 2);
vol_magnitude = to_intconvertor(first2bytes); vol_magnitude = to_intconvertor(first2bytes);
float imaginary = to_intconvertor(last2bytes); float imaginary = to_intconvertor(last2bytes);
printf("vol = %f imag = %f\n",vol_magnitude, imaginary); // printf("vol = %f imag = %f\n",vol_magnitude, imaginary);
live_chart_serie_add(serie, freq); live_chart_serie_add(serie, freq);
@ -80,7 +89,7 @@ gboolean update_images(gpointer* pars){
LLptr = LLfirst; LLptr = LLfirst;
match = 0; match = 0;
while(LLptr != NULL){ while(LLptr != NULL){
printf("pmuid = %d\n",LLptr->pmuid); // printf("pmuid = %d\n",LLptr->pmuid);
if(LLptr->pmuid == id){ if(LLptr->pmuid == id){
match = 1; match = 1;
float lat = LLptr->latitude; float lat = LLptr->latitude;
@ -91,18 +100,17 @@ gboolean update_images(gpointer* pars){
} }
pthread_mutex_unlock(&mutex_Lower_Layer_Details); pthread_mutex_unlock(&mutex_Lower_Layer_Details);
if(match == 1 && cfg_match == 1){ // if(match == 1 && cfg_match == 1){
printf("lat = %f, lon = %f, freq = %f\n",lat,lon,freq); // printf("lat = %f, lon = %f, freq = %f\n",lat,lon,freq);
gboolean green =attack_detect(df,&START,&COUNT,&SUM_OF_FREQUENCY);
if(parameters->g_last_image != 0){ if(parameters->g_last_image != 0){
osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image); osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image);
} }
if (freq > 50.300){ if (DTWvolDistance(df)){
parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image); parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
}else{ }else{
parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image); parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
} }
} // }
df = df->dnext; df = df->dnext;
// i++; // i++;
k++; k++;

View File

@ -156,6 +156,11 @@ void utility_tools(GtkButton *but, gpointer udata)
g_signal_connect(utdata->util_window, "destroy", G_CALLBACK(on_window_destroy), GUINT_TO_POINTER(pid)); g_signal_connect(utdata->util_window, "destroy", G_CALLBACK(on_window_destroy), GUINT_TO_POINTER(pid));
// //g_last_image = osm_gps_map_image_add(util_map,15.4589, 75.0078, g_red_image);
// g_last_image = osm_gps_map_image_add(utdata->util_map,15.518597, 74.925584, g_green_image);
// myParameters parameters = {utdata->util_map, g_red_image, g_green_image, g_last_image};
// gpointer data = (gpointer) &parameters;
// guint pid = g_timeout_add(20, (GSourceFunc) update_images, data);
// gtk_widget_set_vexpand(GTK_WIDGET(utdata->util_map), TRUE); // gtk_widget_set_vexpand(GTK_WIDGET(utdata->util_map), TRUE);
// gtk_widget_set_hexpand(GTK_WIDGET(utdata->util_map), TRUE); // gtk_widget_set_hexpand(GTK_WIDGET(utdata->util_map), TRUE);
gtk_widget_show_all(utdata->util_window); gtk_widget_show_all(utdata->util_window);