diff --git a/bin/NLMS_with_UI.exe b/bin/NLMS_with_UI.exe new file mode 100644 index 0000000..8f93a3c Binary files /dev/null and b/bin/NLMS_with_UI.exe differ diff --git a/bin/NLMSvariants.cs b/bin/NLMSvariants.cs new file mode 100644 index 0000000..0710d17 --- /dev/null +++ b/bin/NLMSvariants.cs @@ -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 x = new Stack(); + 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())); + } + } + } + } + +} + diff --git a/bin/NMLS_Graphisch.exe b/bin/NMLS_Graphisch.exe deleted file mode 100644 index b8a3d5f..0000000 Binary files a/bin/NMLS_Graphisch.exe and /dev/null differ diff --git a/bin/NMLS_c_sharp_variante.cs b/bin/NMLS_c_sharp_variante.cs deleted file mode 100644 index bde6665..0000000 --- a/bin/NMLS_c_sharp_variante.cs +++ /dev/null @@ -1,232 +0,0 @@ - -// Variable mit der anzahl Punkten zwischen 0 - 255 -static int M = 1000; - -// Variable zum tracken der Gewichte -const int tracking = 40; - -// C# only zum erstellen von Randomzahlen -static Random rnd = new Random(); - -// Array mit den Testwerten -static double[] _x = new double[M]; - -// Array mit den Gewichten -static double[,] w = new double[M, M]; - -// Lernrate -static double learnrate = 1; - -/************************************************************** -main() des Programms -***************************************************************/ - -int main(){ - - // Initialisierung des Test Array + Gewichte - for (int i = 0; i < M; i++) - { - _x[i] += ((255.0 / M) * i); - for (int k = 1; k < M; k++) - { - w[k, i] = rnd.NextDouble(); - //Console.WriteLine(String.Format("Weight: {0}", w[k, i])); - } - - } - - // Zum erstellen eines Files mit den Gewichten vor den Updates - for (int i = 0; i < tracking; i++) - { - for (int k = 1; k < tracking; k++) - { - File.AppendAllText("weights.txt", - String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()), - Encoding.UTF8); - } - } - - // Variante die Ausgeführt werden soll - lokalerMittelWert(); - - // Zum erstellen eines Files mit den Gewichten nach den Updates - for (int i = 0; i < tracking; i++) - { - for (int k = 1; k < tracking; k++) - { - File.AppendAllText("weights_after.txt", - String.Format("[{0}][{1}] {2}\n", k, i, Math.Round(w[k, i], 2).ToString()), - Encoding.UTF8); - } - - } - - -} - -/************************************************************** -Errechnet die 1. Variante, mit abziehen des lokalen Mittelwertes -***************************************************************/ - -void lokalerMittelWert() -{ - //Array in dem e(n) gespeichert wird - double[] x_error = new double[M]; - - //Laufzeitvariable - int x_count = 0; - - // x_count + 1 da x_count > 0 sein muss - while (x_count + 1 < M) - { - //Erstellt ein neues Array mit allen werten bis zur Laufzeitvariable - double[] x_part_Array = new double[x_count]; - Array.Copy(_x, 0, x_part_Array, 0, x_count); - double x_middle = (x_count > 0) ? ( x_part_Array.Sum() / x_count) : 0; - - // Variable für die errechnete Zahl - double x_pred = 0.0; - - // Variable mit der eigentlichen Zahl - double x_actual = _x[x_count + 1]; - - for (int i = 1; i < x_count; i++) - { - x_pred += (w[i, x_count] * (_x[x_count - i] - x_middle)); - } - x_pred += x_middle; - - //Console.WriteLine(String.Format("X_sum: {0}", x_middle)); - - //Console.WriteLine(String.Format("X_pred: {0}", x_pred)); - //Console.WriteLine(String.Format("X_actual: {0}", x_actual)); - - x_error[x_count] = x_actual - x_pred; - - // Funktion zum berechnen des Quadrates - double x_square = 0; - for (int i = 1; i <= x_count; i++) - { - x_square += Math.Pow(_x[x_count - i], 2); - } - - // Funktion zum updaten der Gewichte - for (int i = 1; i < x_count; i++) - { - w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * (_x[x_count - i] / x_square); - } - - // Laufzeitvariable hochzählen - x_count += 1; - } - - // Berechenen des mittleren Fehlers - double mittel = x_error.Sum() / x_error.Length; - - // Berechenen der varianz des Fehlers - double varianz = 0.0; - foreach (double x_e in x_error) - { - varianz += Math.Pow(x_e - mittel, 2); - } - varianz /= x_error.Length; - - // Hängt dem Angegebenen File den Vorgegebenen String an - File.AppendAllText("ergebnisse.txt", - String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel), - Encoding.UTF8); -} - -/************************************************************** -Errechnet die 2. Variante, mit abziehen des direkten Vorgängers -***************************************************************/ - -void direkterVorgaenger() -{ - //Array in dem e(n) gespeichert wird - double[] x_error = new double[M]; - - //Laufzeitvariable - int x_count = 0; - - // x_count + 1 da x_count > 0 sein muss - while (x_count + 1 < M) - { - // Variable für die errechnete Zahl - double x_pred = 0.0; - - // Variable mit der eigentlichen Zahl - double x_actual = _x[x_count + 1]; - - // Funktion fürs berechnen der Vorhersagezahl - for (int i = 1; i < x_count; i++) - { - x_pred += (w[i, x_count] * (_x[x_count - i] - _x[x_count - i - 1])); - } - x_pred += _x[x_count - 1]; - - //Console.WriteLine(String.Format("X_pred: {0}", x_pred)); - - // Hängt dem Angegebenen File den Vorgegebenen String an - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_pred {1}\n", x_count, x_pred), - Encoding.UTF8); - - //Console.WriteLine(String.Format("X_actual: {0}", x_actual)); - - // Hängt dem Angegebenen File den Vorgegebenen String an - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_actual {1}\n", x_count, x_actual), - Encoding.UTF8); - - // Berechnung des Fehlers - x_error[x_count] = x_actual - x_pred; - - - //Console.WriteLine(String.Format("X_error: {0}", x_error)); - - // Hängt dem Angegebenen File den Vorgegebenen String an - File.AppendAllText("direkterVorgaenger.txt", - String.Format("{0}. X_error {1}\n\n", x_count, x_error), - Encoding.UTF8); - - // Funktion zum berechnen des Quadrates - double x_square = 0; - for (int i = 1; i < x_count; i++) - { - x_square += Math.Pow(_x[x_count - i] - _x[x_count - i - 1], 2); - } - - //Console.WriteLine(String.Format("X_square: {0}", x_square)); - - // Hängt dem Angegebenen File den Vorgegebenen String an - //File.AppendAllText("direkterVorgaenger.txt", - // String.Format("{0}. X_square {1}\n", x_count, x_square), - // Encoding.UTF8); - - // Funktion zum updaten der Gewichte - for (int i = 1; i < x_count; i++) - { - w[i, x_count + 1] = w[i, x_count] + learnrate * x_error[x_count] * ((_x[x_count - i] - _x[x_count - i - 1]) / x_square); - } - - // Laufzeitvariable hochzählen - x_count += 1; - } - - // Berechenen des mittleren Fehlers - double mittel = x_error.Sum() / x_error.Length; - - // Berechenen der varianz des Fehlers - double varianz = 0.0; - foreach (double x_e in x_error) - { - varianz += Math.Pow(x_e - mittel, 2); - } - varianz /= x_error.Length; - - // Hängt dem Angegebenen File den Vorgegebenen String an - File.AppendAllText("ergebnisse.txt", - String.Format("Quadratische Varianz(x_error): {0}\n Mittelwert(x_error): {1}\n\n", varianz, mittel), - Encoding.UTF8); -} \ No newline at end of file diff --git a/bin/graphResults.html b/bin/graphResults.html index e458220..0750327 100644 --- a/bin/graphResults.html +++ b/bin/graphResults.html @@ -3,12 +3,3053 @@ NLMSvariants | Graphical Output || - Eingangswert | - direkter Vorgaenger | - letzter Mittelwert + Eingangswert | + direkter Vorgaenger | + letzter Mittelwert + - + + NLMSvariants output graph + + + + + + + + + + + + + + + + + + + + + + + - + + + - + - - + + diff --git a/bin/template.svg b/bin/template.svg index 4288f37..6d7b086 100644 --- a/bin/template.svg +++ b/bin/template.svg @@ -1,6 +1,3 @@ - - NLMSvariants output graph @@ -22,7 +19,7 @@ + " fill="none" stroke="red" stroke-width="0.4px" onclick="clicksvg(this)"/> + + - + + -