Advertisement
Guest User

Untitled

a guest
Jul 29th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. import random
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. # Default constants
  6. CENTER = 67
  7. STANDARD_DEVIATION = 2.5
  8. INITIAL_ESTIMATE_ERROR = 2
  9. OPTIMIZED_ERROR = 0.01
  10.  
  11. # This class, upon initialization, generates a gaussian data cloud...
  12. # it includes methods for plotting and summarizing the data.
  13. class GaussianGenerator():
  14.  
  15. def __init__(self, center, stdDev=2, numPts=100000):
  16. self.center = center
  17. self.data = [round(random.gauss(center, stdDev), 2) for i in range(numPts)]
  18.  
  19. # Plots a histogram of the generated data
  20. def plot(self):
  21. plt.figure(1)
  22. plt.subplot()
  23. plt.hist(self.data, 50, color='green')
  24. plt.xlabel('Temperature')
  25. plt.ylabel('Number of Points')
  26. plt.title('Gaussian Distribution of Temperatures')
  27. plt.axis('normal')
  28. plt.grid(True)
  29. plt.show(block=False)
  30.  
  31. # Prints the length of the list DATA as well as its min and max values
  32. def toString(self):
  33. print('Generated {0} points (centered around {1}) with a min of {2} and '\
  34. 'a max of {3}.'.format(len(self.data), self.center, min(self.data),
  35. max(self.data)))
  36.  
  37. # Calculates estimates for TEMPERATURE using Kalman filter
  38. class kalmanFilter():
  39.  
  40. def __init__(self, data):
  41. self.data = data
  42. self.measErr = max(abs(CENTER - min(data)), abs(CENTER - max(data)))
  43. self.estimate = CENTER - max(abs(CENTER - min(data)), abs(CENTER - max(data)))
  44. self.estimateErr = STANDARD_DEVIATION * STANDARD_DEVIATION
  45.  
  46. def calculate(self):
  47. self.calcParams = {'estimates':[], 'estimateErrors':[]}
  48. self.optimized = 0
  49. for i, d in enumerate(self.data):
  50. if (abs(CENTER - self.estimate) < OPTIMIZED_ERROR and not self.optimized):
  51. self.optimized = i
  52. kg = (self.estimateErr/(self.estimateErr + self.measErr))
  53. self.estimate = self.estimate + kg*(d-self.estimate)
  54. self.estimateErr = (1 - kg)*(self.estimateErr)
  55. self.calcParams['estimates'].append(self.estimate)
  56. self.calcParams['estimateErrors'].append(self.estimateErr)
  57.  
  58. if (not self.optimized):
  59. self.optimized = len(self.data)-1
  60.  
  61. def plot(self):
  62. x = np.arange(0, len(self.calcParams['estimates']), 1)
  63. plt.figure(2)
  64. plt.subplot()
  65. plt.plot(x, self.calcParams['estimates'])
  66. plt.xlabel('Data Readings')
  67. plt.ylabel('Estimated Temperature')
  68. plt.title('Kalman Filter Estimation')
  69. plt.axis([0, self.optimized if self.optimized > 0 else len(self.data)//2, min(self.data), max(self. data)])
  70. plt.grid(True)
  71. plt.show(block=False)
  72.  
  73. def toString(self):
  74. print('The filter determined (after {0} steps) that {1} is the best '\
  75. 'estimate for the true temperature.'.format(self.optimized,
  76. round(self.calcParams['estimates'][self.optimized], 6)))
  77.  
  78. if __name__ == '__main__':
  79. # Generate a gaussian distribution of data points -- print summary and plot
  80. d = GaussianGenerator(CENTER, STANDARD_DEVIATION)
  81. d.toString()
  82. d.plot()
  83.  
  84. # Create a Kalman filter -- calculate estimates, print summary, and plot
  85. f = kalmanFilter(d.data)
  86. f.calculate()
  87. f.toString()
  88. f.plot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement