From f597b528f01499b7331a4b70b2f3eb91d9a189d5 Mon Sep 17 00:00:00 2001 From: gurkenhabicht Date: Wed, 16 May 2018 11:18:36 +0200 Subject: [PATCH] restructured --- .DS_Store | Bin 0 -> 6148 bytes bin/.DS_Store | Bin 0 -> 6148 bytes bin/ANSI_C_Implementation/Makefile | 6 - bin/ANSI_C_Implementation/NLMSvariants.c | 789 ------------------ bin/ANSI_C_Implementation/README.MD | 19 - .../graphResults_template.html | 62 -- bin/NLMS_with_UI.exe | Bin 17920 -> 0 bytes bin/NLMSvariants.c | 789 ------------------ bin/NLMSvariants.cpp | 674 --------------- bin/NLMSvariants.cs | 446 ---------- src/.DS_Store | Bin 8196 -> 8196 bytes 11 files changed, 2785 deletions(-) create mode 100644 .DS_Store create mode 100644 bin/.DS_Store delete mode 100644 bin/ANSI_C_Implementation/Makefile delete mode 100644 bin/ANSI_C_Implementation/NLMSvariants.c delete mode 100644 bin/ANSI_C_Implementation/README.MD delete mode 100644 bin/ANSI_C_Implementation/graphResults_template.html delete mode 100644 bin/NLMS_with_UI.exe delete mode 100644 bin/NLMSvariants.c delete mode 100644 bin/NLMSvariants.cpp delete mode 100644 bin/NLMSvariants.cs diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..d6def0598bf36913c9fe5eb038ed97d9fe7385a3 GIT binary patch literal 6148 zcmeHKO>Wab6n;|&u!9hhKx&uXAhD=Wq84R=klc_ixOTH0UcAam&?OA zmoX~TcJT;$|A!V>L=M(|M#Vzbm(^MEq&(rVzQ!kcR#x5aH?iilu3f+3x^1_8tNk`S zl9RBCs=6FRa^Tu`~{z3FbSvOxX5KOf=6XLl#ryud1}NuA{Pnt=sE4;RSfAVa0TV!e4)a@Mmt^_ z_xtLst4p~iVS#dRVC519_>b(~yd=+tEd!Q;|BnH+Klo6E0b^rOZyjj#6#&?RTMKOU zaiD5^#(=RghzLZOQlKdne8mu^9Dbkk0>;LmDJQ{~55Z3sd_xiX=@{SF=_CS!wzUjc z2Id)XOt+)X|J^^||L2Qr&oW>cxKa#=)-W6nFeN@)mnNsqS|54Z41E()1VIr*0iv3c3W*!6qEH|K1vvoZrx0l&5lNukhC6Tt&c^}Z*&ap7 zZqgzY*^>Qcy<^YJ+tqpvAhZ^POP~p$K^3g+(`*pAFItgW^3;hzb3EZ5L(H&1UYYHV z|HyzmyCaP7iY}&DeSUL3#|wJqct(DmPx`%l(&KEtD&d59{-dIYI@_Azf$>9TnDUM2 z>Uc{|e3fz@;|@2tMTh$s zd501#lxU$LwHU#|>5pk%mas6iaD>!+gtT%<9ZJyEnLp}ugv`*bGvExYGq7iuO{xD^ z+x!3ZAir`3oPnKUAk^EPc8f%NEMNJh2b)U11-gzE2a39szQI10WnKh S7}7)W9|5JojWh744EzGxf>O=^ literal 0 HcmV?d00001 diff --git a/bin/ANSI_C_Implementation/Makefile b/bin/ANSI_C_Implementation/Makefile deleted file mode 100644 index 7023380..0000000 --- a/bin/ANSI_C_Implementation/Makefile +++ /dev/null @@ -1,6 +0,0 @@ -all: NLMSvariants.c - gcc -o lms NLMSvariants.c - -clean: - $(RM) lms - diff --git a/bin/ANSI_C_Implementation/NLMSvariants.c b/bin/ANSI_C_Implementation/NLMSvariants.c deleted file mode 100644 index 36d3a10..0000000 --- a/bin/ANSI_C_Implementation/NLMSvariants.c +++ /dev/null @@ -1,789 +0,0 @@ -// -// -// NLMSvariants.c -// -// Created by FBRDNLMS on 26.04.18. -// -// -#include -#include -#include -#include -#include -#include // DBL_MAX - -#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 - -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 { - double xVal[7]; - double yVal[7]; -}point_t; - -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* */ -typedef struct { - unsigned char red, green, blue; -}colorChannel_t; - -typedef struct { - int x, y; - colorChannel_t *data; -}imagePixel_t; - -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* */ -char * mkFileName(char* buffer, size_t max_len, int suffixId); -char *fileSuffix(int id); -void myLogger(FILE* fp, point_t points[]); -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(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( void ) { - double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; // = { { 0.0 }, {0.0} }; -// double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; - char fileName[50]; - 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 < 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 < 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(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 < WINDOWSIZE; k++) { - fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]); - } - - } - fclose(fp1); -*/ - printf("\nDONE!\n"); -} - - -/* -====================================================================================================== - -localMean - -Variant (1/3), substract local mean. - -====================================================================================================== -*/ - -void localMean(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { - //double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; - double (*local_weights)[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[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=====================================LocalMean=====================================\nNo.\txPredicted\txActual\t\txError\n"); - - double xMean = xSamples[0]; - double xSquared = 0.0; - double xPredicted = 0.0; - double xActual = 0.0; - - for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted - - 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]; - - 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[1] = xCount; - points[xCount].yVal[1] = xPredicted; - points[xCount].xVal[4] = xCount; - points[xCount].yVal[4] = xError[xCount]; - - xSquared = 0.0; - for (i = 1; i < _arrayLength; i++) { //get xSquared - xSquared += pow(xSamples[xCount - i] - xMean, 2); - } - if (xSquared == 0.0) { // Otherwise returns Pred: -1.#IND00 in some occassions - xSquared = 1.0; - } - 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]); - } - 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 = 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(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(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { - double (*local_weights)[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 = 0.0; - double xPredicted = 0.0; - // File handling - mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); - FILE *fp3 = fopen(fileName, "w"); - fprintf(fp3, "\n=====================================DirectPredecessor=====================================\nNo.\txPredicted\txAcutal\t\txError\n"); - - - 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 += 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[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 < _arrayLength; i++) { - xSquared += pow(xSamples[xCount - 1] - 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 - 1] - xSamples[xCount - i - 1]) / xSquared); - } - fprintf(fp3, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); - } - - 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] = 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); - - //weightsLogger( local_weights, USED_WEIGHTS ); -} - - -/* -====================================================================================================== - -mkFileName - -Writes the current date plus the suffix with index suffixId -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) { - 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); - return buffer; -} - - -/* -====================================================================================================== - -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","_testvalues.txt", "_differential_predecessor.txt" }; - return suffix[id]; -} - - -/* -====================================================================================================== - -myLogger - -Logs x,y points to svg graph - -====================================================================================================== -*/ - -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,"\n\n\n\n=====================NEXT=====================\n"); - fclose(fp); -} - - -/* -====================================================================================================== - -bufferLogger - -formats output of mkSvgGraph -- Please open graphResults.html to see the output-- - -====================================================================================================== -*/ - -void bufferLogger(char *buffer, point_t points[]) { - int i; - char _buffer[512] = ""; - - for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { // xActual - sprintf(_buffer, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]); - strcat(buffer, _buffer); - } - strcat(buffer, "\" fill=\"none\" id=\"svg_1\" stroke=\"black\" stroke-width=\"0.4px\"/>\n\n\n\n"); -} - - -/* -====================================================================================================== - -sum_array - -Sum of all elements in x within a defined length - -====================================================================================================== -*/ - -double sum_array(double x[], int xlength) { - int i = 0; - double sum = 0.0; - - 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) { - return((rand() % 10000) / 10000.0); -} - - -/* -====================================================================================================== - -rndm - -fills a double variable with random value and returns it - -====================================================================================================== -*/ - -double rndm(void) { - double rndmval = r2(); - return rndmval; -} - - -/* -====================================================================================================== - -mkSvgGraph - -parses template.svg and writes results in said template - -====================================================================================================== -*/ - -void mkSvgGraph(point_t points[]) { - FILE *input = fopen("graphResults_template.html", "r"); - FILE *target = fopen("graphResults.html", "w"); - char line[512]; - char firstGraph[15] = { "x, &image->y) != 2) { - fprintf(stderr, "Invalid image size in %s\n", fileName); - exit(EXIT_FAILURE); - } - if (fscanf(fp, "%d", &rgbColor) != 1) { - fprintf(stderr, "Invalid rgb component in %s\n", fileName); - } - if (rgbColor != RGB_COLOR) { - fprintf(stderr, "Invalid image color range in %s\n", fileName); - exit(EXIT_FAILURE); - } - while (fgetc(fp) != '\n'); - image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t)); - if (!image) { - fprintf(stderr, "malloc() failed"); - exit(EXIT_FAILURE); - } - if (fread(image->data, 3 * image->x, image->y, fp) != image->y) { - fprintf(stderr, "Loading image failed"); - exit(EXIT_FAILURE); - } - fclose(fp); - return image; -} - - -/* -====================================================================================================== - -mkPpmFile - -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 - -====================================================================================================== -*/ - -void mkPpmFile(char *fileName, imagePixel_t *image) { - FILE* fp = fopen(fileName, "wb"); - if (!fp) { - fprintf(stderr, "Opening file failed."); - exit(EXIT_FAILURE); - } - fprintf(fp, "P6\n"); - fprintf(fp, "%d %d\n", image->x, image->y); - fprintf(fp, "%d\n", RGB_COLOR); - fwrite(image->data, 3 * image->x, image->y, fp); - fclose(fp); -} - - -/* -====================================================================================================== - -ppmColorChannel - -gets one of the rgb color channels and writes them to a file - -====================================================================================================== -*/ - -int ppmColorChannel(FILE* fp, imagePixel_t *image) { - // int length = (image->x * image->y) / 3; - int i = 0; - - if (image) { - for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { - fprintf(fp, "%d\n", image->data[i].green); - } - } - 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; -} - - diff --git a/bin/ANSI_C_Implementation/README.MD b/bin/ANSI_C_Implementation/README.MD deleted file mode 100644 index 55c2ff9..0000000 --- a/bin/ANSI_C_Implementation/README.MD +++ /dev/null @@ -1,19 +0,0 @@ -## Installation - -Use the make file or compile it anywhere else. -`$ make`. -It had to be ANSI C so it even compiles on VS. - - -## Features - -This little piece of code compares 3 different implementations of a least mean square filter. - -+ local mean -+ direct predecessor -+ differential predecessor - -Greyscale PPM files can be used for input at this iteration. Output will be genreeated as .txt file with predicted value genereated by the filter and its error value as well as given actual value from the PPM file. Furthermore there is an output as an svg graph to compare the implementatiosn on a visual level. Just open __graphResults.html__. -You can hide graphs by clicking on its name for better visibility. - -Use `$ ./lms -h` for help. diff --git a/bin/ANSI_C_Implementation/graphResults_template.html b/bin/ANSI_C_Implementation/graphResults_template.html deleted file mode 100644 index 5205a76..0000000 --- a/bin/ANSI_C_Implementation/graphResults_template.html +++ /dev/null @@ -1,62 +0,0 @@ - - - - - NLMSvariants | Graphical Output || - Eingangswert | - direkter Vorgaenger | - letzter Mittelwert - - - - - NLMSvariants output graph - - - - - - - - - - - - - - - - - - + + - - + - + - - - - - - - - - - - - - - diff --git a/bin/NLMS_with_UI.exe b/bin/NLMS_with_UI.exe deleted file mode 100644 index 1b5396ac060745d597beb12a7904b81b19fa0af2..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 17920 zcmeHueRLbwb?2P{F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#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 - -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 { - double xVal[7]; - double yVal[7]; -}point_t; - -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* */ -typedef struct { - unsigned char red, green, blue; -}colorChannel_t; - -typedef struct { - int x, y; - colorChannel_t *data; -}imagePixel_t; - -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* */ -char * mkFileName(char* buffer, size_t max_len, int suffixId); -char *fileSuffix(int id); -void myLogger(FILE* fp, point_t points[]); -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(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( void ) { - double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; // = { { 0.0 }, {0.0} }; -// double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; - char fileName[50]; - 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 < 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 < 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(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 < WINDOWSIZE; k++) { - fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]); - } - - } - fclose(fp1); -*/ - printf("\nDONE!\n"); -} - - -/* -====================================================================================================== - -localMean - -Variant (1/3), substract local mean. - -====================================================================================================== -*/ - -void localMean(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { - //double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]; - double (*local_weights)[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[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=====================================LocalMean=====================================\nNo.\txPredicted\txActual\t\txError\n"); - - double xMean = xSamples[0]; - double xSquared = 0.0; - double xPredicted = 0.0; - double xActual = 0.0; - - for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted - - 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]; - - 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[1] = xCount; - points[xCount].yVal[1] = xPredicted; - points[xCount].xVal[4] = xCount; - points[xCount].yVal[4] = xError[xCount]; - - xSquared = 0.0; - for (i = 1; i < _arrayLength; i++) { //get xSquared - xSquared += pow(xSamples[xCount - i] - xMean, 2); - } - if (xSquared == 0.0) { // Otherwise returns Pred: -1.#IND00 in some occassions - xSquared = 1.0; - } - 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]); - } - 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 = 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(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(double weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) { - double (*local_weights)[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 = 0.0; - double xPredicted = 0.0; - // File handling - mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); - FILE *fp3 = fopen(fileName, "w"); - fprintf(fp3, "\n=====================================DirectPredecessor=====================================\nNo.\txPredicted\txAcutal\t\txError\n"); - - - 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 += 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[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 < _arrayLength; i++) { - xSquared += pow(xSamples[xCount - 1] - 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 - 1] - xSamples[xCount - i - 1]) / xSquared); - } - fprintf(fp3, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); - } - - 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] = 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); - - //weightsLogger( local_weights, USED_WEIGHTS ); -} - - -/* -====================================================================================================== - -mkFileName - -Writes the current date plus the suffix with index suffixId -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) { - 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); - return buffer; -} - - -/* -====================================================================================================== - -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","_testvalues.txt", "_differential_predecessor.txt" }; - return suffix[id]; -} - - -/* -====================================================================================================== - -myLogger - -Logs x,y points to svg graph - -====================================================================================================== -*/ - -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,"\n\n\n\n=====================NEXT=====================\n"); - fclose(fp); -} - - -/* -====================================================================================================== - -bufferLogger - -formats output of mkSvgGraph -- Please open graphResults.html to see the output-- - -====================================================================================================== -*/ - -void bufferLogger(char *buffer, point_t points[]) { - int i; - char _buffer[512] = ""; - - for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { // xActual - sprintf(_buffer, "L %f %f\n", points[i].xVal[0], points[i].yVal[0]); - strcat(buffer, _buffer); - } - strcat(buffer, "\" fill=\"none\" id=\"svg_1\" stroke=\"black\" stroke-width=\"0.4px\"/>\n\n\n\n"); -} - - -/* -====================================================================================================== - -sum_array - -Sum of all elements in x within a defined length - -====================================================================================================== -*/ - -double sum_array(double x[], int xlength) { - int i = 0; - double sum = 0.0; - - 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) { - return((rand() % 10000) / 10000.0); -} - - -/* -====================================================================================================== - -rndm - -fills a double variable with random value and returns it - -====================================================================================================== -*/ - -double rndm(void) { - double rndmval = r2(); - return rndmval; -} - - -/* -====================================================================================================== - -mkSvgGraph - -parses template.svg and writes results in said template - -====================================================================================================== -*/ - -void mkSvgGraph(point_t points[]) { - FILE *input = fopen("graphResults_template.html", "r"); - FILE *target = fopen("graphResults.html", "w"); - char line[512]; - char firstGraph[15] = { "x, &image->y) != 2) { - fprintf(stderr, "Invalid image size in %s\n", fileName); - exit(EXIT_FAILURE); - } - if (fscanf(fp, "%d", &rgbColor) != 1) { - fprintf(stderr, "Invalid rgb component in %s\n", fileName); - } - if (rgbColor != RGB_COLOR) { - fprintf(stderr, "Invalid image color range in %s\n", fileName); - exit(EXIT_FAILURE); - } - while (fgetc(fp) != '\n'); - image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t)); - if (!image) { - fprintf(stderr, "malloc() failed"); - exit(EXIT_FAILURE); - } - if (fread(image->data, 3 * image->x, image->y, fp) != image->y) { - fprintf(stderr, "Loading image failed"); - exit(EXIT_FAILURE); - } - fclose(fp); - return image; -} - - -/* -====================================================================================================== - -mkPpmFile - -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 - -====================================================================================================== -*/ - -void mkPpmFile(char *fileName, imagePixel_t *image) { - FILE* fp = fopen(fileName, "wb"); - if (!fp) { - fprintf(stderr, "Opening file failed."); - exit(EXIT_FAILURE); - } - fprintf(fp, "P6\n"); - fprintf(fp, "%d %d\n", image->x, image->y); - fprintf(fp, "%d\n", RGB_COLOR); - fwrite(image->data, 3 * image->x, image->y, fp); - fclose(fp); -} - - -/* -====================================================================================================== - -ppmColorChannel - -gets one of the rgb color channels and writes them to a file - -====================================================================================================== -*/ - -int ppmColorChannel(FILE* fp, imagePixel_t *image) { - // int length = (image->x * image->y) / 3; - int i = 0; - - if (image) { - for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { - fprintf(fp, "%d\n", image->data[i].green); - } - } - 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; -} - - diff --git a/bin/NLMSvariants.cpp b/bin/NLMSvariants.cpp deleted file mode 100644 index 0849c02..0000000 --- a/bin/NLMSvariants.cpp +++ /dev/null @@ -1,674 +0,0 @@ -// -// -// 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 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); -} - -/* *svg graph building* */ -typedef struct { - double xVal[7]; - double yVal[7]; -}point_t; - -point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean - - /* *ppm reader/writer* */ -typedef struct { - unsigned char red, green, blue; -}colorChannel_t; - -typedef struct { - int x, y; - colorChannel_t *data; -}imagePixel_t; - -static imagePixel_t * readPPM(char *fileName); -void mkPpmFile(char *fileNamem, imagePixel_t *image); -int * ppmColorChannel(imagePixel_t *image); - -/* *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[]); - -/* *rand seed* */ -double r2(void); -double rndm(void); - -/* *math* */ -double sum_array(double x[], int length); -void directPredecessor(void); -void localMean(void); - - - -int main(int argc, char **argv) { - char fileName[50]; - int i; - - 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 - } - } - - 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]); - } - } - 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); - 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]); - } - - } - fclose(fp1); - - // getchar(); - printf("DONE!"); - -} - - -/* -======================================================================================= - -localMean - - -Variant (1/3), substract local mean. - -======================================================================================= -*/ - -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; - mkFileName(fileName, sizeof(fileName), LOCAL_MEAN); - FILE* fp4 = fopen(fileName, "w"); - fprintf(fp4, "\n\n\n\n*********************LocalMean*********************\n"); - - 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;// 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 += xMean; - xError[xCount] = xActual - xPredicted; - points[xCount].xVal[2] = xCount; - points[xCount].yVal[2] = xPredicted; - double xSquared = 0.0; - - for (i = 1; i < xCount; i++) { //get x squared - xSquared = +pow(_x[xCount - i], 2); - } - - for (i - 1; i < xCount; i++) { //update weights - w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared); - } - - fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]); - } - int xErrorLength = sizeof(xError) / sizeof(xError[0]); - double mean = sum_array(xError, xErrorLength) / M; - double deviation = 0.0; - - // Mean square - for (i = 0; i < M - 1; i++) { - deviation += pow(xError[i], 2); - } - deviation /= xErrorLength; - - - // 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); - fclose(fp4); -} - - -/* -=================================== - -directPredecessor - - -Variant (2/3), -substract direct predecessor - -=================================== -*/ - -void directPredecessor(void) { - char fileName[512]; - double xError[2048]; - int xCount = 0, i; - double xActual; - - // File handling - mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); - FILE *fp3 = fopen(fileName, "w"); - fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\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])); - } - xPredicted += _x[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; - - 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 < 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 - } - } - - int 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); - } - - - mkSvgGraph(points); - fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean); - fclose(fp3); -} - - - - -/* -========================================================================= - -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. - -========================================================================= -*/ - -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); - return buffer; -} - - - -/* -========================================================================= - -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" }; - return suffix[id]; -} - - -/* -========================================================================== - -svgGraph - - -========================================================================== -*/ -/* -void Graph ( ) { -char fileName[50]; -mkFileName(fileName, sizeof(fileName), GRAPH); -FILE* fp4 = fopen(fileName, "w"); -pfrintf -*/ - - -/* -========================================================================== - -myLogger - - -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]); - } - fprintf(fp, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n\n\n\n (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"); - char line[512]; - // char *ptr; - //size_t len = 0; - //ssize_t read; - //char values[64]; - char firstGraph[15] = { "x, &image->y) != 2) { - fprintf(stderr, "Invalid image size in %s\n", fileName); - exit(EXIT_FAILURE); - } - if (fscanf(fp, "%d", &rgbColor) != 1) { - fprintf(stderr, "Invalid rgb component in %s\n", fileName); - } - if (rgbColor != RGB_COLOR) { - fprintf(stderr, "Invalid image color range in %s\n", fileName); - exit(EXIT_FAILURE); - } - while (fgetc(fp) != '\n'); - image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t)); - if (!image) { - fprintf(stderr, "malloc() failed"); - exit(EXIT_FAILURE); - } - if (fread(image->data, 3 * image->x, image->y, fp) != image->y) { - fprintf(stderr, "Loading image failed"); - exit(EXIT_FAILURE); - } - fclose(fp); - return image; -} - - - -/* -======================================================================================= - -mkPpmFile - -gets output from the result of rdPpmFile and writes a new mkPpmFile. Best Case is a -carbon copy of the source image - -======================================================================================= -*/ - -void mkPpmFile(char *fileName, imagePixel_t *image) { - FILE* fp = fopen(fileName, "wb"); - if (!fp) { - fprintf(stderr, "Opening file failed."); - exit(EXIT_FAILURE); - } - fprintf(fp, "P6\n"); - fprintf(fp, "%d %d\n", image->x, image->y); - fprintf(fp, "%d\n", RGB_COLOR); - fwrite(image->data, 3 * image->x, image->y, fp); - fclose(fp); -} - -/* -====================================================================================== - -ppmColorChannel - -gets one of the rgb color channels and returns the array - -====================================================================================== -*/ - -int * ppmColorChannel(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; - } - } - return buffer; -} \ No newline at end of file diff --git a/bin/NLMSvariants.cs b/bin/NLMSvariants.cs deleted file mode 100644 index 0710d17..0000000 --- a/bin/NLMSvariants.cs +++ /dev/null @@ -1,446 +0,0 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.IO; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; -using System.Windows.Forms.DataVisualization.Charting; - -namespace NMLS_Graphisch -{ - public partial class Form1 : Form - { - public Form1() - { - InitializeComponent(); - } - - static int NumberOfSamples = 1000; - const int tracking = 40; - static Stack x = new Stack(); - static Random rnd = new Random(); - static double[] _x = new double[NumberOfSamples]; - static double[,] w = new double[NumberOfSamples, NumberOfSamples]; - static double learnrate = 0.2; - static double[] pixel_array; - static int windowSize = 5; - - private void button1_Click(object sender, EventArgs e) - { - - NumberOfSamples = Int32.Parse(comboBox2.SelectedItem.ToString()); - chart1.ChartAreas[0].AxisX.Maximum = NumberOfSamples; - chart1.ChartAreas[0].AxisY.Maximum = 300; - chart1.ChartAreas[0].AxisY.Minimum = -5; - - if (checkBox1.Checked) - { - for (int i = 0; i < tracking; i++) - { - for (int k = 0; k < windowSize; k++) - { - File.AppendAllText("weights.txt", - String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()), - Encoding.UTF8); - } - } - } - - Series[] series = new Series[2]; - switch (comboBox1.SelectedItem.ToString()) - { - - case "lokaler Mittelwert": - series = lokalerMittelWert(); - break; - - case "direkter Vorgänger": - series = direkterVorgaenger(); - break; - - case "differenzieller Vorgänger": - series = diffVorgaenger(); - break; - - default: - return; - - } - - foreach (Series s in series) - { - if (chart1.Series.IndexOf(s.Name) < 0) - { - chart1.Series.Add(s); - } - else - { - chart1.Series.RemoveAt(chart1.Series.IndexOf(s.Name)); - chart1.Series.Add(s); - } - } - - if (checkBox1.Checked) - { - for (int i = 0; i < tracking; i++) - { - for (int k = 0; k < windowSize; k++) - { - File.AppendAllText("weights_after.txt", - String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()), - Encoding.UTF8); - } - - } - } - - - } - - Series[] lokalerMittelWert() - { - int x_count = 0; - double[] x_error = new double[NumberOfSamples]; - x_error[0] = 0; - - //Graphischer Stuff - Series lokal_M_error = new Series("Lokaler Mittelwert Error"); - Series lokal_M_predic = new Series("Lokaler Mittelwert Prediction"); - lokal_M_error.ChartType = SeriesChartType.Spline; - lokal_M_predic.ChartType = SeriesChartType.Spline; - - while (x_count + 1 < NumberOfSamples) - { - double[] x_part_Array = new double[x_count]; - - int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0; - int _arrayLength = (x_count > windowSize) ? windowSize + 1 : (x_count > 0) ? x_count : 0; - - Array.Copy(_x, _sourceIndex, x_part_Array, 0, _arrayLength); - double x_middle = (x_count > 0) ? ( x_part_Array.Sum() / _arrayLength) : 0; - double x_pred = 0.0; - double[] x_array = _x; - double x_actual = _x[x_count + 1]; - - for (int i = 1; i < _arrayLength; i++) - { - x_pred += (w[i, x_count] * (x_array[x_count - i] - x_middle)); - } - x_pred += x_middle; - - // Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("lokalerMittelwert.txt", - String.Format("{0}. X_pred {1}\n", x_count, x_pred), - Encoding.UTF8); - - File.AppendAllText("lokalerMittelwert.txt", - String.Format("{0}. X_actual {1}\n", x_count, x_actual), - Encoding.UTF8); - } - - x_error[x_count] = x_actual - x_pred; - double x_square = 0; - - //Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("lokalerMittelwert.txt", - String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]), - Encoding.UTF8); - } - - for (int i = 1; i < _arrayLength; i++) - { - x_square += Math.Pow(x_array[x_count - i] - x_middle, 2); - } - - for (int i = 1; i < _arrayLength; i++) - { - w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_middle) / x_square); - } - - // Graphischer Stuff - lokal_M_error.Points.AddXY(x_count, x_error[x_count]); - lokal_M_predic.Points.AddXY(x_count, x_pred); - - x_count += 1; - } - double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length; - double varianz = 0.0; - foreach (double x_e in x_error) - { - if(!double.IsNaN(x_e)) - varianz += Math.Pow(x_e - mittel, 2); - } - varianz /= x_error.Length; - if (checkBox1.Checked) - { - File.AppendAllText("ergebnisse.txt", - String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel), - Encoding.UTF8); - } - return new Series[] { lokal_M_predic, lokal_M_error }; - } - - Series[] direkterVorgaenger() - { - double[] x_error = new double[NumberOfSamples]; - x_error[0] = 0; - int x_count = 0; - - // Graphischer Stuff - Series direkterVorgaenger_error = new Series("Direkter Vorgänger Error"); - Series direkterVorgaenger_predic = new Series("Direkter Vorgänger Prediction"); - direkterVorgaenger_error.ChartType = SeriesChartType.Spline; - direkterVorgaenger_predic.ChartType = SeriesChartType.Spline; - - while (x_count + 1 < NumberOfSamples) - { - double x_pred = 0.0; - double[] x_array = _x; - double x_actual = _x[x_count + 1]; - - if (x_count > 0) - { - int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count; - - for (int i = 1; i < _arrayLength; i++) - { - x_pred += (w[i, x_count] * (x_array[x_count - 1] - x_array[x_count - i - 1])); - } - x_pred += x_array[x_count - 1]; - - // Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_pred {1}\n", x_count, x_pred), - Encoding.UTF8); - - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_actual {1}\n", x_count, x_actual), - Encoding.UTF8); - } - - - x_error[x_count] = x_actual - x_pred; - - // Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]), - Encoding.UTF8); - } - - double x_square = 0; - for (int i = 1; i < _arrayLength; i++) - { - x_square += Math.Pow(x_array[x_count - 1] - x_array[x_count - i - 1], 2); - } - - for (int i = 1; i < _arrayLength; i++) - { - w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - 1] - x_array[x_count - i - 1]) / x_square); - } - } - - //Graphischer Stuff - direkterVorgaenger_error.Points.AddXY(x_count, x_error[x_count]); - direkterVorgaenger_predic.Points.AddXY(x_count, x_pred); - - x_count += 1; - } - double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length; - double varianz = 0.0; - foreach (double x_e in x_error) - { - if (!double.IsNaN(x_e)) - varianz += Math.Pow(x_e - mittel, 2); - } - varianz /= x_error.Length; - if (checkBox1.Checked) - { - File.AppendAllText("ergebnisse.txt", - String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel), - Encoding.UTF8); - } - - return new Series[] { direkterVorgaenger_error, direkterVorgaenger_predic }; - } - - Series[] diffVorgaenger() - { - double[] x_error = new double[NumberOfSamples]; - x_error[0] = 0; - int x_count = 1; - - //Graphischer Stuff - Series diffVorgaenger_error = new Series("Differenzieller Vorgänger Error"); - Series diffVorgaenger_predic = new Series("Differenzieller Vorgänger Prediction"); - diffVorgaenger_error.ChartType = SeriesChartType.Spline; - diffVorgaenger_predic.ChartType = SeriesChartType.Spline; - - while (x_count + 1 < NumberOfSamples) - { - double x_pred = 0.0; - double[] x_array = _x; - double x_actual = _x[x_count + 1]; - if (x_count > 0) - { - int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count; - - for (int i = 1; i < _arrayLength; i++) - { - x_pred += (w[i, x_count] * (x_array[x_count - i] - x_array[x_count - i - 1])); - } - x_pred += x_array[x_count - 1]; - - // Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("differenziellerVorgaenger.txt", - String.Format("{0}. X_pred {1}\n", x_count, x_pred), - Encoding.UTF8); - - File.AppendAllText("differenziellerVorgaenger.txt", - String.Format("{0}. X_actual {1}\n", x_count, x_actual), - Encoding.UTF8); - } - - - x_error[x_count] = x_actual - x_pred; - - // Output Stuff - if (checkBox1.Checked) - { - File.AppendAllText("differenziellerVorgaenger.txt", - String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]), - Encoding.UTF8); - } - - - double x_square = 0; - for (int i = 1; i < _arrayLength; i++) - { - x_square += Math.Pow(x_array[x_count - i] - x_array[x_count - i - 1], 2); - } - - for (int i = 1; i < _arrayLength; i++) - { - w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_array[x_count - i - 1]) / x_square); - } - - } - - //Graphischer Stuff - diffVorgaenger_error.Points.AddXY(x_count, x_error[x_count]); - diffVorgaenger_predic.Points.AddXY(x_count, x_pred); - - x_count += 1; - } - double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length; - double varianz = 0.0; - foreach (double x_e in x_error) - { - if (!double.IsNaN(x_e)) - varianz += Math.Pow(x_e - mittel, 2); - } - varianz /= x_error.Length; - - if (checkBox1.Checked) - { - File.AppendAllText("ergebnisse.txt", - String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel), - Encoding.UTF8); - } - - return new Series[] { diffVorgaenger_error, diffVorgaenger_predic }; - } - - // Inizialisierung von Arrays - private void Form1_Load(object sender, EventArgs e) - { - comboBox1.SelectedIndex = 0; - comboBox2.SelectedIndex = 0; - chart1.Series.Clear(); - Series x_actual = new Series("Actual x Value"); - x_actual.ChartType = SeriesChartType.Spline; - for (int i = 0; i < NumberOfSamples; i++) - { - _x[i] += ((255.0 / NumberOfSamples) * i); - for (int k = 0; k < windowSize; k++) - { - w[k, i] = rnd.NextDouble(); - //Console.WriteLine(String.Format("Weight[{0}, {1}]: {2}",k,i, w[k, i])); - } - x_actual.Points.AddXY(i, _x[i]); - } - chart1.Series.Add(x_actual); - } - - // Graphen Clearen - private void button2_Click(object sender, EventArgs e) - { - chart1.Series.Clear(); - Series x_actual = new Series("Actual x Value"); - x_actual.ChartType = SeriesChartType.Spline; - for (int i = 0; i < NumberOfSamples; i++) - { - x_actual.Points.AddXY(i, _x[i]); - } - chart1.Series.Add(x_actual); - } - - // Bild Laden - private void button3_Click(object sender, EventArgs e) - { - - OpenFileDialog openFileDialog = new OpenFileDialog(); - - if(openFileDialog.ShowDialog() == DialogResult.OK) - { - try - { - Bitmap img = new Bitmap(openFileDialog.FileName); - pixel_array = new double[img.Width * img.Height]; - - for (int i = 1; i < img.Width; i++) - { - for (int j = 1; j < img.Height; j++) - { - - Color pixel = img.GetPixel(i, j); - pixel_array[j*i] = pixel.R; - - } - } - NumberOfSamples = (img.Width * img.Height) / 2; - comboBox2.Items.Add(NumberOfSamples); - _x = pixel_array; - w = new double[NumberOfSamples, NumberOfSamples]; - for (int i = 0; i < NumberOfSamples; i++) - { - for (int k = 1; k < NumberOfSamples; k++) - { - w[k, i] = rnd.NextDouble(); - } - } - } - catch(Exception exep) - { - MessageBox.Show("Konnte Bild nicht laden."); - MessageBox.Show(String.Format("{0}", exep.ToString())); - } - } - } - } - -} - diff --git a/src/.DS_Store b/src/.DS_Store index 1c97b1a354129b5641d626007e2530c9b9e26788..434a283bd6b9e76e27e0fb134bdd62349f9d341d 100644 GIT binary patch delta 75 xcmZp1XmQveEXZMMXrQBDY;LkyTCkZB&YLVP=(zd0kO>p2NQ&roc65;iOaQle5_$jt delta 75 zcmZp1XmQveEXZMEYO14PY+$@uTCkas!^GT3M?u%fc(Syh
-

- Eingangswert | - direkter Vorgaenger | - letzter Mittelwert -

-