Skip to content

Commit

Permalink
Merge pull request #111 from jaidevd/jd-add-tests
Browse files Browse the repository at this point in the history
Add tests for utility functions.
  • Loading branch information
jaidevd committed Feb 29, 2016
2 parents 245c70f + 988c349 commit ac648af
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 110 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ env:
# command to install dependencies
install: source continuous_integration/install.sh
# command to run tests
script: nosetests --with-coverage --cover-package=tftb
script: nosetests -sv --with-coverage --cover-package=tftb
after_success:
coveralls
15 changes: 15 additions & 0 deletions tftb/generators/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Cube26 product code
#
# (C) Copyright 2015 Cube26 Software Pvt Ltd
# All right reserved.
#
# This file is confidential and NOT open source. Do not distribute.
#

"""
Tests for the generatos module.
"""
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,34 @@
class TestMisc(TestBase):

def test_window_derivative(self):
"""Test if the derivative of a window function is calculated
properly."""
window = hanning(210)
derivative = derive_window(window)
ix_win_maxima = np.argmax(window)
self.assertAlmostEqual(derivative[ix_win_maxima], 0.0, places=3)

def test_altes(self):
"""Test the altes signal generation."""
ideal = np.array([0.00200822, -0.21928398, 0.66719239, 0.66719239,
-0.17666382, -0.17009953, -0.038399, -0.00083597])
actual = misc.altes(8, 0.1, 0.5)
np.testing.assert_allclose(ideal, actual, atol=1e-8, rtol=1e-8)

def test_doppler(self):
"""Test the doppler signal generation."""
fm, am, iflaw = misc.doppler(512, 200.0, 65, 10, 50)
self.assert_is_monotonic_decreasing(iflaw)

def test_klauder(self):
"""Test the klauder wavelet generation."""
ideal = np.array([0.14899879, -0.16633309, -0.42806931, 0.16605633,
0.70769336, 0.16605633, -0.42806931, -0.16633309])
actual = misc.klauder(8)
np.testing.assert_allclose(ideal, actual, atol=1e-8, rtol=1e-8)

def test_mexhat(self):
"""Test the mexhat wavelet generation."""
ideal = np.array([-4.36444274e-09, -4.29488427e-04, -1.47862882e-01,
4.43113463e-01, -1.47862882e-01, -4.29488427e-04,
-4.36444274e-09])
Expand All @@ -53,6 +59,7 @@ def test_mexhat(self):
self.assertCountEqual(minima[0], (2, 4))

