restructre of folder and files
This commit is contained in:
parent
f78156882d
commit
1f198fd26d
Binary file not shown.
Binary file not shown.
|
@ -0,0 +1,446 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.Data;
|
||||
using System.Drawing;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using System.Windows.Forms.DataVisualization.Charting;
|
||||
|
||||
namespace NMLS_Graphisch
|
||||
{
|
||||
public partial class Form1 : Form
|
||||
{
|
||||
public Form1()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
static int NumberOfSamples = 1000;
|
||||
const int tracking = 40;
|
||||
static Stack<double> x = new Stack<double>();
|
||||
static Random rnd = new Random();
|
||||
static double[] _x = new double[NumberOfSamples];
|
||||
static double[,] w = new double[NumberOfSamples, NumberOfSamples];
|
||||
static double learnrate = 0.2;
|
||||
static double[] pixel_array;
|
||||
static int windowSize = 5;
|
||||
|
||||
private void button1_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
NumberOfSamples = Int32.Parse(comboBox2.SelectedItem.ToString());
|
||||
chart1.ChartAreas[0].AxisX.Maximum = NumberOfSamples;
|
||||
chart1.ChartAreas[0].AxisY.Maximum = 300;
|
||||
chart1.ChartAreas[0].AxisY.Minimum = -5;
|
||||
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
for (int i = 0; i < tracking; i++)
|
||||
{
|
||||
for (int k = 0; k < windowSize; k++)
|
||||
{
|
||||
File.AppendAllText("weights.txt",
|
||||
String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Series[] series = new Series[2];
|
||||
switch (comboBox1.SelectedItem.ToString())
|
||||
{
|
||||
|
||||
case "lokaler Mittelwert":
|
||||
series = lokalerMittelWert();
|
||||
break;
|
||||
|
||||
case "direkter Vorgänger":
|
||||
series = direkterVorgaenger();
|
||||
break;
|
||||
|
||||
case "differenzieller Vorgänger":
|
||||
series = diffVorgaenger();
|
||||
break;
|
||||
|
||||
default:
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
foreach (Series s in series)
|
||||
{
|
||||
if (chart1.Series.IndexOf(s.Name) < 0)
|
||||
{
|
||||
chart1.Series.Add(s);
|
||||
}
|
||||
else
|
||||
{
|
||||
chart1.Series.RemoveAt(chart1.Series.IndexOf(s.Name));
|
||||
chart1.Series.Add(s);
|
||||
}
|
||||
}
|
||||
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
for (int i = 0; i < tracking; i++)
|
||||
{
|
||||
for (int k = 0; k < windowSize; k++)
|
||||
{
|
||||
File.AppendAllText("weights_after.txt",
|
||||
String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Series[] lokalerMittelWert()
|
||||
{
|
||||
int x_count = 0;
|
||||
double[] x_error = new double[NumberOfSamples];
|
||||
x_error[0] = 0;
|
||||
|
||||
//Graphischer Stuff
|
||||
Series lokal_M_error = new Series("Lokaler Mittelwert Error");
|
||||
Series lokal_M_predic = new Series("Lokaler Mittelwert Prediction");
|
||||
lokal_M_error.ChartType = SeriesChartType.Spline;
|
||||
lokal_M_predic.ChartType = SeriesChartType.Spline;
|
||||
|
||||
while (x_count + 1 < NumberOfSamples)
|
||||
{
|
||||
double[] x_part_Array = new double[x_count];
|
||||
|
||||
int _sourceIndex = (x_count > windowSize) ? x_count - windowSize : 0;
|
||||
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : (x_count > 0) ? x_count : 0;
|
||||
|
||||
Array.Copy(_x, _sourceIndex, x_part_Array, 0, _arrayLength);
|
||||
double x_middle = (x_count > 0) ? ( x_part_Array.Sum() / _arrayLength) : 0;
|
||||
double x_pred = 0.0;
|
||||
double[] x_array = _x;
|
||||
double x_actual = _x[x_count + 1];
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_pred += (w[i, x_count] * (x_array[x_count - i] - x_middle));
|
||||
}
|
||||
x_pred += x_middle;
|
||||
|
||||
// Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("lokalerMittelwert.txt",
|
||||
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||
Encoding.UTF8);
|
||||
|
||||
File.AppendAllText("lokalerMittelwert.txt",
|
||||
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
x_error[x_count] = x_actual - x_pred;
|
||||
double x_square = 0;
|
||||
|
||||
//Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("lokalerMittelwert.txt",
|
||||
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_square += Math.Pow(x_array[x_count - i] - x_middle, 2);
|
||||
}
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_middle) / x_square);
|
||||
}
|
||||
|
||||
// Graphischer Stuff
|
||||
lokal_M_error.Points.AddXY(x_count, x_error[x_count]);
|
||||
lokal_M_predic.Points.AddXY(x_count, x_pred);
|
||||
|
||||
x_count += 1;
|
||||
}
|
||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||
double varianz = 0.0;
|
||||
foreach (double x_e in x_error)
|
||||
{
|
||||
if(!double.IsNaN(x_e))
|
||||
varianz += Math.Pow(x_e - mittel, 2);
|
||||
}
|
||||
varianz /= x_error.Length;
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("ergebnisse.txt",
|
||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
return new Series[] { lokal_M_predic, lokal_M_error };
|
||||
}
|
||||
|
||||
Series[] direkterVorgaenger()
|
||||
{
|
||||
double[] x_error = new double[NumberOfSamples];
|
||||
x_error[0] = 0;
|
||||
int x_count = 0;
|
||||
|
||||
// Graphischer Stuff
|
||||
Series direkterVorgaenger_error = new Series("Direkter Vorgänger Error");
|
||||
Series direkterVorgaenger_predic = new Series("Direkter Vorgänger Prediction");
|
||||
direkterVorgaenger_error.ChartType = SeriesChartType.Spline;
|
||||
direkterVorgaenger_predic.ChartType = SeriesChartType.Spline;
|
||||
|
||||
while (x_count + 1 < NumberOfSamples)
|
||||
{
|
||||
double x_pred = 0.0;
|
||||
double[] x_array = _x;
|
||||
double x_actual = _x[x_count + 1];
|
||||
|
||||
if (x_count > 0)
|
||||
{
|
||||
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count;
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_pred += (w[i, x_count] * (x_array[x_count - 1] - x_array[x_count - i - 1]));
|
||||
}
|
||||
x_pred += x_array[x_count - 1];
|
||||
|
||||
// Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("direkterVorgaenger.txt",
|
||||
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||
Encoding.UTF8);
|
||||
|
||||
File.AppendAllText("direkterVorgaenger.txt",
|
||||
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
|
||||
x_error[x_count] = x_actual - x_pred;
|
||||
|
||||
// Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("direkterVorgaenger.txt",
|
||||
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
double x_square = 0;
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_square += Math.Pow(x_array[x_count - 1] - x_array[x_count - i - 1], 2);
|
||||
}
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - 1] - x_array[x_count - i - 1]) / x_square);
|
||||
}
|
||||
}
|
||||
|
||||
//Graphischer Stuff
|
||||
direkterVorgaenger_error.Points.AddXY(x_count, x_error[x_count]);
|
||||
direkterVorgaenger_predic.Points.AddXY(x_count, x_pred);
|
||||
|
||||
x_count += 1;
|
||||
}
|
||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||
double varianz = 0.0;
|
||||
foreach (double x_e in x_error)
|
||||
{
|
||||
if (!double.IsNaN(x_e))
|
||||
varianz += Math.Pow(x_e - mittel, 2);
|
||||
}
|
||||
varianz /= x_error.Length;
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("ergebnisse.txt",
|
||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
return new Series[] { direkterVorgaenger_error, direkterVorgaenger_predic };
|
||||
}
|
||||
|
||||
Series[] diffVorgaenger()
|
||||
{
|
||||
double[] x_error = new double[NumberOfSamples];
|
||||
x_error[0] = 0;
|
||||
int x_count = 1;
|
||||
|
||||
//Graphischer Stuff
|
||||
Series diffVorgaenger_error = new Series("Differenzieller Vorgänger Error");
|
||||
Series diffVorgaenger_predic = new Series("Differenzieller Vorgänger Prediction");
|
||||
diffVorgaenger_error.ChartType = SeriesChartType.Spline;
|
||||
diffVorgaenger_predic.ChartType = SeriesChartType.Spline;
|
||||
|
||||
while (x_count + 1 < NumberOfSamples)
|
||||
{
|
||||
double x_pred = 0.0;
|
||||
double[] x_array = _x;
|
||||
double x_actual = _x[x_count + 1];
|
||||
if (x_count > 0)
|
||||
{
|
||||
int _arrayLength = (x_count > windowSize) ? windowSize + 1 : x_count;
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_pred += (w[i, x_count] * (x_array[x_count - i] - x_array[x_count - i - 1]));
|
||||
}
|
||||
x_pred += x_array[x_count - 1];
|
||||
|
||||
// Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||
String.Format("{0}. X_pred {1}\n", x_count, x_pred),
|
||||
Encoding.UTF8);
|
||||
|
||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||
String.Format("{0}. X_actual {1}\n", x_count, x_actual),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
|
||||
x_error[x_count] = x_actual - x_pred;
|
||||
|
||||
// Output Stuff
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("differenziellerVorgaenger.txt",
|
||||
String.Format("{0}. X_error {1}\n\n", x_count, x_error[x_count]),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
|
||||
double x_square = 0;
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
x_square += Math.Pow(x_array[x_count - i] - x_array[x_count - i - 1], 2);
|
||||
}
|
||||
|
||||
for (int i = 1; i < _arrayLength; i++)
|
||||
{
|
||||
w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((x_array[x_count - i] - x_array[x_count - i - 1]) / x_square);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//Graphischer Stuff
|
||||
diffVorgaenger_error.Points.AddXY(x_count, x_error[x_count]);
|
||||
diffVorgaenger_predic.Points.AddXY(x_count, x_pred);
|
||||
|
||||
x_count += 1;
|
||||
}
|
||||
double mittel = x_error.Where(d => !double.IsNaN(d)).Sum() / x_error.Length;
|
||||
double varianz = 0.0;
|
||||
foreach (double x_e in x_error)
|
||||
{
|
||||
if (!double.IsNaN(x_e))
|
||||
varianz += Math.Pow(x_e - mittel, 2);
|
||||
}
|
||||
varianz /= x_error.Length;
|
||||
|
||||
if (checkBox1.Checked)
|
||||
{
|
||||
File.AppendAllText("ergebnisse.txt",
|
||||
String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel),
|
||||
Encoding.UTF8);
|
||||
}
|
||||
|
||||
return new Series[] { diffVorgaenger_error, diffVorgaenger_predic };
|
||||
}
|
||||
|
||||
// Inizialisierung von Arrays
|
||||
private void Form1_Load(object sender, EventArgs e)
|
||||
{
|
||||
comboBox1.SelectedIndex = 0;
|
||||
comboBox2.SelectedIndex = 0;
|
||||
chart1.Series.Clear();
|
||||
Series x_actual = new Series("Actual x Value");
|
||||
x_actual.ChartType = SeriesChartType.Spline;
|
||||
for (int i = 0; i < NumberOfSamples; i++)
|
||||
{
|
||||
_x[i] += ((255.0 / NumberOfSamples) * i);
|
||||
for (int k = 0; k < windowSize; k++)
|
||||
{
|
||||
w[k, i] = rnd.NextDouble();
|
||||
//Console.WriteLine(String.Format("Weight[{0}, {1}]: {2}",k,i, w[k, i]));
|
||||
}
|
||||
x_actual.Points.AddXY(i, _x[i]);
|
||||
}
|
||||
chart1.Series.Add(x_actual);
|
||||
}
|
||||
|
||||
// Graphen Clearen
|
||||
private void button2_Click(object sender, EventArgs e)
|
||||
{
|
||||
chart1.Series.Clear();
|
||||
Series x_actual = new Series("Actual x Value");
|
||||
x_actual.ChartType = SeriesChartType.Spline;
|
||||
for (int i = 0; i < NumberOfSamples; i++)
|
||||
{
|
||||
x_actual.Points.AddXY(i, _x[i]);
|
||||
}
|
||||
chart1.Series.Add(x_actual);
|
||||
}
|
||||
|
||||
// Bild Laden
|
||||
private void button3_Click(object sender, EventArgs e)
|
||||
{
|
||||
|
||||
OpenFileDialog openFileDialog = new OpenFileDialog();
|
||||
|
||||
if(openFileDialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
try
|
||||
{
|
||||
Bitmap img = new Bitmap(openFileDialog.FileName);
|
||||
pixel_array = new double[img.Width * img.Height];
|
||||
|
||||
for (int i = 1; i < img.Width; i++)
|
||||
{
|
||||
for (int j = 1; j < img.Height; j++)
|
||||
{
|
||||
|
||||
Color pixel = img.GetPixel(i, j);
|
||||
pixel_array[j*i] = pixel.R;
|
||||
|
||||
}
|
||||
}
|
||||
NumberOfSamples = (img.Width * img.Height) / 2;
|
||||
comboBox2.Items.Add(NumberOfSamples);
|
||||
_x = pixel_array;
|
||||
w = new double[NumberOfSamples, NumberOfSamples];
|
||||
for (int i = 0; i < NumberOfSamples; i++)
|
||||
{
|
||||
for (int k = 1; k < NumberOfSamples; k++)
|
||||
{
|
||||
w[k, i] = rnd.NextDouble();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception exep)
|
||||
{
|
||||
MessageBox.Show("Konnte Bild nicht laden.");
|
||||
MessageBox.Show(String.Format("{0}", exep.ToString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,674 @@
|
|||
//
|
||||
//
|
||||
// NLMSvariants.c
|
||||
//
|
||||
// Created by FBRDNLMS on 26.04.18.
|
||||
// Copyright © 2018 FBRDNLMS. All rights reserved.
|
||||
//
|
||||
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <float.h> // DBL_MAX
|
||||
#include <codecvt> // std::codecvt_utf8_utf16
|
||||
#include <locale> // std::wstring_convert
|
||||
#include <string> // std::wstring
|
||||
|
||||
#define M 1000
|
||||
#define tracking 40 //Count of weights
|
||||
#define learnrate 1.0
|
||||
#define PURE_WEIGHTS 0
|
||||
#define USED_WEIGHTS 1
|
||||
#define RESULTS 3
|
||||
#define DIRECT_PREDECESSOR 2
|
||||
#define LOCAL_MEAN 4
|
||||
#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 } };
|
||||
|
||||
/* UTF-8 to UTF-16 win Format*/
|
||||
auto wstring_from_utf8(char const* const utf8_string)
|
||||
-> std::wstring
|
||||
{
|
||||
std::wstring_convert< std::codecvt_utf8_utf16< wchar_t > > converter;
|
||||
return converter.from_bytes(utf8_string);
|
||||
}
|
||||
|
||||
/* *svg graph building* */
|
||||
typedef struct {
|
||||
double xVal[7];
|
||||
double yVal[7];
|
||||
}point_t;
|
||||
|
||||
point_t points[M]; // [0]=xActual, [1]=xPredicted from directPredecessor, [2]=xPredicted from localMean
|
||||
|
||||
/* *ppm reader/writer* */
|
||||
typedef struct {
|
||||
unsigned char red, green, blue;
|
||||
}colorChannel_t;
|
||||
|
||||
typedef struct {
|
||||
int x, y;
|
||||
colorChannel_t *data;
|
||||
}imagePixel_t;
|
||||
|
||||
static imagePixel_t * readPPM(char *fileName);
|
||||
void mkPpmFile(char *fileNamem, imagePixel_t *image);
|
||||
int * ppmColorChannel(imagePixel_t *image);
|
||||
|
||||
/* *file handling* */
|
||||
char * mkFileName(char* buffer, size_t max_len, int suffixId);
|
||||
char *fileSuffix(int id);
|
||||
void myLogger(FILE* fp, point_t points[]);
|
||||
#ifdef _WIN32
|
||||
size_t getline(char **lineptr, size_t *n, FILE *stream);
|
||||
#endif
|
||||
void mkSvgGraph(point_t points[]);
|
||||
|
||||
/* *rand seed* */
|
||||
double r2(void);
|
||||
double rndm(void);
|
||||
|
||||
/* *math* */
|
||||
double sum_array(double x[], int length);
|
||||
void directPredecessor(void);
|
||||
void localMean(void);
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
char fileName[50];
|
||||
int i;
|
||||
|
||||
srand((unsigned int)time(NULL));
|
||||
|
||||
for (i = 0; i < M; i++) {
|
||||
_x[i] += ((255.0 / M) * i); // Init test values
|
||||
for (int k = 0; k < M; k++) {
|
||||
w[k][i] = rndm(); // Init weights
|
||||
}
|
||||
}
|
||||
|
||||
mkFileName(fileName, sizeof(fileName), PURE_WEIGHTS);
|
||||
// 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++) {
|
||||
fprintf(fp0, "[%d][%d] %lf\n", k, i, w[k][i]);
|
||||
}
|
||||
}
|
||||
fclose(fp0);
|
||||
|
||||
|
||||
// math magic
|
||||
localMean();
|
||||
directPredecessor(); // TODO: used_weights.txt has gone missing!
|
||||
|
||||
// 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++) {
|
||||
fprintf(fp1, "[%d][%d] %lf\n", k, i, w[k][i]);
|
||||
}
|
||||
|
||||
}
|
||||
fclose(fp1);
|
||||
|
||||
// getchar();
|
||||
printf("DONE!");
|
||||
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
=======================================================================================
|
||||
|
||||
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;
|
||||
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
|
||||
|
||||
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;
|
||||
points[xCount].xVal[2] = xCount;
|
||||
points[xCount].yVal[2] = xPredicted;
|
||||
double xSquared = 0.0;
|
||||
|
||||
for (i = 1; i < xCount; i++) { //get x squared
|
||||
xSquared = +pow(_x[xCount - i], 2);
|
||||
}
|
||||
|
||||
for (i - 1; i < xCount; i++) { //update weights
|
||||
w[i][xCount + 1] = w[i][xCount] + learnrate * xError[xCount] * (_x[xCount - i] / 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;
|
||||
double deviation = 0.0;
|
||||
|
||||
// Mean square
|
||||
for (i = 0; i < M - 1; i++) {
|
||||
deviation += pow(xError[i], 2);
|
||||
}
|
||||
deviation /= xErrorLength;
|
||||
|
||||
|
||||
// write in file
|
||||
mkFileName(fileName, sizeof(fileName), RESULTS);
|
||||
FILE *fp2 = fopen(fileName, "w");
|
||||
fprintf(fp2, "quadr. Varianz(x_error): {%f}\nMittelwert:(x_error): {%f}\n\n", deviation, mean);
|
||||
fclose(fp2);
|
||||
fclose(fp4);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
===================================
|
||||
|
||||
directPredecessor
|
||||
|
||||
|
||||
Variant (2/3),
|
||||
substract direct predecessor
|
||||
|
||||
===================================
|
||||
*/
|
||||
|
||||
void directPredecessor(void) {
|
||||
char fileName[512];
|
||||
double xError[2048];
|
||||
int xCount = 0, i;
|
||||
double xActual;
|
||||
|
||||
// File handling
|
||||
mkFileName(fileName, sizeof(fileName), DIRECT_PREDECESSOR);
|
||||
FILE *fp3 = fopen(fileName, "w");
|
||||
fprintf(fp3, "\n\n\n\n*********************DirectPredecessor*********************\n");
|
||||
|
||||
for (xCount = 1; xCount < M + 1; xCount++) {
|
||||
xActual = _x[xCount + 1];
|
||||
double xPredicted = 0.0;
|
||||
|
||||
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]);
|
||||
points[xCount].xVal[0] = xCount;
|
||||
points[xCount].yVal[0] = xActual;
|
||||
points[xCount].xVal[1] = xCount;
|
||||
points[xCount].yVal[1] = xPredicted;
|
||||
|
||||
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 < 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
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
mkSvgGraph(points);
|
||||
fprintf(fp3, "{%d}.\tLeast Mean Squared{%f}\tMean{%f}\n\n", xCount, deviation, mean);
|
||||
fclose(fp3);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
|
||||
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,
|
||||
only max_len characters will be written.
|
||||
|
||||
=========================================================================
|
||||
*/
|
||||
|
||||
char *mkFileName(char* buffer, size_t max_len, int suffixId) {
|
||||
const char * format_str = "%Y-%m-%d_%H_%M_%S";
|
||||
size_t date_len;
|
||||
const char * suffix = fileSuffix(suffixId);
|
||||
time_t now = time(NULL);
|
||||
|
||||
strftime(buffer, max_len, format_str, localtime(&now));
|
||||
date_len = strlen(buffer);
|
||||
strncat(buffer, suffix, max_len - date_len);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
|
||||
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" };
|
||||
return suffix[id];
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
svgGraph
|
||||
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
/*
|
||||
void Graph ( ) {
|
||||
char fileName[50];
|
||||
mkFileName(fileName, sizeof(fileName), GRAPH);
|
||||
FILE* fp4 = fopen(fileName, "w");
|
||||
pfrintf
|
||||
*/
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
myLogger
|
||||
|
||||
|
||||
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
|
||||
sprintf(_buffer, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
|
||||
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
|
||||
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
|
||||
sprintf(_buffer, "L %f %f\n", points[i].xVal[1], points[i].yVal[1]);
|
||||
strcat(buffer, _buffer);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
=========================================================================
|
||||
|
||||
sum_array
|
||||
|
||||
|
||||
Sum of all elements in x within a defined length
|
||||
|
||||
=========================================================================
|
||||
*/
|
||||
|
||||
double sum_array(double x[], int length) {
|
||||
int i = 0;
|
||||
double sum = 0.0;
|
||||
|
||||
for (i = 0; i< length; i++) {
|
||||
sum += x[i];
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
r2
|
||||
|
||||
returns a random double value between 0 and 1
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
double r2(void) {
|
||||
return((rand() % 10000) / 10000.0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
rndm
|
||||
|
||||
fills a double variable with random value and returns it
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
double rndm(void) {
|
||||
double rndmval = r2();
|
||||
return rndmval;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
getline
|
||||
|
||||
This code is public domain -- Will Hartung 4/9/09 //edited by Kevin Becker
|
||||
Microsoft Windows is not POSIX conform and does not support getline.
|
||||
|
||||
=========================================================================
|
||||
*/
|
||||
#ifdef _WIN32
|
||||
size_t getline(char **lineptr, size_t *n, FILE *stream) {
|
||||
char *bufptr = NULL;
|
||||
char *p = bufptr;
|
||||
size_t size;
|
||||
int c;
|
||||
|
||||
if (lineptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (stream == NULL) {
|
||||
return -1;
|
||||
}
|
||||
if (n == NULL) {
|
||||
return -1;
|
||||
}
|
||||
bufptr = *lineptr;
|
||||
size = *n;
|
||||
|
||||
c = fgetc(stream);
|
||||
if (c == EOF) {
|
||||
return -1;
|
||||
}
|
||||
if (bufptr == NULL) {
|
||||
char c[128];
|
||||
memset(c, 0, sizeof(c));
|
||||
bufptr = c;
|
||||
if (bufptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
size = 128;
|
||||
}
|
||||
p = bufptr;
|
||||
while (c != EOF) {
|
||||
if ((p - bufptr) > (size - 1)) {
|
||||
size = size + 128;
|
||||
realloc(bufptr, size);
|
||||
if (bufptr == NULL) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
*p++ = c;
|
||||
if (c == '\n') {
|
||||
break;
|
||||
}
|
||||
c = fgetc(stream);
|
||||
}
|
||||
|
||||
*p++ = '\0';
|
||||
*lineptr = bufptr;
|
||||
*n = size;
|
||||
|
||||
return p - bufptr - 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/*
|
||||
==========================================================================
|
||||
|
||||
mkSvgGraph
|
||||
|
||||
parses template.svg and writes results in said template
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
void mkSvgGraph(point_t points[]) {
|
||||
FILE *input = fopen("template.svg", "r");
|
||||
FILE *target = fopen("output.svg", "w");
|
||||
char line[512];
|
||||
// char *ptr;
|
||||
//size_t len = 0;
|
||||
//ssize_t read;
|
||||
//char values[64];
|
||||
char firstGraph[15] = { "<path d=\"M0 0" };
|
||||
|
||||
if (input == NULL) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
char buffer[131072] = "";
|
||||
|
||||
memset(buffer, '\0', sizeof(buffer));
|
||||
//int length = 0;
|
||||
while(!feof(input)) {
|
||||
fgets(line, 512, input);
|
||||
strncat(buffer, line,strlen(line));
|
||||
printf("%s\n", line);
|
||||
if (strstr(line, firstGraph) != NULL) {
|
||||
bufferLogger(buffer, points);
|
||||
}
|
||||
|
||||
}
|
||||
fprintf(target, buffer);
|
||||
//int c = 0;
|
||||
puts(buffer);
|
||||
//getchar();
|
||||
|
||||
//if (strstr(line, firstGraph) != NULL) {
|
||||
// //fprintf(target,"HECK!!!\n");
|
||||
// bufferLogger(strstr(line, firstGraph),sizeof(firstGraph), points);
|
||||
//}
|
||||
|
||||
/*while ((read = getline(&line, &len, input)) != -1) {
|
||||
//printf("Retrieved line of length %zu :\n", read);
|
||||
//puts(line); // debug purpose
|
||||
fprintf(target, "%s", line);
|
||||
if (strstr(line, firstGraph) != NULL) {
|
||||
fprintf(target,"HECK!!!\n");
|
||||
myLogger(target, points);
|
||||
}
|
||||
}*/
|
||||
|
||||
/*free(line);
|
||||
free(target);
|
||||
free(input);*/
|
||||
//exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/*
|
||||
===========================================================================
|
||||
|
||||
rdPPM
|
||||
|
||||
reads data from file of type PPM, stores colorchannels in a struct in the
|
||||
size of given picture
|
||||
|
||||
==========================================================================
|
||||
*/
|
||||
|
||||
static imagePixel_t *rdPPM(const char *fileName) {
|
||||
char buffer[16];
|
||||
imagePixel_t *image;
|
||||
int c, rgbColor;
|
||||
|
||||
FILE *fp = fopen(fileName, "rb");
|
||||
if (!fp) {
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (!fgets(buffer, sizeof(buffer), fp)) {
|
||||
perror(fileName);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (buffer[0] != 'P' || buffer[1] != '6') {
|
||||
fprintf(stderr, "No PPM file format\n");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
image = (imagePixel_t *)malloc(sizeof(imagePixel_t));
|
||||
if (!image) {
|
||||
fprintf(stderr, "malloc() failed");
|
||||
}
|
||||
c = getc(fp);
|
||||
while (c == '#') {
|
||||
while (getc(fp) != '\n');
|
||||
c = getc(fp);
|
||||
}
|
||||
ungetc(c, fp);
|
||||
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) {
|
||||
fprintf(stderr, "Invalid rgb component in %s\n", fileName);
|
||||
}
|
||||
if (rgbColor != RGB_COLOR) {
|
||||
fprintf(stderr, "Invalid image color range in %s\n", fileName);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
while (fgetc(fp) != '\n');
|
||||
image->data = (colorChannel_t *)malloc(image->x * image->y * sizeof(imagePixel_t));
|
||||
if (!image) {
|
||||
fprintf(stderr, "malloc() failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (fread(image->data, 3 * image->x, image->y, fp) != image->y) {
|
||||
fprintf(stderr, "Loading image failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
fclose(fp);
|
||||
return image;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
=======================================================================================
|
||||
|
||||
mkPpmFile
|
||||
|
||||
gets output from the result of rdPpmFile and writes a new mkPpmFile. Best Case is a
|
||||
carbon copy of the source image
|
||||
|
||||
=======================================================================================
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/*
|
||||
======================================================================================
|
||||
|
||||
ppmColorChannel
|
||||
|
||||
gets one of the rgb color channels and returns the array
|
||||
|
||||
======================================================================================
|
||||
*/
|
||||
|
||||
int * ppmColorChannel(imagePixel_t *image) {
|
||||
int length = (image->x * image->y) / 3;
|
||||
int i = 0;
|
||||
int buffer[10];
|
||||
realloc(buffer,length);
|
||||
|
||||
printf("%d\n", length);
|
||||
if (image) {
|
||||
for (i = 0; i < length; i++) {
|
||||
buffer[i] = image->data[i].green;
|
||||
//output[i] = image->data[i].blue;
|
||||
//output[i] = image->data[i].red;
|
||||
}
|
||||
}
|
||||
return buffer;
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
|
||||
NLMSvariants | Graphical Output ||
|
||||
<font id="1" color="blue" onclick="clicksvg(this)">Eingangswert</font> |
|
||||
<font id="2" color="red" onclick="clicksvg(this)">direkter Vorgaenger</font> |
|
||||
<font id="3" color="green" onclick="clicksvg(this)">letzter Mittelwert</font>
|
||||
<script>
|
||||
function clicksvg(e){
|
||||
id = e.id
|
||||
graph = document.getElementById("svg_" + id);
|
||||
if(graph.style.visibility == "hidden" || !graph.style.visibility){
|
||||
graph.style.visibility = "visible";
|
||||
}else{
|
||||
graph.style.visibility = "hidden";
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
<body>
|
||||
<svg height="1200" viewBox="100 50 400 -400" width="3000" version="1.1"
|
||||
xmlns="http://www.w3.org/2000/svg">
|
||||
<desc>NLMSvariants output graph
|
||||
</desc>
|
||||
<defs>
|
||||
<pattern id="smallGrid" width="10" height="10" patternUnits="userSpaceOnUse">
|
||||
<path d="M 10 0 L 0 0 0 10" fill="none" stroke="gray" stroke-width="0.5"></path>
|
||||
</pattern>
|
||||
<pattern id="grid10" width="100" height="100" patternUnits="userSpaceOnUse">
|
||||
<rect width="100" height="100" fill="url(#smallGrid)"></rect>
|
||||
<path d="M 100 0 L 0 0 0 100" fill="none" stroke="gray" stroke-width="1"></path>
|
||||
</pattern>
|
||||
</defs>
|
||||
<rect fill="white" height="1200" width="3000" y="0"></rect>
|
||||
<rect fill="url(#grid10)" height="1200" width="3000" y="0"></rect>
|
||||
<g transform="translate(0,0) scale(1, 1)">
|
||||
<line class="l1 s-black " stroke="black" x1="-200" x2="3000" y1="400" y2="400"></line>
|
||||
<line class="l1 s-black " stroke="black" x1="200" x2="200" y1="-200" y2="1200"></line>
|
||||
</g>
|
||||
<g transform="translate(200, 400) scale(1,-1)">
|
||||
<path d="M0 0
|
||||
<text class="t36 t-mid bold f-black" x="50" y="50">+ +</text>
|
||||
<text class="t36 t-mid bold f-black" x="-50" y="50">- +</text>
|
||||
<text class="t36 t-mid bold f-black" x="50" y="-50">+ -</text>
|
||||
<text class="t36 t-mid bold f-black" x="-50" y="-50">- -</text>
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
|
||||
<table width = "100%" border = 1>
|
||||
<tr align = "top">
|
||||
<td colspan = "2" bgcolor = "#fefefe">
|
||||
<h1>
|
||||
<font color="blue">Eingangswert</font> |
|
||||
<font color="red">direkter Vorgaenger</font> |
|
||||
<font color="green">letzter Mittelwert</font>
|
||||
</h1>
|
||||
</td>
|
||||
</tr>
|
||||
</body>
|
||||
<html>
|
Loading…
Reference in New Issue