Compare commits

...

12 Commits

Author SHA1 Message Date
karthik mv 801b369a1a
Merge pull request #7 from karthikmurakonda/kmeans
Kmeans
2022-10-28 19:15:08 +05:30
karthikmurakonda ad4af0adac bug fixes 2022-10-28 19:14:35 +05:30
karthikmurakonda 04af82224d fixed merge error 2022-10-28 18:33:25 +05:30
karthikmurakonda b209e34d1a Merge branch 'master' into kmeans 2022-10-28 18:25:33 +05:30
karthikmurakonda f9cdb4a5b6 updated utility tools(unrefractored) 2022-10-28 18:18:01 +05:30
pavanvpatil 25dfbc2444 phasor format 2022-10-28 18:08:39 +05:30
pavanvpatil 04f81da3d4 DTW for 1 2 2022-10-28 17:37:13 +05:30
pavanvpatil ebb28668a8 algo updated 2022-10-25 23:58:31 +05:30
pavanvpatil 1ee60c80b2 normalised added DTW algo 2022-10-25 21:42:46 +05:30
pavanvpatil 658e423553 AddedDTW 2022-10-24 20:30:30 +05:30
pavanvpatil 2ff209a38b kmeans upgrade 2022-10-20 13:26:43 +05:30
pavanvpatil 09d33644c9 kmeans algo 2022-10-12 02:01:31 +05:30
13 changed files with 36509 additions and 72 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

@ -169,7 +169,7 @@
</object> </object>
</child> </child>
<child> <child>
<!-- n-columns=2 n-rows=2 --> <!-- n-columns=3 n-rows=2 -->
<object class="GtkGrid"> <object class="GtkGrid">
<property name="width-request">100</property> <property name="width-request">100</property>
<property name="height-request">80</property> <property name="height-request">80</property>
@ -179,7 +179,7 @@
<property name="margin-top">10</property> <property name="margin-top">10</property>
<property name="column-spacing">6</property> <property name="column-spacing">6</property>
<child> <child>
<!-- n-columns=1 n-rows=5 --> <!-- n-columns=1 n-rows=9 -->
<object class="GtkGrid"> <object class="GtkGrid">
<property name="width-request">80</property> <property name="width-request">80</property>
<property name="visible">True</property> <property name="visible">True</property>
@ -187,7 +187,7 @@
<property name="row-spacing">35</property> <property name="row-spacing">35</property>
<child> <child>
<object class="GtkButton" id="voltage"> <object class="GtkButton" id="voltage">
<property name="label" translatable="yes">Voltage</property> <property name="label" translatable="yes">Voltage Magnitude</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can-focus">True</property> <property name="can-focus">True</property>
<property name="receives-default">True</property> <property name="receives-default">True</property>
@ -218,7 +218,7 @@
</object> </object>
<packing> <packing>
<property name="left-attach">0</property> <property name="left-attach">0</property>
<property name="top-attach">2</property> <property name="top-attach">3</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -227,13 +227,14 @@
<property name="can-focus">False</property> <property name="can-focus">False</property>
<property name="active">0</property> <property name="active">0</property>
<items> <items>
<item id="0" translatable="yes">kmeans</item> <item id="0" translatable="yes">Gaussian</item>
<item id="1" translatable="yes">simple</item> <item id="1" translatable="yes">kmeans</item>
<item translatable="yes">DTW</item>
</items> </items>
</object> </object>
<packing> <packing>
<property name="left-attach">0</property> <property name="left-attach">0</property>
<property name="top-attach">3</property> <property name="top-attach">5</property>
</packing> </packing>
</child> </child>
<child> <child>
@ -243,15 +244,52 @@
<property name="active">0</property> <property name="active">0</property>
<items> <items>
<item id="0" translatable="yes">frequency only</item> <item id="0" translatable="yes">frequency only</item>
<item id="1" translatable="yes">frequency,voltage</item> <item id="1" translatable="yes">voltage only</item>
<item id="2" translatable="yes">voltage</item> <item id="2" translatable="yes">frequency,voltage</item>
</items> </items>
</object> </object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">7</property>
</packing>
</child>
<child>
<object class="GtkButton" id="dfreq">
<property name="label" translatable="yes">dfreq</property>
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">2</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="algo_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Algorithm</property>
</object>
<packing> <packing>
<property name="left-attach">0</property> <property name="left-attach">0</property>
<property name="top-attach">4</property> <property name="top-attach">4</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkLabel" id="dimm_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Dimmensions</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">6</property>
</packing>
</child>
<child>
<placeholder/>
</child>
</object> </object>
<packing> <packing>
<property name="left-attach">0</property> <property name="left-attach">0</property>
@ -281,6 +319,202 @@
<property name="top-attach">1</property> <property name="top-attach">1</property>
</packing> </packing>
</child> </child>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Legend</property>
<property name="use-markup">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkColorButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="title" translatable="yes">red</property>
<property name="rgba">rgb(238,29,0)</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="legend_label">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Attack/Event</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkColorButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="rgba">rgb(0,255,0)</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Normal</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<placeholder/>
</child>
<child>
<placeholder/>
</child>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkScrolledWindow">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="shadow-type">in</property>
<child>
<object class="GtkViewport">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkBox">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="orientation">vertical</property>
<child>
<object class="GtkLabel" id="graph_legend1">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Graph Legend</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<!-- n-columns=2 n-rows=1 -->
<object class="GtkGrid">
<property name="visible">True</property>
<property name="can-focus">False</property>
<child>
<object class="GtkColorButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
<property name="rgba">rgb(255,120,0)</property>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkLabel">
<property name="visible">True</property>
<property name="can-focus">False</property>
<property name="label" translatable="yes">Pmu</property>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
<child>
<object class="GtkColorButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">2</property>
</packing>
</child>
<child>
<object class="GtkColorButton">
<property name="visible">True</property>
<property name="can-focus">True</property>
<property name="receives-default">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">3</property>
</packing>
</child>
</object>
</child>
</object>
</child>
</object>
<packing>
<property name="left-attach">2</property>
<property name="top-attach">1</property>
</packing>
</child>
<child> <child>
<placeholder/> <placeholder/>
</child> </child>

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*/

