LevelMeter plugin for SDR# using analog-like display Prebuilt archives: https://pub.sigpipe.me/projects/SDRSharp/ Forked from http://levelmeter.sub-web.de/ / https://app.assembla.com/spaces/sdrsoft/subversion/source
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Simova.cs 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. namespace SDRSharp.LevelMeter
  6. {
  7. class Simova
  8. {
  9. /*
  10. Simple Moving Average Class
  11. */
  12. ArrayList _buffer;
  13. int _cnt=0;
  14. bool _isValid = false;
  15. float _average = 0;
  16. float _average_last = 0;
  17. public Simova(int NumberOfElements, float InitValue)
  18. {
  19. _cnt = 0;
  20. _isValid = false;
  21. _buffer = new ArrayList(NumberOfElements);
  22. for (int i = 0; i < NumberOfElements; i++)
  23. {
  24. _buffer.Add(InitValue);
  25. }
  26. }
  27. public float Add(float data)
  28. {
  29. for (int i = 0; i < _buffer.Count-1; i++)
  30. {
  31. float tmp = (float)_buffer[i+1];
  32. _buffer[i + 1] = (float)_buffer[0];
  33. _buffer[0] = tmp;
  34. }
  35. _buffer[0] = data;
  36. if (++_cnt == _buffer.Count)
  37. {
  38. _isValid = true;
  39. }
  40. _average = CalculateAverage();
  41. return _average;
  42. }
  43. public float Average
  44. {
  45. get
  46. {
  47. _average_last = _average;
  48. return _average;
  49. }
  50. }
  51. public float Average_Last
  52. {
  53. get
  54. {
  55. return _average_last;
  56. }
  57. }
  58. public float Current
  59. {
  60. get { return (float)_buffer[0]; }
  61. }
  62. public bool IsValid
  63. {
  64. get { return _isValid; }
  65. }
  66. float CalculateAverage()
  67. {
  68. float Result=0;
  69. for (int i = 0; i < _buffer.Count; i++)
  70. {
  71. Result += (float)_buffer[i];
  72. }
  73. Result /= _buffer.Count;
  74. return Result;
  75. }
  76. public string BufferToString()
  77. {
  78. string res = "";
  79. for (int i = 0; i < _buffer.Count; i++)
  80. {
  81. float x = (float)_buffer[i];
  82. res += x.ToString() + " ";
  83. }
  84. return res;
  85. }
  86. public void TestThis()
  87. {
  88. Simova Sma = new Simova(5, 0);
  89. Console.WriteLine(Sma.Add(1).ToString());
  90. Console.WriteLine(Sma.BufferToString());
  91. Console.WriteLine(Sma.Add(2).ToString());
  92. Console.WriteLine(Sma.BufferToString());
  93. Console.WriteLine(Sma.Add(3).ToString());
  94. Console.WriteLine(Sma.BufferToString());
  95. Console.WriteLine(Sma.Add(4).ToString());
  96. Console.WriteLine(Sma.BufferToString());
  97. Console.WriteLine(Sma.Add(5).ToString());
  98. Console.WriteLine(Sma.BufferToString());
  99. Console.WriteLine(Sma.Add(6).ToString());
  100. Console.WriteLine(Sma.BufferToString());
  101. Console.WriteLine(Sma.Add(7).ToString());
  102. Console.WriteLine(Sma.BufferToString());
  103. if (Sma.Add(8) != 6)
  104. {
  105. throw new Exception("Simova selftest failed!");
  106. }
  107. }
  108. }
  109. }