Program.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace CheckTest
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. List<string> dd = ReadLog();
  14. System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
  15. sw.Start();
  16. List<double> listy = new List<double>();
  17. List<double> listx = new List<double>();
  18. List<int> vadd = new List<int>();
  19. vadd = dd.ConvertAll<int>(x => Convert.ToInt32(x));
  20. for (int k = 166; k < 646; k++)
  21. {
  22. double x = vadd[k] * Math.Sin((-135 + 0.33 * k) * Math.PI / 180);
  23. double y = vadd[k] * Math.Cos((-135 + 0.33 * k) * Math.PI / 180);
  24. listy.Add(y);
  25. listx.Add(x);
  26. }
  27. List<double> dis = lineFit(listx, listy);
  28. bool isHinder = IsHinder(dis, 20);
  29. sw.Stop();
  30. Console.WriteLine("结果:"+isHinder);
  31. Console.WriteLine(sw.ElapsedMilliseconds + "ms");
  32. Console.Read();
  33. }
  34. private static List<string> ReadLog()
  35. {
  36. List<string> digitList = new List<string>();
  37. string filePath = "log//data0.txt";
  38. try
  39. {
  40. List<string> logLines = null;
  41. if (File.Exists(filePath))
  42. {
  43. logLines = new List<string>(File.ReadAllLines(filePath));
  44. }
  45. if (logLines != null)
  46. {
  47. for (int i = 0; i < logLines.Count; i++)
  48. {
  49. string logLine = logLines[i];
  50. digitList.Add(logLine);
  51. }
  52. }
  53. }
  54. catch (Exception e) { Console.WriteLine(e.Message); }
  55. return digitList;
  56. }
  57. private static List<double> lineFit(List<double> x, List<double> y)
  58. {
  59. double a, b, c;
  60. int size = x.Count;
  61. if (size < 2)
  62. {
  63. a = 0;
  64. b = 0;
  65. c = 0;
  66. return null;
  67. }
  68. double x_mean = 0;
  69. double y_mean = 0;
  70. for (int i = 0; i < size; i++)
  71. {
  72. x_mean += x[i];
  73. y_mean += y[i];
  74. }
  75. x_mean /= size;
  76. y_mean /= size; //至此,计算出了 x y 的均值
  77. double Dxx = 0, Dxy = 0, Dyy = 0;
  78. for (int i = 0; i < size; i++)
  79. {
  80. Dxx += (x[i] - x_mean) * (x[i] - x_mean);
  81. Dxy += (x[i] - x_mean) * (y[i] - y_mean);
  82. Dyy += (y[i] - y_mean) * (y[i] - y_mean);
  83. }
  84. double lambda = ((Dxx + Dyy) - Math.Sqrt((Dxx - Dyy) * (Dxx - Dyy) + 4 * Dxy * Dxy)) / 2.0;
  85. double den = Math.Sqrt(Dxy * Dxy + (lambda - Dxx) * (lambda - Dxx));
  86. a = Dxy / den;
  87. b = (lambda - Dxx) / den;
  88. c = -a * x_mean - b * y_mean;
  89. Console.WriteLine(a+" "+b+" "+c);
  90. List<double> dis = new List<double>();
  91. for (int i = 0; i < size; i++)
  92. {
  93. double d = Math.Abs(a * x[i] + b * y[i] + c) / Math.Sqrt(a * a + b * b);
  94. dis.Add(d);
  95. }
  96. return dis;
  97. }
  98. private static bool IsHinder(List<double> testdata, double deviation)
  99. {
  100. bool isHinder = true;
  101. //Data errordata = new Data();
  102. List<int> recorddata = new List<int>();
  103. for(int i = 0; i < testdata.Count; i++)
  104. {
  105. if (testdata[i] > deviation)
  106. {
  107. //errordata.key = i;
  108. //errordata.value = testdata[i];
  109. Console.WriteLine(i+" "+testdata[i]);
  110. recorddata.Add(i);
  111. }
  112. }
  113. if (recorddata.Count <= 5||IsBorderUpon(recorddata,5)<1)
  114. {
  115. isHinder = false;
  116. }
  117. return isHinder;
  118. }
  119. private static int IsBorderUpon(List<int> arr ,int num)
  120. {
  121. var query = arr.OrderBy(p => p).Aggregate<int, List<List<int>>>(null, (m, n) =>
  122. {
  123. if (m == null) return new List<List<int>>() { new List<int>() { n } };
  124. if (m.Last().Last() != n - 1)
  125. {
  126. m.Add(new List<int>() { n });
  127. }
  128. else
  129. {
  130. m.Last().Add(n);
  131. }
  132. return m;
  133. });
  134. int flag = 0;
  135. for (int i = 0; i < query.Count; i++)
  136. {
  137. if (query[i].Count > 5)
  138. {
  139. flag++;
  140. }
  141. }
  142. return flag;
  143. }
  144. }
  145. }