def gaussian(self): self.windowSize = self.params['window_size'] self.std = self.params['std'] self.gauss_window = SmoothingFilter.gaussianCoeffs( self.windowSize, self.std) self.gauss_sum = sum(self.gauss_window) while self.active: if not self.inputQueue.isEmpty(): # Get next dp dp = self.inputQueue.dequeue() # Special handling for end data stream if dp == 'end': self.outputQueue.enqueue(dp) self.completed = True self.active = False return self.data.append(dp) self.window.enqueue(dp) if len(self.window) == self.windowSize: # Do smoothing action and pop. ssum = 0 for i in range(len(self.window)): ssum += self.window[i].mag * self.gauss_window[i] # Average of all points in the window new_dp = Sds(self.window[int(self.windowSize / 2)].time, ssum / self.gauss_sum, self.window[int(self.windowSize / 2)].mag) self.outputQueue.enqueue(new_dp) self.window.dequeue()
def centeredMovingAvg(self): self.windowSize = self.params['window_size'] while self.active: if not self.inputQueue.isEmpty(): # Get next data point dp = self.inputQueue.dequeue() # Special handling for end data stream if dp == 'end': self.outputQueue.enqueue(dp) self.completed = True self.active = False return self.data.append(dp) self.window.enqueue(dp) if len(self.window) == self.windowSize: # Do smoothing action and pop. ssum = 0 for i in range(len(self.window)): ssum += self.window[i].mag # Average of all points in the window new_dp = Sds(self.window[int(self.windowSize / 2)].time, ssum / self.windowSize, self.window[int(self.windowSize / 2)].mag) self.outputQueue.enqueue(new_dp) self.window.dequeue()
def peakDetect(self): while self.active: if not self.inputQueue.isEmpty(): # Get next data point dp = self.inputQueue.dequeue() # Special handling for end case if dp == 'end': self.active = False self.completed = True self.outputQueue.enqueue('end') return # Add to data list self.data.append(dp) # Update statistics self.n += 1 if self.n == 1: # First data point self.mean = dp.mag self.std = 0 elif self.n == 2: # Second data point o_mean = self.mean self.mean = (dp.mag + self.mean) / 2 self.std = math.sqrt((math.pow(dp.mag - self.mean, 2) + math.pow(o_mean - self.mean, 2)) / 2) else: # Iteratively update mean and standard deviation o_mean = self.mean self.mean = (dp.mag + (self.n - 1) * self.mean) / self.n self.std = math.sqrt( ((self.n - 2) * math.pow(self.std, 2) / (self.n - 1)) + math.pow(o_mean - self.mean, 2) + math.pow(dp.mag - self.mean, 2) / self.n) if self.n > 15: # Check if we are above the threshold if (dp.mag - self.mean) > self.std * self.threshold: # Declare this a peak self.outputQueue.enqueue(Sds(dp.time, dp.oldMag)) self.dataout.append(Sds(dp.time, dp.oldMag))
def maxDiff(self): while self.active: if not self.inputQueue.isEmpty(): # Get next data point dp = self.inputQueue.dequeue() # Special case handling for end data point. if dp == 'end': self.completed = True self.active = False self.outputQueue.enqueue('end') return # Add data point to list and queue self.data.append(dp) self.window.enqueue(dp) # Once we reach the window size, do some processing! if len(self.window) == self.windowSize: # Calculate peak score midPoint = int(self.windowSize / 2) maxDiffLeft = -100 maxDiffRight = -100 # Find max difference on left for i in range(0, midPoint): value = self.window[midPoint].mag - self.window[i].mag if value > maxDiffLeft: maxDiffLeft = value # Find max difference on right for i in range(midPoint + 1, len(self.window)): value = self.window[midPoint].mag - self.window[i].mag if value > maxDiffRight: maxDiffRight = value # Calculate peak score and create a new point avg = (maxDiffRight + maxDiffLeft) / 2 new_dp = Sds(self.window[midPoint].time, avg, self.window[midPoint].mag) self.outputQueue.enqueue(new_dp) self.window.dequeue()
def panTompkins(self): while self.active: if not self.inputQueue.isEmpty(): # Get next data point dp = self.inputQueue.dequeue() # Special case handling for end data point. if dp == 'end': self.completed = True self.active = False self.outputQueue.enqueue('end') return # Add data point to list and queue self.data.append(dp) self.window.enqueue(dp) # Once we reach the window size, do some processing! if len(self.window) == self.windowSize: midPoint = int(self.windowSize / 2) # Calculate mean of window ssum = 0 for i in range(0, self.windowSize): ssum += self.window[i].mag mean = ssum / self.windowSize new_mag = 0 if self.window[ midPoint].mag - mean < 0 else self.window[ midPoint].mag - mean # Square it. new_mag *= new_mag # Calculate peak score and create a new point new_dp = Sds(self.window[midPoint].time, new_mag, self.window[midPoint].mag) self.outputQueue.enqueue(new_dp) self.window.dequeue()