Beispiel #1
0
def time_frequency_cc_difference(t, s1, s2, dt_new, width, threshold):
    """
    Straight port of tfa_cc_new.m

    :param t: discrete time
    :param s1: discrete signal 1
    :param s2: discrete signal 2
    :param dt_new: time increment in the tf domain
    :param width: width of the Gaussian window
    :param threshold: fraction of the absolute signal below which the Fourier
        transform is set to zero in order to reduce computation time
    """
    # New time axis
    ti = utils.matlab_range(t[0], t[-1], dt_new)
    # Interpolate both signals to the new time axis
    si1 = interp1d(t, s1, kind=1)(ti)
    si2 = interp1d(t, s2, kind=1)(ti)

    # Extend the time axis, required for the correlation
    N = len(ti)
    t_cc = utils.matlab_range(t[0], (2 * N - 2) * dt_new, dt_new)

    # Rename some variables
    s1 = si1
    s2 = si2
    t_min = t_cc[0]

    # Initialize the meshgrid
    N = len(t_cc)
    dnu = 1.0 / (N * dt_new)

    nu = utils.matlab_range(0, float(N - 1) / (N * dt_new), dnu)
    tau = t_cc
    TAU, NU = np.meshgrid(tau, nu)

    # Compute the time frequency representation
    tfs = np.zeros(NU.shape, dtype="complex128")

    for k in xrange(len(tau)):
        # Window the signals
        w = utils.gaussian_window(ti - tau[k], width)
        f1 = w * s1
        f2 = w * s2

        if np.abs(f1).max() > (threshold * np.abs(s1).max()):
            cc = utils.cross_correlation(f2, f1)
            tfs[k, :] = np.fft.fft(cc) / np.sqrt(2.0 * np.pi) * dt_new
            tfs[k, :] = tfs[k, :] * np.exp(-2.0 * np.pi * 1j * t_min * nu)
        else:
            tfs[k, :] = 0.0
    return TAU, NU, tfs
Beispiel #2
0
def time_frequency_cc_difference(t, s1, s2, dt_new, width, threshold):
    """
    Straight port of tfa_cc_new.m

    :param t: discrete time
    :param s1: discrete signal 1
    :param s2: discrete signal 2
    :param dt_new: time increment in the tf domain
    :param width: width of the Gaussian window
    :param threshold: fraction of the absolute signal below which the Fourier
        transform is set to zero in order to reduce computation time
    """
    # New time axis
    ti = utils.matlab_range(t[0], t[-1], dt_new)
    # Interpolate both signals to the new time axis
    si1 = interp1d(t, s1, kind=1)(ti)
    si2 = interp1d(t, s2, kind=1)(ti)

    # Extend the time axis, required for the correlation
    N = len(ti)
    t_cc = utils.matlab_range(t[0], (2 * N - 2) * dt_new, dt_new)

    # Rename some variables
    s1 = si1
    s2 = si2
    t_min = t_cc[0]

    # Initialize the meshgrid
    N = len(t_cc)
    dnu = 1.0 / (N * dt_new)

    nu = utils.matlab_range(0, float(N - 1) / (N * dt_new), dnu)
    tau = t_cc
    TAU, NU = np.meshgrid(tau, nu)

    # Compute the time frequency representation
    tfs = np.zeros(NU.shape, dtype="complex128")

    for k in xrange(len(tau)):
        # Window the signals
        w = utils.gaussian_window(ti - tau[k], width)
        f1 = w * s1
        f2 = w * s2

        if np.abs(f1).max() > (threshold * np.abs(s1).max()):
            cc = utils.cross_correlation(f2, f1)
            tfs[k, :] = np.fft.fft(cc) / np.sqrt(2.0 * np.pi) * dt_new
            tfs[k, :] = tfs[k, :] * np.exp(-2.0 * np.pi * 1j * t_min * nu)
        else:
            tfs[k, :] = 0.0
    return TAU, NU, tfs
Beispiel #3
0
def test_cross_correlation():
    """
    Tests the cross correlation function and compares it to a reference
    solution calculated in Matlab.
    """
    # Load the matlab file.
    matlab_file = os.path.join(data_dir, "matlab_cross_correlation_reference_solution.mat")
    cc_matlab = loadmat(matlab_file)["cc"][0]

    # Calculate two test signals.
    _, u = utils.get_dispersed_wavetrain()
    _, u0 = utils.get_dispersed_wavetrain(a=3.91, b=0.87, c=0.8, body_wave_factor=0.015, body_wave_freq_scale=1.0 / 2.2)

    cc = utils.cross_correlation(u, u0)
    np.testing.assert_allclose(cc, cc_matlab)
