added stuff

This commit is contained in:
Friese 2018-05-11 11:30:17 +02:00
parent 2b1469f55e
commit 0a8a81677c
1 changed files with 307 additions and 170 deletions

View File

@ -3,7 +3,7 @@
// NLMSvariants.c
//
// Created by FBRDNLMS on 26.04.18.
// Copyright © 2018 FBRDNLMS. All rights reserved.
//
//
#include <stdio.h>
@ -13,24 +13,26 @@
#include <string.h>
#include <float.h> // DBL_MAX
#define M 1000
#define NUMBER_OF_SAMPLES 1000
#define WINDOWSIZE 5
#define tracking 40 //Count of weights
#define learnrate 1.0
#define learnrate 0.8
#define PURE_WEIGHTS 0
#define USED_WEIGHTS 1
#define RESULTS 3
#define DIRECT_PREDECESSOR 2
#define LOCAL_MEAN 4
#define TEST_VALUES 5
#define DIFFERENTIAL_PREDECESSOR 6
#define RGB_COLOR 255
#if defined(_MSC_VER)
#include <BaseTsd.h>
typedef SSIZE_T ssize_t;
#endif
double x[] = { 0 };
double _x[M] = { 0 };
double w[M][M] = { { 0 },{ 0 } };
//double x[] = { 0.0 };
double xSamples[NUMBER_OF_SAMPLES] = { 0.0 };
double w[WINDOWSIZE][NUMBER_OF_SAMPLES] = { { 0.0 },{ 0.0 } };
/* *svg graph building* */
typedef struct {
@ -38,9 +40,9 @@ typedef struct {
double yVal[7];
}point_t;
point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean
point_t points[NUMBER_OF_SAMPLES]; // [0] = xActual, [1]=xpredicted from localMean, [2]=xpredicted from directPredecessor, [3] = xpredicted from differentialpredecessor, [4] = xError from localMean, [5] xError from directPredecessor, [6] xError from differentialPredecessor
/* *ppm reader/writer* */
/* *ppm read, copy, write* */
typedef struct {
unsigned char red, green, blue;
}colorChannel_t;
@ -53,7 +55,7 @@ typedef struct {
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 ppmTo_X( FILE* fp ); // stores color channel values in _x[]
void colorSamples( FILE* fp ); // stores color channel values in xSamples[]
/* *file handling* */
char * mkFileName(char* buffer, size_t max_len, int suffixId);
@ -69,30 +71,32 @@ double rndm(void);
double sum_array(double x[], int length);
void directPredecessor(void);
void localMean(void);
void differentialPredecessor( void );
double *popNAN(double *xError, int xErrorLength); //return new array without NAN values
double windowXMean( int _arraylength, int xCount );
int main(int argc, char **argv) {
char fileName[50];
int i, xLength;
int i, k, xLength;
int *colorChannel;
imagePixel_t *image;
image = rdPPM("beaches.ppm");
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");
ppmTo_X ( fp6 );
colorSamples ( fp6 );
srand((unsigned int)time(NULL));
for (i = 0; i < M; i++) {
for (i = 0; i < NUMBER_OF_SAMPLES; i++) {
// _x[i] += ((255.0 / M) * i); // Init test values
for (int k = 0; k < M; k++) {
for (int k = 0; k < WINDOWSIZE; k++) {
w[k][i]= rndm(); // Init weights
}
}
@ -101,7 +105,7 @@ int main(int argc, char **argv) {
// 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++) {
for ( k = 0; k < WINDOWSIZE; k++) {
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
}
}
@ -110,14 +114,14 @@ int main(int argc, char **argv) {
// math magic
localMean();
directPredecessor(); // TODO: used_weights.txt has gone missing!
//directPredecessor();
//differentialPredecessor();
// 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++) {
for (int k = 0; k < WINDOWSIZE; k++) {
fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]);
}
@ -131,62 +135,83 @@ int main(int argc, char **argv) {
/*
=======================================================================================
======================================================================================================
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;
double xError[NUMBER_OF_SAMPLES]; // includes e(n)
memset(xError, 0.0, NUMBER_OF_SAMPLES);// initialize xError-array with Zero
int xCount = 0, i; // runtime var;
mkFileName(fileName, sizeof(fileName), LOCAL_MEAN);
FILE* fp4 = fopen(fileName, "w");
fprintf(fp4, "\n\n\n\n*********************LocalMean*********************\n");
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
fprintf(fp4, "\n=====================================LocalMean=====================================\n");
double xMean = xSamples[0];
double weightedSum = 0.0;
double filterOutput = 0.0;
double xSquared = 0.0;
double xPredicted = 0.0;
double xActual = _x[xCount + 1];
double xActual = 0.0;
for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted
double xPartArray[xCount]; //includes all values at the size of runtime var
//int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount;
int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount;
//printf("xCount:%d, length:%d\n", xCount, _arrayLength);
xMean = ( xCount > 0 ) ? windowXMean(_arrayLength, xCount) : 0;
// 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++) { //get predicted value
xPredicted += (w[i][xCount] * ( xSamples[xCount - i] - xMean));
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;
printf("Pred: %f\t\tActual:%f\n", xPredicted,xActual);
points[xCount].xVal[1] = xCount;
points[xCount].yVal[1] = xPredicted;
points[xCount].xVal[4] = xCount;
points[xCount].yVal[4] = xError[xCount];
for (i = 1; i < xCount; i++) { //get x squared
xSquared = +pow(_x[xCount - i], 2);
xSquared = 0.0;
for (i = 1; i < _arrayLength; i++) { //get xSquared
//xSquared += pow(xSamples[xCount - i], 2);
xSquared += pow(xSamples[xCount - i] - xMean, 2);
printf("xSquared:%f\n", xSquared);
}
for (i - 1; i < xCount; i++) { //update weights
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared);
if(xSquared == 0.0){ // returns Pred: -1.#IND00
xSquared = 1.0;
}
//printf("%f\n", xSquared);
for (i = 1; i < _arrayLength; i++) { //update weights
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ( (xSamples[xCount - i] - xMean) / xSquared);
}
fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
}
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
double mean = sum_array(xError, xErrorLength) / M;
printf("vor:%d", xErrorLength);
popNAN(xError, xErrorLength);
printf("nach:%d", xErrorLength);
xErrorLength = sizeof(xError) / sizeof(xError[0]);
double mean = sum_array(xError, xErrorLength) / xErrorLength;
double deviation = 0.0;
// Mean square
for (i = 0; i < M - 1; i++) {
deviation += pow(xError[i], 2);
for (i = 0; i < xErrorLength - 1; i++) {
deviation += pow(xError[i] - mean, 2);
}
deviation /= xErrorLength;
@ -199,17 +224,15 @@ void localMean(void) {
fclose(fp4);
}
/*
===================================
======================================================================================================
directPredecessor
Variant (2/3),
substract direct predecessor
===================================
======================================================================================================
*/
void directPredecessor(void) {
@ -217,46 +240,56 @@ void directPredecessor(void) {
double xError[2048];
int xCount = 0, i;
double xActual;
int xPredicted = 0.0;
// File handling
mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);
FILE *fp3 = fopen(fileName, "w");
fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\n");
fprintf(fp3, "\n=====================================DirectPredecessor=====================================\n");
for (xCount = 1; xCount < M + 1; xCount++) {
xActual = _x[xCount + 1];
double xPredicted = 0.0;
for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) {
double xPartArray[xCount]; //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);
double xMean = ( xCount > 0 ) ? windowXMean(_arrayLength, xCount) : 0;
printf("%f\n", windowXMean(_arrayLength, xCount));
xPredicted = 0.0;
xActual = xSamples[xCount + 1];
for (i = 1; i < xCount; i++) {
xPredicted += (w[i][xCount] * (_x[xCount - i] - _x[xCount - i - 1]));
for (i = 1; i < _arrayLength; i++) {
xPredicted += (w[i][xCount] * (xSamples[xCount - 1] - xSamples[xCount - i - 1]));
}
xPredicted += _x[xCount - 1];
xPredicted += xSamples[xCount - 1];
xError[xCount] = xActual - xPredicted;
fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
points[xCount].xVal[0] = xCount;
points[xCount].yVal[0] = xActual;
points[xCount].xVal[1] = xCount;
points[xCount].yVal[1] = xPredicted;
points[xCount].xVal[2] = xCount;
points[xCount].yVal[2] = xPredicted;
points[xCount].xVal[5] = xCount;
points[xCount].yVal[5] = xError[xCount];
double xSquared = 0.0;
for (i = 1; i < xCount; i++) {
xSquared += pow(_x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor
for (i = 1; i < _arrayLength; i++) {
xSquared += pow(xSamples[xCount - 1] - xSamples[xCount - i - 1], 2); // substract direct predecessor
}
for (i = 1; i < xCount; i++) {
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((_x[xCount - i] - _x[xCount - i - 1]) / xSquared); //TODO: double val out of bounds
for (i = 1; i < _arrayLength; i++) {
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared);
}
}
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
printf("vor:%d", xErrorLength);
popNAN(xError, xErrorLength);
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(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
@ -264,19 +297,80 @@ void directPredecessor(void) {
}
/*
======================================================================================================
differentialPredecessor
variant (3/3),
differenital predecessor.
======================================================================================================
*/
void differentialPredecessor ( void ) {
char fileName[512];
double xError[2048];
int xCount = 0, i;
double xActual;
// File handling
mkFileName(fileName, sizeof(fileName), DIFFERENTIAL_PREDECESSOR);
FILE *fp6 = fopen(fileName, "w");
fprintf(fp6, "\n=====================================DifferentialPredecessor=====================================\n");
for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) {
xActual = xSamples[xCount + 1];
double xPredicted = 0.0;
for (i = 1; i < xCount; i++) {
xPredicted += (w[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 < xCount; i++) {
xSquared += pow(xSamples[xCount - i] - xSamples[xCount - i - 1], 2); // substract direct predecessor
}
for (i = 1; i < xCount; i++) {
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xSamples[xCount - i - 1]) / xSquared);
}
}
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);
}
deviation /= xErrorLength;
mkSvgGraph(points);
fprintf(fp6, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
fclose(fp6);
}
/*
=========================================================================
======================================================================================================
mkFileName
Writes the current date plus the suffix with index suffixId
into the given buffer. If[M ?K the total length is longer than max_len,
into the given buffer. If the total length is longer than max_len,
only max_len characters will be written.
=========================================================================
======================================================================================================
*/
char *mkFileName(char* buffer, size_t max_len, int suffixId) {
@ -292,100 +386,115 @@ char *mkFileName(char* buffer, size_t max_len, int suffixId) {
}
/*
=========================================================================
======================================================================================================
fileSuffix
Contains and returns every suffix for all existing filenames
==========================================================================
======================================================================================================
*/
char * fileSuffix(int id) {
char * suffix[] = { "_weights_pure.txt", "_weights_used.txt", "_direct_predecessor.txt", "_ergebnisse.txt", "_localMean.txt","_testvalues.txt" };
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
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<path d=\"M0 0\n");
for (i = 0; i < M - 1; i++) { // xPred from directPredecessor
fprintf(fp, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
}
fprintf(fp, "\" fill=\"none\" stroke=\"green\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i <= M; i++) { //xPred from lastMean
fprintf(fp, "L %f %f\n", points[i].xVal[2], points[i].yVal[2]);
}
}
======================================================================================================
*/
void bufferLogger(char *buffer, point_t points[]) {
int i;
char _buffer[512] = "";
for (i = 0; i <= M; i++) { // xActual
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\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i < M - 1; i++) { // xPred from directPredecessor
strcat(buffer, "\" fill=\"none\" stroke=\"black\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++) { // xPrediceted from localMean
sprintf(_buffer, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
strcat(buffer, _buffer);
}
strcat(buffer, "\" fill=\"none\" stroke=\"green\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i <= M; i++) { //xPred from lastMean
for (i = 0; i <= NUMBER_OF_SAMPLES - 1; i++) { //xPreddicted from directPredecessor
sprintf(_buffer, "L %f %f\n", points[i].xVal[2], points[i].yVal[2]);
strcat(buffer, _buffer);
}
strcat(buffer, "\" fill=\"none\" stroke=\"blue\" stroke-width=\"0.4px\"/>\n<path d=\"M0 0\n");
for (i = 0; i < NUMBER_OF_SAMPLES - 1; i++ ){ //xPredicted from diff Pred
sprintf(_buffer, "L %f %f\n", points[i].xVal[3], points[i].xVal[3]);
strcat(buffer, _buffer);
}
}
/*
=========================================================================
======================================================================================================
sum_array
Sum of all elements in x within a defined length
=========================================================================
======================================================================================================
*/
double sum_array(double x[], int length) {
double sum_array(double x[], int xlength) {
int i = 0;
double sum = 0.0;
for (i = 0; i< length; i++) {
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 xErrorLength ) {
int i, counter;
double noNAN [xErrorLength];
for ( i = 0; i < xErrorLength; i++) {
if ( !isnan(xError[i]) ) {
noNAN[i] = xError[i];
counter++;
}
}
realloc(noNAN, counter * sizeof(double));
int noNANLength = sizeof(noNAN)/ sizeof(noNAN[0]);
memcpy(xError, noNAN, noNANLength);
return xError;
}
/*
======================================================================================================
r2
returns a random double value between 0 and 1
==========================================================================
======================================================================================================
*/
double r2(void) {
@ -393,15 +502,14 @@ double r2(void) {
}
/*
==========================================================================
======================================================================================================
rndm
fills a double variable with random value and returns it
==========================================================================
======================================================================================================
*/
double rndm(void) {
@ -410,15 +518,14 @@ double rndm(void) {
}
/*
==========================================================================
======================================================================================================
mkSvgGraph
parses template.svg and writes results in said template
==========================================================================
======================================================================================================
*/
void mkSvgGraph(point_t points[]) {
@ -448,16 +555,15 @@ void mkSvgGraph(point_t points[]) {
}
/*
===========================================================================
======================================================================================================
rdPPM
reads data from file of type PPM, stores colorchannels in a struct in the
size of given picture
==========================================================================
======================================================================================================
*/
static imagePixel_t *rdPPM(char *fileName) {
@ -513,16 +619,15 @@ static imagePixel_t *rdPPM(char *fileName) {
}
/*
=======================================================================================
======================================================================================================
mkPpmFile
gets output from the result of rdPpmFile and writes a new mkPpmFile. Best Case is a
carbon copy of the source image
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) {
@ -538,42 +643,74 @@ void mkPpmFile(char *fileName, imagePixel_t *image) {
fclose(fp);
}
/*
======================================================================================
======================================================================================================
ppmColorChannel
gets one of the rgb color channels and returns the array
gets one of the rgb color channels and writes them to a file
======================================================================================
======================================================================================================
*/
int ppmColorChannel(FILE* fp, imagePixel_t *image) {
int length = 1000; // (image->x * image->y) / 3;
// int length = 1000; // (image->x * image->y) / 3;
int i = 0;
if (image) {
for ( i = 0; i <= length; i++ ){
for ( i = 0; i < NUMBER_OF_SAMPLES - 1; i++ ){
fprintf(fp,"%d\n", image->data[i].green);
}
}
fclose(fp);
return length;
return NUMBER_OF_SAMPLES;
}
void ppmTo_X( FILE* fp ) {
/*
======================================================================================================
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;
int d, out;
double f;
int length = 1000;
char buffer[length];
char buffer[NUMBER_OF_SAMPLES];
while ( !feof(fp) ) {
if ( fgets(buffer, length, fp) != NULL ) {
sscanf(buffer,"%lf", &_x[i]);
printf("%lf\n", _x[i] );
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);
}
double windowXMean (int _arraylength, int xCount) {
int count;
double sum = 0.0;
double *ptr;
// printf("*window\t\t*base\t\txMean\n\n");
for ( ptr = &xSamples[xCount - _arraylength]; ptr != &xSamples[xCount]; ptr++) { //set ptr to beginning of window
//window = xCount - _arraylength
//base = window - _arraylength;
//sum = 0.0;
//for( count = 0; count < _arraylength; count++){
sum += *ptr;
// printf("%f\n", *base);
//}
}
//printf("\n%lf\t%lf\t%lf\n", *ptr, *ptr2, (sum/(double)WINDOW));
return sum/(double)_arraylength;
}