Advertisement
Guest User

Untitled

a guest
Dec 30th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.67 KB | None | 0 0
  1. Присваивает имя "Sample" выделенному массиву
  2. Sub AssignSample()
  3. Dim MR As Object, R, i
  4. Set MR = Application.Selection
  5. MR.Name = "Sample"
  6. For i = 1 To MR.Count
  7. Cells(i + 1, 1) = i
  8. Next i
  9. R = MR.Count + 2
  10. Range(Cells(R, 1), Cells(1000, 2)).ClearContents
  11. End Sub
  12. 'Перерисовка картинки
  13. Sub Redraw()
  14. Range("F1:G401").Select
  15. Selection.Copy
  16. Range("H1").Select
  17. ActiveSheet.Paste
  18. End Sub
  19. Sub FindMaximums()
  20. Dim i As Integer, k As Integer
  21. Dim D(1 To DensityResolution) As Double
  22. Range("K2:L500").ClearContents
  23. For i = 1 To DensityResolution
  24. D(i) = Cells(i + 1, 7).Value
  25. Next i
  26. k = 1
  27. For i = 2 To DensityResolution - 1
  28. If D(i) > D(i - 1) And D(i) > D(i + 1) Then
  29. k = k + 1
  30. Cells(k, 12).Value = Format(D(i), "0.000")
  31. Cells(k, 11).Value = Cells(i + 1, 6).Value
  32. End If
  33. Next i
  34. End Sub
  35.  
  36. -------------------
  37.  
  38. Option Explicit
  39. Public Const DensityResolution = 400
  40. Const VectorDim = 1000
  41. Dim Vector(1 To VectorDim) As Double
  42. Dim VectorLength As Integer
  43. Dim Coefficients(0 To DensityResolution) As Double
  44. Dim Dimension As Integer 'Размерность метода максимальной энтропии
  45. 'Вычисление коэффициентов метода максимальной энтропии
  46. Private Sub MEMCF(ByRef sigma As Double, ByRef mju As Double)
  47. Dim B0(1 To VectorDim), B1(1 To VectorDim) As Double
  48. Dim Z(0 To DensityResolution) As Double
  49. Dim x, y As Double
  50. Dim i, j As Integer
  51. 'Установка начальных значений
  52. mju = 0
  53. For i = 1 To VectorLength
  54. mju = mju + Vector(i) / VectorLength
  55. Next i
  56. For i = 1 To VectorLength
  57. Vector(i) = Vector(i) - mju
  58. Next i
  59. For i = 1 To Dimension
  60. Coefficients(i) = 0
  61. Z(i) = 0
  62. Next i
  63. Coefficients(0) = -1#
  64. Z(0) = -1#
  65. sigma = 0
  66. For i = 1 To VectorLength
  67. sigma = sigma + Vector(i) ^ 2 / VectorLength
  68. Next i
  69. For i = 1 To VectorLength - 1
  70. B0(i) = Vector(i)
  71. B1(i) = Vector(i + 1)
  72. Next i
  73. 'Главный цикл
  74. For j = 1 To Dimension
  75. If j > 1 Then
  76. For i = 1 To VectorLength - j - 1
  77. B0(i) = B0(i) - Coefficients(j - 1) * B1(i)
  78. B1(i) = B1(i + 1) - Coefficients(j - 1) * B0(i + 1)
  79. Next i
  80. End If
  81. x = 0
  82. y = 0
  83. For i = 1 To VectorLength - j
  84. x = x + 2# / VectorLength * B0(i) * B1(i)
  85. y = y + 1# / VectorLength * (B0(i) ^ 2 + B1(i) ^ 2)
  86. Next i
  87. Coefficients(j) = x / y
  88. sigma = sigma * (1# - Coefficients(j) ^ 2)
  89. For i = 1 To j - 1
  90. Z(i) = Coefficients(i)
  91. Next i
  92. If j > 1 Then
  93. For i = 1 To j - 1
  94. Coefficients(i) = Z(i) - Coefficients(j) * Z(j - i)
  95. Next i
  96. End If
  97. Next j
  98. 'Конец главного цикла
  99. For i = 0 To Dimension
  100. Coefficients(i) = -Coefficients(i)
  101. Next i
  102. x = -1#
  103. For i = 0 To Dimension
  104. x = x - Coefficients(i)
  105. Next i
  106. mju = mju * x
  107. End Sub
  108. 'Вычисление оценки спектральной плотности по методу максимальной
  109. 'энтропии на частоте F
  110. Private Function MEMSD(sigma As Double, F As Double) As Double
  111. Dim Re, Im As Double
  112. Dim i As Integer
  113. Re = 0
  114. Im = 0
  115. For i = 0 To Dimension
  116. Re = Re + Coefficients(i) * Cos(-i * F)
  117. Im = Im + Coefficients(i) * Sin(-i * F)
  118. Next i
  119. MEMSD = sigma / 2 / 3.1415926 / (Re * Re + Im * Im)
  120. End Function
  121. Sub Main()
  122. Dim i
  123. Dim x As Double, y As Double, ini As Double, fin As Double
  124. Dim SI As Double, step As Double
  125. Sheets("Data").Activate
  126. Dimension = Cells(1, 4).Value
  127. ini = Cells(4, 4).Value: fin = Cells(3, 4).Value
  128. step = (fin - ini) / 399
  129. SI = Cells(6, 4).Value
  130. VectorLength = 0
  131. For Each i In Range("Sample")
  132. VectorLength = VectorLength + 1
  133. Vector(VectorLength) = i.Value
  134. Next i
  135. Call MEMCF(x, y)
  136. Cells(1, 5) = "Частота": Cells(1, 6) = "Период"
  137. Cells(1, 7) = "MEM cпектр"
  138. For i = 1 To 400
  139. Cells(i + 1, 5).Value = Format(ini + step * (i - 1), "0.000")
  140. y = MEMSD(x, ini + (i - 1) * step)
  141. Cells(i + 1, 7).Value = y
  142. If (ini + (i - 1) * step) < 1e-09 Then
  143. Cells(i + 1, 6).Value = "~"
  144. Else
  145. y = 2 * 3.1415926 * SI / (ini + (i - 1) * step)
  146. Cells(i + 1, 6).Value = Format(y, "0.00")
  147. End If
  148. Next i
  149.  
  150. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement