From 04f81da3d477391a3e46736f8abab128f13d0b21 Mon Sep 17 00:00:00 2001 From: pavanvpatil Date: Fri, 28 Oct 2022 17:37:13 +0530 Subject: [PATCH] DTW for 1 2 --- iPDC/inc/Dynamic_time_warping.h | 4 +- iPDC/src/Dynamic_time_warping.c | 337 +++++++++++++++++++++++++------- iPDC/src/map_vis.c | 12 +- 3 files changed, 273 insertions(+), 80 deletions(-) diff --git a/iPDC/inc/Dynamic_time_warping.h b/iPDC/inc/Dynamic_time_warping.h index d89f7b3..1ea8edb 100644 --- a/iPDC/inc/Dynamic_time_warping.h +++ b/iPDC/inc/Dynamic_time_warping.h @@ -1,3 +1,5 @@ /*pavan changes*/ -void DTWdistance(struct data_frame *df,gboolean *result); \ No newline at end of file +void DTWfreqDistance(struct data_frame *df); + +void DTWvolDistance(struct data_frame *df); \ No newline at end of file diff --git a/iPDC/src/Dynamic_time_warping.c b/iPDC/src/Dynamic_time_warping.c index 8f84835..6924707 100644 --- a/iPDC/src/Dynamic_time_warping.c +++ b/iPDC/src/Dynamic_time_warping.c @@ -4,87 +4,282 @@ #include #include -long double *freq1; -long double *freq2; -int count_track1 = 0; -int count_track2 = 0; -int flag = 0; - -void DTWdistance(struct data_frame *df, int *result) +typedef struct { - 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) + int idcode; + long double *freq1; + long double *freq2; + int count_track1; + int count_track2; + int flag; + int result; + DTWfreqlist *next; +} DTWfreqlist; + +typedef struct +{ + 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) { - if (flag == 0) - { - flag = 1; - freq1 = (long double *)malloc(500 * sizeof(long double)); - } - freq1[count_track2] = CURR_FREQ; - count_track2++; - if (count_track2 == 500) - { - count_track2 = 0; - flag = 0; - count_track1++; - } - } - else if (count_track1 == 1) - { - if (flag == 0) - { - flag = 1; - freq2 = (long double *)malloc(500 * sizeof(long double)); - } - freq2[count_track2] = CURR_FREQ; - count_track2++; - if (count_track2 == 500) - { - count_track2 = 0; - flag = 0; - count_track1++; - } + 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 { - 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++) + DTWfreqlist *temp = head; + DTWfreqlist *previous; + while (temp != NULL) { - 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++) + if (to_intconvertor(df->idcode) == temp->idcode) { - 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])); - // printf(" DTW[%d][%d]: %f\n",i,j,DTW[i][j]); - } - } + 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("DTWdistance: %f\n", DTW[500][500]); - if (DTW[500][500] >= 35) - { - free(freq2); - *result = 0; + // printf("count1: %d\ncount2: %d\n",count_track1,count_track2); + // printf("curr_freq: %f\n",CURR_FREQ); + if (temp->count_track1 == 0) + { + if (tmp->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; } - else + if (temp == NULL) { - long double *temp = freq1; - freq1 = freq2; - free(temp); - *result = 1; + 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; } - free(DTW); - count_track1 = 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; } } \ No newline at end of file diff --git a/iPDC/src/map_vis.c b/iPDC/src/map_vis.c index 32928cd..997c5ec 100644 --- a/iPDC/src/map_vis.c +++ b/iPDC/src/map_vis.c @@ -9,25 +9,21 @@ #include "Kmeans.h" #include "Dynamic_time_warping.h" -/*used for DTW*/ -int result=1; -/*used for DTW*/ - gboolean update_images(gpointer* pars){ myParameters* parameters = (myParameters*) pars; struct data_frame *df = TSB[0].first_data_frame; if (df == NULL){ return TRUE; } - int freq = to_intconvertor(df->dpmu[0]->freq); - gboolean green =attack_detect(df); + //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); - //DTWdistance(df,&result); + if(parameters->g_last_image != 0){ 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); }else{ parameters->g_last_image = osm_gps_map_image_add(parameters->util_map,15.518597, 74.925584, parameters->g_red_image);