DTW for 1 2

This commit is contained in:
pavanvpatil 2022-10-28 17:37:13 +05:30
parent ebb28668a8
commit 04f81da3d4
3 changed files with 273 additions and 80 deletions

View File

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

View File

@ -4,47 +4,97 @@
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <stdlib.h> #include <stdlib.h>
typedef struct
{
int idcode;
long double *freq1; long double *freq1;
long double *freq2; long double *freq2;
int count_track1 = 0; int count_track1;
int count_track2 = 0; int count_track2;
int flag = 0; int flag;
int result;
DTWfreqlist *next;
} DTWfreqlist;
void DTWdistance(struct data_frame *df, int *result) typedef struct
{ {
float CURR_FREQ = 50 + to_intconvertor(df->dpmu[0]->freq) * 1e-3; int idcode;
long double *vol1;
long double *vol2;
int count_track1;
int count_track2;
int flag;
int result;
DTWvollist *next;
} DTWvollist;
DTWfreqlist *head = NULL;
DTWvollist *headvol = NULL;
void DTWfreqDistance(struct data_frame *df)
{
if (head == NULL)
{
head = (DTWfreqlist *)malloc(sizeof(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
{
DTWfreqlist *temp = head;
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("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 (temp->count_track1 == 0)
{ {
if (flag == 0) if (tmp->flag == 0)
{ {
flag = 1; temp->flag = 1;
freq1 = (long double *)malloc(500 * sizeof(long double)); temp->freq1 = (long double *)malloc(500 * sizeof(long double));
} }
freq1[count_track2] = CURR_FREQ; temp->freq1[temp->count_track2] = CURR_FREQ;
count_track2++; temp->count_track2++;
if (count_track2 == 500) if (temp->count_track2 == 500)
{ {
count_track2 = 0; temp->count_track2 = 0;
flag = 0; temp->flag = 0;
count_track1++; temp->count_track1++;
} }
} }
else if (count_track1 == 1) else if (temp->count_track1 == 1)
{ {
if (flag == 0) if (temp->flag == 0)
{ {
flag = 1; temp->flag = 1;
freq2 = (long double *)malloc(500 * sizeof(long double)); temp->freq2 = (long double *)malloc(500 * sizeof(long double));
} }
freq2[count_track2] = CURR_FREQ; temp->freq2[temp->count_track2] = CURR_FREQ;
count_track2++; temp->count_track2++;
if (count_track2 == 500) if (temp->count_track2 == 500)
{ {
count_track2 = 0; temp->count_track2 = 0;
flag = 0; temp->flag = 0;
count_track1++; temp->count_track1++;
} }
} }
else else
@ -64,7 +114,7 @@ void DTWdistance(struct data_frame *df, int *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(temp->freq1[i - 1] - temp->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]);
@ -74,17 +124,162 @@ void DTWdistance(struct data_frame *df, int *result)
printf("DTWdistance: %f\n", DTW[500][500]); printf("DTWdistance: %f\n", DTW[500][500]);
if (DTW[500][500] >= 35) if (DTW[500][500] >= 35)
{ {
free(freq2); free(temp->freq2);
*result = 0; temp->result = 0;
} }
else else
{ {
long double *temp = freq1; long double *f = temp->freq1;
freq1 = freq2; temp->freq1 = temp->freq2;
free(temp); free(f);
*result = 1; temp->result = 1;
} }
free(DTW); free(DTW);
count_track1 = 1; temp->count_track1 = 1;
}
break;
}
previous = temp;
temp = temp->next;
}
if (temp == NULL)
{
DTWfreqlist *bring = (DTWfreqlist *)malloc(sizeof(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;
}
}
void DTWvolDistance(struct data_frame *df)
{
if (headvol == NULL)
{
headvol = (DTWvollist *)malloc(sizeof(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
{
DTWvollist *temp = headvol;
DTWvollist *previous;
while (temp != NULL)
{
if (to_intconvertor(df->idcode) == temp->idcode)
{
float CURR_vol;
if (df->dpmu[0]->fmt->vol == '0')
{
CURR_vol = 50 + to_intconvertor(df->dpmu[0]->vol) * 1e-3;
}
else
{
CURR_vol = decode_ieee_single(df->dpmu[0]->vol);
}
// printf("count1: %d\ncount2: %d\n",count_track1,count_track2);
// printf("curr_vol: %f\n",CURR_vol);
if (temp->count_track1 == 0)
{
if (tmp->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)
{
DTWvollist *bring = (DTWvollist *)malloc(sizeof(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;
} }
} }

View File

@ -9,25 +9,21 @@
#include "Kmeans.h" #include "Kmeans.h"
#include "Dynamic_time_warping.h" #include "Dynamic_time_warping.h"
/*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;
struct data_frame *df = TSB[0].first_data_frame; struct data_frame *df = TSB[0].first_data_frame;
if (df == NULL){ if (df == NULL){
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); //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);
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 (green){ if (DTWfreqDistance(df)){
parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_green_image); parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_green_image);
}else{ }else{
parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image); parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image);