From 75a5fbf0cc8bd1f2c99953947f5d2a5e7cc7daad Mon Sep 17 00:00:00 2001 From: kbecke23 Date: Wed, 16 May 2018 12:29:58 +0200 Subject: [PATCH] update --- src/cpp_implementation/NLMSvariants.cpp | 747 ++++++++++++++---------- 1 file changed, 431 insertions(+), 316 deletions(-) diff --git a/src/cpp_implementation/NLMSvariants.cpp b/src/cpp_implementation/NLMSvariants.cpp index 0849c02..11f2c0d 100644 --- a/src/cpp_implementation/NLMSvariants.cpp +++ b/src/cpp_implementation/NLMSvariants.cpp @@ -1,45 +1,37 @@ // -// +// // NLMSvariants.c // // Created by FBRDNLMS on 26.04.18. -// Copyright © 2018 FBRDNLMS. All rights reserved. // - +// #include #include #include #include #include #include // DBL_MAX -#include // std::codecvt_utf8_utf16 -#include // std::wstring_convert -#include // std::wstring -#define M 1000 -#define tracking 40 //Count of weights -#define learnrate 1.0 -#define PURE_WEIGHTS 0 -#define USED_WEIGHTS 1 -#define RESULTS 3 -#define DIRECT_PREDECESSOR 2 -#define LOCAL_MEAN 4 +#define NUMBER_OF_SAMPLES 500 +#define WINDOWSIZE 5 +#define learnrate 0.8 #define RGB_COLOR 255 #if defined(_MSC_VER) #include typedef SSIZE_T ssize_t; #endif -double x[] = { 0 }; -double _x[M] = { 0 }; -double w[M][M] = { { 0 },{ 0 } }; -/* UTF-8 to UTF-16 win Format*/ -auto wstring_from_utf8(char const* const utf8_string) --> std::wstring -{ - std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > converter; - return converter.from_bytes(utf8_string); -} +enum fileSuffix_t { // used in conjunction with mkFileName() + PURE_WEIGHTS, + USED_WEIGHTS, + DIRECT_PREDECESSOR, + RESULTS, + LOCAL_MEAN, + TEST_VALUES, + DIFFERENTIAL_PREDECESSOR +}; + +double xSamples[NUMBER_OF_SAMPLES] = { 0.0 }; /* *svg graph building* */ typedef struct { @@ -47,9 +39,9 @@ typedef struct { double yVal[7]; }point_t; -point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean +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 reader/writer* */ + /* *ppm read, copy, write* */ typedef struct { unsigned char red, green, blue; }colorChannel_t; @@ -59,223 +51,363 @@ typedef struct { colorChannel_t *data; }imagePixel_t; -static imagePixel_t * readPPM(char *fileName); -void mkPpmFile(char *fileNamem, imagePixel_t *image); -int * ppmColorChannel(imagePixel_t *image); +static imagePixel_t * rdPPM(char *fileName); // read PPM file format +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 +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 *fileSuffix(int id); void myLogger(FILE* fp, point_t points[]); -#ifdef _WIN32 -size_t getline(char **lineptr, size_t *n, FILE *stream); -#endif void mkSvgGraph(point_t points[]); - +//void weightsLogger(double *weights, int var); /* *rand seed* */ double r2(void); double rndm(void); /* *math* */ double sum_array(double x[], int length); -void directPredecessor(void); -void localMean(void); +void directPredecessor(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]); +void localMean(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]); +void differentialPredecessor(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]); +//void differentialPredecessor(double *weights); +double *popNAN(double *xError); //return new array without NAN values +double windowXMean(int _arraylength, int xCount); - -int main(int argc, char **argv) { +//int main(int argc, char **argv) { +int main(void) { + double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; // = { { 0.0 }, {0.0} }; + // double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; char fileName[50]; - int i; + int i, k, xLength; + imagePixel_t *image; + + image = rdPPM("cow.ppm"); + mkFileName(fileName, sizeof(fileName), TEST_VALUES); + FILE* fp5 = fopen(fileName, "w"); + xLength = ppmColorChannel(fp5, image); + printf("%d\n", xLength); + FILE* fp6 = fopen(fileName, "r"); + colorSamples(fp6); srand((unsigned int)time(NULL)); - - for (i = 0; i < M; i++) { - _x[i] += ((255.0 / M) * i); // Init test values - for (int k = 0; k < M; k++) { - w[k][i] = rndm(); // Init weights + for (i = 0; i < NUMBER_OF_SAMPLES; i++) { + for (int k = 0; k < WINDOWSIZE; k++) { + weights[k][i] = rndm(); // Init weights } } - mkFileName(fileName, sizeof(fileName), PURE_WEIGHTS); // save plain test_array before math magic happens FILE *fp0 = fopen(fileName, "w"); - for (i = 0; i <= tracking; i++) { - for (int k = 0; k < tracking; k++) { - fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]); + for (i = 0; i < NUMBER_OF_SAMPLES; i++) { + for (k = 0; k < WINDOWSIZE; k++) { + fprintf(fp0, "[%d][%d]%lf\n", k, i, weights[k][i]); } } fclose(fp0); - - // math magic - localMean(); - directPredecessor(); // TODO: used_weights.txt has gone missing! - - // save test_array after math magic happened - // memset( fileName, '\0', sizeof(fileName) ); - mkFileName(fileName, sizeof(fileName), USED_WEIGHTS); + localMean(weights); + directPredecessor(weights); + differentialPredecessor(weights); + mkSvgGraph(points); + // save test_array after math magic happened + // memset( fileName, '\0', sizeof(fileName) ); + /* mkFileName(fileName, sizeof(fileName), USED_WEIGHTS); FILE *fp1 = fopen(fileName, "w"); for (i = 0; i < tracking; i++) { - for (int k = 0; k < tracking; k++) { - fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]); - } + for (int k = 0; k < WINDOWSIZE; k++) { + fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]); + } } fclose(fp1); - - // getchar(); - printf("DONE!"); - + */ + printf("\nDONE!\n"); } /* -======================================================================================= +====================================================================================================== localMean - Variant (1/3), substract local mean. -======================================================================================= +====================================================================================================== */ -void localMean(void) { +void localMean(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { + //double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; + double(*local_weights)[WINDOWSIZE] = (double(*)[WINDOWSIZE])malloc(sizeof(double) * (WINDOWSIZE + 1) * (NUMBER_OF_SAMPLES + 1)); + // double *local_weights[WINDOWSIZE]; + memcpy(local_weights, weights, sizeof(double) * WINDOWSIZE * NUMBER_OF_SAMPLES); char fileName[50]; - double xError[M]; // includes e(n) - memset(xError, 0, M);// initialize xError-array with Zero - int xCount = 0; // runtime var - int i; + double xError[2048]; // includes e(n) + memset(xError, 0.0, NUMBER_OF_SAMPLES);// initialize xError-array with Zero + int xCount = 0, i; // runtime var; mkFileName(fileName, sizeof(fileName), LOCAL_MEAN); FILE* fp4 = fopen(fileName, "w"); - fprintf(fp4, "\n\n\n\n*********************LocalMean*********************\n"); + fprintf(fp4, "\n=====================================LocalMean=====================================\nNo.\txPredicted\txActual\t\txError\n"); - for (xCount = 1; xCount < M; xCount++) { + double xMean = xSamples[0]; + double xSquared = 0.0; + double xPredicted = 0.0; + double xActual = 0.0; - //double xPartArray[xCount]; //includes all values at the size of runtime var + for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted - double xMean = (xCount > 0) ? (sum_array(_x, xCount) / xCount) : 0;// xCount can not be zero + int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; + xMean = (xCount > 0) ? windowXMean(_arrayLength, xCount) : 0; + xPredicted = 0.0; + xActual = xSamples[xCount + 1]; + // weightedSum += _x[ xCount-1 ] * w[xCount][0]; - double xPredicted = 0.0; - double xActual = _x[xCount + 1]; - - for (i = 1; i < xCount; i++) { //get predicted value - xPredicted += (w[i][xCount] * (_x[xCount - i] - xMean)); + for (i = 1; i < _arrayLength; i++) { //get predicted value + xPredicted += (local_weights[i][xCount] * (xSamples[xCount - i] - xMean)); } - xPredicted += xMean; xError[xCount] = xActual - xPredicted; - points[xCount].xVal[2] = xCount; - points[xCount].yVal[2] = xPredicted; - double xSquared = 0.0; + points[xCount].xVal[1] = xCount; + points[xCount].yVal[1] = xPredicted; + points[xCount].xVal[4] = xCount; + points[xCount].yVal[4] = xError[xCount]; - for (i = 1; i < xCount; i++) { //get x squared - xSquared = +pow(_x[xCount - i], 2); + xSquared = 0.0; + for (i = 1; i < _arrayLength; i++) { //get xSquared + xSquared += pow(xSamples[xCount - i] - xMean, 2); } - - for (i - 1; i < xCount; i++) { //update weights - w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared); + if (xSquared == 0.0) { // Otherwise returns Pred: -1.#IND00 in some occassions + xSquared = 1.0; } - - fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); + for (i = 1; i < _arrayLength; i++) { //update weights + local_weights[i][xCount + 1] = local_weights[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xMean) / xSquared); + } + fprintf(fp4, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); } - int xErrorLength = sizeof(xError) / sizeof(xError[0]); - double mean = sum_array(xError, xErrorLength) / M; + double *xErrorPtr = popNAN(xError); // delete NAN values from xError[] + double xErrorLength = *xErrorPtr; // Watch popNAN()! + xErrorPtr[0] = 0.0; + printf("Xerrorl:%lf", xErrorLength); + double mean = sum_array(xErrorPtr, xErrorLength) / xErrorLength; double deviation = 0.0; // Mean square - for (i = 0; i < M - 1; i++) { - deviation += pow(xError[i], 2); + for (i = 1; i < xErrorLength; i++) { + deviation += pow(xError[i] - mean, 2); } deviation /= xErrorLength; - + printf("mean:%lf, devitation:%lf", mean, deviation); // write in file - mkFileName(fileName, sizeof(fileName), RESULTS); - FILE *fp2 = fopen(fileName, "w"); - fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean); - fclose(fp2); + //mkFileName(fileName, sizeof(fileName), RESULTS); + //FILE *fp2 = fopen(fileName, "w"); + fprintf(fp4, "\nQuadratische Varianz(x_error): %f\nMittelwert:(x_error): %f\n\n", deviation, mean); + //fclose(fp2); + free(local_weights); fclose(fp4); + + //weightsLogger( local_weights, USED_WEIGHTS ); } - /* -=================================== +====================================================================================================== directPredecessor - Variant (2/3), substract direct predecessor -=================================== +====================================================================================================== */ -void directPredecessor(void) { +void directPredecessor(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { + double(*local_weights)[WINDOWSIZE] = (double(*)[WINDOWSIZE])malloc(sizeof(double) * (WINDOWSIZE + 1) * (NUMBER_OF_SAMPLES + 1)); + + // double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; + memcpy(local_weights, weights, sizeof(double) * WINDOWSIZE * NUMBER_OF_SAMPLES); char fileName[512]; double xError[2048]; int xCount = 0, i; - double xActual; - + double xActual = 0.0; + double xPredicted = 0.0; // File handling mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); FILE *fp3 = fopen(fileName, "w"); - fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\n"); + fprintf(fp3, "\n=====================================DirectPredecessor=====================================\nNo.\txPredicted\txAcutal\t\txError\n"); - for (xCount = 1; xCount < M + 1; xCount++) { - xActual = _x[xCount + 1]; - double xPredicted = 0.0; - for (i = 1; i < xCount; i++) { - xPredicted += (w[i][xCount] * (_x[xCount - i] - _x[xCount - i - 1])); + for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted + //double xPartArray[1000]; //includes all values at the size of runtime var + //int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount; + int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; + //printf("xCount:%d, length:%d\n", xCount, _arrayLength); + // printf("WINDOWSIZE:%f\n", windowXMean(_arrayLength, xCount)); + xPredicted = 0.0; + xActual = xSamples[xCount + 1]; + // weightedSum += _x[ xCount-1 ] * w[xCount][0]; + + for (i = 1; i < _arrayLength; i++) { + xPredicted += (local_weights[i][xCount] * (xSamples[xCount - 1] - xSamples[xCount - i - 1])); } - xPredicted += _x[xCount - 1]; + xPredicted += xSamples[xCount - 1]; xError[xCount] = xActual - xPredicted; - fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); - points[xCount].xVal[0] = xCount; - points[xCount].yVal[0] = xActual; - points[xCount].xVal[1] = xCount; - points[xCount].yVal[1] = xPredicted; + //fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); + points[xCount].xVal[2] = xCount; + points[xCount].yVal[2] = xPredicted; + points[xCount].xVal[5] = xCount; + points[xCount].yVal[5] = xError[xCount]; double xSquared = 0.0; - for (i = 1; i < xCount; i++) { - xSquared += pow(_x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor + for (i = 1; i < _arrayLength; i++) { + xSquared += pow(xSamples[xCount - 1] - xSamples[xCount - i - 1], 2); // substract direct predecessor } - for (i = 1; i < xCount; i++) { - w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((_x[xCount - i] - _x[xCount - i - 1]) / xSquared); //TODO: double val out of bounds + for (i = 1; i < _arrayLength; i++) { + local_weights[i][xCount + 1] = local_weights[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared); } + fprintf(fp3, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); } - int xErrorLength = sizeof(xError) / sizeof(xError[0]); + double *xErrorPtr = popNAN(xError); // delete NAN values from xError[] + //printf("%lf", xErrorPtr[499]); + double xErrorLength = *xErrorPtr; // Watch popNAN()! + xErrorPtr[0] = 0.0; + printf("Xerrorl:%lf", xErrorLength); + + double mean = sum_array(xErrorPtr, xErrorLength) / xErrorLength; + double deviation = 0.0; + + // Mean square + for (i = 1; i < xErrorLength; i++) { + deviation += pow(xError[i] - mean, 2); + } + deviation /= xErrorLength; + printf("mean:%lf, devitation:%lf", mean, deviation); + + // write in file + //mkFileName(fileName, sizeof(fileName), RESULTS); + //FILE *fp2 = fopen(fileName, "wa"); + fprintf(fp3, "\nQuadratische Varianz(x_error): %f\nMittelwert:(x_error): %f\n\n", deviation, mean); + fclose(fp3); + //fclose(fp2); + free(local_weights); + + //weightsLogger( local_weights, USED_WEIGHTS ); +} + + +/* +====================================================================================================== + +differentialPredecessor + +variant (3/3), +differential predecessor. + +====================================================================================================== +*/ +void differentialPredecessor(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { + // double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; + double(*local_weights)[WINDOWSIZE] = (double(*)[WINDOWSIZE])malloc(sizeof(double) * (WINDOWSIZE + 1) * (NUMBER_OF_SAMPLES + 1)); + + memcpy(local_weights, weights, sizeof(double) * WINDOWSIZE * NUMBER_OF_SAMPLES); + char fileName[512]; + double xError[2048]; + int xCount = 0, i; + double xPredicted = 0.0; + double xActual = 0.0; + + // File handling + mkFileName(fileName, sizeof(fileName), DIFFERENTIAL_PREDECESSOR); + FILE *fp6 = fopen(fileName, "w"); + fprintf(fp6, "\n=====================================DifferentialPredecessor=====================================\nNo.\txPredicted\txAcutal\t\txError\n"); + + for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted + + int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; + xPredicted = 0.0; + xActual = xSamples[xCount + 1]; + + for (i = 1; i < _arrayLength; i++) { + xPredicted += (local_weights[i][xCount] * (xSamples[xCount - i] - xSamples[xCount - i - 1])); + } + xPredicted += xSamples[xCount - 1]; + xError[xCount] = xActual - xPredicted; + + //fprintf(fp6, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); + points[xCount].xVal[3] = xCount; + points[xCount].yVal[3] = xPredicted; + points[xCount].xVal[6] = xCount; + points[xCount].yVal[6] = xError[xCount]; + double xSquared = 0.0; + + for (i = 1; i < _arrayLength; i++) { + xSquared += pow(xSamples[xCount - i] - xSamples[xCount - i - 1], 2); // substract direct predecessor + } + for (i = 1; i < _arrayLength; i++) { + local_weights[i][xCount + 1] = local_weights[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xSamples[xCount - i - 1]) / xSquared); + } + fprintf(fp6, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); + } + + /* int xErrorLength = sizeof(xError) / sizeof(xError[0]); + printf("vor:%d", xErrorLength); + popNAN(xError); + printf("nach:%d", xErrorLength); + xErrorLength = sizeof(xError) / sizeof(xError[0]); double mean = sum_array(xError, xErrorLength) / xErrorLength; double deviation = 0.0; for (i = 0; i < xErrorLength - 1; i++) { + deviation += pow(xError[i] - mean, 2); + } + deviation /= xErrorLength; + + //mkSvgGraph(points); + fprintf(fp6, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean); + */ + double *xErrorPtr = popNAN(xError); // delete NAN values from xError[] + //printf("%lf", xErrorPtr[499]); + double xErrorLength = *xErrorPtr; // Watch popNAN()! + xErrorPtr[0] = 0.0; + printf("Xerrorl:%lf", xErrorLength); + + double mean = sum_array(xErrorPtr, xErrorLength) / xErrorLength; + double deviation = 0.0; + + // Mean square + for (i = 1; i < xErrorLength; i++) { deviation += pow(xError[i] - mean, 2); } + deviation /= xErrorLength; + printf("mean:%lf, devitation:%lf", mean, deviation); + // write in file + //mkFileName(fileName, sizeof(fileName), RESULTS); + //FILE *fp2 = fopen(fileName, "wa"); + fprintf(fp6, "\nQuadratische Varianz(x_error): %f\nMittelwert:(x_error): %f\n\n", deviation, mean); + //fclose(fp2); + fclose(fp6); + free(local_weights); - mkSvgGraph(points); - fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean); - fclose(fp3); + //weightsLogger( local_weights, USED_WEIGHTS ); } - - /* -========================================================================= +====================================================================================================== mkFileName - Writes the current date plus the suffix with index suffixId -into the given buffer. If[M ?K 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. -========================================================================= +====================================================================================================== + */ char *mkFileName(char* buffer, size_t max_len, int suffixId) { @@ -291,117 +423,151 @@ char *mkFileName(char* buffer, size_t max_len, int suffixId) { } - /* -========================================================================= +====================================================================================================== fileSuffix Contains and returns every suffix for all existing filenames -========================================================================== +====================================================================================================== */ char * fileSuffix(int id) { - char * suffix[] = { "_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt" }; + char * suffix[] = { "_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt","_testvalues.txt", "_differential_predecessor.txt" }; return suffix[id]; } /* -========================================================================== - -svgGraph - - -========================================================================== -*/ -/* -void Graph ( ) { -char fileName[50]; -mkFileName(fileName, sizeof(fileName), GRAPH); -FILE* fp4 = fopen(fileName, "w"); -pfrintf -*/ - - -/* -========================================================================== +====================================================================================================== myLogger +Logs x,y points to svg graph -Logs on filepointer, used for svg graphing - -========================================================================== +====================================================================================================== */ -void myLogger(FILE* fp, point_t points[]) { - /*int i; - for (i = 0; i <= M; i++) { // xActual - fprintf(fp, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]); +void weightsLogger(double weights[WINDOWSIZE], int val) { + char fileName[512]; + int i; + mkFileName(fileName, sizeof(fileName), val); + FILE* fp = fopen(fileName, "wa"); + for (i = 0; i < WINDOWSIZE; i++) { + // for (int k = 0; k < WINDOWSIZE; k++) { + fprintf(fp, "[%d]%lf\n", i, weights[i]); + // } } - fprintf(fp, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n\n\n\n\n\n\n\n"); } /* -========================================================================= +====================================================================================================== sum_array - Sum of all elements in x within a defined length -========================================================================= +====================================================================================================== */ -double sum_array(double x[], int length) { +double sum_array(double x[], int xlength) { int i = 0; double sum = 0.0; - for (i = 0; i< length; i++) { - sum += x[i]; + if (xlength != 0) { + for (i = 0; i < xlength; i++) { + sum += x[i]; + } } return sum; } /* -========================================================================== +====================================================================================================== + +popNanLength + +returns length of new array without NAN values + +====================================================================================================== +*/ + +double *popNAN(double *xError) { + int i, counter = 1; + double tmpLength = 0.0; + double *tmp = NULL; + double *more_tmp = NULL; + + for (i = 0; i < NUMBER_OF_SAMPLES; i++) { + counter++; + more_tmp = (double *)realloc(tmp, counter*(sizeof(double))); + if (!isnan(xError[i])) { + tmp = more_tmp; + tmp[counter - 1] = xError[i]; + printf("xERROR:%lf\n", tmp[counter - 1]); + tmpLength++; + } + } + counter += 1; + more_tmp = (double *)realloc(tmp, counter * sizeof(double)); + tmp = more_tmp; + *tmp = tmpLength; // Length of array has to be stored in tmp[0], + // Cause length is needed later on in the math functions. + // xError counting has to begin with 1 in the other functions ! + printf("tmpLength in tmp:%lf, %lf\n", tmp[counter - 2], *tmp); + return tmp; + +} +/* +====================================================================================================== r2 returns a random double value between 0 and 1 -========================================================================== +====================================================================================================== */ double r2(void) { @@ -409,15 +575,14 @@ double r2(void) { } - /* -========================================================================== +====================================================================================================== rndm fills a double variable with random value and returns it -========================================================================== +====================================================================================================== */ double rndm(void) { @@ -426,149 +591,53 @@ double rndm(void) { } - /* -========================================================================== - -getline - -This code is public domain -- Will Hartung 4/9/09 //edited by Kevin Becker -Microsoft Windows is not POSIX conform and does not support getline. - -========================================================================= -*/ -#ifdef _WIN32 -size_t getline(char **lineptr, size_t *n, FILE *stream) { - char *bufptr = NULL; - char *p = bufptr; - size_t size; - int c; - - if (lineptr == NULL) { - return -1; - } - if (stream == NULL) { - return -1; - } - if (n == NULL) { - return -1; - } - bufptr = *lineptr; - size = *n; - - c = fgetc(stream); - if (c == EOF) { - return -1; - } - if (bufptr == NULL) { - char c[128]; - memset(c, 0, sizeof(c)); - bufptr = c; - if (bufptr == NULL) { - return -1; - } - size = 128; - } - p = bufptr; - while (c != EOF) { - if ((p - bufptr) > (size - 1)) { - size = size + 128; - realloc(bufptr, size); - if (bufptr == NULL) { - return -1; - } - } - *p++ = c; - if (c == '\n') { - break; - } - c = fgetc(stream); - } - - *p++ = '\0'; - *lineptr = bufptr; - *n = size; - - return p - bufptr - 1; -} -#endif - - -/* -========================================================================== +====================================================================================================== mkSvgGraph parses template.svg and writes results in said template -========================================================================== +====================================================================================================== */ void mkSvgGraph(point_t points[]) { - FILE *input = fopen("template.svg", "r"); - FILE *target = fopen("output.svg", "w"); + FILE *input = fopen("graphResults_template.html", "r"); + FILE *target = fopen("graphResults.html", "w"); char line[512]; - // char *ptr; - //size_t len = 0; - //ssize_t read; - //char values[64]; - char firstGraph[15] = { "x * image->y) / 3; +int ppmColorChannel(FILE* fp, imagePixel_t *image) { + // int length = (image->x * image->y) / 3; int i = 0; - int buffer[10]; - realloc(buffer,length); - printf("%d\n", length); if (image) { - for (i = 0; i < length; i++) { - buffer[i] = image->data[i].green; - //output[i] = image->data[i].blue; - //output[i] = image->data[i].red; + for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { + fprintf(fp, "%d\n", image->data[i].green); } } - return buffer; -} \ No newline at end of file + fclose(fp); + return NUMBER_OF_SAMPLES; +} + + +/* +====================================================================================================== + +colorSamples + +reads colorChannel values from file and stores them in xSamples as well as points datatype for +creating the SVG graph + +====================================================================================================== +*/ +void colorSamples(FILE* fp) { + int i = 0; + char buffer[NUMBER_OF_SAMPLES]; + + while (!feof(fp)) { + if (fgets(buffer, NUMBER_OF_SAMPLES, fp) != NULL) { + sscanf(buffer, "%lf", &xSamples[i]); + //printf("%lf\n", xSamples[i] ); + points[i].yVal[0] = xSamples[i]; + points[i].xVal[0] = i; + ++i; + } + } + fclose(fp); +} + + +/* +====================================================================================================== + +windowXMean + +returns mean value of given input, which has a length of WINDOWSIZE + +====================================================================================================== +*/ + +double windowXMean(int _arraylength, int xCount) { + double sum = 0.0; + double *ptr; + + for (ptr = &xSamples[xCount - _arraylength]; ptr != &xSamples[xCount]; ptr++) { //set ptr to beginning of window + sum += *ptr; + } + return sum / (double)_arraylength; +} + +