def test_fourier_spectra_with_motion():
    record_path = TEST_DATA_DIR

    record_filename = 'test_motion_dt0p01.txt'
    motion_dt = 0.01
    rec = np.loadtxt(record_path + record_filename)

    rec2 = np.zeros(2 ** 13)
    rec2[:len(rec)] = rec
    acc_signal = AccSignal(-rec, motion_dt)

    nfreq = len(acc_signal.fa_spectrum)
    test_filename = 'test_motion_true_fourier_spectra.csv'
    data = np.loadtxt(record_path + test_filename, skiprows=1, delimiter=",")
    freqs = data[:nfreq - 1, 0]
    fa = data[:nfreq - 1, 1]
    phase = data[:nfreq - 1, 2]

    fa_eqsig = abs(acc_signal.fa_spectrum)
    freq_eqsig = acc_signal.fa_frequencies
    org_phases = np.angle(acc_signal.fa_spectrum)
    ss_phases = np.angle(np.fft.rfft(rec2))[:len(org_phases)] + 0.0001

    for i in range(10):
        print(phase[i], acc_signal.fa_spectrum[i + 1], org_phases[i + 1])

    assert ct.isclose(freqs[0], freq_eqsig[1], rel_tol=0.001), freqs[0]
    assert ct.isclose(freqs[20], freq_eqsig[21], rel_tol=0.0001)
    assert ct.isclose(freqs[-1], freq_eqsig[-1], rel_tol=0.001)
    for i in range(len(fa)):
        assert ct.isclose(fa[i], fa_eqsig[i + 1], abs_tol=0.00001), i
def test_butterpass():
    record_path = TEST_DATA_DIR
    record_filename = 'noise_test_1.txt'
    rec = load_test_record_from_file(record_path, record_filename)
    ssq_org = np.sum(rec.values ** 2)

    x = np.linspace(0, 1.0, rec.npts)
    rec.add_series(0.2 * x - 0.5 * x ** 2)
    ssq_w_linear = np.sum(rec.values ** 2)

    rec.butter_pass([0.2, 25])
    ssq_corrected = np.sum(rec.values ** 2)

    assert ssq_org != ssq_w_linear
    assert ct.isclose(ssq_org, 31854.72888, rel_tol=0.0001)
    assert ct.isclose(ssq_corrected, 10663.4862479, rel_tol=0.0001)
def test_duration_stats():
    record_path = TEST_DATA_DIR
    record_filename = 'noise_test_1.txt'

    rec = load_test_record_from_file(record_path, record_filename, scale=9.8)
    rec.generate_duration_stats()
    assert ct.isclose(rec.t_595, 18.03)
def test_butterpass():
    record_path = TEST_DATA_DIR
    record_filename = 'test_motion_dt0p01.txt'
    motion_step = 0.01
    rec = np.loadtxt(record_path + record_filename)
    acc_signal = Signal(rec, motion_step)

    ssq_org = np.sum(acc_signal.values ** 2)

    x = np.linspace(0, 1.0, acc_signal.npts)
    acc_signal.add_series(0.2 * x - 0.5 * x ** 2)
    ssq_w_linear = np.sum(acc_signal.values ** 2)

    acc_signal.butter_pass([0.2, 25])
    ssq_corrected = np.sum(acc_signal.values ** 2)

    assert ssq_org != ssq_w_linear
    assert ct.isclose(ssq_org, 395.9361125, rel_tol=0.0001)
    assert ct.isclose(ssq_corrected, 393.7198723, rel_tol=0.0001)
def test_remove_polyfit_1():
    record_path = TEST_DATA_DIR
    record_filename = 'test_motion_dt0p01.txt'
    motion_step = 0.01
    rec = np.loadtxt(record_path + record_filename)
    acc_signal = Signal(rec, motion_step)

    # Remove any trend
    acc_signal.remove_poly(poly_fit=1)
    ssq_cleaned = np.sum(acc_signal.values ** 2)

    # Add a trend
    acc_signal.add_series(np.linspace(0, 0.2, acc_signal.npts))
    ssq_w_linear = np.sum(acc_signal.values ** 2)

    # remove the trend
    acc_signal.remove_poly(poly_fit=1)
    ssq_corrected = np.sum(acc_signal.values ** 2)

    assert not ct.isclose(ssq_cleaned, ssq_w_linear)
    assert ct.isclose(ssq_cleaned, ssq_corrected)
def test_fourier_spectra_stable_against_aliasing():
    record_path = TEST_DATA_DIR

    record_filename = 'test_motion_dt0p01.txt'
    motion_step = 0.01
    rec = np.loadtxt(record_path + record_filename)
    rec2 = np.zeros(2 ** 13)
    rec2[:len(rec)] = rec
    org_signal = AccSignal(rec, motion_step)
    extended_signal = AccSignal(rec2, motion_step)

    rec_split = []
    for i in range(int(len(rec2) / 2)):
        rec_split.append(rec2[i * 2])

    acc_split = AccSignal(rec_split, motion_step * 2)

    org_fa = abs(org_signal.fa_spectrum)
    split_fa = abs(acc_split.fa_spectrum)
    ext_fa = abs(extended_signal.fa_spectrum)

    org_freq = abs(org_signal.fa_frequencies)
    split_freq = abs(acc_split.fa_frequencies)
    ext_freq = abs(extended_signal.fa_frequencies)

    for i in range(len(org_signal.fa_spectrum)):

        if i > 1830:
            abs_tol = 0.03
        else:
            abs_tol = 0.02

        assert ct.isclose(org_freq[i], ext_freq[i])
        assert ct.isclose(org_fa[i], ext_fa[i])

        if i < 2048:
            assert ct.isclose(org_freq[i], split_freq[i])
            assert ct.isclose(org_fa[i], split_fa[i], abs_tol=abs_tol), i
def test_remove_polyfit_1():
    record_path = TEST_DATA_DIR
    record_filename = 'noise_test_1.txt'
    rec = load_test_record_from_file(record_path, record_filename)
    rec.remove_poly(poly_fit=1)
    ssq_org = np.sum(rec.values ** 2)

    rec.add_series(np.linspace(0, 0.2, rec.npts))
    ssq_w_linear = np.sum(rec.values ** 2)

    rec.remove_poly(poly_fit=1)
    ssq_corrected = np.sum(rec.values ** 2)

    assert ssq_org != ssq_w_linear
    assert ct.isclose(ssq_org, ssq_corrected)