algo updated

This commit is contained in:
pavanvpatil 2022-10-25 23:58:31 +05:30
parent 1ee60c80b2
commit ebb28668a8
7 changed files with 16248 additions and 11433 deletions

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

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

File diff suppressed because it is too large Load Diff

View File

@ -3,58 +3,37 @@
#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

@ -28,13 +28,13 @@ gboolean kmeans(struct data_frame *df)
long double diff_A = fabs(A - CURR_FREQ); long double diff_A = fabs(A - CURR_FREQ);
long double diff_B = fabs(B - CURR_FREQ); long double diff_B = fabs(B - CURR_FREQ);
long double diff_C = fabs(C - CURR_FREQ); long double diff_C = fabs(C - CURR_FREQ);
fp = fopen("points.csv","a"); // fp = fopen("points.csv","a");
printf("A: %Lf, B: %Lf, C: %Lf\n",A,B,C); // printf("A: %Lf, B: %Lf, C: %Lf\n",A,B,C);
fprintf(fp,"%Lf, %Lf, %Lf\n",A,B,C); // fprintf(fp,"%Lf, %Lf, %Lf\n",A,B,C);
fclose(fp); // fclose(fp);
/*Updating std_dev and mean*/ /*Updating std_dev and mean*/
printf("std_dev: %Lf, mean: %Lf\n",std_dev,mean); // printf("std_dev: %Lf, mean: %Lf\n",std_dev,mean);
mean = ((mean * count) + CURR_FREQ_temp) / (++count); 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 = (((std_dev * std_dev) * (count - 1)) + (CURR_FREQ * std_dev * CURR_FREQ * std_dev)) / count;
std_dev = sqrt(std_dev); std_dev = sqrt(std_dev);

View File

@ -20,9 +20,9 @@ gboolean update_images(gpointer* pars){
return TRUE; return TRUE;
} }
int freq = to_intconvertor(df->dpmu[0]->freq); int freq = to_intconvertor(df->dpmu[0]->freq);
//gboolean green =attack_detect(df,&START,&COUNT,&SUM_OF_FREQUENCY); gboolean green =attack_detect(df);
//printf("map_vis A: %Lf, B: %Lf,C: %Lf\n",A,B,C); //printf("map_vis A: %Lf, B: %Lf,C: %Lf\n",A,B,C);
gboolean green = kmeans(df); //gboolean green = kmeans(df);
//DTWdistance(df,&result); //DTWdistance(df,&result);
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);