Compare commits

..

No commits in common. "c3f46cb6a8dd27fc1cfc40e1c233d5ade9aaa1b3" and "3213347dcb24340fd465ec0b336e2cbc8048c015" have entirely different histories.

4 changed files with 9600 additions and 26952 deletions

View File

@ -1,3 +1,3 @@
int *getRandoms(int lower, int upper, int count); int *getRandoms(int lower, int upper, int count);
long double distance(struct Point* A, struct Point* B); long double distance(struct Point* A, struct Point* B);
bool Kmeans2(struct data_frame *df); void Kmeans2(struct data_frame *df);

File diff suppressed because it is too large Load Diff

View File

@ -13,17 +13,10 @@ struct Point
long double minDist; // default infinite dist to nearest cluster long double minDist; // default infinite dist to nearest cluster
}; };
struct Kmeans2 int count = 0;
{ struct Point *P;
int idcode;
int count;
struct Point *P;
struct Kmeans2 *next;
};
struct Kmeans2 *head_of_kmeans2 = NULL; long double distance(struct Point* A, struct Point* B)
long double distance(struct Point *A, struct Point *B)
{ {
return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y))); return (((A->x - B->x) * (A->x - B->x)) + ((A->y - B->y) * (A->y - B->y)));
} }
@ -63,183 +56,149 @@ int *getRandoms(int lower, int upper, int count)
} }
} }
bool Kmeans2(struct data_frame *df) void Kmeans2(struct data_frame *df)
{ {
if (head_of_kmeans2 == NULL) printf("count: %d\n",count);
if(count==0)
{ {
head_of_kmeans2 = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2)); P = (struct Point *)malloc(sizeof(struct Point) * 500);
head_of_kmeans2->idcode = to_intconvertor(df->idcode); }
head_of_kmeans2->count = 0; if (count != 500)
head_of_kmeans2->next = NULL; {
head_of_kmeans2->P = NULL; float CURR_FREQ;
return true; 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);
}
float CURR_vol;
if (df->dpmu[0]->fmt->phasor == '0')
{
if (df->dpmu[0]->fmt->polar == '0')
{
unsigned char s1[2];
unsigned char s2[2];
strncpy(s1, df->dpmu[0]->phasors[0], 2);
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 2);
long double v1 = to_intconvertor(s1);
long double v2 = to_intconvertor(s2);
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
}
else
{
unsigned char s1[2];
strncpy(s1, df->dpmu[0]->phasors[0], 2);
CURR_vol = to_intconvertor(s1);
}
}
else
{
if (df->dpmu[0]->fmt->polar == '0')
{
unsigned char s1[4];
unsigned char s2[4];
strncpy(s1, df->dpmu[0]->phasors[0], 4);
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 4);
long double v1 = decode_ieee_single(s1);
long double v2 = decode_ieee_single(s2);
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
}
else
{
unsigned char s1[4];
strncpy(s1, df->dpmu[0]->phasors[0], 4);
CURR_vol = decode_ieee_single(s1);
}
}
P[count].x = CURR_FREQ;
P[count].y = CURR_vol;
P[count].cluster = -1;
P[count].minDist = __DBL_MAX__;
count++;
} }
else else
{ {
struct Kmeans2 *temp = head_of_kmeans2; int no_of_clusters = 5;
struct Kmeans2 *previous = NULL; int epochs = 20;
while (temp != NULL) int *c = getRandoms(0, 499, no_of_clusters);
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++)
{ {
if (temp->idcode == to_intconvertor(df->idcode)) Centroids[i].x = P[c[i]].x;
Centroids[i].y = P[c[i]].y;
Centroids[i].minDist = __DBL_MAX__;
Centroids[i].cluster = -1;
}
free(c);
while (epochs--)
{
for (int i = 0; i < no_of_clusters; i++)
{ {
printf("count: %d\n",temp->count); for (int j = 0; j < 500; j++)
if (temp->count == 0)
{ {
temp->P = (struct Point *)malloc(sizeof(struct Point) * 500); long double dist = distance(&Centroids[i], &P[j]);
if (P[j].minDist > dist)
{
P[j].minDist = dist;
P[j].cluster = i;
}
} }
if (temp->count != 500)
{
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);
}
float CURR_vol;
if (df->dpmu[0]->fmt->phasor == '0')
{
if (df->dpmu[0]->fmt->polar == '0')
{
unsigned char s1[2];
unsigned char s2[2];
strncpy(s1, df->dpmu[0]->phasors[0], 2);
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 2);
long double v1 = to_intconvertor(s1);
long double v2 = to_intconvertor(s2);
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
}
else
{
unsigned char s1[2];
strncpy(s1, df->dpmu[0]->phasors[0], 2);
CURR_vol = to_intconvertor(s1);
}
}
else
{
if (df->dpmu[0]->fmt->polar == '0')
{
unsigned char s1[4];
unsigned char s2[4];
strncpy(s1, df->dpmu[0]->phasors[0], 4);
strncpy(s2, df->dpmu[0]->phasors[0] + 2, 4);
long double v1 = decode_ieee_single(s1);
long double v2 = decode_ieee_single(s2);
CURR_vol = sqrt((v1 * v1) + (v2 * v2));
}
else
{
unsigned char s1[4];
strncpy(s1, df->dpmu[0]->phasors[0], 4);
CURR_vol = decode_ieee_single(s1);
}
}
temp->P[temp->count].x = CURR_FREQ;
temp->P[temp->count].y = CURR_vol;
temp->P[temp->count].cluster = -1;
temp->P[temp->count].minDist = __DBL_MAX__;
temp->count++;
}
else
{
int no_of_clusters = 5;
int epochs = 20;
int *c = getRandoms(0, 499, no_of_clusters);
struct Point *Centroids = (struct Point *)malloc(sizeof(struct Point) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++)
{
Centroids[i].x = temp->P[c[i]].x;
Centroids[i].y = temp->P[c[i]].y;
Centroids[i].minDist = __DBL_MAX__;
Centroids[i].cluster = -1;
}
free(c);
while (epochs--)
{
for (int i = 0; i < no_of_clusters; i++)
{
for (int j = 0; j < 500; j++)
{
long double dist = distance(&Centroids[i], &temp->P[j]);
if (temp->P[j].minDist > dist)
{
temp->P[j].minDist = dist;
temp->P[j].cluster = i;
}
}
}
int *nPoints = (int *)malloc(sizeof(int) * no_of_clusters);
long double *Sumx = (long double *)malloc(sizeof(long double) * no_of_clusters);
long double *Sumy = (long double *)malloc(sizeof(long double) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++)
{
nPoints[i] = 0;
Sumx[i] = 0;
Sumy[i] = 0;
}
for (int i = 0; i < 500; i++)
{
nPoints[temp->P[i].cluster]++;
Sumx[temp->P[i].cluster] += temp->P[i].x;
Sumy[temp->P[i].cluster] += temp->P[i].y;
temp->P[i].minDist = __DBL_MAX__;
}
for (int i = 0; i < no_of_clusters; i++)
{
Centroids[i].x = Sumx[i] / nPoints[i];
Centroids[i].y = Sumy[i] / nPoints[i];
}
free(nPoints);
free(Sumx);
free(Sumy);
}
temp->count = 0;
FILE *fp;
fp = fopen("kmeans.txt", "a");
for (int i = 0; i < 500; i++)
{
fprintf(fp, "%Lf, %Lf, %d\n", temp->P[i].x, temp->P[i].y, temp->P[i].cluster);
}
fprintf(fp, "\n\n");
for (int i = 0; i < no_of_clusters; i++)
{
fprintf(fp, "%d : %Lf, %Lf\n", i, Centroids[i].x, Centroids[i].y);
}
fprintf(fp, "\n\n");
fclose(fp);
free(Centroids);
free(temp->P);
}
return true;
break;
} }
previous = temp;
temp = temp->next; int *nPoints = (int *)malloc(sizeof(int) * no_of_clusters);
long double *Sumx = (long double *)malloc(sizeof(long double) * no_of_clusters);
long double *Sumy = (long double *)malloc(sizeof(long double) * no_of_clusters);
for (int i = 0; i < no_of_clusters; i++)
{
nPoints[i] = 0;
Sumx[i] = 0;
Sumy[i] = 0;
}
for (int i = 0; i < 500; i++)
{
nPoints[P[i].cluster]++;
Sumx[P[i].cluster] += P[i].x;
Sumy[P[i].cluster] += P[i].y;
P[i].minDist = __DBL_MAX__;
}
for (int i = 0; i < no_of_clusters; i++)
{
Centroids[i].x = Sumx[i] / nPoints[i];
Centroids[i].y = Sumy[i] / nPoints[i];
}
free(nPoints);
free(Sumx);
free(Sumy);
} }
if (temp == NULL) count = 0;
FILE *fp;
fp = fopen("kmeans.txt","a");
for(int i=0;i<500;i++)
{ {
struct Kmeans2 *bring = (struct Kmeans2 *)malloc(sizeof(struct Kmeans2)); fprintf(fp,"%Lf, %Lf, %d\n",P[i].x,P[i].y,P[i].cluster);
bring->idcode = to_intconvertor(df->idcode);
bring->count = 0;
bring->next = NULL;
bring->P = NULL;
previous->next = bring;
return true;
} }
fprintf(fp,"\n\n");
for(int i=0;i<no_of_clusters;i++)
{
fprintf(fp,"%d : %Lf, %Lf\n",i,Centroids[i].x,Centroids[i].y);
}
fprintf(fp,"\n\n");
fclose(fp);
free(Centroids);
free(P);
} }
} }

View File

@ -1,6 +1,5 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <stdbool.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include <pthread.h> #include <pthread.h>
#include "global.h" #include "global.h"
@ -155,11 +154,7 @@ gboolean update_images(gpointer* pars){
}else if (algorithm==1 && dimmension == 1){ }else if (algorithm==1 && dimmension == 1){
}else if (algorithm==1 && dimmension == 2){ }else if (algorithm==1 && dimmension == 2){
if(!Kmeans2(df)){ Kmeans2(df);
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);
}else{
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_green_image);
}
}else if (algorithm==2 && dimmension == 0){ }else if (algorithm==2 && dimmension == 0){
if(!DTWvolDistance(df)){ if(!DTWvolDistance(df)){
vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image); vis_ptr->last_image = osm_gps_map_image_add(parameters->util_map,lat, lon, parameters->g_red_image);