11 :param sample_rate: Sample rate in Hz
12 :param duraton: Duration, in seconds, of noise sample capture
16 self
.duration
= duration
17 self
.sample_rate
= sample_rate
19 def get_amplitudes_at_frequency_ranges(self
, ranges
):
20 """Return the mean amplitude of frequencies in the given ranges.
22 :param ranges: List of ranges including a start and end range
25 recording
= self
._record
()
26 magnitude
= numpy
.abs(numpy
.fft
.rfft(recording
[:, 0], n
=self
.sample_rate
))
30 result
.append(numpy
.mean(magnitude
[start
:end
]))
33 def get_amplitude_at_frequency_range(self
, start
, end
):
34 """Return the mean amplitude of frequencies in the specified range.
36 :param start: Start frequency (in Hz)
37 :param end: End frequency (in Hz)
40 n
= self
.sample_rate
// 2
41 if start
> n
or end
> n
:
42 raise ValueError("Maxmimum frequency is {}".format(n
))
44 recording
= self
._record
()
45 magnitude
= numpy
.abs(numpy
.fft
.rfft(recording
[:, 0], n
=self
.sample_rate
))
46 return numpy
.mean(magnitude
[start
:end
])
48 def get_noise_profile(self
,
53 """Returns a noise charateristic profile.
55 Bins all frequencies into 3 weighted groups expressed as a percentage of the total frequency range.
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%
65 high
= 1.0 - low
- mid
67 recording
= self
._record
()
68 magnitude
= numpy
.abs(numpy
.fft
.rfft(recording
[:, 0], n
=self
.sample_rate
))
70 sample_count
= (self
.sample_rate
// 2) - noise_floor
72 mid_start
= noise_floor
+ int(sample_count
* low
)
73 high_start
= mid_start
+ int(sample_count
* mid
)
74 noise_ceiling
= high_start
+ int(sample_count
* high
)
76 amp_low
= numpy
.mean(magnitude
[noise_floor
:mid_start
])
77 amp_mid
= numpy
.mean(magnitude
[mid_start
:high_start
])
78 amp_high
= numpy
.mean(magnitude
[high_start
:noise_ceiling
])
79 amp_total
= (low
+ mid
+ high
) / 3.0
81 return amp_low
, amp_mid
, amp_high
, amp_total
84 return sounddevice
.rec(
85 int(self
.duration
* self
.sample_rate
),
86 samplerate
=self
.sample_rate
,