Add DocStrings and linting
authorPhil Howard <phil@gadgetoid.com>
Wed, 16 Oct 2019 09:41:23 +0000 (10:41 +0100)
committerPhil Howard <phil@gadgetoid.com>
Wed, 16 Oct 2019 09:41:23 +0000 (10:41 +0100)
library/enviroplus/noise.py

index 57e869d89ef91b66315bbc55d0d6d9b56e82f8c5..2e7472d1ea8120eec7739efeee989d358d7e728f 100644 (file)
@@ -1,17 +1,27 @@
 import sounddevice
 import numpy
 import sounddevice
 import numpy
-import math
+
 
 class Noise():
 
 class Noise():
-    def __init__(
-        self,
-        sample_rate=16000,
-        duration=0.5):
+    def __init__(self,
+                 sample_rate=16000,
+                 duration=0.5):
+        """Noise measurement.
+
+        :param sample_rate: Sample rate in Hz
+        :param duraton: Duration, in seconds, of noise sample capture
+
+        """
 
         self.duration = duration
         self.sample_rate = sample_rate
 
     def get_amplitudes_at_frequency_ranges(self, ranges):
 
         self.duration = duration
         self.sample_rate = sample_rate
 
     def get_amplitudes_at_frequency_ranges(self, ranges):
+        """Return the mean amplitude of frequencies in the given ranges.
+
+        :param ranges: List of ranges including a start and end range
+
+        """
         recording = self._record()
         magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
         result = []
         recording = self._record()
         magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
         result = []
@@ -21,6 +31,12 @@ class Noise():
         return result
 
     def get_amplitude_at_frequency_range(self, start, end):
         return result
 
     def get_amplitude_at_frequency_range(self, start, end):
+        """Return the mean amplitude of frequencies in the specified range.
+
+        :param start: Start frequency (in Hz)
+        :param end: End frequency (in Hz)
+
+        """
         n = self.sample_rate // 2
         if start > n or end > n:
             raise ValueError("Maxmimum frequency is {}".format(n))
         n = self.sample_rate // 2
         if start > n or end > n:
             raise ValueError("Maxmimum frequency is {}".format(n))
@@ -29,12 +45,21 @@ class Noise():
         magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
         return numpy.mean(magnitude[start:end])
 
         magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
         return numpy.mean(magnitude[start:end])
 
-    def get_noise_profile(
-        self,
-        noise_floor=100,
-        low=0.12,
-        mid=0.36,
-        high=None):
+    def get_noise_profile(self,
+                          noise_floor=100,
+                          low=0.12,
+                          mid=0.36,
+                          high=None):
+        """Returns a noise charateristic profile.
+
+        Bins all frequencies into 3 weighted groups expressed as a percentage of the total frequency range.
+
+        :param noise_floor: "High-pass" frequency, exclude frequencies below this value
+        :param low: Percentage of frequency ranges to count in the low bin (as a float, 0.5 = 50%)
+        :param mid: Percentage of frequency ranges to count in the mid bin (as a float, 0.5 = 50%)
+        :param high: Optional percentage for high bin, effectively creates a "Low-pass" if total percentage is less than 100%
+
+        """
 
         if high is None:
             high = 1.0 - low - mid
 
         if high is None:
             high = 1.0 - low - mid
@@ -63,4 +88,3 @@ class Noise():
             channels=1,
             dtype='float64'
         )
             channels=1,
             dtype='float64'
         )
-