class LowPassFilterTest(unittest.TestCase): def setUp(self): self.p1 = LowPassFilter() self.s1 = DiracSource() self.p2 = LowPassFilter() self.s2 = SineSource(freq=400, amp=1.0) self.p3 = LowPassFilter() self.s3 = SineSource(freq=40, amp=1.0) def test_on_dirac(self): chunk = list(islice(self.s1.read(), 0, 100)) proc = self.p1.process(chunk) # Lets check if IIR filter is really infinite. self.assertTrue(abs(proc[-1][0]) > 0.001) def test_on_sin(self): # A bit weird way to check if LP filter passing low frequences # Freq for filter is 150 Hz, so: # 400 Hz freq should be dimmed chunk_len = self.s2.rate / 400 chunk = list(islice(self.s2.read(), 0, chunk_len * 2)) proc = self.p2.process(chunk) self.assertTrue(max(map(lambda x: abs(x[0]), proc)) < 0.3) # 40 Hz should be pretty much left intact chunk_len = self.s2.rate / 40 chunk = list(islice(self.s3.read(), 0, chunk_len * 2)) proc = self.p3.process(chunk) self.assertTrue(max(map(lambda x: abs(x[0]), proc)) > 0.8)
def setUp(self): self.p1 = LowPassFilter() self.s1 = DiracSource() self.p2 = LowPassFilter() self.s2 = SineSource(freq=400, amp=1.0) self.p3 = LowPassFilter() self.s3 = SineSource(freq=40, amp=1.0)
class RMSTest(unittest.TestCase): def setUp(self): self.p = RMS() self.s = SineSource(freq=1000, amp=0.5) def test_on_sin(self): chunk_len = 100 * self.s.rate / self.s._freq chunk = list(islice(self.s.read(), 0, chunk_len)) self.p.process(chunk) # RMS for sine is Amplitude / sqrt(2) self.assertAlmostEqual(self.p.get_state()["rms"][0], self.s._amp / sqrt(2.0), 3)
def setUp(self): self.p = RMS() self.s = SineSource(freq=1000, amp=0.5)