153 lines
4.1 KiB
C#
153 lines
4.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Drawing.Printing;
|
|
using ZedGraph;
|
|
|
|
namespace GenesisDisplayTool
|
|
{
|
|
|
|
public enum InitialCrcValue { Zeros, NonZero1 = 0xffff, NonZero2 = 0x1D0F }
|
|
|
|
public class Crc16Ccitt
|
|
{
|
|
const ushort poly = 4129;
|
|
ushort[] table = new ushort[256];
|
|
ushort initialValue = 0;
|
|
|
|
public ushort ComputeChecksum(byte[] bytes)
|
|
{
|
|
ushort crc = this.initialValue;
|
|
for (int i = 0; i < bytes.Length; ++i)
|
|
{
|
|
crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
public byte[] ComputeChecksumBytes(byte[] bytes)
|
|
{
|
|
ushort crc = ComputeChecksum(bytes);
|
|
return BitConverter.GetBytes(crc);
|
|
}
|
|
|
|
public Crc16Ccitt(InitialCrcValue initialValue)
|
|
{
|
|
this.initialValue = (ushort)initialValue;
|
|
ushort temp, a;
|
|
for (int i = 0; i < table.Length; ++i)
|
|
{
|
|
temp = 0;
|
|
a = (ushort)(i << 8);
|
|
for (int j = 0; j < 8; ++j)
|
|
{
|
|
if (((temp ^ a) & 0x8000) != 0)
|
|
{
|
|
temp = (ushort)((temp << 1) ^ poly);
|
|
}
|
|
else
|
|
{
|
|
temp <<= 1;
|
|
}
|
|
a <<= 1;
|
|
}
|
|
table[i] = temp;
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class MyListExtensions
|
|
{
|
|
public static double Mean(this List<double> values)
|
|
{
|
|
return values.Count == 0 ? 0 : values.Mean(0, values.Count);
|
|
}
|
|
|
|
public static double Mean(this List<double> values, int start, int end)
|
|
{
|
|
double s = 0;
|
|
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
s += values[i];
|
|
}
|
|
|
|
return s / (end - start);
|
|
}
|
|
|
|
public static double Variance(this List<double> values)
|
|
{
|
|
return values.Variance(values.Mean(), 0, values.Count);
|
|
}
|
|
|
|
public static double Variance(this List<double> values, double mean)
|
|
{
|
|
return values.Variance(mean, 0, values.Count);
|
|
}
|
|
|
|
public static double Variance(this List<double> values, double mean, int start, int end)
|
|
{
|
|
double variance = 0;
|
|
|
|
for (int i = start; i < end; i++)
|
|
{
|
|
variance += (double)Math.Pow((values[i] - mean), 2);
|
|
}
|
|
|
|
int n = end - start;
|
|
if (start > 0) n -= 1;
|
|
|
|
return variance / (n);
|
|
}
|
|
|
|
public static double StandardDeviation(this List<double> values)
|
|
{
|
|
return values.Count == 0 ? 0 : values.StandardDeviation(0, values.Count);
|
|
}
|
|
|
|
public static double StandardDeviation(this List<double> values, int start, int end)
|
|
{
|
|
double mean = values.Mean(start, end);
|
|
double variance = values.Variance(mean, start, end);
|
|
|
|
return (double)Math.Sqrt(variance);
|
|
}
|
|
|
|
}
|
|
|
|
public class HexadecimalEncoding
|
|
{
|
|
public static string ToHexString(string str)
|
|
{
|
|
var sb = new StringBuilder();
|
|
|
|
var bytes = Encoding.Unicode.GetBytes(str);
|
|
foreach (var t in bytes)
|
|
{
|
|
sb.Append(t.ToString("X2"));
|
|
}
|
|
|
|
return sb.ToString(); // returns: "48656C6C6F20776F726C64" for "Hello world"
|
|
}
|
|
|
|
public static string FromHexString(string hexString)
|
|
{
|
|
var bytes = new byte[hexString.Length / 2];
|
|
for (var i = 0; i < bytes.Length; i++)
|
|
{
|
|
bytes[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
|
|
}
|
|
|
|
return Encoding.Unicode.GetString(bytes); // returns: "Hello world" for "48656C6C6F20776F726C64"
|
|
}
|
|
|
|
|
|
}
|
|
|
|
} |