def test_bach(self): "e. what is the loudest 75 Hz of Bach 10 seconds?" start = time.time() low, high, filtered = loudest_band(music, frame_rate, 75) duration = time.time() - start testing_time['bach'] = duration self.assertEqual(music.shape, filtered.shape) self.assertAlmostEqual(low / 823.5, 1.0, 2) self.assertAlmostEqual(high / 898.3, 1.0, 2)
def test_find_band_dc(self): "c. DC is the loudest" frame_rate,T,ftest,bandwidth = 1000,1,100,20 t = linspace(0,T,T*frame_rate,endpoint=False) m = ones_like(t) + sin(2*pi*bandwidth//2*t) low,high,filtered = loudest_band(m,frame_rate,bandwidth) self.assertEqual(m.shape,filtered.shape) self.assertEqual(high,bandwidth) self.assertEqual(low,0) self.assertLess(match_signals(filtered,m),0.1,msg="filtered signal incorrect")
def test_find_band(self): "a. sine wave at 100 Hz, 1 kHz frame rate" frame_rate,T,ftest,bandwidth = 1000,1,100,10 m = sin(2*pi*ftest * linspace(0,T,T*frame_rate,endpoint=False)) low,high,filtered = loudest_band(m,frame_rate,bandwidth) self.assertEqual(m.shape,filtered.shape) self.assertLess(low,ftest,msg="low of band incorrect") self.assertLess(ftest,high,msg="high of band incorrect") self.assertEqual(bandwidth,high-low,msg="high-low must match bandwidth") self.assertLess(match_signals(filtered,m),0.1,msg="filtered signal incorrect")
def test_find_band_split(self): "d. two sines 80% of bw apart" frame_rate,T,ftest,bandwidth = 10000,1,100,10 t = linspace(0,T,T*frame_rate,endpoint=False) m = sin(2*pi*ftest*t)+sin(2*pi*(ftest+0.8*bandwidth)*t) low,high,filtered = loudest_band(m,frame_rate,bandwidth) self.assertEqual(m.shape,filtered.shape) self.assertLessEqual(low,ftest,msg="low of band incorrect") self.assertLessEqual(ftest+.8*bandwidth,high,msg="high of band incorrect") self.assertEqual(bandwidth,high-low) self.assertLess(match_signals(filtered,m),0.1,msg="filtered signal incorrect")
def test_find_energy(self): "b. cosines at 10 11 12 and 30" frame_rate, T, ftest, bandwidth = 10000, 1, 100, 10 t = linspace(0, T, T * frame_rate, endpoint=False) m = zeros_like(t) for a, f in [(1, 10), (1, 11), (1, 12), (2, 30)]: m += a * cos(2 * pi * f * t) low, high, filtered = loudest_band(m, frame_rate, bandwidth) self.assertEqual(m.shape, filtered.shape) self.assertLessEqual(low, 30, msg="low of band incorrect") self.assertLessEqual(30, high, msg="high of band incorrect") self.assertEqual(bandwidth, high - low)