added stuff

This commit is contained in:
Friese 2018-05-11 14:19:20 +02:00
parent 66177c97da
commit e73cfda76c
1 changed files with 68 additions and 36 deletions

View File

@ -32,7 +32,8 @@ typedef SSIZE_T ssize_t;
//double x[] = { 0.0 }; //double x[] = { 0.0 };
double xSamples[NUMBER_OF_SAMPLES] = { 0.0 }; double xSamples[NUMBER_OF_SAMPLES] = { 0.0 };
double w[WINDOWSIZE][NUMBER_OF_SAMPLES] = { { 0.0 },{ 0.0 } };
/* *svg graph building* */ /* *svg graph building* */
typedef struct { typedef struct {
@ -69,14 +70,16 @@ double rndm(void);
/* *math* */ /* *math* */
double sum_array(double x[], int length); double sum_array(double x[], int length);
void directPredecessor(void); void directPredecessor(double localweights[WINDOWSIZE][NUMBER_OF_SAMPLES]);
void localMean(void); void localMean(double localweights[WINDOWSIZE][NUMBER_OF_SAMPLES]);
void differentialPredecessor(void); void differentialPredecessor(double localweights[WINDOWSIZE][NUMBER_OF_SAMPLES]);
double *popNAN(double *xError, int xErrorLength); //return new array without NAN values double *popNAN(double *xError, int xErrorLength); //return new array without NAN values
double windowXMean(int _arraylength, int xCount); double windowXMean(int _arraylength, int xCount);
int main(int argc, char **argv) { int main(int argc, char **argv) {
double w[WINDOWSIZE][NUMBER_OF_SAMPLES] = { { 0.0 },{ 0.0 } };
double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES];
char fileName[50]; char fileName[50];
int i, k, xLength; int i, k, xLength;
int *colorChannel; int *colorChannel;
@ -113,9 +116,26 @@ int main(int argc, char **argv) {
// math magic // math magic
localMean(); for (i = 0; i < NUMBER_OF_SAMPLES; i++){
//directPredecessor(); for (k = 0; k < WINDOWSIZE; k++){
//differentialPredecessor(); local_weights[k][i] = w[k][i];
}
}
localMean(local_weights);
for (i = 0; i < NUMBER_OF_SAMPLES; i++){
for (k = 0; k < WINDOWSIZE; k++){
local_weights[k][i] = w[k][i];
}
}
//directPredecessor(local_weights);
for (i = 0; i < NUMBER_OF_SAMPLES; i++){
for (k = 0; k < WINDOWSIZE; k++){
local_weights[k][i] = w[k][i];
}
}
//differentialPredecessor(local_weights);
mkSvgGraph(points);
// save test_array after math magic happened // save test_array after math magic happened
// memset( fileName, '\0', sizeof(fileName) ); // memset( fileName, '\0', sizeof(fileName) );
mkFileName(fileName, sizeof(fileName), USED_WEIGHTS); mkFileName(fileName, sizeof(fileName), USED_WEIGHTS);
@ -144,9 +164,10 @@ Variant (1/3), substract local mean.
====================================================================================================== ======================================================================================================
*/ */
void localMean(void) { void localMean(double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) {
char fileName[50]; char fileName[50];
double xError[NUMBER_OF_SAMPLES]; // includes e(n) double xError[2048]; // includes e(n)
memset(xError, 0.0, NUMBER_OF_SAMPLES);// initialize xError-array with Zero memset(xError, 0.0, NUMBER_OF_SAMPLES);// initialize xError-array with Zero
int xCount = 0, i; // runtime var; int xCount = 0, i; // runtime var;
mkFileName(fileName, sizeof(fileName), LOCAL_MEAN); mkFileName(fileName, sizeof(fileName), LOCAL_MEAN);
@ -173,7 +194,7 @@ void localMean(void) {
// weightedSum += _x[ xCount-1 ] * w[xCount][0]; // weightedSum += _x[ xCount-1 ] * w[xCount][0];
for (i = 1; i < _arrayLength; i++) { //get predicted value for (i = 1; i < _arrayLength; i++) { //get predicted value
xPredicted += (w[i][xCount] * (xSamples[xCount - i] - xMean)); xPredicted += (local_weights[i][xCount] * (xSamples[xCount - i] - xMean));
} }
xPredicted += xMean; xPredicted += xMean;
@ -195,13 +216,13 @@ void localMean(void) {
} }
//printf("%f\n", xSquared); //printf("%f\n", xSquared);
for (i = 1; i < _arrayLength; i++) { //update weights for (i = 1; i < _arrayLength; i++) { //update weights
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xMean) / xSquared); local_weights[i][xCount + 1] = local_weights[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]); fprintf(fp4, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
} }
int xErrorLength = sizeof(xError) / sizeof(xError[0]); /* int xErrorLength = sizeof(xError) / sizeof(xError[0]);
printf("vor:%d", xErrorLength); printf("vor:%d", xErrorLength);
popNAN(xError, xErrorLength); popNAN(xError, xErrorLength);
printf("nach:%d", xErrorLength); printf("nach:%d", xErrorLength);
@ -215,13 +236,14 @@ void localMean(void) {
} }
deviation /= xErrorLength; deviation /= xErrorLength;
// write in file // write in file
mkFileName(fileName, sizeof(fileName), RESULTS); mkFileName(fileName, sizeof(fileName), RESULTS);
FILE *fp2 = fopen(fileName, "w"); FILE *fp2 = fopen(fileName, "w");
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean); fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
fclose(fp2); fclose(fp2);
fclose(fp4); fclose(fp4);
*/
//mkSvgGraph(points);
} }
/* /*
@ -235,29 +257,30 @@ substract direct predecessor
====================================================================================================== ======================================================================================================
*/ */
void directPredecessor(void) { void directPredecessor(double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) {
char fileName[512]; char fileName[512];
double xError[2048]; double xError[2048];
int xCount = 0, i; int xCount = 0, i;
double xActual; double xActual = 0.0;
int xPredicted = 0.0; int xPredicted = 0.0;
// File handling // File handling
mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR); mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);
FILE *fp3 = fopen(fileName, "w"); FILE *fp3 = fopen(fileName, "w");
fprintf(fp3, "\n=====================================DirectPredecessor=====================================\n"); fprintf(fp3, "\n=====================================DirectPredecessor=====================================\n");
for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) {
//double xPartArray[xCount]; //includes all values at the size of runtime var for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted
//double xPartArray[1000]; //includes all values at the size of runtime var
//int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount; //int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount;
int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount; int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount;
printf("xCount:%d, length:%d\n", xCount, _arrayLength); //printf("xCount:%d, length:%d\n", xCount, _arrayLength);
double xMean = (xCount > 0) ? windowXMean(_arrayLength, xCount) : 0; // printf("WINDOWSIZE:%f\n", windowXMean(_arrayLength, xCount));
printf("%f\n", windowXMean(_arrayLength, xCount));
xPredicted = 0.0; xPredicted = 0.0;
xActual = xSamples[xCount + 1]; xActual = xSamples[xCount + 1];
// weightedSum += _x[ xCount-1 ] * w[xCount][0];
for (i = 1; i < _arrayLength; i++) { for (i = 1; i < _arrayLength; i++) {
xPredicted += (w[i][xCount] * (xSamples[xCount - 1] - xSamples[xCount - i - 1])); xPredicted += (local_weights[i][xCount] * (xSamples[xCount - 1] - xSamples[xCount - i - 1]));
} }
xPredicted += xSamples[xCount - 1]; xPredicted += xSamples[xCount - 1];
xError[xCount] = xActual - xPredicted; xError[xCount] = xActual - xPredicted;
@ -274,10 +297,10 @@ void directPredecessor(void) {
xSquared += pow(xSamples[xCount - 1] - xSamples[xCount - i - 1], 2); // substract direct predecessor xSquared += pow(xSamples[xCount - 1] - xSamples[xCount - i - 1], 2); // substract direct predecessor
} }
for (i = 1; i < _arrayLength; i++) { for (i = 1; i < _arrayLength; i++) {
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared); local_weights[i][xCount + 1] = local_weights[i][xCount] + learnrate * xError[xCount] * ( (xSamples[xCount - 1] - xSamples[xCount - i - 1]) / xSquared);
} }
} }
/*
int xErrorLength = sizeof(xError) / sizeof(xError[0]); int xErrorLength = sizeof(xError) / sizeof(xError[0]);
printf("vor:%d", xErrorLength); printf("vor:%d", xErrorLength);
popNAN(xError, xErrorLength); popNAN(xError, xErrorLength);
@ -291,8 +314,9 @@ void directPredecessor(void) {
} }
deviation /= xErrorLength; deviation /= xErrorLength;
mkSvgGraph(points); // mkSvgGraph(points);
fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean); fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
*/
fclose(fp3); fclose(fp3);
} }
@ -307,24 +331,26 @@ differenital predecessor.
====================================================================================================== ======================================================================================================
*/ */
void differentialPredecessor(void) { void differentialPredecessor(double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES]) {
char fileName[512]; char fileName[512];
double xError[2048]; double xError[2048];
int xCount = 0, i; int xCount = 0, i;
double xActual; double xPredicted = 0.0;
double xActual = 0.0;
// File handling // File handling
mkFileName(fileName, sizeof(fileName), DIFFERENTIAL_PREDECESSOR); mkFileName(fileName, sizeof(fileName), DIFFERENTIAL_PREDECESSOR);
FILE *fp6 = fopen(fileName, "w"); FILE *fp6 = fopen(fileName, "w");
fprintf(fp6, "\n=====================================DifferentialPredecessor=====================================\n"); fprintf(fp6, "\n=====================================DifferentialPredecessor=====================================\n");
for (xCount = 1; xCount < NUMBER_OF_SAMPLES + 1; xCount++) { for (xCount = 1; xCount < NUMBER_OF_SAMPLES; xCount++) { // first value will not get predicted
xActual = xSamples[xCount + 1];
int _arrayLength = (xCount > WINDOWSIZE) ? WINDOWSIZE + 1 : xCount;
double xPredicted = 0.0; double xPredicted = 0.0;
for (i = 1; i < xCount; i++) { for (i = 1; i < _arrayLength; i++) {
xPredicted += (w[i][xCount] * (xSamples[xCount - i] - xSamples[xCount - i - 1])); xPredicted += (local_weights[i][xCount] * (xSamples[xCount - i] - xSamples[xCount - i - 1]));
} }
xPredicted += xSamples[xCount - 1]; xPredicted += xSamples[xCount - 1];
xError[xCount] = xActual - xPredicted; xError[xCount] = xActual - xPredicted;
@ -336,14 +362,19 @@ void differentialPredecessor(void) {
points[xCount].yVal[6] = xError[xCount]; points[xCount].yVal[6] = xError[xCount];
double xSquared = 0.0; double xSquared = 0.0;
for (i = 1; i < xCount; i++) { for (i = 1; i < _arrayLength; i++) {
xSquared += pow(xSamples[xCount - i] - xSamples[xCount - i - 1], 2); // substract direct predecessor xSquared += pow(xSamples[xCount - i] - xSamples[xCount - i - 1], 2); // substract direct predecessor
} }
for (i = 1; i < xCount; i++) { for (i = 1; i < _arrayLength; i++) {
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xSamples[xCount - i - 1]) / xSquared); local_weights[i][xCount + 1] = local_weights[i][xCount] + learnrate * xError[xCount] * ((xSamples[xCount - i] - xSamples[xCount - i - 1]) / xSquared);
} }
} }
/*
int xErrorLength = sizeof(xError) / sizeof(xError[0]); 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 mean = sum_array(xError, xErrorLength) / xErrorLength;
double deviation = 0.0; double deviation = 0.0;
@ -352,8 +383,9 @@ void differentialPredecessor(void) {
} }
deviation /= xErrorLength; deviation /= xErrorLength;
mkSvgGraph(points); //mkSvgGraph(points);
fprintf(fp6, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean); fprintf(fp6, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
*/
fclose(fp6); fclose(fp6);
@ -530,8 +562,8 @@ parses template.svg and writes results in said template
*/ */
void mkSvgGraph(point_t points[]) { void mkSvgGraph(point_t points[]) {
FILE *input = fopen("template.svg", "r"); FILE *input = fopen("GraphResults_template.html", "r");
FILE *target = fopen("output.svg", "w"); FILE *target = fopen("GraphResults.html", "w");
char line[512]; char line[512];
char firstGraph[15] = { "<path d=\"M0 0" }; char firstGraph[15] = { "<path d=\"M0 0" };