From d71470afc1b20a8011880814cd563f0deb732e19 Mon Sep 17 00:00:00 2001 From: Friese Date: Thu, 3 May 2018 18:30:42 +0200 Subject: [PATCH] runnable version, xSquared kicks everything out of bounds in directPredecessor --- bin/NLMSvariants.c | 244 ++++++++++++++++++++++++++------------------- 1 file changed, 142 insertions(+), 102 deletions(-) diff --git a/bin/NLMSvariants.c b/bin/NLMSvariants.c index af1b6d9..292902c 100644 --- a/bin/NLMSvariants.c +++ b/bin/NLMSvariants.c @@ -11,101 +11,112 @@ #include #include #include +#include // DBL_MAX #define M 1000 #define tracking 40 //Count of weights #define learnrate 1.0 -#define WGHTS 1 -#define WGHTSFTR 2 -#define RES 3 -#define DRCTPRD 4 +#define PURE_WEIGHTS 0 +#define USED_WEIGHTS 1 +#define RESULTS 2 +#define DIRECT_PREDECESSOR 3 -double x[] ={0}; +double x[] = {0}; double _x[M] = {0}; double w [M][M]={{0},{0}}; -const char *fileName(int id); -double r2(void); -double rnd(void); -double sum_array(double x[], int length); -void direkterVorgaenger(void); -void lokalerMittelWert(void); +/* *file handling* */ +char * mkFileName( char* buffer, size_t max_len, int suffixId ); +char *fileSuffix( int id ); +void myLogger( FILE* fp, int myVar); + +/* *rand seed functions* */ +double r2( void ); +double rndm( void ); + +/* *math functions * */ +double sum_array( double x[], int length ); +void directPredecessor( void ); +void localMean( void ); int main(int argc, char **argv ) { - - //init test_array, fill in weights by random + char fileName[50]; int i; - // srand(NULL); + + srand( (unsigned int) time(NULL) ); + for (i = 0; i < M; i++) { - _x[i] += ((255.0 / M) * i); - for (int k = 1; k < M; k++) - { - w[k][i] = rnd(); + _x[i] += ((255.0 / M) * i); // Init test values + for (int k = 1; k < M; k++){ + w[k][i] = rndm(); // Init weights } } - // save plain test_array before math magic happened - FILE *fp0 = fopen(fileName(WGHTS),"wb+"); + 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 = 1; k < tracking; k++ ){ - fprintf(fp0, "[%f][%f] %.2f\n", k, i, w[k][i]); + fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]); } } fclose(fp0); // math magic - direkterVorgaenger(); + //directPredecessor(); // TODO: needs some love! + localMean(); // save test_array after math magic happened - FILE *fp1 = fopen(fileName(WGHTSFTR),"wb+"); + // 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 = 1; k < tracking; k++) { - fprintf(fp1, "[%f][%f] %.2f\n", k,i, w[k][i]); + fprintf(fp1, "[%d][%d] %lf\n", k,i, w[k][i]); } } fclose(fp1); - getchar(); + // getchar(); + printf("DONE!"); } -/* +/* + ======================================================================================= - =================================== - - lokalerMittelwert() + localMean - Variant (1/3), - substract local mean + Variant (1/3), substract local mean. - =================================== + ======================================================================================= */ -void lokalerMittelWert() { - +void localMean( void ) { + 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; - for (xCount = 1; xCount < M; xCount++){ // x_cout can not be zero + for (xCount = 1; xCount < M; xCount++){ //double xPartArray[xCount]; //includes all values at the size of runtime var - double xMean = (xCount > 0) ? ( sum_array(_x, xCount) / xCount) : 0; + double xMean = ( xCount > 0 ) ? ( sum_array(_x, xCount) / xCount ) : 0;// xCount can not be zero 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)) ; + xPredicted += ( w[i][xCount] * ( _x[xCount - i] - xMean )) ; } xPredicted += xMean; @@ -127,14 +138,15 @@ void lokalerMittelWert() { double deviation = 0.0; // Mean square - for( i = 0; i < M-1; i++ ){ + for( i = 0; i < M - 1; i++ ){ deviation += pow( xError[i], 2 ); } deviation /= xErrorLength; // write in file - FILE *fp2 = fopen(fileName(RES), "wb+"); + 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); @@ -142,10 +154,9 @@ void lokalerMittelWert() { /* - =================================== - direkterVorgaenger() + directPredecessor Variant (2/3), @@ -154,36 +165,36 @@ void lokalerMittelWert() { =================================== */ -void direkterVorgaenger() { - +void directPredecessor( void ) { + char fileName[50]; double xError [M]; int xCount = 0, i; - + double xActual; + // File handling - FILE *fp3 = fopen(fileName(DRCTPRD), "wb+"); + mkFileName( fileName, sizeof(fileName), DIRECT_PREDECESSOR); + FILE *fp3 = fopen(fileName, "w"); - for ( xCount = 1; xCount < M; xCount++ ){ + for ( xCount = 1; xCount < M+1; xCount++ ){ + xActual = _x[xCount+1]; double xPredicted = 0.0; - double xActual = _x[xCount+1]; - for ( i = 1; i < xCount; i++ ){ - xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1])); - } + xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1] )); + } xPredicted += _x[xCount-1]; xError[xCount] = xActual - xPredicted; fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); - + double xSquared = 0.0; //get x squared - double xSquared = 0; for ( i = 1; i < xCount; i++ ){ - xSquared += pow( _x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor + xSquared += pow( _x[xCount - i] - _x[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 - 1] ) / xSquared ); + for ( i = 1; i < xCount; i++){ + w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i - 1] ) / xSquared ); //TODO: double val out of bounds } } int xErrorLength = sizeof(xError) / sizeof(xError[0]); @@ -200,51 +211,84 @@ void direkterVorgaenger() { } -/* - - =================================== - - fileName() - - - generates filename with date for - logging purposes - =================================== + +/* + ========================================================================= + + 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, + only max_len characters will be written. + + ========================================================================= */ -const char *fileName(int id){ - - static char* suffix[] = {"_weights.txt","_weights_after.txt", "_results.txt", "direct_predecessor.txt"}; - char *date; - time_t now; - now = time(NULL); - strftime(date, 20, "%Y-%m-%d_%H_%M_%S", localtime(&now)); - strcat(suffix[id], date); - return suffix[id]; +char *mkFileName( char* buffer, size_t max_len, int suffixId) { + const char * format_str = "%Y-%m-%d_%H_%M_%S"; + size_t date_len; + const char * suffix = fileSuffix( suffixId ); + time_t now = time(NULL); + + strftime( buffer, max_len, format_str, localtime(&now) ); + date_len = strlen(buffer); + strncat( buffer, suffix, max_len - date_len ); } + /* - - =================================== + ========================================================================= + + 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"}; + return suffix[id]; +} + + + +/* + ========================================================================== + + myLogger + + + Pipes to stdout or files by using a filepointer for logging purposes + + ========================================================================== + */ + +void myLogger ( FILE* fp, int myVar ){ + fprintf( fp, "Logging: %d\n", myVar); +} + + + +/* + ========================================================================= 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 length){ - //int length = 0; +double sum_array( double x[], int length ){ int i = 0; double sum = 0.0; - //length = sizeof(x)/sizeof(x[0]); - for (i=0; i< length; i++){ + + for( i=0; i< length; i++ ) { sum += x[i]; } return sum; @@ -252,36 +296,32 @@ double sum_array(double x[], int length){ /* + ========================================================================== - =================================== + r2 - r2() - - returns a double value between - 0 and 1 - - =================================== + returns a random double value between 0 and 1 + ========================================================================== */ -double r2() -{ + +double r2( void ) { return((rand() % 10000) / 10000.0); } + + /* + ========================================================================== - =================================== + rndm - int rnd() - - fills a double variable with - random value and returns it - - =================================== + fills a double variable with random value and returns it + ========================================================================= */ -double rnd() -{ + +double rndm( void ) { double rndmval= r2(); return rndmval; }