From 59ff35e5b6a81150a87cc0b5a972a91bd64c3ab9 Mon Sep 17 00:00:00 2001 From: andreas128 Date: Mon, 29 May 2017 21:55:44 +0100 Subject: Add subsample_alignment and it's test --- src/dab_util.py | 40 ++++++++++++++++++++++++---------------- src/dab_util_test.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/subsample_align.py | 13 +++++++------ 3 files changed, 80 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/dab_util.py b/src/dab_util.py index 617bd9a..3187036 100644 --- a/src/dab_util.py +++ b/src/dab_util.py @@ -2,6 +2,7 @@ import numpy as np import scipy import matplotlib.pyplot as plt import src.dabconst as dabconst +import src.subsample_align as sa from scipy import signal c = {} @@ -76,22 +77,26 @@ def lag_upsampling(sig_orig, sig_rec, n_up): l_orig = float(l) / n_up return l_orig -def fftlag(sig_orig, sig_rec, n_upsampling = 1): +def subsample_align(sig1, sig2): """ - Efficient way to find lag between two signals - Args: - sig_orig: The signal that has been sent - sig_rec: The signal that has been recored + Returns an aligned version of sig1 and sig2 by cropping and subsample alignment """ - #off = sig_rec.shape[0] - #fft1 = np.fft.fft(sig_orig, n=sig_orig.shape[0]) - #fft2 = np.fft.fft(np.flipud(sig_rec), n=sig_rec.shape[0]) - #fftc = fft1 * fft2 - #c = np.fft.ifft(fftc) - c = signal.convolve(sig_orig, np.flipud(sig_rec)) - #c = signal.correlate(sig_orig, sig_rec) - return c - return np.argmax(c) - off + 1 + off_meas = lag_upsampling(sig2, sig1, n_up=1) + off = int(abs(off_meas)) + + if off_meas > 0: + sig1 = sig1[:-off] + sig2 = sig2[off:] + elif off_meas < 0: + sig1 = sig1[off:] + sig2 = sig2[:-off] + + if off % 2 == 1: + sig1 = sig1[:-1] + sig2 = sig2[:-1] + + sig2 = sa.subsample_align(sig2, sig1) + return sig1, sig2 def get_amp_ratio(ampl_1, ampl_2, a_out_abs, a_in_abs): idxs = (a_in_abs > ampl_1) & (a_in_abs < ampl_2) @@ -108,5 +113,8 @@ def get_transmission_frame_indices(n_frames, offset, rate = 2048000): indices = [tm1.S_F * i + offset for i in range(n_frames)] return indices -def fromfile(filename, offset, length): - return np.memmap(filename, dtype=np.complex64, mode='r', offset=64/8*offset, shape=length) +def fromfile(filename, offset=0, length=None): + if length is None: + return np.memmap(filename, dtype=np.complex64, mode='r', offset=64/8*offset) + else: + return np.memmap(filename, dtype=np.complex64, mode='r', offset=64/8*offset, shape=length) diff --git a/src/dab_util_test.py b/src/dab_util_test.py index be36d53..3f9e941 100644 --- a/src/dab_util_test.py +++ b/src/dab_util_test.py @@ -1,5 +1,7 @@ from scipy import signal import numpy as np +import pandas as pd +from tqdm import tqdm import src.gen_source as gs import src.dab_util as du @@ -28,7 +30,54 @@ def test_phase_offset(lag_function, tol): res.append(np.abs(off-off_meas)