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 <gtk/gtk.h>
/* variables declared of attack_detect function */
float SUM_OF_FREQUENCY=0;
float COUNT=0;
time_t START;
/* 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 */

View File

@ -6,17 +6,24 @@ 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[2]))
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()

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -6,85 +6,85 @@
long double *freq1;
long double *freq2;
int count_track1=0;
int count_track2=0;
int flag=0;
int count_track1 = 0;
int count_track2 = 0;
int flag = 0;
void DTWdistance(struct data_frame *df,int *result)
void DTWdistance(struct data_frame *df, int *result)
{
float CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq)*1e-3;
float CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3;
// printf("count1: %d\ncount2: %d\n",count_track1,count_track2);
// printf("curr_freq: %f\n",CURR_FREQ);
if(count_track1==0)
if (count_track1 == 0)
{
if(flag==0)
if (flag == 0)
{
flag=1;
flag = 1;
freq1 = (long double *)malloc(500 * sizeof(long double));
}
freq1[count_track2]=CURR_FREQ;
freq1[count_track2] = CURR_FREQ;
count_track2++;
if(count_track2==500)
if (count_track2 == 500)
{
count_track2=0;
flag=0;
count_track2 = 0;
flag = 0;
count_track1++;
}
}
else if(count_track1==1)
else if (count_track1 == 1)
{
if(flag==0)
if (flag == 0)
{
flag=1;
flag = 1;
freq2 = (long double *)malloc(500 * sizeof(long double));
}
freq2[count_track2]=CURR_FREQ;
freq2[count_track2] = CURR_FREQ;
count_track2++;
if(count_track2==500)
if (count_track2 == 500)
{
count_track2=0;
flag=0;
count_track2 = 0;
flag = 0;
count_track1++;
}
}
else
{
float** DTW = (float**)malloc(501 * sizeof(float*));
float **DTW = (float **)malloc(501 * sizeof(float *));
for (int i = 0; i < 501; i++)
DTW[i] = (float*)malloc(501 * sizeof(float));
DTW[i] = (float *)malloc(501 * sizeof(float));
for(int i=0;i<501;i++)
for (int i = 0; i < 501; i++)
{
for(int j=0;j<501;j++)
DTW[i][j]=1000;
for (int j = 0; j < 501; j++)
DTW[i][j] = 1000;
}
DTW[0][0]=0;
DTW[0][0] = 0;
for(int i=1;i<501;i++)
for (int i = 1; i < 501; i++)
{
for(int j=1;j<501;j++)
for (int j = 1; j < 501; j++)
{
float cost=fabs(freq1[i-1]-freq2[j-1]);
float cost = fabs(freq1[i - 1] - 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]));
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)
printf("DTWdistance: %f\n", DTW[500][500]);
if (DTW[500][500] >= 35)
{
free(freq2);
*result=0;
}
*result = 0;
}
else
{
long double *temp=freq1;
freq1=freq2;
long double *temp = freq1;
freq1 = freq2;
free(temp);
*result=1;
*result = 1;
}
free(DTW);
count_track1=1;
count_track1 = 1;
}
}

View File

@ -14,30 +14,30 @@ 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 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 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;
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);
// 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);
// 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)
{

View File

@ -20,9 +20,9 @@ gboolean update_images(gpointer* pars){
return TRUE;
}
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);
gboolean green = kmeans(df);
//gboolean green = kmeans(df);
//DTWdistance(df,&result);
if(parameters->g_last_image != 0){
osm_gps_map_image_remove(parameters->util_map, parameters->g_last_image);