Browse Source

Add header documentation

Add Processing script for visualizing results
Change camelCase strings to _underscore notation
master
Apostolos Fanakis 7 years ago
parent
commit
3d19e31ec1
  1. 4
      Makefile
  2. 35
      output/visualization/visualization.pde
  3. 6
      serial.c
  4. 28
      serialDeclarations.h
  5. 71
      serial_declarations.c
  6. 50
      serial_declarations.h

4
Makefile

@ -6,8 +6,8 @@ SHELL := /bin/bash
CC = gcc CC = gcc
RM = rm -f RM = rm -f
CFLAGS=-lm -O3 -Wall -I. CFLAGS=-lm -O3 -Wall -I.
OBJ=serial.o serialDeclarations.o OBJ=serial.o serial_declarations.o
DEPS=serialDeclarations.h DEPS=serial_declarations.h
# ========================================== # ==========================================
# TARGETS # TARGETS

35
output/visualization/visualization.pde

@ -0,0 +1,35 @@
int frame = 1;
PShape frameS;
void setup() {
size(1280, 720);
frameRate(12);
}
int scale = 40;
float radius = 2;
void draw() {
background(255);
stroke(0);
translate(220, 0);
//scale(scale);
fill(0);
String[] lines;
lines = loadStrings("../output_" + frame);
if (lines == null){
delay(5000);
exit();
} else {
for (int i = 0; i < lines.length; i++) {
String[] pieces = split(lines[i], ",");
frameS = createShape(ELLIPSE, Float.parseFloat(pieces[0])*scale,Float.parseFloat(pieces[1])*scale, radius, radius);
shape(frameS, 0, 0);
}
}
frame++;
//Uncomment to save each frame to a jpg file
//saveFrame("out-######.jpg");
delay(600);
}

6
serial.c

