From: Phil Howard Date: Tue, 17 Sep 2019 10:45:48 +0000 (+0100) Subject: Noise library and examples for basic FFT and frequency binning X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02-2.git/commitdiff_plain/aa747a416652b64a182d6e2594c08e2476d17d76?ds=sidebyside Noise library and examples for basic FFT and frequency binning --- diff --git a/examples/noise-amps-at-freqs.py b/examples/noise-amps-at-freqs.py new file mode 100644 index 0000000..0a8040b --- /dev/null +++ b/examples/noise-amps-at-freqs.py @@ -0,0 +1,41 @@ +import ST7735 +from PIL import Image, ImageDraw +from enviroplus.noise import Noise + +SAMPLERATE = 16000 + +FREQ_LOW = 100.0 +FREQ_HIGH = 2000.0 +WIDTH = 100 + +noise = Noise() + +disp = ST7735.ST7735( + port=0, + cs=ST7735.BG_SPI_CS_FRONT, + dc=9, + backlight=12, + rotation=90) + +disp.begin() + +img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0)) +draw = ImageDraw.Draw(img) + + +while True: + amps = noise.get_amplitudes_at_frequency_ranges([ + (100,200), + (500,600), + (1000,1200) + ]) + amps = [n * 32 for n in amps] + img2 = img.copy() + draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0)) + img.paste(img2, (1, 0)) + draw.line((0, 0, 0, amps[0]), fill=(0, 0, 255)) + draw.line((0, 0, 0, amps[1]), fill=(0, 255, 0)) + draw.line((0, 0, 0, amps[2]), fill=(255, 0, 0)) + + disp.display(img) + diff --git a/examples/noise-profile.py b/examples/noise-profile.py new file mode 100644 index 0000000..70aa6ab --- /dev/null +++ b/examples/noise-profile.py @@ -0,0 +1,39 @@ +import ST7735 +from PIL import Image, ImageDraw +from enviroplus.noise import Noise + +SAMPLERATE = 16000 + +FREQ_LOW = 100.0 +FREQ_HIGH = 2000.0 +WIDTH = 100 + +noise = Noise() + +disp = ST7735.ST7735( + port=0, + cs=ST7735.BG_SPI_CS_FRONT, + dc=9, + backlight=12, + rotation=90) + +disp.begin() + +img = Image.new('RGB', (disp.width, disp.height), color=(0, 0, 0)) +draw = ImageDraw.Draw(img) + + +while True: + low, mid, high, amp = noise.measure() + low *= 128 + mid *= 128 + high *= 128 + amp *= 64 + + img2 = img.copy() + draw.rectangle((0, 0, disp.width, disp.height), (0, 0, 0)) + img.paste(img2, (1, 0)) + draw.line((0, 0, 0, amp), fill=(int(low), int(mid), int(high))) + + disp.display(img) + diff --git a/library/enviroplus/noise.py b/library/enviroplus/noise.py index e69de29..57e869d 100644 --- a/library/enviroplus/noise.py +++ b/library/enviroplus/noise.py @@ -0,0 +1,66 @@ +import sounddevice +import numpy +import math + +class Noise(): + def __init__( + self, + sample_rate=16000, + duration=0.5): + + self.duration = duration + self.sample_rate = sample_rate + + def get_amplitudes_at_frequency_ranges(self, ranges): + recording = self._record() + magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate)) + result = [] + for r in ranges: + start, end = r + result.append(numpy.mean(magnitude[start:end])) + return result + + def get_amplitude_at_frequency_range(self, start, end): + n = self.sample_rate // 2 + if start > n or end > n: + raise ValueError("Maxmimum frequency is {}".format(n)) + + recording = self._record() + 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): + + if high is None: + high = 1.0 - low - mid + + recording = self._record() + magnitude = numpy.abs(numpy.fft.rfft(recording[:, 0], n=self.sample_rate)) + + sample_count = (self.sample_rate // 2) - noise_floor + + mid_start = noise_floor + int(sample_count * low) + high_start = mid_start + int(sample_count * mid) + noise_ceiling = high_start + int(sample_count * high) + + amp_low = numpy.mean(magnitude[self.noise_floor:mid_start]) + amp_mid = numpy.mean(magnitude[mid_start:high_start]) + amp_high = numpy.mean(magnitude[high_start:noise_ceiling]) + amp_total = (low + mid + high) / 3.0 + + return amp_low, amp_mid, amp_high, amp_total + + def _record(self): + return sounddevice.rec( + int(self.duration * self.sample_rate), + samplerate=self.sample_rate, + blocking=True, + channels=1, + dtype='float64' + ) +