Skip to content

Commit

Permalink
coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
tomershay100 committed Apr 15, 2021
1 parent 2cec6da commit f7db571
Show file tree
Hide file tree
Showing 11 changed files with 715 additions and 820 deletions.
398 changes: 214 additions & 184 deletions AnomalyData.cs

Large diffs are not rendered by default.

86 changes: 52 additions & 34 deletions AnomalyDetectionUtil.cs
Original file line number Diff line number Diff line change
@@ -1,64 +1,78 @@
using System;
using System.Collections.Generic;

namespace DesktopApp
{
//Line class
public class Line
{
public float a, b;
private readonly float _a;
private readonly float _b;

public Line()
{
a = 0;
b = 0;
_a = 0;
_b = 0;
}

public Line(float a, float b)
{
this.a = a;
this.b = b;
this._a = a;
this._b = b;
}
public float f(float x)

public float F(float x)
{
return a * x + b;
return _a * x + _b;
}
}

//Point class
public class Point
{
public float x, y;
public readonly float X;
public readonly float Y;

public Point(float x, float y)
{
this.x = x;
this.y = y;
X = x;
Y = y;
}
}

//AnomalyDetectionUtil library
public class AnomalyDetectionUtil
public static class AnomalyDetectionUtil
{
//average of x
public static float Avg(float[] x)
private static float Avg(IReadOnlyList<float> x)
{
float sum = 0;
for (int i = 0; i < x.Length; sum += x[i], i++) ;
return sum / x.Length;
for (var i = 0; i < x.Count; sum += x[i], i++)
{
}

return sum / x.Count;
}

// returns the variance of X and Y
public static float Var(float[] x)
private static float Var(IReadOnlyList<float> x)
{
float av = Avg(x);
var av = Avg(x);
float sum = 0;
for (int i = 0; i < x.Length; i++)
foreach (var t in x)
{
sum += x[i] * x[i];
sum += t * t;
}

return sum / x.Length - av * av;
return sum / x.Count - av * av;
}

// returns the covariance of X and Y
public static float Cov(float[] x, float[] y)
private static float Cov(IReadOnlyList<float> x, IReadOnlyList<float> y)
{
int size = Math.Min(y.Length, x.Length); //same as y.Length
var size = Math.Min(y.Count, x.Count); //same as y.Length
float sum = 0;
for (int i = 0; i < size; i++)
for (var i = 0; i < size; i++)
{
sum += x[i] * y[i];
}
Expand All @@ -67,39 +81,43 @@ public static float Cov(float[] x, float[] y)

return sum - Avg(x) * Avg(y);
}

// returns the Pearson correlation coefficient of X and Y
public static float Pearson(float[] x, float[] y)
{
float r = Cov(x, y) / (float) (Math.Sqrt(Var(x)) * Math.Sqrt(Var(y)));
var r = Cov(x, y) / (float) (Math.Sqrt(Var(x)) * Math.Sqrt(Var(y)));
return float.IsNaN(r) ? 0 : r;
}

// performs a linear regression and returns the line equation
public static Line LinearReg(Point[] points)
{
int size = points.Length;
float[] x = new float[size];
float[] y = new float[size];
for (int i = 0; i < size; i++)
var size = points.Length;
var x = new float[size];
var y = new float[size];
for (var i = 0; i < size; i++)
{
x[i] = points[i].x;
y[i] = points[i].y;
x[i] = points[i].X;
y[i] = points[i].Y;
}

float a = Cov(x, y) / Var(x);
float b = Avg(y) - a * (Avg(x));
var a = Cov(x, y) / Var(x);
var b = Avg(y) - a * (Avg(x));

return new Line(a, b);
}

// returns the deviation between point p and the line equation of the points
public static float Dev(Point p, Point[] points)
{
Line l = LinearReg(points);
var l = LinearReg(points);
return Dev(p, l);
}

// returns the deviation between point p and the line
public static float Dev(Point p, Line l)
private static float Dev(Point p, Line l)
{
float x = p.y - l.f(p.x);
var x = p.Y - l.F(p.X);
if (x < 0)
x *= -1;
return x;
Expand Down
80 changes: 39 additions & 41 deletions Client.cs
Original file line number Diff line number Diff line change
@@ -1,93 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.ComponentModel;

namespace DesktopApp
{
//Client class
public class Client
{
protected int _port;
protected TcpClient _client;
protected NetworkStream _stream;
protected bool isRunning = false;
private int _port;
protected readonly TcpClient TcpClient;
protected readonly NetworkStream Stream;
protected bool IsRunning = false;

public Client(int port)
protected Client(int port)
{
_port = port;
try
{
_client = new TcpClient("127.0.0.1", port);
_stream = _client.GetStream();
isRunning = true;
TcpClient = new TcpClient("127.0.0.1", port);
Stream = TcpClient.GetStream();
IsRunning = true;
}
catch (Exception)
{
MessageBox.Show("Connection Error", "Alert", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
}

//connect to the FlightGear app
public class FlightGearClient : Client, INotifyPropertyChanged
{
protected double _sendSpeed;
protected double SendSpeed;
public event PropertyChangedEventHandler PropertyChanged;

private int _num_line; //the line number that currently being sent.
public int _Num_line
private int _numLine; //the line number that currently being sent.

public int _NumLine
{
get { return _num_line; }
get => _numLine;
set
{
//m.WaitOne();
_num_line = value;
_numLine = value;
//m.ReleaseMutex();
NotifyPropertyChanged("_Num_line");
}
}
public static Mutex m = new Mutex();

protected static readonly Mutex M = new Mutex();

//Constructor
public FlightGearClient() : base(5400)
protected FlightGearClient() : base(5400)
{
_sendSpeed = 10;
_Num_line = 0;
SendSpeed = 10;
_NumLine = 0;
}

//Constructor
public FlightGearClient(int port) : base(port)
protected FlightGearClient(int port) : base(port)
{
_sendSpeed = 10;
_Num_line = 0;
SendSpeed = 10;
_NumLine = 0;
}
//Distructor

//Destructor
~FlightGearClient()
{
if (isRunning)
{
// Close socket.
_stream.Close();
_client.Close();
}
if (!IsRunning) return;
// Close socket.
Stream.Close();
TcpClient.Close();
}

//Sets the sending speed.
public void SetSpeed(double d)
{
m.WaitOne();
this._sendSpeed = d;
m.ReleaseMutex();
M.WaitOne();
SendSpeed = d;
M.ReleaseMutex();
}
public void NotifyPropertyChanged(string propName)

protected void NotifyPropertyChanged(string propName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
}
}
Loading

0 comments on commit f7db571

Please sign in to comment.