Commit 36d97a92 authored by elagil's avatar elagil
Browse files

various fixes to sensor

parent 5187573c
from typing import Type
import numpy as np
import scipy.signal as ss
from collections import deque
class SensorSettings():
def __init__(self, frequencyRange, measurementRate, distanceRangeBins=None, points=100):
self.frequencyRange = frequencyRange
self.distanceRangeBins = distanceRangeBins
def __init__(self, frequencyLimit, measurementRate, distanceLimitIndices=None, points=100):
self.frequencyLimit = frequencyLimit
self.distanceLimitIndices = distanceLimitIndices
self.measurementRate = measurementRate
self.points = points
self.fftPoints = 1024
self.fftPoints = 4096
self.bufferDuration = 0
......@@ -24,11 +25,11 @@ class SensorSettings():
def calculateSettings(self):
self.bufferDuration = self.points / self.measurementRate
self.actualfrequencyRangeBins = self.frequenciesToIndices(
self.frequencyRange)
self.actualfrequencyLimitIndices = self.frequenciesToIndices(
self.frequencyLimit)
self.actualFrequencyRange = self.indicesToFrequencies(
self.actualfrequencyRangeBins)
self.actualFrequencyLimit = self.indicesToFrequencies(
self.actualfrequencyLimitIndices)
self.frequencyScale = np.linspace(
0, self.measurementRate/2, num=int(self.fftPoints/2+1), endpoint=False)
......@@ -46,14 +47,14 @@ class SensorSettings():
"An integer is expected for the amount of sampling points")
@ property
def frequencyRange(self):
return self._frequencyRange
@ frequencyRange.setter
def frequencyRange(self, new_frequencyRange):
if isinstance(new_frequencyRange, tuple) and len(new_frequencyRange) == 2:
if new_frequencyRange[0] < new_frequencyRange[1]:
self._frequencyRange = new_frequencyRange
def frequencyLimit(self):
return self._frequencyLimit
@ frequencyLimit.setter
def frequencyLimit(self, new_frequencyLimit):
if isinstance(new_frequencyLimit, tuple) and len(new_frequencyLimit) == 2:
if new_frequencyLimit[0] < new_frequencyLimit[1]:
self._frequencyLimit = new_frequencyLimit
else:
raise ValueError(
"Tuple is expected to have rising values in its range.")
......@@ -62,18 +63,18 @@ class SensorSettings():
"Expected a tuple of size 2 for the frequency range.")
@ property
def distanceRangeBins(self):
return self._distanceRangeBins
def distanceLimitIndices(self):
return self._distanceLimitIndices
@ distanceRangeBins.setter
def distanceRangeBins(self, new_distanceRangeBins):
if new_distanceRangeBins is None:
self._distanceRangeBins = None
@ distanceLimitIndices.setter
def distanceLimitIndices(self, new_distanceLimitIndices):
if new_distanceLimitIndices is None:
self._distanceLimitIndices = (0, -1)
else:
if isinstance(new_distanceRangeBins, tuple) and len(new_distanceRangeBins) == 2:
if new_distanceRangeBins[0] < new_distanceRangeBins[1]:
self._distanceRangeBins = new_distanceRangeBins
if isinstance(new_distanceLimitIndices, tuple) and len(new_distanceLimitIndices) == 2:
if new_distanceLimitIndices[0] < new_distanceLimitIndices[1]:
self._distanceLimitIndices = new_distanceLimitIndices
else:
raise ValueError(
"Tuple is expected to have rising values in its range.")
......@@ -92,6 +93,8 @@ class Sensor():
self.buffer = None
self.filtered = None
self.phaseProgressionSignal = None
def setup(self, sensorSettings, fmcwHelper):
self.fmcwHelper = fmcwHelper
......@@ -102,10 +105,18 @@ class Sensor():
def run(self, data):
self.__feed__(data)
self.__sense__()
self.__findMaximumVibration__()
return self.__bufferFilled__()
bufferFilled = self.__bufferFilled__()
if bufferFilled:
self.__sense__()
self.__findMaximumVibration__()
self.__extractPhaseProgression__()
return bufferFilled
def __calculateEnergy__(self, dataset, axis=-1):
return np.sum(np.abs(dataset), axis=axis)
def __bufferFilled__(self):
filled = False
......@@ -117,23 +128,25 @@ class Sensor():
return filled
def __findMaximumVibration__(self):
if self.__bufferFilled__():
lowFrequency, highFrequency = self.sensorSettings.actualfrequencyRangeBins
lowDistance, highDistance = self.sensorSettings.distanceRangeBins
reducedVibration = self.vibration[lowDistance:highDistance,
lowFrequency:highFrequency]
reducedVibrationAmplitude = np.abs(self.reducedVibration)
meanVibration = np.mean(reducedVibrationAmplitude, axis=-1)
maxVibration = np.max(reducedVibrationAmplitude, axis=-1)
self.maxEnergyIndex = np.argmax(np.divide(maxVibration, meanVibration))
energy = np.sum(np.square(np.abs(reducedVibration)), axis=-1)
localPeaks, _ = ss.find_peaks(
reducedVibrationAmplitude[self.maxEnergyIndex, :])
localMaxEnergyIndex = np.argmax(energy)
self.peakValues = reducedVibrationAmplitude[self.maxEnergyIndex, localPeaks]
localPeaks, properties = ss.find_peaks(
np.abs(reducedVibration[localMaxEnergyIndex, :]))
self.rateIndices = localPeaks + \
self.sensorSettings.actualfrequencyLimitIndices[0]
self.maxEnergyIndex = localMaxEnergyIndex + lowDistance
self.rates = self.sensorSettings.frequencyScale[self.rateIndices]
self.peaks = localPeaks + lowFrequency
self.dominantRate = self.rates[np.argmax(self.peakValues)]
def __feed__(self, data):
"""
......@@ -150,12 +163,22 @@ class Sensor():
"""
Correlation functionality for filtering the dataset
"""
if self.__bufferFilled__():
data = np.array(self.buffer).swapaxes(0, -1)
data = np.array(self.buffer).swapaxes(0, -1)
shape = data.shape
lowDistance, highDistance = self.sensorSettings.distanceLimitIndices
reduced = data[lowDistance:highDistance, int(shape[1]/2), :]
self.rangeEnergies = self.__calculateEnergy__(reduced)
self.phaseProgression = np.unwrap(np.angle(reduced))
self.vibration = np.fft.rfft(
self.phaseProgression, n=self.sensorSettings.fftPoints)
shape = data.shape
reduced = data[:, int(shape[1]/2), :]
lowFrequency, highFrequency = self.sensorSettings.actualfrequencyLimitIndices
self.reducedVibration = self.vibration[:, lowFrequency:highFrequency]
self.phaseProgression = np.unwrap(np.angle(reduced))
self.vibration = np.fft.rfft(
self.phaseProgression, n=self.sensorSettings.fftPoints)
def __extractPhaseProgression__(self):
self.phaseProgressionSignal = self.phaseProgression[self.maxEnergyIndex, :]
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment