2018-04-26 16:39:47 +02:00
|
|
|
//
|
|
|
|
// main.c
|
|
|
|
// NLMS
|
|
|
|
//
|
2018-04-30 14:38:28 +02:00
|
|
|
// Created by FBRDNLMS on 26.04.18.
|
|
|
|
// Copyright © 2018 FBRDNLMS. All rights reserved.
|
2018-04-26 16:39:47 +02:00
|
|
|
//
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define M 1000
|
2018-04-30 14:38:28 +02:00
|
|
|
#define tracking 40 //Count of weights
|
2018-04-26 16:39:47 +02:00
|
|
|
//static Stack<double> x = new Stack<double>();
|
|
|
|
//static Random rnd = new Random();
|
|
|
|
//static double[] _x = new double[M];
|
|
|
|
//static double[,] w = new double[M, M];
|
|
|
|
#define learnrate 1.0
|
|
|
|
|
|
|
|
double x[] ={0};
|
|
|
|
double _x[M] = {0};
|
|
|
|
double w [M][M]={{0},{0}};
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
void fileName(char *name_suffix);
|
|
|
|
double r2(void);
|
|
|
|
double rnd(void);
|
2018-04-26 16:39:47 +02:00
|
|
|
double sum_array(double x[], int length);
|
|
|
|
void direkterVorgaenger(void);
|
|
|
|
void lokalerMittelWert(void);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv ) {
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
//init test_array, fill in weights by random
|
2018-04-26 16:39:47 +02:00
|
|
|
int i = 0;
|
|
|
|
for (i = 0; i < M; i++) {
|
|
|
|
_x[i] += ((255.0 / M) * i);
|
|
|
|
for (int k = 1; k < M; k++)
|
|
|
|
{
|
|
|
|
w[k][i] = rnd();
|
|
|
|
}
|
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
// save plain test_array before math magic happened
|
|
|
|
char weightsBefore [] = "_weights.txt";
|
|
|
|
fileName(&weightsBefore);
|
|
|
|
FILE *fp0 = fopen(weightsBefore,"wb+");
|
|
|
|
|
2018-04-26 16:39:47 +02:00
|
|
|
for (i = 0; i < tracking; i++){
|
2018-04-30 14:38:28 +02:00
|
|
|
for ( int k = 1; k < tracking; k++ ){
|
|
|
|
fprintf(fp0, "[%f][%f] %.2f\n", k, i, w[k][i]);
|
|
|
|
}
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
fclose(fp0);
|
|
|
|
|
|
|
|
|
|
|
|
// math magic
|
2018-04-26 16:39:47 +02:00
|
|
|
direkterVorgaenger();
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
// save test_array after math magic happened
|
|
|
|
char weightsAfter [] = "_weights_after.txt";
|
|
|
|
fileName(&weightsAfter);
|
|
|
|
FILE *fp1 = fopen(weightsAfter,"wb+");
|
|
|
|
|
2018-04-26 16:39:47 +02:00
|
|
|
for (i = 0; i < tracking; i++) {
|
|
|
|
for (int k = 1; k < tracking; k++) {
|
2018-04-30 14:38:28 +02:00
|
|
|
fprintf(fp1, "[%f][%f] %.2f\n", k,i, w[k][i]);
|
|
|
|
}
|
2018-04-26 16:39:47 +02:00
|
|
|
|
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
fclose(fp1);
|
2018-04-26 16:39:47 +02:00
|
|
|
|
|
|
|
getchar();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
lokalerMittelwert()
|
|
|
|
|
|
|
|
|
|
|
|
Variant (1/3),
|
|
|
|
substract local mean
|
|
|
|
|
|
|
|
===================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
void lokalerMittelWert() {
|
|
|
|
|
|
|
|
double xError[M]; // includes e(n)
|
|
|
|
memset(xError, 0, M);// initialize xError-array with Zero
|
|
|
|
int xCount = 0; // runtime var
|
2018-04-26 16:39:47 +02:00
|
|
|
int i;
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
for (xCount = 1; xCount < M; xCount++){ // x_cout can not be zero
|
2018-04-26 16:39:47 +02:00
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
//double xPartArray[xCount]; //includes all values at the size of runtime var
|
|
|
|
|
|
|
|
double xMean = (xCount > 0) ? ( sum_array(_x, xCount) / xCount) : 0;
|
2018-04-26 16:39:47 +02:00
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
double xPredicted = 0.0;
|
|
|
|
double xActual = _x[xCount + 1];
|
|
|
|
|
|
|
|
for ( i = 1; i < xCount; i++ ){ //get predicted value
|
|
|
|
xPredicted += (w[i][xCount] * (_x[xCount - i] - xMean)) ;
|
|
|
|
}
|
|
|
|
|
|
|
|
xPredicted += xMean;
|
|
|
|
xError [xCount] = xActual - xPredicted;
|
|
|
|
|
|
|
|
double xSquared = 0.0;
|
|
|
|
|
|
|
|
for ( i = 1; i < xCount; i++ ){ //get x squared
|
|
|
|
xSquared =+ pow(_x[xCount-i],2);
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
for ( i - 1; i < xCount; i++ ){ //update weights
|
|
|
|
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / xSquared);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int xErrorLength = sizeof(xError) / sizeof(xError[0]);
|
|
|
|
double mean = sum_array(xError, xErrorLength) / M;
|
|
|
|
double deviation = 0.0;
|
|
|
|
|
|
|
|
// Mean square
|
|
|
|
for( i = 0; i < M-1; i++ ){
|
|
|
|
deviation += pow( xError[i], 2 );
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
deviation /= xErrorLength;
|
|
|
|
|
|
|
|
|
|
|
|
// write in file
|
|
|
|
char results [] = "_results.txt";
|
|
|
|
fileName(&results);
|
|
|
|
FILE *fp2 = fopen(results, "wb+");
|
|
|
|
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
|
|
|
|
fclose(fp2);
|
|
|
|
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
direkterVorgaenger()
|
|
|
|
|
|
|
|
|
|
|
|
Variant (2/3),
|
|
|
|
substract direct predecessor
|
|
|
|
|
|
|
|
===================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
void direkterVorgaenger() {
|
|
|
|
|
|
|
|
double xError [M];
|
|
|
|
int xCount = 0, i;
|
|
|
|
|
|
|
|
// File handling
|
|
|
|
char direkterVorgaenger [] = "_direkterVorgaenger.txt";
|
|
|
|
fileName(&direkterVorgaenger);
|
|
|
|
FILE *fp3 = fopen(direkterVorgaenger, "wb+");
|
|
|
|
|
|
|
|
for ( xCount = 1; xCount < M; xCount++ ){
|
|
|
|
double xPredicted = 0.0;
|
|
|
|
double xActual = _x[xCount+1];
|
|
|
|
|
|
|
|
for ( i = 1; i < xCount; i++ ){
|
|
|
|
xPredicted += ( w[i][xCount] * ( _x[xCount - i] - _x[xCount - i - 1]));
|
|
|
|
}
|
|
|
|
|
|
|
|
xPredicted += _x[xCount-1];
|
|
|
|
xError[xCount] = xActual - xPredicted;
|
|
|
|
|
|
|
|
fprintf(fp3, "{%d}.\txPredicted{%f}\txActual{%f}\txError{%f}\n", xCount, xPredicted, xActual, xError[xCount]);
|
|
|
|
|
|
|
|
|
|
|
|
//get x squared
|
|
|
|
double xSquared = 0;
|
|
|
|
for ( i = 1; i < xCount; i++ ){
|
|
|
|
xSquared += pow( _x[xCount - i] - _x[xCount - i - 1], 2); // substract direct predecessor
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( i = 1; x < xCount; i++){
|
|
|
|
w[i][xCount+1] = w[i][xCount] + learnrate * xError[xCount] * ( ( _x[xCount - i - 1] ) / xSquared );
|
|
|
|
}
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
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);
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
|
|
|
|
fclose(fp3);
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
fileName()
|
|
|
|
|
|
|
|
|
|
|
|
generates filename with date for
|
|
|
|
logging purposes
|
|
|
|
|
|
|
|
===================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
void fileName(char *name_suffix){
|
2018-04-26 16:39:47 +02:00
|
|
|
//filename
|
|
|
|
char date[34];
|
|
|
|
//char name[13] = "_results.txt";
|
|
|
|
time_t now;
|
|
|
|
now = time(NULL);
|
|
|
|
strftime(date, 20, "%Y-%m-%d_%H_%M_%S", localtime(&now));
|
2018-04-30 14:38:28 +02:00
|
|
|
strcpy(date, *name_suffix);
|
2018-04-26 16:39:47 +02:00
|
|
|
//return &date[0];
|
2018-04-30 14:38:28 +02:00
|
|
|
return;
|
2018-04-26 16:39:47 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
|
2018-04-26 16:39:47 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
sum_array
|
|
|
|
|
|
|
|
|
|
|
|
sum of all elements in x
|
|
|
|
within a defined length
|
|
|
|
|
2018-04-30 14:38:28 +02:00
|
|
|
===================================
|
2018-04-26 16:39:47 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
double sum_array(double x[], int length){
|
|
|
|
//int length = 0;
|
2018-04-30 14:38:28 +02:00
|
|
|
int i = 0;
|
2018-04-26 16:39:47 +02:00
|
|
|
double sum = 0.0;
|
|
|
|
//length = sizeof(x)/sizeof(x[0]);
|
|
|
|
for (i=0; i< length; i++){
|
|
|
|
sum += x[i];
|
|
|
|
}
|
|
|
|
return sum;
|
|
|
|
}
|
2018-04-30 14:38:28 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
|
|
|
|
r2()
|
|
|
|
|
|
|
|
returns a double value between
|
|
|
|
0 and 1
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
*/
|
|
|
|
double r2()
|
|
|
|
{
|
|
|
|
return((rand() % 10000) / 10000.0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
|
|
|
|
int rnd()
|
|
|
|
|
|
|
|
fills a double variable with
|
|
|
|
random value and returns it
|
|
|
|
|
|
|
|
===================================
|
|
|
|
|
|
|
|
*/
|
|
|
|
double rnd()
|
|
|
|
{
|
|
|
|
double u;
|
|
|
|
u = r2();
|
|
|
|
return u;
|
|
|
|
}
|