@ -2,7 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <sys/time.h> #include <sys/time.h>
#include "serialDeclarations.h" #include "serial_declarations.h"
int NUMBER_OF_POINTS = 600; int NUMBER_OF_POINTS = 600;
int DIMENSIONS = 2; int DIMENSIONS = 2;
@ -56,11 +56,11 @@ int main(int argc, char **argv){
parameters *opt; parameters *opt;
opt = &params; opt = &params;
double **shiftedPoints; double **shifted_points;
// tic // tic
gettimeofday (&startwtime, NULL); gettimeofday (&startwtime, NULL);
int iterations = meanshift(vectors, &shiftedPoints, h, opt, 1); int iterations = meanshift(vectors, &shifted_points, h, opt, 1);
// toc // toc
gettimeofday (&endwtime, NULL); gettimeofday (&endwtime, NULL);

28
serialDeclarations.h

@ -1,28 +0,0 @@
#ifndef SERIAL_DECLARATIONS_H /* Include guard */
#define SERIAL_DECLARATIONS_H
#include <stdbool.h>
extern int NUMBER_OF_POINTS;
extern int DIMENSIONS;
extern char* POINTS_FILENAME;
extern char* LABELS_FILENAME;
typedef struct parameters {
double epsilon;
bool verbose;
bool display;
} parameters;
void get_args(int argc, char **argv, int *h);
int meanshift(double **originalPoints, double ***shiftedPoints, int h
, parameters *opt, int iteration);
double norm(double ** m, int rows, int cols);
void multiply(double ** matrix1, double ** matrix2, double ** output);
double calculateDistance(double *, double *);
double **alloc_2d_double(int rows, int cols);
double **duplicate(double **a, double **b, int rows, int cols);
void print_matrix(double ** array, int rows, int cols);
void save_matrix(double **matrix,int iteration);
#endif //SERIAL_DECLARATIONS_H

71
serialDeclarations.c → serial_declarations.c

@ -4,7 +4,7 @@
#include <float.h> #include <float.h>
#include <string.h> #include <string.h>
#include "serialDeclarations.h" #include "serial_declarations.h"
void get_args(int argc, char **argv, int *h){ void get_args(int argc, char **argv, int *h){
if (argc != 6) { if (argc != 6) {
@ -24,47 +24,47 @@ void get_args(int argc, char **argv, int *h){
LABELS_FILENAME = argv[5]; LABELS_FILENAME = argv[5];
} }
int meanshift(double **originalPoints, double ***shiftedPoints, int h int meanshift(double **original_points, double ***shifted_points, int h
, parameters *opt, int iteration){ , parameters *opt, int iteration){
// allocates space and copies original points on first iteration // allocates space and copies original points on first iteration
if (iteration == 1){ if (iteration == 1){
(*shiftedPoints) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); (*shifted_points) = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
(*shiftedPoints) = duplicate(originalPoints, (*shiftedPoints) duplicate(original_points, NUMBER_OF_POINTS, DIMENSIONS, shifted_points);
, NUMBER_OF_POINTS, DIMENSIONS);
} }
// mean shift vector // mean shift vector
double **meanShiftVector; double **mean_shift_vector;
meanShiftVector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); mean_shift_vector = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
// initialize elements of meanShiftVector to inf // initialize elements of mean_shift_vector to inf
for (int i=0;i<NUMBER_OF_POINTS;i++){ for (int i=0;i<NUMBER_OF_POINTS;i++){
for (int j=0;j<DIMENSIONS;j++){ for (int j=0;j<DIMENSIONS;j++){
meanShiftVector[i][j] = DBL_MAX; mean_shift_vector[i][j] = DBL_MAX;
} }
} }
double **kernelMatrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS); double **kernel_matrix = alloc_2d_double(NUMBER_OF_POINTS, NUMBER_OF_POINTS);
double *denominator = malloc(NUMBER_OF_POINTS * sizeof(double)); double *denominator = malloc(NUMBER_OF_POINTS * sizeof(double));
// find pairwise distance matrix (inside radius) // find pairwise distance matrix (inside radius)
// [I, D] = rangesearch(x,y,h); // [I, D] = rangesearch(x,y,h);
for (int i=0; i<NUMBER_OF_POINTS; i++){ for (int i=0; i<NUMBER_OF_POINTS; i++){
double sum =0; double sum = 0;
for (int j=0; j<NUMBER_OF_POINTS; j++){ for (int j=0; j<NUMBER_OF_POINTS; j++){
double dist = calculateDistance((*shiftedPoints)[i],originalPoints[j]); double dist = calculateDistance((*shifted_points)[i]
, original_points[j]);
if (i == j){ if (i == j){
kernelMatrix[i][j] = 1; kernel_matrix[i][j] = 1;
} else if (dist < h*h){ } else if (dist < h*h){
kernelMatrix[i][j] = dist * dist; kernel_matrix[i][j] = dist * dist;
// compute kernel matrix // compute kernel matrix
double pow = ((-1)*(kernelMatrix[i][j]))/(2*(h*h)); double pow = ((-1)*(kernel_matrix[i][j]))/(2*(h*h));
kernelMatrix[i][j] = exp(pow); kernel_matrix[i][j] = exp(pow);
} else { } else {
kernelMatrix[i][j] = 0; kernel_matrix[i][j] = 0;
} }
sum = sum + kernelMatrix[i][j]; sum = sum + kernel_matrix[i][j];
} }
denominator[i] = sum; denominator[i] = sum;
} }
@ -72,25 +72,25 @@ int meanshift(double **originalPoints, double ***shiftedPoints, int h
// create new y vector // create new y vector
double **y_new = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS); double **y_new = alloc_2d_double(NUMBER_OF_POINTS, DIMENSIONS);
multiply(kernelMatrix, originalPoints, y_new); multiply(kernel_matrix, original_points, y_new);
// divide element-wise // divide element-wise
for (int i=0; i<NUMBER_OF_POINTS; i++){ for (int i=0; i<NUMBER_OF_POINTS; i++){
for (int j=0; j<DIMENSIONS; j++){ for (int j=0; j<DIMENSIONS; j++){
y_new[i][j] = y_new[i][j] / denominator[i]; y_new[i][j] = y_new[i][j] / denominator[i];
// calculate mean-shift vector // calculate mean-shift vector
meanShiftVector[i][j] = y_new[i][j] - (*shiftedPoints)[i][j]; mean_shift_vector[i][j] = y_new[i][j] - (*shifted_points)[i][j];
} }
} }
shiftedPoints = &y_new; shifted_points = &y_new;
save_matrix((*shiftedPoints), iteration); save_matrix((*shifted_points), iteration);
double current_norm = norm(meanShiftVector, NUMBER_OF_POINTS, DIMENSIONS); double current_norm = norm(mean_shift_vector, NUMBER_OF_POINTS, DIMENSIONS);
printf("Iteration n. %d, error %f \n", iteration, current_norm); printf("Iteration n. %d, error %f \n", iteration, current_norm);
/** iterate until convergence **/ /** iterate until convergence **/
if (current_norm > opt->epsilon) { if (current_norm > opt->epsilon) {
return meanshift(originalPoints, shiftedPoints, h, opt, ++iteration); return meanshift(original_points, shifted_points, h, opt, ++iteration);
} }
return iteration; return iteration;
@ -98,11 +98,11 @@ int meanshift(double **originalPoints, double ***shiftedPoints, int h
// TODO check why there's is a difference in the norm calculate in matlab // TODO check why there's is a difference in the norm calculate in matlab
double norm(double **matrix, int rows, int cols){ double norm(double **matrix, int rows, int cols){
double sum=0, tempMul=0; double sum=0, temp_mul=0;
for (int i=0; i<rows; i++) { for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) { for (int j=0; j<cols; j++) {
tempMul = matrix[i][j] * matrix[i][j]; temp_mul = matrix[i][j] * matrix[i][j];
sum = sum + tempMul; sum = sum + temp_mul;
} }
} }
double norm = sqrt(sum); double norm = sqrt(sum);
@ -133,7 +133,6 @@ double calculateDistance(double *y, double *x){
return distance; return distance;
} }
// allocates a 2d array in continuous memory positions
double **alloc_2d_double(int rows, int cols) { double **alloc_2d_double(int rows, int cols) {
double *data = (double *) malloc(rows*cols*sizeof(double)); double *data = (double *) malloc(rows*cols*sizeof(double));
double **array = (double **) malloc(rows*sizeof(double*)); double **array = (double **) malloc(rows*sizeof(double*));
@ -142,14 +141,12 @@ double **alloc_2d_double(int rows, int cols) {
return array; return array;
} }
// copy the values of a 2d double array to another void duplicate(double **source, int rows, int cols, double ***dest){
double **duplicate(double **a, double **b, int rows, int cols){
for (int i=0; i<rows; i++){ for (int i=0; i<rows; i++){
for (int j=0; j<cols; j++){ for (int j=0; j<cols; j++){
b[i][j] = a[i][j]; (*dest)[i][j] = source[i][j];
} }
} }
return b;
} }
void print_matrix(double **array, int rows, int cols){ void print_matrix(double **array, int rows, int cols){
@ -164,15 +161,15 @@ void print_matrix(double **array, int rows, int cols){
void save_matrix(double **matrix, int iteration){ void save_matrix(double **matrix, int iteration){
char filename[18]; char filename[18];
snprintf(filename, sizeof(filename), "%s%d", "output/output_", iteration); snprintf(filename, sizeof(filename), "%s%d", "output/output_", iteration);
FILE *iterOutput; FILE *file;
iterOutput = fopen(filename, "w"); file = fopen(filename, "w");
for (int rows=0; rows<NUMBER_OF_POINTS; ++rows){ for (int rows=0; rows<NUMBER_OF_POINTS; ++rows){
for (int cols=0; cols<DIMENSIONS; ++cols){ for (int cols=0; cols<DIMENSIONS; ++cols){
fprintf(iterOutput, "%f", matrix[rows][cols]); fprintf(file, "%f", matrix[rows][cols]);
if (cols != DIMENSIONS - 1){ if (cols != DIMENSIONS - 1){
fprintf(iterOutput, ","); fprintf(file, ",");
} }
} }
fprintf(iterOutput, "\n"); fprintf(file, "\n");
} }
} }

50
serial_declarations.h

@ -0,0 +1,50 @@
#ifndef SERIAL_DECLARATIONS_H /* Include guard */
#define SERIAL_DECLARATIONS_H
#include <stdbool.h>
extern int NUMBER_OF_POINTS;
extern int DIMENSIONS;
extern char* POINTS_FILENAME;
extern char* LABELS_FILENAME;
typedef struct parameters {
double epsilon;
bool verbose;
bool display;
} parameters;
//Function get_args parses command line arguments.
void get_args(int argc, char **argv, int *h);
//Function meanshift recursively shifts original points according to th
//mean-shift algorithm saving the result to shiftedPoints. Struct opt has user
//options, h is the desirable deviation, iteration is this call's iteration
//number.
int meanshift(double **original_points, double ***shifted_points, int h
, parameters *opt, int iteration);
//Function norm returns the second norm of matrix of dimensions rowsXcols.
double norm(double **matrix, int rows, int cols);
//Function multiply calculates the product of matrices 1 and 2 into output.
void multiply(double **matrix1, double **matrix2, double **output);
//Function calculateDistance returns the distance between x and y vectors.
double calculateDistance(double *y, double *x);
//Function alloc_2d_double allocates rows*cols bytes of continuous memory.
double **alloc_2d_double(int rows, int cols);
//Function duplicate copies the values of source array to dest array.
void duplicate(double **source, int rows, int cols, double ***dest);
//Function print_matrix prints array of dimensions rowsXcols to the console.
void print_matrix(double **array, int rows, int cols);
//Function save_matrix prints matrix in a csv file with path/filename
//"output/output_iteration". If a file already exists new lines are concatenated.
void save_matrix(double **matrix
, int iteration);
#endif //SERIAL_DECLARATIONS_H
Loading…
Cancel
Save