def test_signal1d_clip(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) res1 = [2, 3, 4] res2 = [1, 2, 3, 4, 5] res3 = [3, 4, 5] res4 = [1] res5 = [] r1 = signal.clip((1, 3)) r2 = signal.clip((0, 4)) r3 = signal.clip((2, 6)) r4 = signal.clip((0, 0)) r5 = signal.clip((5, 5)) self.assertIsInstance(r1, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3) self.assertListEqual(r4.get(), res4) self.assertListEqual(r5.get(), res5) with self.assertRaises(ValueError): signal.clip((-2, 2)) with self.assertRaises(ValueError): signal.clip((2, -2)) with self.assertRaises(ValueError): signal.clip((-2, -2)) signal = Signal1D() self.assertListEqual(signal.clip((1, 3)).get(), [])
def test_signal1d__init__(self): data = [1, 2, 3, 4, 5] datax = [0, 1, 2, 3, 4] name = "signal" xunits = "x units" yunits = "y units" signal = Signal1D(y=data, xunits=xunits, yunits=yunits, name=name) self.assertEqual(signal.length, len(data)) self.assertEqual(signal.sfreq, 1) self.assertEqual(signal.xunits, xunits) self.assertEqual(signal.yunits, yunits) self.assertEqual(signal.name, name) self.assertEqual(signal.nsamples, len(data)) self.assertEqual(signal.dim, (len(data), )) self.assertEqual(signal.ndim, 1) self.assertIsInstance(signal.plot, Plotter1D) y, x = signal.get(alls=True) self.assertListEqual(y, data) self.assertListEqual(x, datax) self.assertEqual(signal.domain, Transforms.TIMESPACE_DOMAIN) with self.assertRaises(ValueError): Signal1D(x=datax) with self.assertRaises(ValueError): Signal1D(y=data, length=-10)
def test_signal1d_properties(self): data = [1, 2, 3, 4, 5] signal = Signal1D(y=data) self.assertEqual(signal.length, len(data)) self.assertEqual(signal.sfreq, 1) self.assertIsInstance(signal.dim, tuple) self.assertEqual(len(signal.dim), 1) self.assertEqual(signal.dim[0], len(data)) self.assertEqual(signal.ndim, 1) self.assertEqual(signal.nsamples, len(data)) self.assertEqual(signal.domain, Transforms.TIMESPACE_DOMAIN) self.assertIsInstance(signal.plot, Plotter1D) # setters with self.assertRaises(AttributeError): signal.length = 0 with self.assertRaises(AttributeError): signal.sfreq = 0 with self.assertRaises(AttributeError): signal.dim = 0 with self.assertRaises(AttributeError): signal.ndim = 0 with self.assertRaises(AttributeError): signal.nsamples = 0 signal.xunits = "xunits" signal.yunits = "yunits" signal.name = "signal" self.assertEqual(signal.xunits, "xunits") self.assertEqual(signal.yunits, "yunits") self.assertEqual(signal.name, "signal") signal = Signal1D() self.assertEqual(signal.length, 0) self.assertEqual(signal.dim[0], 0) self.assertEqual(signal.nsamples, 0)
def test_signal1d_pad(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) res = [[0, 1, 2, 3, 4, 5, 0, 0, 0], [0, 0, 1, 2, 3, 4, 5, 0, 0], [1, 2, 3, 4, 5, 0], [0, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5]] pad = [(1, 3), (2, 2), (0, 1), (1, 0), (0, 0)] padder = [1, 2, 3, 4, 5] s = [] for p in pad: s.append(signal.pad(p)) for sig, res in zip(s, res): self.assertIsInstance(sig, Signal) self.assertListEqual(sig.get(), res) with self.assertRaises(ValueError): signal.pad((-2, 2)) with self.assertRaises(ValueError): signal.pad((2, -2)) with self.assertRaises(ValueError): signal.pad((-2, -2)) res = [[1, 1, 2, 3, 4, 5, 1, 1, 1], [2, 2, 1, 2, 3, 4, 5, 2, 2], [1, 2, 3, 4, 5, 3], [4, 1, 2, 3, 4, 5], [1, 2, 3, 4, 5]] s = [] for p, pr in zip(pad, padder): s.append(signal.pad(p, pr)) for sig, res in zip(s, res): self.assertListEqual(sig.get(), res) signal = Signal1D() self.assertListEqual(signal.pad(pad[0]).get(), [])
def test_signal1d_stddev(self): s1 = Signal1D(y=[1, 2, -3, 4, 5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertAlmostEqual(s1.stddev(), 2.7856, places=3) self.assertEqual(s2.stddev(), 0) self.assertIsNone(s3.stddev())
def test_signal1d_power(self): s1 = Signal1D(y=[1, 2, -3, 4, 5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertEqual(s1.power(), 11) self.assertEqual(s2.power(), 0) self.assertIsNone(s3.power())
def test_signal1d_energy(self): s1 = Signal1D(y=[1, 2, 3, 4, -5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertEqual(s1.energy(), 55) self.assertEqual(s2.energy(), 0) self.assertIsNone(s3.energy())
def test_signal1d_rms(self): s1 = Signal1D(y=[1, 2, -3, 4, 5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertAlmostEqual(s1.rms(), 3.3166, places=3) self.assertEqual(s2.rms(), 0) self.assertIsNone(s3.rms())
def test_signal1d_mean(self): s1 = Signal1D(y=[1, 2, -3, 4, 5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertAlmostEqual(s1.mean(), 1.80, places=1) self.assertEqual(s2.mean(), 0) self.assertIsNone(s3.mean())
def test_signal1d_variance(self): s1 = Signal1D(y=[1, 2, -3, 4, 5]) s2 = Signal1D(y=[0, 0, 0]) s3 = Signal1D(y=[]) self.assertAlmostEqual(s1.variance(), 7.76, places=1) self.assertEqual(s2.variance(), 0) self.assertIsNone(s3.variance())
def test_signal1d_is_empty(self): signal1 = Signal1D() signal2 = Signal1D(y=[]) signal3 = Signal1D(x=[]) signal4 = Signal1D(y=[], x=[]) self.assertTrue(signal1.is_empty()) self.assertTrue(signal2.is_empty()) self.assertTrue(signal3.is_empty()) self.assertTrue(signal4.is_empty())
def test_signal1d_zero_pad_to(self): s0 = Signal1D(y=[1, 2, 3, 4, 5]) s1 = Signal1D(y=[1, 2, 3]) s2 = Signal1D(y=[3, 4, 5]) r1 = s1.zero_pad_to(s0) r2 = s2.zero_pad_to(s1) self.assertIsInstance(r1, Signal) self.assertListEqual(r1.get(), [1, 2, 3, 0, 0]) self.assertListEqual(r2.get(), [3, 4, 5]) with self.assertRaises(ValueError): s0.zero_pad_to(s1)
def test_signal1d_flip(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) res1 = [5, 4, 3, 2, 1] res2 = [5, 4, 3, 2, 1] r1 = signal.flip() r2 = signal.flip((1, )) self.assertIsInstance(r1, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) signal = Signal1D(y=[]) r1 = signal.flip() self.assertListEqual(r1.get(), [])
def test_signal1d_to_real(self): s1 = Signal1D(y=[1 - 1j, 1 + 2j, -3j, 5]) s2 = Signal1D(y=[0, -3, 1]) s3 = Signal1D(y=[]) res1 = [1, 1, 0, 5] res2 = [0, -3, 1] res3 = [] r1 = s1.to_real() r2 = s2.to_real() r3 = s3.to_real() self.assertIsInstance(r1, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3)
def test_signal1d__neg__(self): s1 = Signal1D(y=[2, 4, 6, 8, 10]) s2 = Signal1D(y=[1, -1, 1, -1, 1]) s3 = Signal1D(y=[]) res1 = [-2, -4, -6, -8, -10] res2 = [-1, 1, -1, 1, -1] res3 = [] r1 = -s1 r2 = -s2 r3 = -s3 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3)
def test_signal1d__pow__(self): s1 = Signal1D(y=[2, 4, 6, 8, 10]) s2 = Signal1D(y=[4, 9, 16, 25, 36]) s3 = Signal1D(y=[]) res1 = [4, 16, 36, 64, 100] res2 = [2, 3, 4, 5, 6] res3 = [] r1 = s1**2 r2 = s2**0.5 r3 = s3**1 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3)
def test_signal1d_spectrum(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) spec = signal.fft().spectrum() self.assertIsInstance(spec, Signal) self.assertEqual(spec.domain, Transforms.FREQUENCY_DOMAIN) self.assertEqual(spec.ndim, signal.ndim) self.assertEqual(spec.dim, signal.dim)
def test_signal1d_ifft(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) fft = signal.fft() self.assertIsInstance(fft, Signal) self.assertEqual(fft.domain, Transforms.FREQUENCY_DOMAIN) ifft = fft.ifft() self.assertIsInstance(ifft, Signal) self.assertEqual(ifft.domain, Transforms.TIMESPACE_DOMAIN)
def test_signal1d_max(self): s1 = Signal1D(y=[10, -3, 4, 1, -3.1]) s2 = Signal1D(y=[3 - 1j, 1 + 2j, -3j, 5]) s3 = Signal1D(y=[1]) s4 = Signal1D(y=[]) s5 = Signal1D(y=[3, 1 + 2j, -3j, 5]) res1 = 10 res2 = 5 res3 = 1 r1 = s1.max() r2 = s2.max() r3 = s3.max() r4 = s4.max() self.assertEqual(r1, res1) self.assertEqual(r2, res2) self.assertEqual(r3, res3) self.assertIsNone(r4) with self.assertRaises(TypeError): s5.max() # undefined
def test_signal1d__add__(self): s0 = Signal1D(y=[1, 2, 3, 4, 5]) s1 = Signal1D(y=[1, 1, 1, 1, 1]) s3 = Signal1D(y=[1, 1, 1]) s4 = Signal1D(y=[]) s5 = Signal1D(y=[]) res1 = [2, 3, 4, 5, 6] res2 = [6, 7, 8, 9, 10] res3 = [] r1 = s0 + s1 r2 = s0 + 5 r3 = s4 + s5 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3) with self.assertRaises(ValueError): s0 + s3
def test_signal1d__mul__(self): s0 = Signal1D(y=[1, 2, 3, 4, 5]) s1 = Signal1D(y=[1, 1, 1, 1, 1]) s3 = Signal1D(y=[1, 1, 1]) s4 = Signal1D(y=[]) s5 = Signal1D(y=[]) res1 = [1, 2, 3, 4, 5] res2 = [5, 10, 15, 20, 25] res3 = [] r1 = s0 * s1 r2 = s0 * 5 r3 = s4 * s5 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3) with self.assertRaises(ValueError): s0 * s3
def test_signal1d__sub__(self): s0 = Signal1D(y=[1, 2, 3, 4, 5]) s1 = Signal1D(y=[1, 1, 1, 1, 1]) s3 = Signal1D(y=[1, 1, 1]) s4 = Signal1D(y=[]) s5 = Signal1D(y=[]) res1 = [0, 1, 2, 3, 4] res2 = [-4, -3, -2, -1, 0] res3 = [] r1 = s0 - s1 r2 = s0 - 5 r3 = s4 - s5 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3) with self.assertRaises(ValueError): s0 - s3
def test_signal1d__truediv__(self): s0 = Signal1D(y=[2, 4, 6, 8, 10]) s1 = Signal1D(y=[1, 1, 1, 1, 1]) s3 = Signal1D(y=[1, 1, 1]) s4 = Signal1D(y=[]) s5 = Signal1D(y=[]) res1 = [2, 4, 6, 8, 10] res2 = [1, 2, 3, 4, 5] res3 = [] r1 = s0 / s1 r2 = s0 / 2 r3 = s4 / s5 self.assertIsInstance(r1, Signal) self.assertIsInstance(r2, Signal) self.assertIsInstance(r3, Signal) self.assertListEqual(r1.get(), res1) self.assertListEqual(r2.get(), res2) self.assertListEqual(r3.get(), res3) with self.assertRaises(ValueError): s0 / s3
def test_signal1d_utos(self): data = [1, 2, 3, 4, 5] signal = Signal1D(y=data, length=20) self.assertEqual(signal.sfreq, 0.25) self.assertEqual(signal.length, 20) self.assertEqual(signal.utos(0), 0) self.assertEqual(signal.utos(3), 1) self.assertEqual(signal.utos(4), 1) self.assertEqual(signal.utos(6), 2) self.assertEqual(signal.utos(9), 2) self.assertEqual(signal.utos(17), 4) self.assertEqual(signal.utos(20), 5)
def test_signal1d_transform(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) t = signal.transform(Transforms.FREQUENCY_DOMAIN) self.assertIsInstance(t, Signal) self.assertEqual(t.domain, Transforms.FREQUENCY_DOMAIN) self.assertEqual(t.ndim, signal.ndim) self.assertEqual(t.dim, signal.dim) it = t.transform(Transforms.TIMESPACE_DOMAIN) self.assertIsInstance(it, Signal) self.assertEqual(it.domain, Transforms.TIMESPACE_DOMAIN) self.assertEqual(it.ndim, signal.ndim) self.assertEqual(it.dim, signal.dim)
def test_signal1d_normalize(self): signal = Signal1D(y=[1, 2, 3, 4, 5]) s1 = signal.normalize() s2 = signal.normalize(-1, 1) s3 = signal.normalize(10, 100) s4 = signal.normalize(0.5, 1.5) self.assertEqual(s1.get()[0], 0) self.assertEqual(s1.get()[4], 1) self.assertEqual(s2.get()[0], -1) self.assertEqual(s2.get()[4], 1) self.assertEqual(s3.get()[0], 10) self.assertEqual(s3.get()[4], 100) self.assertEqual(s4.get()[0], 0.5) self.assertEqual(s4.get()[4], 1.5)
def test_signal1d_set(self): data = [1, 2, 3, 4, 5] datax = [10, 11, 12, 13, 14] datax2 = [10, 11, 12, 13] signal = Signal1D() signal.set(data) self.assertListEqual(signal.get(), data) self.assertListEqual(signal.get(alls=True)[0], data) self.assertListEqual(signal.get(alls=True)[1], [0, 1, 2, 3, 4]) self.assertEqual(signal.length, len(data)) self.assertEqual(signal.sfreq, 1) signal.set(data, datax) self.assertListEqual(signal.get(), data) self.assertListEqual(signal.get(alls=True)[0], data) self.assertListEqual(signal.get(alls=True)[1], datax) with self.assertRaises(ValueError): signal.set([]) with self.assertRaises(ValueError): signal.set([], datax) with self.assertRaises(ValueError): signal.set([], datax2)
def test_signal1d__len__(self): s0 = Signal1D(y=[1, 2, 3, 4, 5]) s1 = Signal1D(y=[]) self.assertEqual(len(s0), 5) self.assertEqual(len(s1), 0)
IPATH = "examples/data/" OPATH = "examples/data/" AUDIOFILE = IPATH + "music.wav" IMAGEFILE = OPATH + "cimage.png" # Signal creation # =============== # Create a 3-second sine wave at 5Hz swave = Sinewave1D(f=5, length=3, sfreq=80) # Create normally distributed (gaussian) noise gnoise = Noise1D(pdf="normal", length=3, sfreq=80) # Create a custom 1D signal y(x)=x^3-4x+12 in [-3, 5] cust = Signal1D(y=[x**3 - 4 * x + 12 for x in range(-3, 5)], x=[x for x in range(-3, 5)]) # Create a 2D Gaussian centered at (5, 5) with std of (2, 2) gauss = Gaussian2D(u=(5, 5), s=(2, 2), length=(10, 10), sfreq=5) # Create a list of 1D signals from the audio channels in a file audio = AudioChannel.from_file(AUDIOFILE) # Create a 1D signal from audio downmixed to mono audio1 = AudioChannel.from_file(AUDIOFILE, mono=True)[0] # Create a list of 1D signals from the color channels of an image image = ImageChannel.from_file(IMAGEFILE) # Create a 1D signal from a color image downmixed to grey image1 = ImageChannel.from_file(IMAGEFILE, mono=True)[0]