Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import numpy as np
- import matplotlib.pyplot as plt
- # Default constants
- CENTER = 67
- STANDARD_DEVIATION = 2.5
- INITIAL_ESTIMATE_ERROR = 2
- OPTIMIZED_ERROR = 0.01
- # This class, upon initialization, generates a gaussian data cloud...
- # it includes methods for plotting and summarizing the data.
- class GaussianGenerator():
- def __init__(self, center, stdDev=2, numPts=100000):
- self.center = center
- self.data = [round(random.gauss(center, stdDev), 2) for i in range(numPts)]
- # Plots a histogram of the generated data
- def plot(self):
- plt.figure(1)
- plt.subplot()
- plt.hist(self.data, 50, color='green')
- plt.xlabel('Temperature')
- plt.ylabel('Number of Points')
- plt.title('Gaussian Distribution of Temperatures')
- plt.axis('normal')
- plt.grid(True)
- plt.show(block=False)
- # Prints the length of the list DATA as well as its min and max values
- def toString(self):
- print('Generated {0} points (centered around {1}) with a min of {2} and '\
- 'a max of {3}.'.format(len(self.data), self.center, min(self.data),
- max(self.data)))
- # Calculates estimates for TEMPERATURE using Kalman filter
- class kalmanFilter():
- def __init__(self, data):
- self.data = data
- self.measErr = max(abs(CENTER - min(data)), abs(CENTER - max(data)))
- self.estimate = CENTER - max(abs(CENTER - min(data)), abs(CENTER - max(data)))
- self.estimateErr = STANDARD_DEVIATION * STANDARD_DEVIATION
- def calculate(self):
- self.calcParams = {'estimates':[], 'estimateErrors':[]}
- self.optimized = 0
- for i, d in enumerate(self.data):
- if (abs(CENTER - self.estimate) < OPTIMIZED_ERROR and not self.optimized):
- self.optimized = i
- kg = (self.estimateErr/(self.estimateErr + self.measErr))
- self.estimate = self.estimate + kg*(d-self.estimate)
- self.estimateErr = (1 - kg)*(self.estimateErr)
- self.calcParams['estimates'].append(self.estimate)
- self.calcParams['estimateErrors'].append(self.estimateErr)
- if (not self.optimized):
- self.optimized = len(self.data)-1
- def plot(self):
- x = np.arange(0, len(self.calcParams['estimates']), 1)
- plt.figure(2)
- plt.subplot()
- plt.plot(x, self.calcParams['estimates'])
- plt.xlabel('Data Readings')
- plt.ylabel('Estimated Temperature')
- plt.title('Kalman Filter Estimation')
- plt.axis([0, self.optimized if self.optimized > 0 else len(self.data)//2, min(self.data), max(self. data)])
- plt.grid(True)
- plt.show(block=False)
- def toString(self):
- print('The filter determined (after {0} steps) that {1} is the best '\
- 'estimate for the true temperature.'.format(self.optimized,
- round(self.calcParams['estimates'][self.optimized], 6)))
- if __name__ == '__main__':
- # Generate a gaussian distribution of data points -- print summary and plot
- d = GaussianGenerator(CENTER, STANDARD_DEVIATION)
- d.toString()
- d.plot()
- # Create a Kalman filter -- calculate estimates, print summary, and plot
- f = kalmanFilter(d.data)
- f.calculate()
- f.toString()
- f.plot()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement