updated .exe and src file
This commit is contained in:
		
							parent
							
								
									f48178dba2
								
							
						
					
					
						commit
						aa84dad655
					
				
							
								
								
									
										
											BIN
										
									
								
								bin/CPP_NLMS.exe
								
								
								
								
							
							
						
						
									
										
											BIN
										
									
								
								bin/CPP_NLMS.exe
								
								
								
								
							
										
											Binary file not shown.
										
									
								
							| 
						 | 
				
			
			@ -11,7 +11,7 @@ Created by Stefan Friese on 26.04.2018
 | 
			
		|||
#include <time.h>
 | 
			
		||||
#include <stdlib.h>
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include "nlms_types.h" // added types
 | 
			
		||||
#include "nlms_types.h"
 | 
			
		||||
 | 
			
		||||
#define RGB_COLOR 255
 | 
			
		||||
#if defined(_MSC_VER)
 | 
			
		||||
| 
						 | 
				
			
			@ -23,118 +23,120 @@ double *xSamples;		// Input color values from PPM
 | 
			
		|||
mldata_t *mlData = NULL;	// Machine learning realted data
 | 
			
		||||
point_t *points = NULL;		// Graphing			
 | 
			
		||||
 | 
			
		||||
/* *Graph building* */
 | 
			
		||||
							/* *Graph building* */
 | 
			
		||||
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
 | 
			
		||||
		char *colorChannel, mldata_t *mlData); 	
 | 
			
		||||
	char *colorChannel, mldata_t *mlData);
 | 
			
		||||
void colorSamples(FILE* fp, mldata_t *mlData); 			// Stores color channel values in xSamples
 | 
			
		||||
 | 
			
		||||
/* *File handling* */
 | 
			
		||||
char * mkFileName ( char* buffer, 				// Date+suffix as filename					
 | 
			
		||||
		size_t max_len, int suffixId );			
 | 
			
		||||
char *fileSuffix ( int id );					// Filename ending of logs
 | 
			
		||||
char *fileHeader ( int id );					// Header inside the logfiles
 | 
			
		||||
														/* *File handling* */
 | 
			
		||||
char * mkFileName(char* buffer, 				// Date+suffix as filename					
 | 
			
		||||
	size_t max_len, int suffixId);
 | 
			
		||||
char *fileSuffix(int id);					// Filename ending of logs
 | 
			
		||||
char *fileHeader(int id);					// Header inside the logfiles
 | 
			
		||||
void bufferLogger(char *buffer, point_t points[]);		// Writes points to graph template
 | 
			
		||||
void mkSvgGraph ( point_t points[], char *templatePath);				// Parses graph template and calls bufferLogger()
 | 
			
		||||
void weightsLogger ( double *weights, int suffix );		// Writes updated weights to a file
 | 
			
		||||
void mkSvgGraph(point_t points[], char *templatePath);				// Parses graph template and calls bufferLogger()
 | 
			
		||||
void weightsLogger(double *weights, int suffix);		// Writes updated weights to a file
 | 
			
		||||
 | 
			
		||||
/* *rand seed* */
 | 
			
		||||
double r2 ( void );						// Random val between 0 and 1
 | 
			
		||||
double rndm ( void );
 | 
			
		||||
														/* *rand seed* */
 | 
			
		||||
double r2(void);						// Random val between 0 and 1
 | 
			
		||||
double rndm(void);
 | 
			
		||||
 | 
			
		||||
/* *args parser* */
 | 
			
		||||
void usage ( char **argv ); 						// Help text called by args parser
 | 
			
		||||
void usage(char **argv); 						// Help text called by args parser
 | 
			
		||||
 | 
			
		||||
/* *math* */
 | 
			
		||||
												/* *math* */
 | 
			
		||||
mldata_t * init_mldata_t(unsigned windowSize, unsigned samplesCount, double learnrate);
 | 
			
		||||
double sum_array(double x[], int length);
 | 
			
		||||
 | 
			
		||||
void localMean ( mldata_t *mlData,point_t points[] );			// First,
 | 
			
		||||
void directPredecessor ( mldata_t *mlData, point_t points[] );		// Second,
 | 
			
		||||
void differentialPredecessor ( mldata_t *mlData, point_t points[] );	// Third filter implementation
 | 
			
		||||
void localMean(mldata_t *mlData, point_t points[]);			// First,
 | 
			
		||||
void directPredecessor(mldata_t *mlData, point_t points[]);		// Second,
 | 
			
		||||
void differentialPredecessor(mldata_t *mlData, point_t points[]);	// Third filter implementation
 | 
			
		||||
 | 
			
		||||
double windowXMean(int _arraylength, int xCount);		// Returns mean value of given window
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
int main( int argc, char **argv ) {
 | 
			
		||||
	char *colorChannel = (char *) malloc(sizeof(char)* 32);
 | 
			
		||||
int main(int argc, char **argv) {
 | 
			
		||||
	char *colorChannel = (char *)malloc(sizeof(char) * 32);
 | 
			
		||||
	char *inputfile = (char *)malloc(sizeof(char) * 32);
 | 
			
		||||
	unsigned *seed = NULL;
 | 
			
		||||
	unsigned k, include = 0;
 | 
			
		||||
	unsigned windowSize = 5;
 | 
			
		||||
	unsigned samplesCount = 512;
 | 
			
		||||
	char *stdcolor = "green", xBuffer[512];
 | 
			
		||||
	char *stdcolor = (char*)"green", xBuffer[512];
 | 
			
		||||
	colorChannel = stdcolor;
 | 
			
		||||
	unsigned int uint_buffer[1], windowBuffer[1];
 | 
			
		||||
	double learnrate = 0.4;
 | 
			
		||||
	char *istrue = "true";
 | 
			
		||||
	char *istrue = (char*)"true";
 | 
			
		||||
	char *templatePath = NULL;
 | 
			
		||||
 | 
			
		||||
	while( (argc > 1) && (argv[1][0] == '-')  ) {	// Parses parameters from stdin
 | 
			
		||||
			switch( argv[1][1] ) {
 | 
			
		||||
				case 'i':
 | 
			
		||||
					inputfile = &argv[1][3];
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				case 'w':
 | 
			
		||||
					sscanf(&argv[1][3], "%u", windowBuffer);
 | 
			
		||||
					windowSize = windowBuffer[0];		
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				case 'c':	
 | 
			
		||||
					colorChannel = &argv[1][3];	
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				case 's':	
 | 
			
		||||
					sscanf(&argv[1][3], "%u", uint_buffer);
 | 
			
		||||
					seed = &uint_buffer[0];
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				case 'n':
 | 
			
		||||
					sscanf(&argv[1][3], "%u", &samplesCount);
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;			
 | 
			
		||||
				case'h':
 | 
			
		||||
					printf("Program name: %s\n", argv[0]);
 | 
			
		||||
					usage(argv);
 | 
			
		||||
					break;
 | 
			
		||||
				case 'l':
 | 
			
		||||
					sscanf(&argv[1][3], "%lf", &learnrate);
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				case 'g':
 | 
			
		||||
					sscanf(&argv[1][3], "%s", xBuffer);
 | 
			
		||||
					if ( strstr(xBuffer, istrue) ) {
 | 
			
		||||
						include = 1;
 | 
			
		||||
					} else if ( xBuffer && !strstr(xBuffer, istrue) ) {
 | 
			
		||||
						templatePath = xBuffer; 
 | 
			
		||||
						include = 1;
 | 
			
		||||
					} else {
 | 
			
		||||
						printf(	"Wrong Argruments: %s\n", argv[1]);
 | 
			
		||||
						usage(argv);
 | 
			
		||||
					}
 | 
			
		||||
					++argv;
 | 
			
		||||
					--argc;
 | 
			
		||||
					break;
 | 
			
		||||
				default:
 | 
			
		||||
					printf("Wrong Arguments: %s\n", argv[1]);
 | 
			
		||||
					usage(argv);		
 | 
			
		||||
	while ((argc > 1) && (argv[1][0] == '-')) {	// Parses parameters from stdin
 | 
			
		||||
		switch (argv[1][1]) {
 | 
			
		||||
		case 'i':
 | 
			
		||||
			inputfile = &argv[1][3];
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'w':
 | 
			
		||||
			sscanf(&argv[1][3], "%u", windowBuffer);
 | 
			
		||||
			windowSize = windowBuffer[0];
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'c':
 | 
			
		||||
			colorChannel = &argv[1][3];
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case 's':
 | 
			
		||||
			sscanf(&argv[1][3], "%u", uint_buffer);
 | 
			
		||||
			seed = &uint_buffer[0];
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'n':
 | 
			
		||||
			sscanf(&argv[1][3], "%u", &samplesCount);
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case'h':
 | 
			
		||||
			printf("Program name: %s\n", argv[0]);
 | 
			
		||||
			usage(argv);
 | 
			
		||||
			break;
 | 
			
		||||
		case 'l':
 | 
			
		||||
			sscanf(&argv[1][3], "%lf", &learnrate);
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		case 'g':
 | 
			
		||||
			sscanf(&argv[1][3], "%s", xBuffer);
 | 
			
		||||
			if (strstr(xBuffer, istrue)) {
 | 
			
		||||
				include = 1;
 | 
			
		||||
			}
 | 
			
		||||
			else if (xBuffer && !strstr(xBuffer, istrue)) {
 | 
			
		||||
				templatePath = xBuffer;
 | 
			
		||||
				include = 1;
 | 
			
		||||
			}
 | 
			
		||||
			else {
 | 
			
		||||
				printf("Wrong Argruments: %s\n", argv[1]);
 | 
			
		||||
				usage(argv);
 | 
			
		||||
			}
 | 
			
		||||
			++argv;
 | 
			
		||||
			--argc;
 | 
			
		||||
			break;
 | 
			
		||||
		default:
 | 
			
		||||
			printf("Wrong Arguments: %s\n", argv[1]);
 | 
			
		||||
			usage(argv);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		++argv;
 | 
			
		||||
		--argc;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	init_mldata_t ( windowSize, samplesCount, learnrate );
 | 
			
		||||
	xSamples = (double *) malloc ( sizeof(double) * mlData->samplesCount ); 	// Resize input values
 | 
			
		||||
	points = (point_t *) malloc ( sizeof(point_t) * mlData->samplesCount);		// Resize points
 | 
			
		||||
	init_mldata_t(windowSize, samplesCount, learnrate);
 | 
			
		||||
	xSamples = (double *)malloc(sizeof(double) * mlData->samplesCount); 	// Resize input values
 | 
			
		||||
	points = (point_t *)malloc(sizeof(point_t) * mlData->samplesCount);		// Resize points
 | 
			
		||||
	imagePixel_t *image;
 | 
			
		||||
	image = rdPPM(inputfile);							// Set Pointer on input values	
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -146,11 +148,12 @@ int main( int argc, char **argv ) {
 | 
			
		|||
	FILE* fp6 = fopen(fileName, "r");
 | 
			
		||||
	colorSamples(fp6, mlData);
 | 
			
		||||
 | 
			
		||||
	if ( (seed != NULL) ){ 			
 | 
			
		||||
		srand( *seed );								// Seed for random number generating
 | 
			
		||||
	if ((seed != NULL)) {
 | 
			
		||||
		srand(*seed);								// Seed for random number generating
 | 
			
		||||
		printf("srand is reproducable\n");
 | 
			
		||||
	} else {
 | 
			
		||||
		srand( (unsigned int)time(NULL) );
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		srand((unsigned int)time(NULL));
 | 
			
		||||
		printf("srand depends on time\n");					// Default seed is time(NULL)
 | 
			
		||||
	}
 | 
			
		||||
	printf("generated weights:\n");
 | 
			
		||||
| 
						 | 
				
			
			@ -167,11 +170,11 @@ int main( int argc, char **argv ) {
 | 
			
		|||
	}
 | 
			
		||||
	fclose(fp0);
 | 
			
		||||
 | 
			
		||||
	localMean ( mlData, points );							// math magic functions
 | 
			
		||||
	directPredecessor ( mlData, points );
 | 
			
		||||
	differentialPredecessor( mlData, points );
 | 
			
		||||
	localMean(mlData, points);							// math magic functions
 | 
			
		||||
	directPredecessor(mlData, points);
 | 
			
		||||
	differentialPredecessor(mlData, points);
 | 
			
		||||
 | 
			
		||||
	if ( include == 1 ) {
 | 
			
		||||
	if (include == 1) {
 | 
			
		||||
		mkSvgGraph(points, templatePath);							// Graph building
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -192,20 +195,22 @@ Variant (1/3), substract local mean.
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void localMean ( mldata_t *mlData, point_t points[] ) {
 | 
			
		||||
	double *localWeights = (double *) malloc ( sizeof(double) * mlData->windowSize + 1);		
 | 
			
		||||
void localMean(mldata_t *mlData, point_t points[]) {
 | 
			
		||||
	double *localWeights = (double *)malloc(sizeof(double) * mlData->windowSize + 1);
 | 
			
		||||
	localWeights = mlData->weights;
 | 
			
		||||
 | 
			
		||||
	char fileName[50];
 | 
			
		||||
	const unsigned xErrorLength = mlData->samplesCount;                                  
 | 
			
		||||
    double xError[xErrorLength];            
 | 
			
		||||
    unsigned i, xCount = 0; 			            						// Runtime vars
 | 
			
		||||
	unsigned xErrorLength = mlData->samplesCount;
 | 
			
		||||
	double *xError = (double *)malloc(sizeof(double) * xErrorLength+1);
 | 
			
		||||
	memset(xError, 0.0, sizeof(double) * xErrorLength);
 | 
			
		||||
	
 | 
			
		||||
	unsigned i, xCount = 0; 			            						// Runtime vars
 | 
			
		||||
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), LOCAL_MEAN);						// Create Logfile and its filename
 | 
			
		||||
	FILE* fp4 = fopen(fileName, "w");
 | 
			
		||||
	fprintf( fp4, fileHeader(LOCAL_MEAN_HEADER) );		
 | 
			
		||||
	fprintf(fp4, fileHeader(LOCAL_MEAN_HEADER));
 | 
			
		||||
 | 
			
		||||
	mkFileName ( fileName, sizeof(fileName), USED_WEIGHTS_LOCAL_MEAN);
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), USED_WEIGHTS_LOCAL_MEAN);
 | 
			
		||||
	FILE *fp9 = fopen(fileName, "w");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -214,14 +219,14 @@ void localMean ( mldata_t *mlData, point_t points[] ) {
 | 
			
		|||
	double xPredicted = 0.0;
 | 
			
		||||
	double xActual = 0.0;
 | 
			
		||||
 | 
			
		||||
	for ( xCount = 1; xCount < mlData->samplesCount-1; xCount++ ) { 					// First value will not get predicted
 | 
			
		||||
		unsigned _arrayLength = ( xCount > mlData->windowSize ) ? mlData->windowSize + 1 : xCount;	// Ensures corect length at start
 | 
			
		||||
	for (xCount = 1; xCount < mlData->samplesCount - 1; xCount++) { 					// First value will not get predicted
 | 
			
		||||
		unsigned _arrayLength = (xCount > mlData->windowSize) ? mlData->windowSize + 1 : xCount;	// Ensures corect length at start
 | 
			
		||||
		xMean = (xCount > 0) ? windowXMean(_arrayLength, xCount) : 0;
 | 
			
		||||
		xPredicted = 0.0;
 | 
			
		||||
		xActual = xSamples[xCount];
 | 
			
		||||
 | 
			
		||||
		for ( i = 1; i < _arrayLength; i++ ) { 								// Get predicted value
 | 
			
		||||
			xPredicted += ( localWeights[i - 1] * (xSamples[xCount - i] - xMean) );			
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) { 								// Get predicted value
 | 
			
		||||
			xPredicted += (localWeights[i - 1] * (xSamples[xCount - i] - xMean));
 | 
			
		||||
		}
 | 
			
		||||
		xPredicted += xMean;
 | 
			
		||||
		xError[xCount] = xActual - xPredicted;								// Get error value
 | 
			
		||||
| 
						 | 
				
			
			@ -229,13 +234,13 @@ void localMean ( mldata_t *mlData, point_t points[] ) {
 | 
			
		|||
		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
 | 
			
		||||
		if (xSquared == 0.0) { 									// Otherwise returns Pred: -1.#IND00 in some occassions
 | 
			
		||||
			xSquared = 1.0;
 | 
			
		||||
		}
 | 
			
		||||
		for ( i = 1; i < _arrayLength; i++ ) { 								// Update weights
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) { 								// Update weights
 | 
			
		||||
			localWeights[i] = localWeights[i - 1] + mlData->learnrate * xError[xCount]  		// Substract localMean
 | 
			
		||||
				* ( (xSamples[xCount - i] - xMean) / xSquared );
 | 
			
		||||
			fprintf( fp9, "%lf\n", localWeights[i] );
 | 
			
		||||
				* ((xSamples[xCount - i] - xMean) / xSquared);
 | 
			
		||||
			fprintf(fp9, "%lf\n", localWeights[i]);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		fprintf(fp4, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]);			// Write to logfile
 | 
			
		||||
| 
						 | 
				
			
			@ -271,31 +276,32 @@ substract direct predecessor
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void directPredecessor( mldata_t *mlData, point_t points[]) {
 | 
			
		||||
	double *localWeights = ( double * ) malloc ( sizeof(double) * mlData->windowSize + 1 );
 | 
			
		||||
void directPredecessor(mldata_t *mlData, point_t points[]) {
 | 
			
		||||
	double *localWeights = (double *)malloc(sizeof(double) * mlData->windowSize + 1);
 | 
			
		||||
	localWeights = mlData->weights;
 | 
			
		||||
 | 
			
		||||
	char fileName[512];
 | 
			
		||||
    const unsigned xErrorLength = mlData->samplesCount;
 | 
			
		||||
    double xError[xErrorLength];
 | 
			
		||||
   	unsigned xCount = 0, i;
 | 
			
		||||
	const unsigned xErrorLength = mlData->samplesCount;
 | 
			
		||||
	double *xError = (double *)malloc(sizeof(double) * xErrorLength);
 | 
			
		||||
	memset(xError, 0.0, sizeof(double) * xErrorLength);
 | 
			
		||||
	unsigned xCount = 0, i;
 | 
			
		||||
	double xActual = 0.0;
 | 
			
		||||
	double xPredicted = 0.0;
 | 
			
		||||
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);								// Logfile and name handling
 | 
			
		||||
	FILE *fp3 = fopen(fileName, "w");
 | 
			
		||||
	fprintf( fp3, fileHeader(DIRECT_PREDECESSOR_HEADER) );
 | 
			
		||||
	fprintf(fp3, fileHeader(DIRECT_PREDECESSOR_HEADER));
 | 
			
		||||
 | 
			
		||||
	mkFileName ( fileName, sizeof(fileName), USED_WEIGHTS_DIR_PRED);
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), USED_WEIGHTS_DIR_PRED);
 | 
			
		||||
	FILE *fp9 = fopen(fileName, "w");
 | 
			
		||||
 | 
			
		||||
	for (xCount = 1; xCount < mlData->samplesCount-1; xCount++) { 								// first value will not get predicted
 | 
			
		||||
		unsigned _arrayLength = ( xCount > mlData->windowSize ) ? mlData->windowSize + 1 : xCount;
 | 
			
		||||
	for (xCount = 1; xCount < mlData->samplesCount - 1; xCount++) { 								// first value will not get predicted
 | 
			
		||||
		unsigned _arrayLength = (xCount > mlData->windowSize) ? mlData->windowSize + 1 : xCount;
 | 
			
		||||
		xPredicted = 0.0;
 | 
			
		||||
		xActual = xSamples[xCount];
 | 
			
		||||
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) {
 | 
			
		||||
			xPredicted += ( localWeights[i - 1] * (xSamples[xCount - 1] - xSamples[xCount - i - 1]));
 | 
			
		||||
			xPredicted += (localWeights[i - 1] * (xSamples[xCount - 1] - xSamples[xCount - i - 1]));
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		xPredicted += xSamples[xCount - 1];
 | 
			
		||||
| 
						 | 
				
			
			@ -305,16 +311,16 @@ void directPredecessor( mldata_t *mlData, point_t points[]) {
 | 
			
		|||
		for (i = 1; i < _arrayLength; i++) {
 | 
			
		||||
			xSquared += pow(xSamples[xCount - 1] - xSamples[xCount - i - 1], 2); 					// substract direct predecessor
 | 
			
		||||
		}
 | 
			
		||||
		if ( xSquared == 0.0 ) { 											// Otherwise returns Pred: -1.#IND00 in some occassions
 | 
			
		||||
		if (xSquared == 0.0) { 											// Otherwise returns Pred: -1.#IND00 in some occassions
 | 
			
		||||
			xSquared = 1.0;
 | 
			
		||||
		}
 | 
			
		||||
		for ( i = 1; i < _arrayLength; i++ ) {										// Update weights
 | 
			
		||||
			localWeights[i] = localWeights[i-1] + mlData->learnrate * xError[xCount] 				
 | 
			
		||||
				* ( (xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared);				
 | 
			
		||||
			fprintf( fp9, "%lf\n", localWeights[i] );
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) {										// Update weights
 | 
			
		||||
			localWeights[i] = localWeights[i - 1] + mlData->learnrate * xError[xCount]
 | 
			
		||||
				* ((xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared);
 | 
			
		||||
			fprintf(fp9, "%lf\n", localWeights[i]);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	        fprintf(fp3, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]);					// Write to logfile
 | 
			
		||||
		fprintf(fp3, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]);					// Write to logfile
 | 
			
		||||
		points[xCount].xVal[2] = xCount; 										// Fill point_t array for graph building
 | 
			
		||||
		points[xCount].yVal[2] = xPredicted;
 | 
			
		||||
		points[xCount].xVal[5] = xCount;
 | 
			
		||||
| 
						 | 
				
			
			@ -345,12 +351,13 @@ differential predecessor.
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void differentialPredecessor ( mldata_t *mlData, point_t points[] ) {
 | 
			
		||||
	double *localWeights = (double *) malloc ( sizeof(double) * mlData->windowSize + 1 );
 | 
			
		||||
void differentialPredecessor(mldata_t *mlData, point_t points[]) {
 | 
			
		||||
	double *localWeights = (double *)malloc(sizeof(double) * mlData->windowSize + 1);
 | 
			
		||||
	localWeights = mlData->weights;
 | 
			
		||||
    const unsigned xErrorLength = mlData->samplesCount; 
 | 
			
		||||
	const unsigned xErrorLength = mlData->samplesCount;
 | 
			
		||||
	char fileName[512];
 | 
			
		||||
    double xError[xErrorLength];
 | 
			
		||||
	double *xError = (double *)malloc(sizeof(double) * xErrorLength);
 | 
			
		||||
	memset(xError, 0.0, sizeof(double) * xErrorLength);
 | 
			
		||||
 | 
			
		||||
	unsigned xCount = 0, i;
 | 
			
		||||
	double xPredicted = 0.0;
 | 
			
		||||
| 
						 | 
				
			
			@ -358,19 +365,19 @@ void differentialPredecessor ( mldata_t *mlData, point_t points[] ) {
 | 
			
		|||
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), DIFFERENTIAL_PREDECESSOR);							// File handling
 | 
			
		||||
	FILE *fp6 = fopen(fileName, "w");
 | 
			
		||||
	fprintf(fp6, fileHeader(DIFFERENTIAL_PREDECESSOR_HEADER) );
 | 
			
		||||
	fprintf(fp6, fileHeader(DIFFERENTIAL_PREDECESSOR_HEADER));
 | 
			
		||||
 | 
			
		||||
	mkFileName ( fileName, sizeof(fileName), USED_WEIGHTS_DIFF_PRED);
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), USED_WEIGHTS_DIFF_PRED);
 | 
			
		||||
	FILE *fp9 = fopen(fileName, "w");
 | 
			
		||||
 | 
			
		||||
		for (xCount = 1; xCount < mlData->samplesCount-1; xCount++) { 							// First value will not get predicted
 | 
			
		||||
	for (xCount = 1; xCount < mlData->samplesCount - 1; xCount++) { 							// First value will not get predicted
 | 
			
		||||
 | 
			
		||||
		unsigned _arrayLength = (xCount > mlData->windowSize) ? mlData->windowSize + 1 : xCount;
 | 
			
		||||
		xPredicted = 0.0;
 | 
			
		||||
		xActual = xSamples[xCount];
 | 
			
		||||
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) {
 | 
			
		||||
			xPredicted += ( localWeights[i - 1] * (xSamples[xCount - i] - xSamples[xCount - i - 1]));
 | 
			
		||||
			xPredicted += (localWeights[i - 1] * (xSamples[xCount - i] - xSamples[xCount - i - 1]));
 | 
			
		||||
		}
 | 
			
		||||
		xPredicted += xSamples[xCount - 1];
 | 
			
		||||
		xError[xCount] = xActual - xPredicted;
 | 
			
		||||
| 
						 | 
				
			
			@ -379,17 +386,17 @@ void differentialPredecessor ( mldata_t *mlData, point_t points[] ) {
 | 
			
		|||
		for (i = 1; i < _arrayLength; i++) {
 | 
			
		||||
			xSquared += pow(xSamples[xCount - i] - xSamples[xCount - i - 1], 2); 					// Substract direct predecessor
 | 
			
		||||
		}
 | 
			
		||||
		if ( xSquared == 0.0 ) { 											// Otherwise returns Pred: -1.#IND00 in some occassions
 | 
			
		||||
		if (xSquared == 0.0) { 											// Otherwise returns Pred: -1.#IND00 in some occassions
 | 
			
		||||
			xSquared = 1.0;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for (i = 1; i < _arrayLength; i++) {
 | 
			
		||||
			localWeights[i] = localWeights[i-1] + mlData->learnrate * xError[xCount] 
 | 
			
		||||
			localWeights[i] = localWeights[i - 1] + mlData->learnrate * xError[xCount]
 | 
			
		||||
				* ((xSamples[xCount - i] - xSamples[xCount - i - 1]) / xSquared);
 | 
			
		||||
			fprintf( fp9, "%lf\n", localWeights[i] );
 | 
			
		||||
			fprintf(fp9, "%lf\n", localWeights[i]);
 | 
			
		||||
 | 
			
		||||
		}
 | 
			
		||||
       		fprintf(fp6, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); 					// Write to logfile
 | 
			
		||||
		fprintf(fp6, "%d\t%f\t%f\t%f\n", xCount, xPredicted, xActual, xError[xCount]); 					// Write to logfile
 | 
			
		||||
 | 
			
		||||
		points[xCount].xVal[3] = xCount;
 | 
			
		||||
		points[xCount].yVal[3] = xPredicted;
 | 
			
		||||
| 
						 | 
				
			
			@ -402,7 +409,6 @@ void differentialPredecessor ( mldata_t *mlData, point_t points[] ) {
 | 
			
		|||
	double mean = sum_array(xError, xErrorLength) / xErrorLength;
 | 
			
		||||
	double deviation = 0.0;
 | 
			
		||||
	
 | 
			
		||||
	
 | 
			
		||||
	for (i = 1; i < xErrorLength; i++) {											// Mean square
 | 
			
		||||
		deviation += pow(xError[i] - mean, 2);
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -444,16 +450,16 @@ Contains and returns every suffix for all existing filenames
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
char * fileSuffix ( int id ) {
 | 
			
		||||
	char * suffix[] = { 	"_weights_pure.txt", 
 | 
			
		||||
				"_weights_used_dir_pred_.txt", 
 | 
			
		||||
				"_direct_predecessor.txt", 
 | 
			
		||||
				"_ergebnisse.txt", 
 | 
			
		||||
				"_localMean.txt",
 | 
			
		||||
				"_testvalues.txt", 
 | 
			
		||||
				"_differential_predecessor.txt",
 | 
			
		||||
				"_weights_used_local_mean.txt",
 | 
			
		||||
				"_weights_used_diff_pred.txt",
 | 
			
		||||
char * fileSuffix(int id) {
 | 
			
		||||
	char * suffix[] = { (char*)"_weights_pure.txt",
 | 
			
		||||
		(char*)"_weights_used_dir_pred_.txt",
 | 
			
		||||
		(char*)"_direct_predecessor.txt",
 | 
			
		||||
		(char*)"_ergebnisse.txt",
 | 
			
		||||
		(char*)"_localMean.txt",
 | 
			
		||||
		(char*)"_testvalues.txt",
 | 
			
		||||
		(char*)"_differential_predecessor.txt",
 | 
			
		||||
		(char*)"_weights_used_local_mean.txt",
 | 
			
		||||
		(char*)"_weights_used_diff_pred.txt",
 | 
			
		||||
	};
 | 
			
		||||
	return suffix[id];
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -467,10 +473,10 @@ Contains and returns header from logfiles
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
char * fileHeader ( int id ) {
 | 
			
		||||
	char * header[] = {	"\n=========================== Local Mean ===========================\nNo.\txPredicted\txAcutal\t\txError\n",
 | 
			
		||||
				"\n=========================== Direct Predecessor ===========================\nNo.\txPredicted\txAcutal\t\txError\n",
 | 
			
		||||
				"\n=========================== Differential Predecessor ===========================\nNo.\txPredicted\txAcutal\t\txError\n"
 | 
			
		||||
char * fileHeader(int id) {
 | 
			
		||||
	char * header[] = { (char*)"\n=========================== Local Mean ===========================\nNo.\txPredicted\txAcutal\t\txError\n",
 | 
			
		||||
		(char*)"\n=========================== Direct Predecessor ===========================\nNo.\txPredicted\txAcutal\t\txError\n",
 | 
			
		||||
		(char*)"\n=========================== Differential Predecessor ===========================\nNo.\txPredicted\txAcutal\t\txError\n"
 | 
			
		||||
	};
 | 
			
		||||
	return header[id];
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -484,7 +490,7 @@ Logs used weights to logfile - not used right now
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void weightsLogger (double *weights, int val ) {				
 | 
			
		||||
void weightsLogger(double *weights, int val) {
 | 
			
		||||
	char fileName[512];
 | 
			
		||||
	unsigned i;
 | 
			
		||||
	mkFileName(fileName, sizeof(fileName), val);
 | 
			
		||||
| 
						 | 
				
			
			@ -501,13 +507,13 @@ void weightsLogger (double *weights, int val ) {
 | 
			
		|||
bufferLogger
 | 
			
		||||
 | 
			
		||||
formats output of mkSvgGraph -- Please open graphResults.html to see the output--
 | 
			
		||||
	[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
 | 
			
		||||
[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
 | 
			
		||||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
| 
						 | 
				
			
			@ -594,14 +600,15 @@ parses template.svg and writes results in said template
 | 
			
		|||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void mkSvgGraph(point_t points[], char *templatePath) {
 | 
			
		||||
    FILE* input = NULL;
 | 
			
		||||
    FILE *target = fopen("graphResults.html", "w");
 | 
			
		||||
    if (templatePath){ 
 | 
			
		||||
        printf("\ngraph template src at: %s\n", templatePath);
 | 
			
		||||
        input = fopen(templatePath, "r");
 | 
			
		||||
    } else {
 | 
			
		||||
        input = fopen("graphResults_template.html", "r");
 | 
			
		||||
    }
 | 
			
		||||
	FILE* input = NULL;
 | 
			
		||||
	FILE *target = fopen("graphResults.html", "w");
 | 
			
		||||
	if (templatePath) {
 | 
			
		||||
		printf("\ngraph template src at: %s\n", templatePath);
 | 
			
		||||
		input = fopen(templatePath, "r");
 | 
			
		||||
	}
 | 
			
		||||
	else {
 | 
			
		||||
		input = fopen("graphResults_template.html", "r");
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	char line[512];
 | 
			
		||||
	char firstGraph[15] = { "<path d=\"M0 0" };			// Position where points will be written after
 | 
			
		||||
| 
						 | 
				
			
			@ -663,30 +670,30 @@ static imagePixel_t *rdPPM(char *fileName) {
 | 
			
		|||
		c = getc(fp);
 | 
			
		||||
	}
 | 
			
		||||
	ungetc(c, fp);
 | 
			
		||||
	if ( fscanf(fp, "%d %d", &image->x, &image->y) != 2 ) {
 | 
			
		||||
	if (fscanf(fp, "%d %d", &image->x, &image->y) != 2) {
 | 
			
		||||
		fprintf(stderr, "Invalid image size in %s\n", fileName);
 | 
			
		||||
		exit(EXIT_FAILURE);
 | 
			
		||||
	}
 | 
			
		||||
	if ( fscanf(fp, "%d", &rgbColor) != 1 ) {
 | 
			
		||||
	if (fscanf(fp, "%d", &rgbColor) != 1) {
 | 
			
		||||
		fprintf(stderr, "Invalid rgb component in %s\n", fileName);
 | 
			
		||||
	}
 | 
			
		||||
	if ( rgbColor != RGB_COLOR ) {
 | 
			
		||||
	if (rgbColor != RGB_COLOR) {
 | 
			
		||||
		fprintf(stderr, "Invalid image color range in %s\n", fileName);
 | 
			
		||||
		exit(EXIT_FAILURE);
 | 
			
		||||
	}
 | 
			
		||||
	while ( fgetc(fp) != '\n' );
 | 
			
		||||
	while (fgetc(fp) != '\n');
 | 
			
		||||
	image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t));
 | 
			
		||||
	if (!image) {
 | 
			
		||||
		fprintf(stderr, "malloc() on image->data failed");
 | 
			
		||||
		exit(EXIT_FAILURE);
 | 
			
		||||
	}
 | 
			
		||||
	if ( (image->x * image->y) < mlData->samplesCount) {
 | 
			
		||||
		printf("Changing \"-n\" to %d, image max data size\n", ( image->x * image->y ) );
 | 
			
		||||
		tmp = (double *) realloc ( xSamples, sizeof(double) * (image->x * image->y) );
 | 
			
		||||
	if ((image->x * image->y) < mlData->samplesCount) {
 | 
			
		||||
		printf("Changing \"-n\" to %d, image max data size\n", (image->x * image->y));
 | 
			
		||||
		tmp = (double *)realloc(xSamples, sizeof(double) * (image->x * image->y));
 | 
			
		||||
		xSamples = tmp;
 | 
			
		||||
		mlData->samplesCount = (image->x * image->y );
 | 
			
		||||
		mlData->samplesCount = (image->x * image->y);
 | 
			
		||||
	}
 | 
			
		||||
	if ( fread( image->data, 3 * image->x, image->y, fp) != image->y) {
 | 
			
		||||
	if (fread(image->data, 3 * image->x, image->y, fp) != image->y) {
 | 
			
		||||
		fprintf(stderr, "Loading image failed");
 | 
			
		||||
		exit(EXIT_FAILURE);
 | 
			
		||||
	}
 | 
			
		||||
| 
						 | 
				
			
			@ -730,23 +737,26 @@ int ppmColorChannel(FILE* fp, imagePixel_t *image, char *colorChannel, mldata_t
 | 
			
		|||
	unsigned i = 0;
 | 
			
		||||
 | 
			
		||||
	printf("colorChannel : %s\n", colorChannel);
 | 
			
		||||
	if ( image ) {											// RGB channel can be set through args from cli 
 | 
			
		||||
		if ( strcmp(colorChannel, "green") == 0 ){						
 | 
			
		||||
			for ( i = 0; i < mlData->samplesCount - 1; i++ ) {
 | 
			
		||||
				fprintf ( fp, "%d\n", image->data[i].green );
 | 
			
		||||
	if (image) {											// RGB channel can be set through args from cli 
 | 
			
		||||
		if (strcmp(colorChannel, "green") == 0) {
 | 
			
		||||
			for (i = 0; i < mlData->samplesCount - 1; i++) {
 | 
			
		||||
				fprintf(fp, "%d\n", image->data[i].green);
 | 
			
		||||
			}
 | 
			
		||||
		} else if ( strcmp(colorChannel, "red") == 0 ){				
 | 
			
		||||
			for ( i = 0; i < mlData->samplesCount - 1; i++ ) {
 | 
			
		||||
				fprintf ( fp, "%d\n", image->data[i].red );
 | 
			
		||||
		}
 | 
			
		||||
		else if (strcmp(colorChannel, "red") == 0) {
 | 
			
		||||
			for (i = 0; i < mlData->samplesCount - 1; i++) {
 | 
			
		||||
				fprintf(fp, "%d\n", image->data[i].red);
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
		} else if ( strcmp(colorChannel, "blue") == 0 ) {
 | 
			
		||||
			for ( i = 0; i < mlData->samplesCount - 1; i++ ) {
 | 
			
		||||
				fprintf ( fp, "%d\n", image->data[i].blue );	
 | 
			
		||||
		}
 | 
			
		||||
		else if (strcmp(colorChannel, "blue") == 0) {
 | 
			
		||||
			for (i = 0; i < mlData->samplesCount - 1; i++) {
 | 
			
		||||
				fprintf(fp, "%d\n", image->data[i].blue);
 | 
			
		||||
			}
 | 
			
		||||
		} else { 
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			printf("Colorchannels are red, green and blue. Pick one of them!");
 | 
			
		||||
			exit( EXIT_FAILURE );
 | 
			
		||||
			exit(EXIT_FAILURE);
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	fclose(fp);
 | 
			
		||||
| 
						 | 
				
			
			@ -763,9 +773,9 @@ creating the SVG graph
 | 
			
		|||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void colorSamples ( FILE* fp, mldata_t *mlData ) {
 | 
			
		||||
void colorSamples(FILE* fp, mldata_t *mlData) {
 | 
			
		||||
	int i = 0;
 | 
			
		||||
	char *buffer = (char *) malloc(sizeof(char) * mlData->samplesCount + 1);
 | 
			
		||||
	char *buffer = (char *)malloc(sizeof(char) * mlData->samplesCount + 1);
 | 
			
		||||
 | 
			
		||||
	while (!feof(fp)) {
 | 
			
		||||
		if (fgets(buffer, mlData->samplesCount, fp) != NULL) {
 | 
			
		||||
| 
						 | 
				
			
			@ -792,7 +802,7 @@ double windowXMean(int _arraylength, int xCount) {
 | 
			
		|||
	double *ptr;
 | 
			
		||||
 | 
			
		||||
	for (ptr = &xSamples[xCount - _arraylength]; ptr != &xSamples[xCount]; ptr++) { 	// Set ptr to beginning of window and iterate through array
 | 
			
		||||
        sum += *ptr;
 | 
			
		||||
		sum += *ptr;
 | 
			
		||||
	}
 | 
			
		||||
	return sum / (double)_arraylength;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -800,13 +810,13 @@ double windowXMean(int _arraylength, int xCount) {
 | 
			
		|||
/*
 | 
			
		||||
======================================================================================================
 | 
			
		||||
 | 
			
		||||
 usage 
 | 
			
		||||
usage
 | 
			
		||||
 | 
			
		||||
 used in conjunction with the args parser. Returns help section of "-h"
 | 
			
		||||
used in conjunction with the args parser. Returns help section of "-h"
 | 
			
		||||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
void usage ( char **argv ) {
 | 
			
		||||
void usage(char **argv) {
 | 
			
		||||
	printf("Usage: %s [POSIX style options] -i file ...\n", &argv[0][0]);
 | 
			
		||||
	printf("POSIX options:\n");
 | 
			
		||||
	printf("\t-h\t\t\tDisplay this information.\n");
 | 
			
		||||
| 
						 | 
				
			
			@ -825,18 +835,18 @@ void usage ( char **argv ) {
 | 
			
		|||
/*
 | 
			
		||||
======================================================================================================
 | 
			
		||||
 | 
			
		||||
 init_mldata_t 
 | 
			
		||||
init_mldata_t
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 Init meachine learning data
 | 
			
		||||
Init meachine learning data
 | 
			
		||||
 | 
			
		||||
======================================================================================================
 | 
			
		||||
*/
 | 
			
		||||
mldata_t * init_mldata_t(unsigned windowSize, unsigned samplesCount, double learnrate) {
 | 
			
		||||
	mlData = (mldata_t *) malloc( sizeof(mldata_t) );
 | 
			
		||||
	mlData = (mldata_t *)malloc(sizeof(mldata_t));
 | 
			
		||||
	mlData->windowSize = windowSize;
 | 
			
		||||
	mlData->samplesCount = samplesCount;
 | 
			
		||||
	mlData->learnrate = learnrate;
 | 
			
		||||
	mlData->weights = (double *) malloc ( sizeof(double) * windowSize  + 1 );
 | 
			
		||||
	mlData->weights = (double *)malloc(sizeof(double) * windowSize + 1);
 | 
			
		||||
	return mlData;
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue