Skip to content

Commit 4d6b7f9

Browse files
committed
Add DocStrings and linting
1 parent c0ac7b9 commit 4d6b7f9

File tree

1 file changed

+36
-12
lines changed

1 file changed

+36
-12
lines changed

library/enviroplus/noise.py

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
import sounddevice
22
import numpy
3-
import math
3+
44

55
class Noise():
6-
def __init__(
7-
self,
8-
sample_rate=16000,
9-
duration=0.5):
6+
def __init__(self,
7+
sample_rate=16000,
8+
duration=0.5):
9+
"""Noise measurement.
10+
11+
:param sample_rate: Sample rate in Hz
12+
:param duraton: Duration, in seconds, of noise sample capture
13+
14+
"""
1015

1116
self.duration = duration
1217
self.sample_rate = sample_rate
1318

1419
def get_amplitudes_at_frequency_ranges(self, ranges):
20+
"""Return the mean amplitude of frequencies in the given ranges.
21+
22+
:param ranges: List of ranges including a start and end range
23+
24+
"""
1525
recording = self._record()
1626
magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
1727
result = []
@@ -21,6 +31,12 @@ def get_amplitudes_at_frequency_ranges(self, ranges):
2131
return result
2232

2333
def get_amplitude_at_frequency_range(self, start, end):
34+
"""Return the mean amplitude of frequencies in the specified range.
35+
36+
:param start: Start frequency (in Hz)
37+
:param end: End frequency (in Hz)
38+
39+
"""
2440
n = self.sample_rate // 2
2541
if start > n or end > n:
2642
raise ValueError("Maxmimum frequency is {}".format(n))
@@ -29,12 +45,21 @@ def get_amplitude_at_frequency_range(self, start, end):
2945
magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate))
3046
return numpy.mean(magnitude[start:end])
3147

32-
def get_noise_profile(
33-
self,
34-
noise_floor=100,
35-
low=0.12,
36-
mid=0.36,
37-
high=None):
48+
def get_noise_profile(self,
49+
noise_floor=100,
50+
low=0.12,
51+
mid=0.36,
52+
high=None):
53+
"""Returns a noise charateristic profile.
54+
55+
Bins all frequencies into 3 weighted groups expressed as a percentage of the total frequency range.
56+
57+
:param noise_floor: "High-pass" frequency, exclude frequencies below this value
58+
:param low: Percentage of frequency ranges to count in the low bin (as a float, 0.5 = 50%)
59+
:param mid: Percentage of frequency ranges to count in the mid bin (as a float, 0.5 = 50%)
60+
:param high: Optional percentage for high bin, effectively creates a "Low-pass" if total percentage is less than 100%
61+
62+
"""
3863

3964
if high is None:
4065
high = 1.0 - low - mid
@@ -63,4 +88,3 @@ def _record(self):
6388
channels=1,
6489
dtype='float64'
6590
)
66-

0 commit comments

Comments
 (0)