2018-04-26 16:39:47 +02:00
//
2018-05-11 11:30:17 +02:00
//
2018-05-02 09:44:53 +02:00
// NLMSvariants.c
2018-04-26 16:39:47 +02:00
//
2018-04-30 14:38:28 +02:00
// Created by FBRDNLMS on 26.04.18.
2018-05-11 11:30:17 +02:00
//
2018-04-26 16:39:47 +02:00
//
# include <stdio.h>
# include <math.h>
# include <time.h>
# include <stdlib.h>
# include <string.h>
2018-05-03 18:30:42 +02:00
# include <float.h> // DBL_MAX
2018-04-26 16:39:47 +02:00
2018-05-13 21:41:44 +02:00
# define NUMBER_OF_SAMPLES 500
2018-05-11 11:30:17 +02:00
# define WINDOWSIZE 5
# define learnrate 0.8
2018-05-07 00:18:25 +02:00
# define RGB_COLOR 255
2018-05-07 15:36:46 +02:00
# if defined(_MSC_VER)
# include <BaseTsd.h>
typedef SSIZE_T ssize_t ;
# endif
2018-05-07 15:49:17 +02:00
2018-05-14 19:25:53 +02:00
enum fileSuffix_t { // used in conjunction with mkFileName()
PURE_WEIGHTS ,
USED_WEIGHTS ,
DIRECT_PREDECESSOR ,
RESULTS ,
LOCAL_MEAN ,
TEST_VALUES ,
DIFFERENTIAL_PREDECESSOR
} ;
2018-05-11 11:30:17 +02:00
double xSamples [ NUMBER_OF_SAMPLES ] = { 0.0 } ;
2018-05-11 14:19:20 +02:00
2018-05-07 00:18:25 +02:00
/* *svg graph building* */
2018-05-07 15:36:46 +02:00
typedef struct {
2018-05-04 16:30:12 +02:00
double xVal [ 7 ] ;
double yVal [ 7 ] ;
} point_t ;
2018-05-11 11:30:17 +02:00
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
2018-05-04 16:30:12 +02:00
2018-05-14 16:06:03 +02:00
/* *ppm read, copy, write* */
2018-05-07 00:18:25 +02:00
typedef struct {
unsigned char red , green , blue ;
} colorChannel_t ;
typedef struct {
2018-05-07 15:36:46 +02:00
int x , y ;
2018-05-07 00:18:25 +02:00
colorChannel_t * data ;
} imagePixel_t ;
2018-05-07 22:39:31 +02:00
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
2018-05-11 12:00:37 +02:00
void colorSamples ( FILE * fp ) ; // stores color channel values in xSamples[]
2018-05-07 00:18:25 +02:00
2018-05-13 21:41:44 +02:00
/* *file handling* */
2018-05-07 15:36:46 +02:00
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 [ ] ) ;
2018-05-13 21:41:44 +02:00
//void weightsLogger(double *weights, int var);
2018-05-04 01:28:36 +02:00
/* *rand seed* */
2018-05-07 15:36:46 +02:00
double r2 ( void ) ;
double rndm ( void ) ;
2018-05-03 18:30:42 +02:00
2018-05-04 01:28:36 +02:00
/* *math* */
2018-05-07 15:36:46 +02:00
double sum_array ( double x [ ] , int length ) ;
2018-05-13 21:41:44 +02:00
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
2018-05-11 12:00:37 +02:00
double windowXMean ( int _arraylength , int xCount ) ;
2018-05-07 15:36:46 +02:00
2018-05-13 21:41:44 +02:00
//int main(int argc, char **argv) {
int main ( void ) {
2018-05-14 16:06:03 +02:00
double weights [ WINDOWSIZE ] [ NUMBER_OF_SAMPLES ] ; // = { { 0.0 }, {0.0} };
2018-05-13 21:41:44 +02:00
// double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES];
2018-05-07 15:36:46 +02:00
char fileName [ 50 ] ;
2018-05-14 13:54:56 +02:00
int i , k , xLength ;
2018-05-07 17:10:43 +02:00
imagePixel_t * image ;
2018-05-14 13:54:56 +02:00
image = rdPPM ( " cow.ppm " ) ;
2018-05-07 22:39:31 +02:00
mkFileName ( fileName , sizeof ( fileName ) , TEST_VALUES ) ;
FILE * fp5 = fopen ( fileName , " w " ) ;
xLength = ppmColorChannel ( fp5 , image ) ;
2018-05-11 11:30:17 +02:00
printf ( " %d \n " , xLength ) ;
2018-05-07 22:39:31 +02:00
FILE * fp6 = fopen ( fileName , " r " ) ;
2018-05-11 12:00:37 +02:00
colorSamples ( fp6 ) ;
2018-05-07 15:36:46 +02:00
2018-05-14 18:59:05 +02:00
srand ( ( unsigned int ) time ( NULL ) ) ;
2018-05-11 11:30:17 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES ; i + + ) {
for ( int k = 0 ; k < WINDOWSIZE ; k + + ) {
2018-05-13 21:41:44 +02:00
weights [ k ] [ i ] = rndm ( ) ; // Init weights
2018-05-07 15:36:46 +02:00
}
}
mkFileName ( fileName , sizeof ( fileName ) , PURE_WEIGHTS ) ;
// save plain test_array before math magic happens
FILE * fp0 = fopen ( fileName , " w " ) ;
2018-05-14 18:59:05 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES ; i + + ) {
2018-05-11 12:00:37 +02:00
for ( k = 0 ; k < WINDOWSIZE ; k + + ) {
2018-05-13 21:41:44 +02:00
fprintf ( fp0 , " [%d][%d]%lf \n " , k , i , weights [ k ] [ i ] ) ;
2018-05-07 15:36:46 +02:00
}
}
fclose ( fp0 ) ;
// math magic
2018-05-14 16:06:03 +02:00
localMean ( weights ) ;
directPredecessor ( weights ) ;
2018-05-14 13:54:56 +02:00
differentialPredecessor ( weights ) ;
2018-05-11 14:19:20 +02:00
mkSvgGraph ( points ) ;
2018-05-11 12:00:37 +02:00
// save test_array after math magic happened
// memset( fileName, '\0', sizeof(fileName) );
2018-05-13 21:41:44 +02:00
/* mkFileName(fileName, sizeof(fileName), USED_WEIGHTS);
2018-05-07 15:36:46 +02:00
FILE * fp1 = fopen ( fileName , " w " ) ;
for ( i = 0 ; i < tracking ; i + + ) {
2018-05-11 11:30:17 +02:00
for ( int k = 0 ; k < WINDOWSIZE ; k + + ) {
2018-05-07 15:36:46 +02:00
fprintf ( fp1 , " [%d][%d] %lf \n " , k , i , w [ k ] [ i ] ) ;
}
}
fclose ( fp1 ) ;
2018-05-13 21:41:44 +02:00
*/
printf ( " \n DONE! \n " ) ;
2018-04-26 16:39:47 +02:00
}
2018-04-30 14:38:28 +02:00
2018-05-07 15:36:46 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
localMean
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
Variant ( 1 / 3 ) , substract local mean .
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-14 13:54:56 +02:00
void localMean ( double weights [ WINDOWSIZE ] [ NUMBER_OF_SAMPLES ] ) {
2018-05-13 21:41:44 +02:00
//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 ) ;
2018-05-07 15:36:46 +02:00
char fileName [ 50 ] ;
2018-05-11 14:19:20 +02:00
double xError [ 2048 ] ; // includes e(n)
2018-05-11 11:30:17 +02:00
memset ( xError , 0.0 , NUMBER_OF_SAMPLES ) ; // initialize xError-array with Zero
int xCount = 0 , i ; // runtime var;
2018-05-07 15:36:46 +02:00
mkFileName ( fileName , sizeof ( fileName ) , LOCAL_MEAN ) ;
FILE * fp4 = fopen ( fileName , " w " ) ;
2018-05-14 16:06:03 +02:00
fprintf ( fp4 , " \n =====================================LocalMean===================================== \n No. \t xPredicted \t xActual \t \t xError \n " ) ;
2018-05-11 11:30:17 +02:00
2018-05-14 13:54:56 +02:00
double xMean = xSamples [ 0 ] ;
2018-05-11 12:00:37 +02:00
double xSquared = 0.0 ;
double xPredicted = 0.0 ;
double xActual = 0.0 ;
2018-05-14 13:54:56 +02:00
2018-05-11 11:30:17 +02:00
for ( xCount = 1 ; xCount < NUMBER_OF_SAMPLES ; xCount + + ) { // first value will not get predicted
2018-05-14 19:31:50 +02:00
2018-05-13 21:41:44 +02:00
int _arrayLength = ( xCount > WINDOWSIZE ) ? WINDOWSIZE + 1 : xCount ;
2018-05-11 12:00:37 +02:00
xMean = ( xCount > 0 ) ? windowXMean ( _arrayLength , xCount ) : 0 ;
2018-05-11 11:30:17 +02:00
xPredicted = 0.0 ;
xActual = xSamples [ xCount + 1 ] ;
2018-05-11 12:00:37 +02:00
// weightedSum += _x[ xCount-1 ] * w[xCount][0];
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) { //get predicted value
2018-05-11 14:19:20 +02:00
xPredicted + = ( local_weights [ i ] [ xCount ] * ( xSamples [ xCount - i ] - xMean ) ) ;
2018-05-07 15:36:46 +02:00
}
xPredicted + = xMean ;
xError [ xCount ] = xActual - xPredicted ;
2018-05-11 11:30:17 +02:00
points [ xCount ] . xVal [ 1 ] = xCount ;
points [ xCount ] . yVal [ 1 ] = xPredicted ;
points [ xCount ] . xVal [ 4 ] = xCount ;
points [ xCount ] . yVal [ 4 ] = xError [ xCount ] ;
2018-05-11 12:00:37 +02:00
xSquared = 0.0 ;
2018-05-11 11:30:17 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) { //get xSquared
xSquared + = pow ( xSamples [ xCount - i ] - xMean , 2 ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-13 21:41:44 +02:00
if ( xSquared = = 0.0 ) { // Otherwise returns Pred: -1.#IND00 in some occassions
2018-05-11 12:00:37 +02:00
xSquared = 1.0 ;
2018-05-11 11:30:17 +02:00
}
for ( i = 1 ; i < _arrayLength ; i + + ) { //update weights
2018-05-13 21:41:44 +02:00
local_weights [ i ] [ xCount + 1 ] = local_weights [ i ] [ xCount ] + learnrate * xError [ xCount ] * ( ( xSamples [ xCount - i ] - xMean ) / xSquared ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-14 16:06:03 +02:00
fprintf ( fp4 , " %d \t %f \t %f \t %f \n " , xCount , xPredicted , xActual , xError [ xCount ] ) ;
2018-04-30 14:38:28 +02:00
}
2018-05-14 13:54:56 +02:00
double * xErrorPtr = popNAN ( xError ) ; // delete NAN values from xError[]
double xErrorLength = * xErrorPtr ; // Watch popNAN()!
2018-05-14 16:06:03 +02:00
xErrorPtr [ 0 ] = 0.0 ;
2018-05-13 21:41:44 +02:00
printf ( " Xerrorl:%lf " , xErrorLength ) ;
2018-05-14 16:06:03 +02:00
double mean = sum_array ( xErrorPtr , xErrorLength ) / xErrorLength ;
2018-05-07 15:36:46 +02:00
double deviation = 0.0 ;
// Mean square
2018-05-13 21:41:44 +02:00
for ( i = 1 ; i < xErrorLength ; i + + ) {
2018-05-11 11:30:17 +02:00
deviation + = pow ( xError [ i ] - mean , 2 ) ;
2018-05-07 15:36:46 +02:00
}
deviation / = xErrorLength ;
2018-05-14 16:06:03 +02:00
printf ( " mean:%lf, devitation:%lf " , mean , deviation ) ;
2018-05-07 15:36:46 +02:00
// write in file
2018-05-14 18:59:05 +02:00
//mkFileName(fileName, sizeof(fileName), RESULTS);
//FILE *fp2 = fopen(fileName, "w");
fprintf ( fp4 , " \n Quadratische Varianz(x_error): %f \n Mittelwert:(x_error): %f \n \n " , deviation , mean ) ;
//fclose(fp2);
2018-05-13 21:41:44 +02:00
free ( local_weights ) ;
2018-05-07 15:36:46 +02:00
fclose ( fp4 ) ;
2018-05-14 13:54:56 +02:00
2018-05-14 18:59:05 +02:00
//weightsLogger( local_weights, USED_WEIGHTS );
2018-04-26 16:39:47 +02:00
}
2018-04-30 14:38:28 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
directPredecessor
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
Variant ( 2 / 3 ) ,
substract direct predecessor
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-13 21:41:44 +02:00
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 ) ;
2018-05-07 15:36:46 +02:00
char fileName [ 512 ] ;
double xError [ 2048 ] ;
int xCount = 0 , i ;
2018-05-11 14:19:20 +02:00
double xActual = 0.0 ;
2018-05-13 21:41:44 +02:00
double xPredicted = 0.0 ;
2018-05-07 15:36:46 +02:00
// File handling
mkFileName ( fileName , sizeof ( fileName ) , DIRECT_PREDECESSOR ) ;
FILE * fp3 = fopen ( fileName , " w " ) ;
2018-05-14 16:06:03 +02:00
fprintf ( fp3 , " \n =====================================DirectPredecessor===================================== \n No. \t xPredicted \t xAcutal \t \t xError \n " ) ;
2018-05-11 11:30:17 +02:00
2018-05-11 14:19:20 +02:00
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
2018-05-11 12:00:37 +02:00
//int _sourceIndex = (xCount > WINDOWSIZE) ? xCount - WINDOWSIZE : xCount;
2018-05-11 11:30:17 +02:00
int _arrayLength = ( xCount > WINDOWSIZE ) ? WINDOWSIZE + 1 : xCount ;
2018-05-11 14:19:20 +02:00
//printf("xCount:%d, length:%d\n", xCount, _arrayLength);
// printf("WINDOWSIZE:%f\n", windowXMean(_arrayLength, xCount));
2018-05-11 11:30:17 +02:00
xPredicted = 0.0 ;
xActual = xSamples [ xCount + 1 ] ;
2018-05-11 14:19:20 +02:00
// weightedSum += _x[ xCount-1 ] * w[xCount][0];
2018-05-11 11:30:17 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
2018-05-11 14:19:20 +02:00
xPredicted + = ( local_weights [ i ] [ xCount ] * ( xSamples [ xCount - 1 ] - xSamples [ xCount - i - 1 ] ) ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-11 11:30:17 +02:00
xPredicted + = xSamples [ xCount - 1 ] ;
2018-05-07 15:36:46 +02:00
xError [ xCount ] = xActual - xPredicted ;
2018-05-14 16:06:03 +02:00
//fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
2018-05-11 11:30:17 +02:00
points [ xCount ] . xVal [ 2 ] = xCount ;
points [ xCount ] . yVal [ 2 ] = xPredicted ;
points [ xCount ] . xVal [ 5 ] = xCount ;
points [ xCount ] . yVal [ 5 ] = xError [ xCount ] ;
2018-05-07 15:36:46 +02:00
double xSquared = 0.0 ;
2018-05-11 11:30:17 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
xSquared + = pow ( xSamples [ xCount - 1 ] - xSamples [ xCount - i - 1 ] , 2 ) ; // substract direct predecessor
2018-05-07 15:36:46 +02:00
}
2018-05-11 11:30:17 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
2018-05-13 21:41:44 +02:00
local_weights [ i ] [ xCount + 1 ] = local_weights [ i ] [ xCount ] + learnrate * xError [ xCount ] * ( ( xSamples [ xCount - 1 ] - xSamples [ xCount - i - 1 ] ) / xSquared ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-14 16:06:03 +02:00
fprintf ( fp3 , " %d \t %f \t %f \t %f \n " , xCount , xPredicted , xActual , xError [ xCount ] ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-14 18:59:05 +02:00
2018-05-14 16:06:03 +02:00
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 ) ;
2018-05-13 21:41:44 +02:00
2018-05-14 16:06:03 +02:00
double mean = sum_array ( xErrorPtr , xErrorLength ) / xErrorLength ;
2018-05-07 15:36:46 +02:00
double deviation = 0.0 ;
2018-05-14 16:06:03 +02:00
// Mean square
for ( i = 1 ; i < xErrorLength ; i + + ) {
2018-05-07 15:36:46 +02:00
deviation + = pow ( xError [ i ] - mean , 2 ) ;
}
2018-05-11 11:30:17 +02:00
deviation / = xErrorLength ;
2018-05-14 16:06:03 +02:00
printf ( " mean:%lf, devitation:%lf " , mean , deviation ) ;
2018-05-07 15:36:46 +02:00
2018-05-14 16:06:03 +02:00
// write in file
2018-05-14 18:59:05 +02:00
//mkFileName(fileName, sizeof(fileName), RESULTS);
//FILE *fp2 = fopen(fileName, "wa");
fprintf ( fp3 , " \n Quadratische Varianz(x_error): %f \n Mittelwert:(x_error): %f \n \n " , deviation , mean ) ;
fclose ( fp3 ) ;
//fclose(fp2);
2018-05-14 16:06:03 +02:00
free ( local_weights ) ;
2018-05-13 21:41:44 +02:00
2018-05-14 18:59:05 +02:00
//weightsLogger( local_weights, USED_WEIGHTS );
2018-04-26 16:39:47 +02:00
}
2018-05-11 11:30:17 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
differentialPredecessor
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
variant ( 3 / 3 ) ,
2018-05-14 16:06:03 +02:00
differential predecessor .
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
2018-05-13 21:41:44 +02:00
void differentialPredecessor ( double weights [ WINDOWSIZE ] [ NUMBER_OF_SAMPLES ] ) {
2018-05-14 13:54:56 +02:00
// double local_weights[WINDOWSIZE][NUMBER_OF_SAMPLES];
2018-05-13 21:41:44 +02:00
double ( * local_weights ) [ WINDOWSIZE ] = malloc ( sizeof ( double ) * ( WINDOWSIZE + 1 ) * ( NUMBER_OF_SAMPLES + 1 ) ) ;
2018-05-11 11:30:17 +02:00
2018-05-13 21:41:44 +02:00
memcpy ( local_weights , weights , sizeof ( double ) * WINDOWSIZE * NUMBER_OF_SAMPLES ) ;
2018-05-11 11:30:17 +02:00
char fileName [ 512 ] ;
double xError [ 2048 ] ;
int xCount = 0 , i ;
2018-05-11 14:19:20 +02:00
double xPredicted = 0.0 ;
double xActual = 0.0 ;
2018-05-11 11:30:17 +02:00
// File handling
mkFileName ( fileName , sizeof ( fileName ) , DIFFERENTIAL_PREDECESSOR ) ;
FILE * fp6 = fopen ( fileName , " w " ) ;
2018-05-14 16:06:03 +02:00
fprintf ( fp6 , " \n =====================================DifferentialPredecessor===================================== \n No. \t xPredicted \t xAcutal \t \t xError \n " ) ;
2018-05-11 11:30:17 +02:00
2018-05-11 14:19:20 +02:00
for ( xCount = 1 ; xCount < NUMBER_OF_SAMPLES ; xCount + + ) { // first value will not get predicted
int _arrayLength = ( xCount > WINDOWSIZE ) ? WINDOWSIZE + 1 : xCount ;
2018-05-13 21:41:44 +02:00
xPredicted = 0.0 ;
xActual = xSamples [ xCount + 1 ] ;
2018-05-11 11:30:17 +02:00
2018-05-11 14:19:20 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
xPredicted + = ( local_weights [ i ] [ xCount ] * ( xSamples [ xCount - i ] - xSamples [ xCount - i - 1 ] ) ) ;
2018-05-11 11:30:17 +02:00
}
xPredicted + = xSamples [ xCount - 1 ] ;
xError [ xCount ] = xActual - xPredicted ;
2018-05-14 16:06:03 +02:00
//fprintf(fp6, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
2018-05-11 11:30:17 +02:00
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 ;
2018-05-11 14:19:20 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
2018-05-11 11:30:17 +02:00
xSquared + = pow ( xSamples [ xCount - i ] - xSamples [ xCount - i - 1 ] , 2 ) ; // substract direct predecessor
}
2018-05-11 14:19:20 +02:00
for ( i = 1 ; i < _arrayLength ; i + + ) {
2018-05-13 21:41:44 +02:00
local_weights [ i ] [ xCount + 1 ] = local_weights [ i ] [ xCount ] + learnrate * xError [ xCount ] * ( ( xSamples [ xCount - i ] - xSamples [ xCount - i - 1 ] ) / xSquared ) ;
2018-05-11 11:30:17 +02:00
}
2018-05-14 16:06:03 +02:00
fprintf ( fp6 , " %d \t %f \t %f \t %f \n " , xCount , xPredicted , xActual , xError [ xCount ] ) ;
2018-05-11 11:30:17 +02:00
}
2018-05-14 18:59:05 +02:00
2018-05-14 16:06:03 +02:00
/* int xErrorLength = sizeof(xError) / sizeof(xError[0]);
2018-05-11 14:19:20 +02:00
printf ( " vor:%d " , xErrorLength ) ;
2018-05-13 21:41:44 +02:00
popNAN ( xError ) ;
2018-05-11 14:19:20 +02:00
printf ( " nach:%d " , xErrorLength ) ;
xErrorLength = sizeof ( xError ) / sizeof ( xError [ 0 ] ) ;
2018-05-11 11:30:17 +02:00
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 ;
2018-05-11 14:19:20 +02:00
//mkSvgGraph(points);
2018-05-11 11:30:17 +02:00
fprintf ( fp6 , " {%d}. \t Least Mean Squared{%f} \t Mean{%f} \n \n " , xCount , deviation , mean ) ;
2018-05-14 13:54:56 +02:00
*/
2018-05-14 16:06:03 +02:00
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
2018-05-14 18:59:05 +02:00
//mkFileName(fileName, sizeof(fileName), RESULTS);
//FILE *fp2 = fopen(fileName, "wa");
fprintf ( fp6 , " \n Quadratische Varianz(x_error): %f \n Mittelwert:(x_error): %f \n \n " , deviation , mean ) ;
//fclose(fp2);
fclose ( fp6 ) ;
2018-05-14 16:06:03 +02:00
free ( local_weights ) ;
2018-05-14 18:59:05 +02:00
//weightsLogger( local_weights, USED_WEIGHTS );
2018-05-11 11:30:17 +02:00
}
2018-05-03 18:30:42 +02:00
2018-04-30 14:38:28 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
mkFileName
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
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 .
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
char * mkFileName ( char * buffer , size_t max_len , int suffixId ) {
2018-05-03 18:30:42 +02:00
const char * format_str = " %Y-%m-%d_%H_%M_%S " ;
size_t date_len ;
2018-05-07 15:36:46 +02:00
const char * suffix = fileSuffix ( suffixId ) ;
2018-05-03 18:30:42 +02:00
time_t now = time ( NULL ) ;
2018-05-07 15:36:46 +02:00
strftime ( buffer , max_len , format_str , localtime ( & now ) ) ;
2018-05-03 18:30:42 +02:00
date_len = strlen ( buffer ) ;
2018-05-07 15:36:46 +02:00
strncat ( buffer , suffix , max_len - date_len ) ;
return buffer ;
2018-04-26 16:39:47 +02:00
}
2018-04-30 14:38:28 +02:00
2018-04-26 16:39:47 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
fileSuffix
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
Contains and returns every suffix for all existing filenames
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-03 18:30:42 +02:00
*/
2018-05-07 15:36:46 +02:00
char * fileSuffix ( int id ) {
2018-05-11 11:30:17 +02:00
char * suffix [ ] = { " _weights_pure.txt " , " _weights_used.txt " , " _direct_predecessor.txt " , " _ergebnisse.txt " , " _localMean.txt " , " _testvalues.txt " , " _differential_predecessor.txt " } ;
2018-05-03 18:30:42 +02:00
return suffix [ id ] ;
}
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
myLogger
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
Logs x , y points to svg graph
2018-05-03 18:30:42 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-13 21:41:44 +02:00
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 " ) ;
2018-05-14 18:59:05 +02:00
fclose ( fp ) ;
2018-05-13 21:41:44 +02:00
}
2018-05-14 13:54:56 +02:00
2018-05-13 21:41:44 +02:00
2018-05-14 18:59:05 +02:00
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
bufferLogger
formats output of mkSvgGraph - - Please open graphResults . html to see the output - -
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
2018-05-07 15:36:46 +02:00
void bufferLogger ( char * buffer , point_t points [ ] ) {
int i ;
char _buffer [ 512 ] = " " ;
2018-05-03 18:30:42 +02:00
2018-05-11 11:30:17 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES - 1 ; i + + ) { // xActual
2018-05-07 15:36:46 +02:00
sprintf ( _buffer , " L %f %f \n " , points [ i ] . xVal [ 0 ] , points [ i ] . yVal [ 0 ] ) ;
strcat ( buffer , _buffer ) ;
}
2018-05-11 11:42:51 +02:00
strcat ( buffer , " \" fill= \" none \" id= \" svg_1 \" stroke= \" black \" stroke-width= \" 0.4px \" /> \n <path d= \" M0 0 \n " ) ;
2018-05-14 13:54:56 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES - 1 ; i + + ) { // xPredicted from localMean
2018-05-07 15:36:46 +02:00
sprintf ( _buffer , " L %f %f \n " , points [ i ] . xVal [ 1 ] , points [ i ] . yVal [ 1 ] ) ;
strcat ( buffer , _buffer ) ;
}
2018-05-11 11:42:51 +02:00
strcat ( buffer , " \" fill= \" none \" id= \" svg_2 \" stroke= \" green \" stroke-width= \" 0.4px \" /> \n <path d= \" M0 0 \n " ) ;
2018-05-11 11:30:17 +02:00
for ( i = 0 ; i < = NUMBER_OF_SAMPLES - 1 ; i + + ) { //xPreddicted from directPredecessor
2018-05-07 15:36:46 +02:00
sprintf ( _buffer , " L %f %f \n " , points [ i ] . xVal [ 2 ] , points [ i ] . yVal [ 2 ] ) ;
strcat ( buffer , _buffer ) ;
}
2018-05-11 11:42:51 +02:00
strcat ( buffer , " \" fill= \" none \" id= \" svg_3 \" stroke= \" blue \" stroke-width= \" 0.4px \" /> \n <path d= \" M0 0 \n " ) ;
2018-05-11 12:00:37 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES - 1 ; i + + ) { //xPredicted from diff Pred
2018-05-14 16:06:03 +02:00
sprintf ( _buffer , " L %f %f \n " , points [ i ] . xVal [ 3 ] , points [ i ] . yVal [ 3 ] ) ;
2018-05-11 11:30:17 +02:00
strcat ( buffer , _buffer ) ;
}
2018-05-14 13:54:56 +02:00
strcat ( buffer , " \" fill= \" none \" id= \" svg_4 \" stroke= \" red \" stroke-width= \" 0.4px \" /> \n " ) ;
2018-05-07 15:36:46 +02:00
}
2018-05-03 18:30:42 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
sum_array
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
Sum of all elements in x within a defined length
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-11 11:30:17 +02:00
double sum_array ( double x [ ] , int xlength ) {
2018-05-07 15:36:46 +02:00
int i = 0 ;
double sum = 0.0 ;
2018-05-11 12:00:37 +02:00
if ( xlength ! = 0 ) {
for ( i = 0 ; i < xlength ; i + + ) {
sum + = x [ i ] ;
}
}
2018-05-07 15:36:46 +02:00
return sum ;
2018-04-26 16:39:47 +02:00
}
2018-04-30 14:38:28 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
popNanLength
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
returns length of new array without NAN values
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-11 11:30:17 +02:00
*/
2018-05-13 21:41:44 +02:00
double * popNAN ( double * xError ) {
2018-05-14 13:54:56 +02:00
int i , counter = 1 ;
2018-05-13 21:41:44 +02:00
double tmpLength = 0.0 ;
double * tmp = NULL ;
double * more_tmp = NULL ;
2018-05-14 13:54:56 +02:00
2018-05-13 21:41:44 +02:00
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 ] ) ;
2018-05-14 13:54:56 +02:00
tmpLength + + ;
2018-05-13 21:41:44 +02:00
}
}
counter + = 1 ;
more_tmp = ( double * ) realloc ( tmp , counter * sizeof ( double ) ) ;
tmp = more_tmp ;
2018-05-14 13:54:56 +02:00
* tmp = tmpLength ; // Length of array has to be stored in tmp[0],
2018-05-13 21:41:44 +02:00
// 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 ;
2018-05-11 11:30:17 +02:00
}
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
r2
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
returns a random double value between 0 and 1
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-04-30 14:38:28 +02:00
*/
2018-05-03 18:30:42 +02:00
2018-05-07 15:36:46 +02:00
double r2 ( void ) {
return ( ( rand ( ) % 10000 ) / 10000.0 ) ;
2018-04-30 14:38:28 +02:00
}
2018-05-03 18:30:42 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
rndm
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
fills a double variable with random value and returns it
2018-04-30 14:38:28 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-04-30 14:38:28 +02:00
*/
2018-05-03 18:30:42 +02:00
2018-05-07 15:36:46 +02:00
double rndm ( void ) {
double rndmval = r2 ( ) ;
return rndmval ;
2018-04-30 14:38:28 +02:00
}
2018-05-04 01:28:36 +02:00
2018-05-04 16:30:12 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-04 16:30:12 +02:00
2018-05-11 12:00:37 +02:00
mkSvgGraph
2018-05-04 16:30:12 +02:00
2018-05-11 12:00:37 +02:00
parses template . svg and writes results in said template
2018-05-04 01:28:36 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-04 01:28:36 +02:00
*/
2018-05-07 15:36:46 +02:00
void mkSvgGraph ( point_t points [ ] ) {
2018-05-13 21:41:44 +02:00
FILE * input = fopen ( " graphResults_template.html " , " r " ) ;
FILE * target = fopen ( " graphResults.html " , " w " ) ;
2018-05-07 15:36:46 +02:00
char line [ 512 ] ;
2018-05-11 11:30:17 +02:00
char firstGraph [ 15 ] = { " <path d= \" M0 0 " } ;
2018-05-07 15:36:46 +02:00
if ( input = = NULL ) {
exit ( EXIT_FAILURE ) ;
}
2018-05-11 11:30:17 +02:00
2018-05-07 15:36:46 +02:00
char buffer [ 131072 ] = " " ;
2018-05-11 11:30:17 +02:00
2018-05-07 15:36:46 +02:00
memset ( buffer , ' \0 ' , sizeof ( buffer ) ) ;
2018-05-11 12:00:37 +02:00
while ( ! feof ( input ) ) {
2018-05-07 15:36:46 +02:00
fgets ( line , 512 , input ) ;
2018-05-11 12:00:37 +02:00
strncat ( buffer , line , strlen ( line ) ) ;
2018-05-07 15:36:46 +02:00
if ( strstr ( line , firstGraph ) ! = NULL ) {
bufferLogger ( buffer , points ) ;
}
}
fprintf ( target , buffer ) ;
2018-05-04 01:28:36 +02:00
}
2018-05-07 15:49:17 +02:00
2018-05-07 00:18:25 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
rdPPM
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
reads data from file of type PPM , stores colorchannels in a struct in the
size of given picture
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-07 17:10:43 +02:00
static imagePixel_t * rdPPM ( char * fileName ) {
2018-05-07 15:36:46 +02:00
char buffer [ 16 ] ;
2018-05-07 00:18:25 +02:00
imagePixel_t * image ;
2018-05-07 15:36:46 +02:00
int c , rgbColor ;
FILE * fp = fopen ( fileName , " rb " ) ;
if ( ! fp ) {
exit ( EXIT_FAILURE ) ;
2018-05-07 00:18:25 +02:00
}
2018-05-07 15:36:46 +02:00
if ( ! fgets ( buffer , sizeof ( buffer ) , fp ) ) {
2018-05-07 00:18:25 +02:00
perror ( fileName ) ;
exit ( EXIT_FAILURE ) ;
}
2018-05-07 15:36:46 +02:00
if ( buffer [ 0 ] ! = ' P ' | | buffer [ 1 ] ! = ' 6 ' ) {
fprintf ( stderr , " No PPM file format \n " ) ;
2018-05-07 00:18:25 +02:00
exit ( EXIT_FAILURE ) ;
}
2018-05-07 15:36:46 +02:00
image = ( imagePixel_t * ) malloc ( sizeof ( imagePixel_t ) ) ;
if ( ! image ) {
2018-05-07 00:18:25 +02:00
fprintf ( stderr , " malloc() failed " ) ;
}
c = getc ( fp ) ;
2018-05-07 15:36:46 +02:00
while ( c = = ' # ' ) {
while ( getc ( fp ) ! = ' \n ' ) ;
2018-05-07 00:18:25 +02:00
c = getc ( fp ) ;
}
ungetc ( c , fp ) ;
2018-05-07 15:36:46 +02:00
if ( fscanf ( fp , " %d %d " , & image - > x , & image - > y ) ! = 2 ) {
2018-05-07 00:18:25 +02:00
fprintf ( stderr , " Invalid image size in %s \n " , fileName ) ;
exit ( EXIT_FAILURE ) ;
}
2018-05-07 15:36:46 +02:00
if ( fscanf ( fp , " %d " , & rgbColor ) ! = 1 ) {
2018-05-07 00:18:25 +02:00
fprintf ( stderr , " Invalid rgb component in %s \n " , fileName ) ;
}
2018-05-07 15:36:46 +02:00
if ( rgbColor ! = RGB_COLOR ) {
fprintf ( stderr , " Invalid image color range in %s \n " , fileName ) ;
2018-05-07 00:18:25 +02:00
exit ( EXIT_FAILURE ) ;
}
2018-05-07 15:36:46 +02:00
while ( fgetc ( fp ) ! = ' \n ' ) ;
image - > data = ( colorChannel_t * ) malloc ( image - > x * image - > y * sizeof ( imagePixel_t ) ) ;
if ( ! image ) {
fprintf ( stderr , " malloc() failed " ) ;
2018-05-07 00:18:25 +02:00
exit ( EXIT_FAILURE ) ;
}
2018-05-07 15:36:46 +02:00
if ( fread ( image - > data , 3 * image - > x , image - > y , fp ) ! = image - > y ) {
2018-05-07 00:18:25 +02:00
fprintf ( stderr , " Loading image failed " ) ;
exit ( EXIT_FAILURE ) ;
}
fclose ( fp ) ;
return image ;
}
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
mkPpmFile
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
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
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 00:18:25 +02:00
*/
2018-05-07 15:36:46 +02:00
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 ) ;
2018-05-07 00:18:25 +02:00
}
2018-05-11 11:30:17 +02:00
2018-05-07 00:18:25 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
2018-05-11 12:00:37 +02:00
ppmColorChannel
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
gets one of the rgb color channels and writes them to a file
2018-05-07 00:18:25 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-07 15:36:46 +02:00
*/
2018-05-07 00:18:25 +02:00
2018-05-07 22:39:31 +02:00
int ppmColorChannel ( FILE * fp , imagePixel_t * image ) {
2018-05-14 18:59:05 +02:00
// int length = (image->x * image->y) / 3;
2018-05-11 11:30:17 +02:00
int i = 0 ;
2018-05-07 15:36:46 +02:00
if ( image ) {
2018-05-11 12:00:37 +02:00
for ( i = 0 ; i < NUMBER_OF_SAMPLES - 1 ; i + + ) {
fprintf ( fp , " %d \n " , image - > data [ i ] . green ) ;
2018-05-07 00:18:25 +02:00
}
2018-05-11 11:30:17 +02:00
}
2018-05-07 22:39:31 +02:00
fclose ( fp ) ;
2018-05-11 11:30:17 +02:00
return NUMBER_OF_SAMPLES ;
2018-05-07 22:39:31 +02:00
}
2018-05-11 11:30:17 +02:00
/*
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
colorSamples
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
reads colorChannel values from file and stores them in xSamples as well as points datatype for
creating the SVG graph
2018-05-11 11:30:17 +02:00
2018-05-11 12:00:37 +02:00
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2018-05-11 11:30:17 +02:00
*/
2018-05-11 12:00:37 +02:00
void colorSamples ( FILE * fp ) {
2018-05-14 13:54:56 +02:00
int i = 0 ;
2018-05-11 11:30:17 +02:00
char buffer [ NUMBER_OF_SAMPLES ] ;
2018-05-07 22:39:31 +02:00
2018-05-11 12:00:37 +02:00
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 ;
2018-05-07 22:39:31 +02:00
}
2018-05-11 11:30:17 +02:00
}
2018-05-07 22:39:31 +02:00
fclose ( fp ) ;
2018-05-07 00:18:25 +02:00
}
2018-05-11 11:30:17 +02:00
2018-05-14 18:59:05 +02:00
/*
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
windowXMean
returns mean value of given input , which has a length of WINDOWSIZE
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
*/
2018-05-11 12:00:37 +02:00
double windowXMean ( int _arraylength , int xCount ) {
double sum = 0.0 ;
double * ptr ;
2018-05-11 11:30:17 +02:00
2018-05-14 18:59:05 +02:00
for ( ptr = & xSamples [ xCount - _arraylength ] ; ptr ! = & xSamples [ xCount ] ; ptr + + ) { //set ptr to beginning of window
sum + = * ptr ;
2018-05-11 12:00:37 +02:00
}
return sum / ( double ) _arraylength ;
2018-05-11 11:30:17 +02:00
}
2018-05-13 21:41:44 +02:00