def test_gdpower(self):
"""Test the gdpower generation."""
ideal_sig = np.array([0.08540661 + 0.05077147j, 0.16735776 + 0.11542816j,
-0.08825763 + 0.17010894j, 0.04412953 - 0.01981114j,
-0.04981628 + 0.34985966j, -0.56798889 - 0.07983783j,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
class TestNoise(TestBase):

def test_noisecu(self):
"""Test uniform white noise generation."""
x = noise.noisecu(128)
self.assertAlmostEqual(x.std() ** 2, 1, places=1)

def test_noisecg(self):
"""Test Gaussian white noise generation."""
x = noise.noisecg(128)
self.assertAlmostEqual(x.std() ** 2, 1, places=1)

def test_dopnoise(self):
"""Test doppler noise generation"""
signal, iflaw = noise.dopnoise(500, 200, 60, 10, 70, 128)
energy = np.sum(np.abs(signal) ** 2)
self.assertAlmostEqual(energy, 1, 3)
Expand Down
37 changes: 37 additions & 0 deletions tftb/generators/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Cube26 product code
#
# (C) Copyright 2015 Cube26 Software Pvt Ltd
# All right reserved.
#
# This file is confidential and NOT open source. Do not distribute.
#

"""
Tests for tftb.generators.utils
"""

import unittest
import numpy as np

from tftb.generators import utils, fmlin


class TestUtils(unittest.TestCase):

def test_sigmerge(self):
"""Test merging of signals with a given SNR."""
signal = fmlin(128)[0]
noise = np.random.randn(128,)
noisy_signal = utils.sigmerge(signal, noise)
gamma_estimate = np.sqrt(signal.var() / noise.var())
np.testing.assert_allclose(noisy_signal,
signal + gamma_estimate * noise, rtol=1e-2,
atol=1e-2)


if __name__ == '__main__':
unittest.main()
19 changes: 2 additions & 17 deletions tftb/generators/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import numpy as np
from scipy.signal import hilbert
from scipy.integrate import trapz
# from tftb.utils import nextpow2


Expand All @@ -26,22 +27,6 @@ def sigmerge(x1, x2, ratio=0.0):
return sig


def integrate(y, x):
n = y.shape[0]
dy = y[:(n - 1)] + y[1:]
dx = (x[1:n] - x[:(n - 1)]) / 2.0
return np.dot(dy, dx)


def integ2d(mat, x, y):
m, n = mat.shape
mat = np.sum(mat.T, axis=0).T - mat[:, 0] / 2 - mat[:, n - 1] / 2
mat *= (x[1] - x[0])
dmat = mat[:(m - 1)] + mat[1:m]
dy = (y[1:m] - y[:(m - 1)]) / 2
return np.sum(dmat * dy)


def scale(X, a, fmin, fmax, N):
"""Scale a signal with the Mellin transform.
Expand Down Expand Up @@ -97,7 +82,7 @@ def scale(X, a, fmin, fmax, N):
geo_f.reshape((1, 128))))
dilate_sig = np.zeros((2 * Mcurrent,), dtype=complex)
for kk in range(2 * int(Mcurrent)):
dilate_sig[kk] = integrate(itfmatx[kk, :] * DS[:N], geo_f)
dilate_sig[kk] = trapz(itfmatx[kk, :] * DS[:N], geo_f)
S[(Mmax - Mcurrent):(Mmax + Mcurrent)] = dilate_sig
ptr += 1

Expand Down
2 changes: 2 additions & 0 deletions tftb/processing/tests/test_cohen.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
class TestCohen(TestBase):

def test_spectrogram_reality(self):
"""Test the reality property of the spectrogram."""
signal, _ = fmlin(128, 0.1, 0.4)
window = kaiser(17, 3 * np.pi)
tfr, _, _ = cohen.Spectrogram(signal, n_fbins=64, fwindow=window).run()
self.assertTrue(np.all(np.isreal(tfr)))

def test_spectrogram_linearity(self):
"""Test the linearity property of the spectrogram."""
signal, _ = fmlin(128, 0.1, 0.4)
window = kaiser(17, 3 * np.pi)
tfr1, _, _ = cohen.Spectrogram(signal, n_fbins=64,
Expand Down
3 changes: 3 additions & 0 deletions tftb/processing/tests/test_freq_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@
class TestFrequencyDomainProcessing(TestBase):

def test_instfreq(self):
"""Test instantaneous frequency calculation."""
signal, _ = fm.fmlin(128, 0.05, 0.3, 50)
ifreq = fproc.inst_freq(signal)[0]
self.assertAlmostEqual(ifreq.min(), 0.05, places=2)
self.assertAlmostEqual(ifreq.max(), 0.3, places=2)
self.assert_is_linear(ifreq)

def test_locfreq(self):
"""Test calculation of localized frequency characteristics."""
signal, _ = fm.fmlin(128, 0.05, 0.3, 50)
input_avg_freq = (0.05 + 0.3) / 2.0
avg_norm_freq, bandwidth = fproc.locfreq(signal)
self.assertAlmostEqual(avg_norm_freq, input_avg_freq, places=2)

def test_group_delay(self):
"""Test Group delay calculation."""
n_points = 128
signal = am.amgauss(n_points, 64, 30) * fm.fmlin(n_points, 0.1, 0.4)[0]
fnorm = np.arange(0.1, 0.38, step=0.04)
Expand Down
1 change: 1 addition & 0 deletions tftb/processing/tests/test_time_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
class TestTimeDomainProcessors(TestBase):

def test_loctime(self):
"""Test computation of localized time characteristics."""
signal = am.amgauss(160, 80, 50)
tm, T = tmd.loctime(signal)
self.assertAlmostEqual(tm, 79, places=6)
Expand Down
34 changes: 34 additions & 0 deletions tftb/processing/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Cube26 product code
#
# (C) Copyright 2015 Cube26 Software Pvt Ltd
# All right reserved.
#
# This file is confidential and NOT open source. Do not distribute.
#

"""
Tests for tftb.processing.utils
"""

import unittest
import numpy as np
from tftb.processing import utils


class TestUtils(unittest.TestCase):

def test_derive_window(self):
"""Test derivative of window function."""
from scipy.signal import gaussian
g = gaussian(129, 10)
dwindow = utils.derive_window(g)
self.assertEqual(dwindow[64], 0)
self.assertTrue(np.all(dwindow[:64] >= 0))
self.assertTrue(np.all(dwindow[64:] <= 0))

if __name__ == '__main__':
unittest.main()
76 changes: 76 additions & 0 deletions tftb/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Cube26 product code
#
# (C) Copyright 2015 Cube26 Software Pvt Ltd
# All right reserved.
#
# This file is confidential and NOT open source. Do not distribute.
#

"""
Tests for tftb.utils
"""

import unittest
import numpy as np
from tftb import utils


class TestUtils(unittest.TestCase):

def test_is_linear(self):
"""Test the is_linear function."""
x = np.arange(10)
self.assertTrue(utils.is_linear(x))
x = np.sin(x)
self.assertFalse(utils.is_linear(x))

def test_nextpow2(self):
"""Test the nextpow2 function."""
self.assertEqual(utils.nextpow2(2), 1)
self.assertEqual(utils.nextpow2(17), 5)
import warnings
with warnings.catch_warnings(record=True) as catcher:
utils.nextpow2(-3)
self.assertEqual(len(catcher), 1)
self.assertTrue(catcher[-1].category, RuntimeWarning)

def test_divider(self):
"""Test the divider function."""
self.assertItemsEqual(utils.divider(4), (2, 2))
self.assertItemsEqual(utils.divider(17), (1, 17))
self.assertItemsEqual(utils.divider(60), (10, 6))
x = np.arange(1, 101)
lowers = np.zeros(x.shape)
uppers = np.zeros(x.shape)
for i, num in enumerate(x):
a, b = utils.divider(num)
lowers[i] = a
uppers[i] = b
perfect_squares = np.arange(1, 11) ** 2
np.testing.assert_allclose(perfect_squares, x[lowers == uppers])

def test_nearest_odd(self):
"""Test the nearest_odd function."""
self.assertEqual(utils.nearest_odd(0), 1)
self.assertEqual(utils.nearest_odd(2), 3)
self.assertEqual(utils.nearest_odd(-0.00001), -1)

def test_modulo(self):
"""Test the modulo function."""
x = np.arange(1, 11)
np.testing.assert_allclose(utils.modulo(x, 1), np.ones(x.shape))
np.testing.assert_allclose(utils.modulo(x, 2),
np.array([1, 2, 1, 2, 1, 2, 1, 2, 1, 2]))
np.testing.assert_allclose(utils.modulo(x, 3),
np.array([1, 2, 3, 1, 2, 3, 1, 2, 3, 1]))
np.testing.assert_allclose(utils.modulo(x, 4),
np.array([1, 2, 3, 4, 1, 2, 3, 4, 1, 2]))
np.testing.assert_allclose(utils.modulo(x, 5),
np.array([1, 2, 3, 4, 5, 1, 2, 3, 4, 5]))

if __name__ == '__main__':
unittest.main()
Loading

0 comments on commit ac648af

Please sign in to comment.