This commit is contained in:
kbecke23 2018-05-11 12:00:37 +02:00
parent 62ffcca286
commit 66177c97da
1 changed files with 153 additions and 152 deletions

View File

@ -42,7 +42,7 @@ typedef struct {
point_t points[NUMBER_OF_SAMPLES]; // [0] = xActual, [1]=xpredicted from localMean, [2]=xpredicted from directPredecessor, [3] = xpredicted from differentialpredecessor, [4] = xError from localMean, [5] xError from directPredecessor, [6] xError from differentialPredecessor point_t points[NUMBER_OF_SAMPLES]; // [0] = xActual, [1]=xpredicted from localMean, [2]=xpredicted from directPredecessor, [3] = xpredicted from differentialpredecessor, [4] = xError from localMean, [5] xError from directPredecessor, [6] xError from differentialPredecessor
/* *ppm read, copy, write* */ /* *ppm read, copy, write* */
typedef struct { typedef struct {
unsigned char red, green, blue; unsigned char red, green, blue;
}colorChannel_t; }colorChannel_t;
@ -55,9 +55,9 @@ typedef struct {
static imagePixel_t * rdPPM(char *fileName); // read PPM file format static imagePixel_t * rdPPM(char *fileName); // read PPM file format
void mkPpmFile(char *fileName, imagePixel_t *image); // writes PPM file void mkPpmFile(char *fileName, imagePixel_t *image); // writes PPM file
int ppmColorChannel(FILE* fp, imagePixel_t *image); // writes colorChannel from PPM file to log file int ppmColorChannel(FILE* fp, imagePixel_t *image); // writes colorChannel from PPM file to log file
void colorSamples( FILE* fp ); // stores color channel values in xSamples[] void colorSamples(FILE* fp); // stores color channel values in xSamples[]
/* *file handling* */ /* *file handling* */
char * mkFileName(char* buffer, size_t max_len, int suffixId); char * mkFileName(char* buffer, size_t max_len, int suffixId);
char *fileSuffix(int id); char *fileSuffix(int id);
void myLogger(FILE* fp, point_t points[]); void myLogger(FILE* fp, point_t points[]);
@ -71,9 +71,9 @@ double rndm(void);
double sum_array(double x[], int length); double sum_array(double x[], int length);
void directPredecessor(void); void directPredecessor(void);
void localMean(void); void localMean(void);
void differentialPredecessor( void ); void differentialPredecessor(void);
double *popNAN(double *xError, int xErrorLength); //return new array without NAN values double *popNAN(double *xError, int xErrorLength); //return new array without NAN values
double windowXMean( int _arraylength, int xCount ); double windowXMean(int _arraylength, int xCount);
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -90,14 +90,14 @@ int main(int argc, char **argv) {
printf("%d\n", xLength); printf("%d\n", xLength);
FILE* fp6 = fopen(fileName, "r"); FILE* fp6 = fopen(fileName, "r");
colorSamples ( fp6 ); colorSamples(fp6);
srand((unsigned int)time(NULL)); srand((unsigned int)time(NULL));
for (i = 0; i < NUMBER_OF_SAMPLES; i++) { for (i = 0; i < NUMBER_OF_SAMPLES; i++) {
// _x[i] += ((255.0 / M) * i); // Init test values // _x[i] += ((255.0 / M) * i); // Init test values
for (int k = 0; k < WINDOWSIZE; k++) { for (int k = 0; k < WINDOWSIZE; k++) {
w[k][i]= rndm(); // Init weights w[k][i] = rndm(); // Init weights
} }
} }
@ -105,7 +105,7 @@ int main(int argc, char **argv) {
// save plain test_array before math magic happens // save plain test_array before math magic happens
FILE *fp0 = fopen(fileName, "w"); FILE *fp0 = fopen(fileName, "w");
for (i = 0; i <= tracking; i++) { for (i = 0; i <= tracking; i++) {
for ( k = 0; k < WINDOWSIZE; k++) { for (k = 0; k < WINDOWSIZE; k++) {
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]); fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
} }
} }
@ -116,8 +116,8 @@ int main(int argc, char **argv) {
localMean(); localMean();
//directPredecessor(); //directPredecessor();
//differentialPredecessor(); //differentialPredecessor();
// save test_array after math magic happened // save test_array after math magic happened
// memset( fileName, '\0', sizeof(fileName) ); // memset( fileName, '\0', sizeof(fileName) );
mkFileName(fileName, sizeof(fileName), USED_WEIGHTS); mkFileName(fileName, sizeof(fileName), USED_WEIGHTS);
FILE *fp1 = fopen(fileName, "w"); FILE *fp1 = fopen(fileName, "w");
for (i = 0; i < tracking; i++) { for (i = 0; i < tracking; i++) {
@ -135,13 +135,13 @@ int main(int argc, char **argv) {
/* /*
====================================================================================================== ======================================================================================================
localMean localMean
Variant (1/3), substract local mean. Variant (1/3), substract local mean.
====================================================================================================== ======================================================================================================
*/ */
void localMean(void) { void localMean(void) {
@ -153,49 +153,49 @@ void localMean(void) {
FILE* fp4 = fopen(fileName, "w"); FILE* fp4 = fopen(fileName, "w");
fprintf(fp4, "\n=====================================LocalMean=====================================\n"); fprintf(fp4, "\n=====================================LocalMean=====================================\n");
double xMean = xSamples[0]; double xMean = xSamples[0];
double weightedSum = 0.0; double weightedSum = 0.0;
double filterOutput = 0.0; double filterOutput = 0.0;
double xSquared = 0.0; double xSquared = 0.0;
double xPredicted = 0.0; double xPredicted = 0.0;
double xActual = 0.0; double xActual = 0.0;
for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted
double xPartArray[xCount]; //includes all values at the size of runtime var //double xPartArray[1000]; //includes all values at the size of runtime var
//int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount; //int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount;
int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount;
//printf("xCount:%d, length:%d\n", xCount, _arrayLength); //printf("xCount:%d, length:%d\n", xCount, _arrayLength);
xMean = ( xCount > 0 ) ? windowXMean(_arrayLength, xCount) : 0; xMean = (xCount > 0) ? windowXMean(_arrayLength, xCount) : 0;
// printf("WINDOWSIZE:%f\n", windowXMean(_arrayLength, xCount)); // printf("WINDOWSIZE:%f\n", windowXMean(_arrayLength, xCount));
xPredicted = 0.0; xPredicted = 0.0;
xActual = xSamples[xCount + 1]; xActual = xSamples[xCount + 1];
// weightedSum += _x[ xCount-1 ] * w[xCount][0]; // weightedSum += _x[ xCount-1 ] * w[xCount][0];
for (i = 1; i < _arrayLength ; i++) { //get predicted value for (i = 1; i < _arrayLength; i++) { //get predicted value
xPredicted += (w[i][xCount] * ( xSamples[xCount - i] - xMean)); xPredicted += (w[i][xCount] * (xSamples[xCount - i] - xMean));
} }
xPredicted += xMean; xPredicted += xMean;
xError[xCount] = xActual - xPredicted; xError[xCount] = xActual - xPredicted;
printf("Pred: %f\t\tActual:%f\n", xPredicted,xActual); printf("Pred: %f\t\tActual:%f\n", xPredicted, xActual);
points[xCount].xVal[1] = xCount; points[xCount].xVal[1] = xCount;
points[xCount].yVal[1] = xPredicted; points[xCount].yVal[1] = xPredicted;
points[xCount].xVal[4] = xCount; points[xCount].xVal[4] = xCount;
points[xCount].yVal[4] = xError[xCount]; points[xCount].yVal[4] = xError[xCount];
xSquared = 0.0; xSquared = 0.0;
for (i = 1; i < _arrayLength; i++) { //get xSquared for (i = 1; i < _arrayLength; i++) { //get xSquared
//xSquared += pow(xSamples[xCount - i], 2); //xSquared += pow(xSamples[xCount - i], 2);
xSquared += pow(xSamples[xCount - i] - xMean, 2); xSquared += pow(xSamples[xCount - i] - xMean, 2);
printf("xSquared:%f\n", xSquared); printf("xSquared:%f\n", xSquared);
} }
if(xSquared == 0.0){ // returns Pred: -1.#IND00 if (xSquared == 0.0) { // returns Pred: -1.#IND00
xSquared = 1.0; xSquared = 1.0;
} }
//printf("%f\n", xSquared); //printf("%f\n", xSquared);
for (i = 1; i < _arrayLength; i++) { //update weights for (i = 1; i < _arrayLength; i++) { //update weights
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ( (xSamples[xCount - i] - xMean) / xSquared); w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xMean) / xSquared);
} }
fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
@ -225,14 +225,14 @@ void localMean(void) {
} }
/* /*
====================================================================================================== ======================================================================================================
directPredecessor directPredecessor
Variant (2/3), Variant (2/3),
substract direct predecessor substract direct predecessor
====================================================================================================== ======================================================================================================
*/ */
void directPredecessor(void) { void directPredecessor(void) {
@ -240,19 +240,19 @@ void directPredecessor(void) {
double xError[2048]; double xError[2048];
int xCount = 0, i; int xCount = 0, i;
double xActual; double xActual;
int xPredicted = 0.0; int xPredicted = 0.0;
// File handling // File handling
mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);
FILE *fp3 = fopen(fileName, "w"); FILE *fp3 = fopen(fileName, "w");
fprintf(fp3, "\n=====================================DirectPredecessor=====================================\n"); fprintf(fp3, "\n=====================================DirectPredecessor=====================================\n");
for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) { for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) {
double xPartArray[xCount]; //includes all values at the size of runtime var //double xPartArray[xCount]; //includes all values at the size of runtime var
//int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount; //int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount;
int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount;
printf("xCount:%d, length:%d\n", xCount, _arrayLength); printf("xCount:%d, length:%d\n", xCount, _arrayLength);
double xMean = ( xCount > 0 ) ? windowXMean(_arrayLength, xCount) : 0; double xMean = (xCount > 0) ? windowXMean(_arrayLength, xCount) : 0;
printf("%f\n", windowXMean(_arrayLength, xCount)); printf("%f\n", windowXMean(_arrayLength, xCount));
xPredicted = 0.0; xPredicted = 0.0;
xActual = xSamples[xCount + 1]; xActual = xSamples[xCount + 1];
@ -279,7 +279,7 @@ void directPredecessor(void) {
} }
int xErrorLength = sizeof(xError) / sizeof(xError[0]); int xErrorLength = sizeof(xError) / sizeof(xError[0]);
printf("vor:%d", xErrorLength); printf("vor:%d", xErrorLength);
popNAN(xError, xErrorLength); popNAN(xError, xErrorLength);
printf("nach:%d", xErrorLength); printf("nach:%d", xErrorLength);
xErrorLength = sizeof(xError) / sizeof(xError[0]); xErrorLength = sizeof(xError) / sizeof(xError[0]);
@ -298,16 +298,16 @@ void directPredecessor(void) {
/* /*
====================================================================================================== ======================================================================================================
differentialPredecessor differentialPredecessor
variant (3/3), variant (3/3),
differenital predecessor. differenital predecessor.
====================================================================================================== ======================================================================================================
*/ */
void differentialPredecessor ( void ) { void differentialPredecessor(void) {
char fileName[512]; char fileName[512];
double xError[2048]; double xError[2048];
@ -361,15 +361,15 @@ void differentialPredecessor ( void ) {
/* /*
====================================================================================================== ======================================================================================================
mkFileName mkFileName
Writes the current date plus the suffix with index suffixId Writes the current date plus the suffix with index suffixId
into the given buffer. If the total length is longer than max_len, into the given buffer. If the total length is longer than max_len,
only max_len characters will be written. only max_len characters will be written.
====================================================================================================== ======================================================================================================
*/ */
@ -387,13 +387,13 @@ char *mkFileName(char* buffer, size_t max_len, int suffixId) {
/* /*
====================================================================================================== ======================================================================================================
fileSuffix fileSuffix
Contains and returns every suffix for all existing filenames Contains and returns every suffix for all existing filenames
====================================================================================================== ======================================================================================================
*/ */
char * fileSuffix(int id) { char * fileSuffix(int id) {
@ -403,13 +403,13 @@ char * fileSuffix(int id) {
/* /*
====================================================================================================== ======================================================================================================
myLogger myLogger
Logs x,y points to svg graph Logs x,y points to svg graph
====================================================================================================== ======================================================================================================
*/ */
void bufferLogger(char *buffer, point_t points[]) { void bufferLogger(char *buffer, point_t points[]) {
int i; int i;
@ -430,71 +430,72 @@ void bufferLogger(char *buffer, point_t points[]) {
strcat(buffer, _buffer); strcat(buffer, _buffer);
} }
strcat(buffer, "\" fill=\"none\" id=\"svg_3\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n"); strcat(buffer, "\" fill=\"none\" id=\"svg_3\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++ ){ //xPredicted from diff Pred for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { //xPredicted from diff Pred
sprintf(_buffer, "L %f %f\n", points[i].xVal[3], points[i].xVal[3]); sprintf(_buffer, "L %f %f\n", points[i].xVal[3], points[i].xVal[3]);
strcat(buffer, _buffer); strcat(buffer, _buffer);
} }
strcat(buffer, "\" fill=\"none\" id=\"svg_4\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n"); strcat(buffer, "\" fill=\"none\" id=\"svg_4\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n");
} }
/* /*
====================================================================================================== ======================================================================================================
sum_array sum_array
Sum of all elements in x within a defined length Sum of all elements in x within a defined length
====================================================================================================== ======================================================================================================
*/ */
double sum_array(double x[], int xlength) { double sum_array(double x[], int xlength) {
int i = 0; int i = 0;
double sum = 0.0; double sum = 0.0;
if (xlength !=0 ){ if (xlength != 0) {
for (i = 0; i < xlength; i++) { for (i = 0; i < xlength; i++) {
sum += x[i]; sum += x[i];
} }
} }
return sum; return sum;
} }
/* /*
====================================================================================================== ======================================================================================================
popNanLength popNanLength
returns length of new array without NAN values returns length of new array without NAN values
====================================================================================================== ======================================================================================================
*/ */
double *popNAN( double *xError,int xErrorLength ) { double *popNAN(double *xError, int xErrorLength) {
int i, counter; int i, counter;
double noNAN [xErrorLength]; double noNAN[10];
realloc(noNAN, xErrorLength);
for ( i = 0; i < xErrorLength; i++) { for (i = 0; i < xErrorLength; i++) {
if ( !isnan(xError[i]) ) { if (!isnan(xError[i])) {
noNAN[i] = xError[i]; noNAN[i] = xError[i];
counter++; counter++;
} }
} }
realloc(noNAN, counter * sizeof(double)); realloc(noNAN, counter * sizeof(double));
int noNANLength = sizeof(noNAN)/ sizeof(noNAN[0]); int noNANLength = sizeof(noNAN) / sizeof(noNAN[0]);
memcpy(xError, noNAN, noNANLength); memcpy(xError, noNAN, noNANLength);
return xError; return xError;
} }
/* /*
====================================================================================================== ======================================================================================================
r2 r2
returns a random double value between 0 and 1 returns a random double value between 0 and 1
====================================================================================================== ======================================================================================================
*/ */
double r2(void) { double r2(void) {
@ -503,13 +504,13 @@ double r2(void) {
/* /*
====================================================================================================== ======================================================================================================
rndm rndm
fills a double variable with random value and returns it fills a double variable with random value and returns it
====================================================================================================== ======================================================================================================
*/ */
double rndm(void) { double rndm(void) {
@ -519,13 +520,13 @@ double rndm(void) {
/* /*
====================================================================================================== ======================================================================================================
mkSvgGraph mkSvgGraph
parses template.svg and writes results in said template parses template.svg and writes results in said template
====================================================================================================== ======================================================================================================
*/ */
void mkSvgGraph(point_t points[]) { void mkSvgGraph(point_t points[]) {
@ -541,10 +542,10 @@ void mkSvgGraph(point_t points[]) {
char buffer[131072] = ""; char buffer[131072] = "";
memset(buffer, '\0', sizeof(buffer)); memset(buffer, '\0', sizeof(buffer));
while(!feof(input)) { while (!feof(input)) {
fgets(line, 512, input); fgets(line, 512, input);
strncat(buffer, line,strlen(line)); strncat(buffer, line, strlen(line));
// printf("%s\n", line); // printf("%s\n", line);
if (strstr(line, firstGraph) != NULL) { if (strstr(line, firstGraph) != NULL) {
bufferLogger(buffer, points); bufferLogger(buffer, points);
} }
@ -556,14 +557,14 @@ void mkSvgGraph(point_t points[]) {
/* /*
====================================================================================================== ======================================================================================================
rdPPM rdPPM
reads data from file of type PPM, stores colorchannels in a struct in the reads data from file of type PPM, stores colorchannels in a struct in the
size of given picture size of given picture
====================================================================================================== ======================================================================================================
*/ */
static imagePixel_t *rdPPM(char *fileName) { static imagePixel_t *rdPPM(char *fileName) {
@ -620,14 +621,14 @@ static imagePixel_t *rdPPM(char *fileName) {
/* /*
====================================================================================================== ======================================================================================================
mkPpmFile mkPpmFile
gets output from the result of rdPpmFile and writes a new PPM file. Best Case is a gets output from the result of rdPpmFile and writes a new PPM file. Best Case is a
carbon copy of the source image. Build for debugging carbon copy of the source image. Build for debugging
====================================================================================================== ======================================================================================================
*/ */
void mkPpmFile(char *fileName, imagePixel_t *image) { void mkPpmFile(char *fileName, imagePixel_t *image) {
@ -645,22 +646,22 @@ void mkPpmFile(char *fileName, imagePixel_t *image) {
/* /*
====================================================================================================== ======================================================================================================
ppmColorChannel ppmColorChannel
gets one of the rgb color channels and writes them to a file gets one of the rgb color channels and writes them to a file
====================================================================================================== ======================================================================================================
*/ */
int ppmColorChannel(FILE* fp, imagePixel_t *image) { int ppmColorChannel(FILE* fp, imagePixel_t *image) {
// int length = 1000; // (image->x * image->y) / 3; // int length = 1000; // (image->x * image->y) / 3;
int i = 0; int i = 0;
if (image) { if (image) {
for ( i = 0; i < NUMBER_OF_SAMPLES - 1; i++ ){ for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) {
fprintf(fp,"%d\n", image->data[i].green); fprintf(fp, "%d\n", image->data[i].green);
} }
} }
fclose(fp); fclose(fp);
@ -669,48 +670,48 @@ int ppmColorChannel(FILE* fp, imagePixel_t *image) {
/* /*
====================================================================================================== ======================================================================================================
colorSamples colorSamples
reads colorChannel values from file and stores them in xSamples as well as points datatype for reads colorChannel values from file and stores them in xSamples as well as points datatype for
creating the SVG graph creating the SVG graph
====================================================================================================== ======================================================================================================
*/ */
void colorSamples( FILE* fp ) { void colorSamples(FILE* fp) {
int i = 0; int i = 0;
int d, out; int d, out;
double f; double f;
char buffer[NUMBER_OF_SAMPLES]; char buffer[NUMBER_OF_SAMPLES];
while ( !feof(fp) ) { while (!feof(fp)) {
if ( fgets(buffer, NUMBER_OF_SAMPLES, fp) != NULL ) { if (fgets(buffer, NUMBER_OF_SAMPLES, fp) != NULL) {
sscanf(buffer,"%lf", &xSamples[i]); sscanf(buffer, "%lf", &xSamples[i]);
//printf("%lf\n", xSamples[i] ); //printf("%lf\n", xSamples[i] );
points[i].yVal[0] = xSamples[i]; points[i].yVal[0] = xSamples[i];
points[i].xVal[0] = i; points[i].xVal[0] = i;
++i; ++i;
} }
} }
fclose(fp); fclose(fp);
} }
double windowXMean (int _arraylength, int xCount) { double windowXMean(int _arraylength, int xCount) {
int count; int count;
double sum = 0.0; double sum = 0.0;
double *ptr; double *ptr;
// printf("*window\t\t*base\t\txMean\n\n"); // printf("*window\t\t*base\t\txMean\n\n");
for ( ptr = &xSamples[xCount - _arraylength]; ptr != &xSamples[xCount]; ptr++) { //set ptr to beginning of window for (ptr = &xSamples[xCount - _arraylength]; ptr != &xSamples[xCount]; ptr++) { //set ptr to beginning of window
//window = xCount - _arraylength //window = xCount - _arraylength
//base = window - _arraylength; //base = window - _arraylength;
//sum = 0.0; //sum = 0.0;
//for( count = 0; count < _arraylength; count++){ //for( count = 0; count < _arraylength; count++){
sum += *ptr; sum += *ptr;
// printf("%f\n", *base); // printf("%f\n", *base);
//} //}
} }
//printf("\n%lf\t%lf\t%lf\n", *ptr, *ptr2, (sum/(double)WINDOW)); //printf("\n%lf\t%lf\t%lf\n", *ptr, *ptr2, (sum/(double)WINDOW));
return sum/(double)_arraylength; return sum / (double)_arraylength;
} }