View File

@ -8,5 +8,15 @@ typedef struct{
int current_selection; int current_selection;
} myParameters; } myParameters;
struct vis_data{
int id;
float lat;
float lon;
GdkPixbuf *last_image;
GTimeVal last_update_time;
struct vis_data *next;
};
struct vis_data *head;
gboolean update_images(gpointer* pars); gboolean update_images(gpointer* pars);

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,46 +30,66 @@ 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;
float freq; float freq,vol_magnitude,angle, dfreq;
unsigned char fmt; unsigned char freq_fmt, anal_fmt, phas_fmt, polar_fmt;
while (df!=NULL){ while (df!=NULL){
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);
fmt = temp_cfg->pmu[0]->fmt->freq; freq_fmt = temp_cfg->pmu[0]->fmt->freq;
anal_fmt = temp_cfg->pmu[0]->fmt->analog;
phas_fmt = temp_cfg->pmu[0]->fmt->phasor;
polar_fmt = temp_cfg->pmu[0]->fmt->polar;
break; break;
} else { } else {
temp_cfg = temp_cfg->cfgnext; temp_cfg = temp_cfg->cfgnext;
} }
} }
pthread_mutex_unlock(&mutex_cfg); pthread_mutex_unlock(&mutex_cfg);
if(fmt == '1'){
// get data from df.
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;
} }
unsigned char first2bytes[2];
strncpy(first2bytes, df->dpmu[i]->phasors[0], 2);
unsigned char last2bytes[2];
strncpy(last2bytes, df->dpmu[i]->phasors[0]+2, 2);
vol_magnitude = to_intconvertor(first2bytes);
float imaginary = to_intconvertor(last2bytes);
// printf("vol = %f imag = %f\n",vol_magnitude, imaginary);
live_chart_serie_add(serie, freq); live_chart_serie_add(serie, freq);
// check lower layer details to get longitude and lattitude.
pthread_mutex_lock(&mutex_Lower_Layer_Details); pthread_mutex_lock(&mutex_Lower_Layer_Details);
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;
@ -76,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

@ -105,12 +105,15 @@ void utility_tools(GtkButton *but, gpointer udata)
"map-source", OSM_GPS_MAP_SOURCE_OSMC_TRAILS, "map-source", OSM_GPS_MAP_SOURCE_OSMC_TRAILS,
"tile-cache", "/tmp/", "tile-cache", "/tmp/",
NULL); NULL);
// TODO: centering the map
osm_gps_map_set_center_and_zoom(utdata->util_map, 15.4589, 75.0078, 10); osm_gps_map_set_center_and_zoom(utdata->util_map, 15.4589, 75.0078, 10);
// TODO: add the points to window. // TODO: add the points to window.
g_last_image = osm_gps_map_image_add(utdata->util_map, 15.4589, 75.0078, g_red_image); g_last_image = osm_gps_map_image_add(utdata->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); g_last_image = osm_gps_map_image_add(utdata->util_map, 15.518597, 74.925584, g_green_image);
curr_measurement = 0; curr_measurement = 0;
algorithm = 0; algorithm = 0;
dimmension = 0; dimmension = 0;
@ -153,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);