Beispiel #4
0
def time_frequency_cc_difference(t, s1, s2, width, threshold=1E-2):
    """
    Straight port of tfa_cc_new.m

    :param t: discrete time
    :param s1: discrete signal 1
    :param s2: discrete signal 2
    :param width: width of the Gaussian window
    :param threshold: fraction of the absolute signal below which the Fourier
        transform is set to zero in order to reduce computation time
    """
    dt = t[1] - t[0]

    # Extend the time axis, required for the correlation
    N = len(t)
    t_cc = np.linspace(t[0], t[0] + (2 * N - 2) * dt, (2 * N - 1))

    N = len(t_cc)
    dnu = 1.0 / (N * dt)

    nu = np.linspace(0, (N - 1) * dnu, N)
    tau = t_cc

    cc_freqs = scipy.fftpack.fftfreq(len(t_cc), d=dt)
    freqs = scipy.fftpack.fftfreq(len(t), d=dt)

    # Compute the time frequency representation
    tfs = np.zeros((len(t), len(t)), dtype="complex128")

    threshold = np.abs(s1).max() * threshold

    for k in range(len(t)):
        # Window the signals
        w = utils.gaussian_window(t - tau[k], width)
        f1 = w * s1
        f2 = w * s2

        if min(np.abs(f1).max(), np.abs(f2).max()) < threshold:
            continue

        cc = utils.cross_correlation(f2, f1)
        tfs[k, :] = \
            scipy.interpolate.interp1d(cc_freqs, scipy.fftpack.fft(cc))(freqs)
    tfs *= dt / np.sqrt(2.0 * np.pi)

    return tau, nu, tfs
Beispiel #5
0
def time_frequency_cc_difference(t, s1, s2, width, threshold=1E-2):
    """
    Straight port of tfa_cc_new.m

    :param t: discrete time
    :param s1: discrete signal 1
    :param s2: discrete signal 2
    :param width: width of the Gaussian window
    :param threshold: fraction of the absolute signal below which the Fourier
        transform is set to zero in order to reduce computation time
    """
    dt = t[1] - t[0]

    # Extend the time axis, required for the correlation
    N = len(t)
    t_cc = np.linspace(t[0], t[0] + (2 * N - 2) * dt, (2 * N - 1))

    N = len(t_cc)
    dnu = 1.0 / (N * dt)

    nu = np.linspace(0, (N - 1) * dnu, N)
    tau = t_cc

    cc_freqs = scipy.fftpack.fftfreq(len(t_cc), d=dt)
    freqs = scipy.fftpack.fftfreq(len(t), d=dt)

    # Compute the time frequency representation
    tfs = np.zeros((len(t), len(t)), dtype="complex128")

    threshold = np.abs(s1).max() * threshold

    for k in xrange(len(t)):
        # Window the signals
        w = utils.gaussian_window(t - tau[k], width)
        f1 = w * s1
        f2 = w * s2

        if min(np.abs(f1).max(), np.abs(f2).max()) < threshold:
            continue

        cc = utils.cross_correlation(f2, f1)
        tfs[k, :] = \
            scipy.interpolate.interp1d(cc_freqs, scipy.fftpack.fft(cc))(freqs)
    tfs *= dt / np.sqrt(2.0 * np.pi)

    return tau, nu, tfs
Beispiel #6
0
    def test_cross_correlation(self):
        """
        Tests the cross correlation function and compares it to a reference
        solution calculated in Matlab.
        """
        # Load the matlab file.
        matlab_file = os.path.join(
            self.data_dir, "matlab_cross_correlation_reference_solution.mat")
        cc_matlab = loadmat(matlab_file)["cc"][0]

        # Calculate two test signals.
        _, u = utils.get_dispersed_wavetrain()
        _, u0 = utils.get_dispersed_wavetrain(a=3.91,
                                              b=0.87,
                                              c=0.8,
                                              body_wave_factor=0.015,
                                              body_wave_freq_scale=1.0 / 2.2)

        cc = utils.cross_correlation(u, u0)
        np.testing.assert_allclose(cc, cc_matlab)