normalised added DTW algo

This commit is contained in:
pavanvpatil 2022-10-25 21:42:46 +05:30
parent 658e423553
commit 1ee60c80b2
6 changed files with 31020 additions and 29684 deletions

View File

@ -1,4 +1,3 @@
/*pavan changes*/ /*pavan changes*/
void DTWdistance(struct data_frame *df,gboolean *result); void DTWdistance(struct data_frame *df,gboolean *result);

View File

@ -2,15 +2,9 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
// intial variables // intial variables
unsigned long long int count_A=500;
unsigned long long int count_B=500;
unsigned long long int count_C=500;
long double A=50;
long double B=49.5;
long double C=50.5;
//function declaration //function declaration
gboolean kmeans(struct data_frame *df,unsigned long long int *count_A,unsigned long long int *count_B, gboolean kmeans(struct data_frame *df);
unsigned long long int *count_C,long double *A,long double *B,long double *C);
/*Pavan Changes*/ /*Pavan Changes*/

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,3 @@
#include "parser.h" #include "parser.h"
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
@ -11,11 +10,11 @@ int count_track1=0;
int count_track2=0; int count_track2=0;
int flag=0; int flag=0;
void DTWdistance(struct data_frame *df,gboolean *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("count1: %d\ncount2: %d\n",count_track1,count_track2);
//printf("curr_freq: %f\n",CURR_FREQ); // printf("curr_freq: %f\n",CURR_FREQ);
if(count_track1==0) if(count_track1==0)
{ {
if(flag==0) if(flag==0)
@ -66,18 +65,26 @@ void DTWdistance(struct data_frame *df,gboolean *result)
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); // 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(" DTW[%d][%d]: %f\n",i,j,DTW[i][j]);
} }
} }
printf("DTWdistance: %f\n",DTW[500][500]); printf("DTWdistance: %f\n",DTW[500][500]);
if(DTW[500][500]>=35) *result=false; if(DTW[500][500]>=35)
else *result=true; {
free(freq1); free(freq2);
free(freq2); *result=0;
}
else
{
long double *temp=freq1;
freq1=freq2;
free(temp);
*result=1;
}
free(DTW); free(DTW);
count_track1=0; count_track1=1;
} }
} }

View File

@ -2,32 +2,56 @@
#include "parser.h" #include "parser.h"
#include <stdlib.h> #include <stdlib.h>
c #include <stdio.h>
#include <gtk/gtk.h>
#include <math.h> #include <math.h>
FILE *fp; 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;
gboolean kmeans(struct data_frame *df,unsigned long long int *count_A,unsigned long long int *count_B, /*In India frequency ranges from 49.5 to 50.2 Hz*/
unsigned long long int *count_C,long double *A,long double *B,long double *C) 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)
{ {
float CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq)*1e-3; long double CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3;
long double diff_A = fabs(*A-CURR_FREQ); long double CURR_FREQ_temp=CURR_FREQ;
long double diff_B = fabs(*B-CURR_FREQ); CURR_FREQ = (CURR_FREQ - mean) / std_dev;
long double diff_C = fabs(*C-CURR_FREQ); long double diff_A = fabs(A - CURR_FREQ);
//fp = fopen("points.csv","a"); long double diff_B = fabs(B - CURR_FREQ);
//printf("A: %Lf, B: %Lf, C: %Lf\n",*A,*B,*C); long double diff_C = fabs(C - CURR_FREQ);
//fprintf(fp,"%Lf, %Lf, %Lf\n",*A,*B,*C); fp = fopen("points.csv","a");
//fclose(fp); printf("A: %Lf, B: %Lf, C: %Lf\n",A,B,C);
if(diff_A <= diff_B && diff_A <= diff_C){ fprintf(fp,"%Lf, %Lf, %Lf\n",A,B,C);
*A = ((*count_A*(*A))+CURR_FREQ)/(++*count_A); 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; return TRUE;
} }
else if(diff_B < diff_C){ else if (diff_B < diff_C)
*B = ((*count_B*(*B))+CURR_FREQ)/(++*count_B); {
B = ((count_B * (B)) + CURR_FREQ) / (++count_B);
return FALSE; return FALSE;
} }
else{ else
*C = ((*count_C*(*C))+CURR_FREQ)/(++*count_C); {
C = ((count_C * (C)) + CURR_FREQ) / (++count_C);
return FALSE; return FALSE;
} }
} }

View File

@ -1,5 +1,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <gtk/gtk.h>
#include "align_sort.h" #include "align_sort.h"
#include "map_vis.h" #include "map_vis.h"
#include "parser.h" #include "parser.h"
@ -8,7 +9,9 @@
#include "Kmeans.h" #include "Kmeans.h"
#include "Dynamic_time_warping.h" #include "Dynamic_time_warping.h"
gboolean value = true; /*used for DTW*/
int result=1;
/*used for DTW*/
gboolean update_images(gpointer* pars){ gboolean update_images(gpointer* pars){
myParameters* parameters = (myParameters*) pars; myParameters* parameters = (myParameters*) pars;
@ -19,9 +22,8 @@ gboolean update_images(gpointer* pars){
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,&START,&COUNT,&SUM_OF_FREQUENCY);
//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,&count_A,&count_B,&count_C,&A,&B,&C); gboolean green = kmeans(df);
DTWdistance(df,&value); //DTWdistance(df,&result);
gboolean green=value;
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);
} }