Typedefs | Functions

src/distances.h File Reference

STATUS: unstable Distances between: points, signals, trials. More...

#include "array.h"
#include "linalg.h"
#include "clustering.h"
#include "definitions.h"
#include "recurrence_plot.h"
#include "nonlinear.h"
#include "warping.h"
#include "optarg.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Typedefs

typedef double **(* PointwiseDistanceFunction )(double *, int, double *, int, double **, OptArgList *)
typedef double(* VectorDistanceFunction )(const double *, const double *, int, OptArgList *)

Functions

double dist_point_line (double *p, double *x, double *y)
 Compute distance from point p to the line through points x and y.
Arraydistmatrix_signaldist (VectorDistanceFunction f, const Array *s1, const Array *s2, Array *out, OptArgList *optargs)
 Pointwise distance matrix:$ d_{ij} = d( s1_i, s2_j ) $.
double ** eeg_distmatrix (EEG *eeg, VectorDistanceFunction f, double **d, OptArgList *optargs)
Arraymatrix_distmatrix (VectorDistanceFunction f, const Array *X, Array *D, OptArgList *optargs)
double pathdist_euclidean_dt (WarpPath *p1, WarpPath *p2)
 computes the distance between two warping-functions.
double ** signaldist_euclidean (double *s1, int n1, double *s2, int n2, double **d, OptArgList *optargs)
double ** signaldist_euclidean_derivative (double *s1, int n1, double *s2, int n2, double **d, OptArgList *optargs)
double ** signaldist_stft (double *s1, int n1, double *s2, int n2, double **d, OptArgList *optargs)
double ** vectordist_distmatrix (VectorDistanceFunction f, const double **X, int n, int p, double **D, ProgressBarFunction progress, OptArgList *optargs)
double vectordist_dtw (const double *x1, const double *x2, int p, OptArgList *optargs)
double vectordist_euclidean (const double *x1, const double *x2, int p, OptArgList *optargs)
 Euclidean distance between vector x1 and x2.
double vectordist_euclidean_normalized (const double *x1, const double *x2, int p, OptArgList *optargs)
 Euclidean distance between normalized vector x1 and x2.
double vectordist_regularized_dtw (const double *x1, const double *x2, int p, OptArgList *optargs)

Detailed Description

STATUS: unstable Distances between: points, signals, trials.

Vector Distance Functions

The distances (functions starting with vectordist_*()) are between two vectors of the same size, i.e. they calculate

\[ ||\vec{x}_i - \vec{x}_j|| \]

for two vectors and some metric. vectordist_distmatrix() applies one of these functions to all pairs of columns in the matrix$\mathbf{X}$, yielding a distance matrix

\[ D_{ij} = ||\vec{x}_i - \vec{x}_j|| \]

Definition in file distances.h.


Typedef Documentation

typedef double**(* PointwiseDistanceFunction)(double *, int, double *, int, double **, OptArgList *)

Definition at line 54 of file distances.h.

typedef double(* VectorDistanceFunction)(const double *, const double *, int, OptArgList *)

Definition at line 55 of file distances.h.


Function Documentation

double dist_point_line ( double *  p,
double *  x,
double *  y 
)

Compute distance from point p to the line through points x and y.

     d = abs(det([y-x;p-x]))/norm(y-x);
Parameters:
p point in question (x,y)-plane
x,y points specify the line x=(x_1,x_2), y=(y_1, y_2)

Definition at line 330 of file distances.c.

Array* distmatrix_signaldist ( VectorDistanceFunction  f,
const Array s1,
const Array s2,
Array out,
OptArgList optargs 
)

Pointwise distance matrix:$ d_{ij} = d( s1_i, s2_j ) $.

Parameters:
f the distance function between individual points in signals s1,s2
s1 a 2D (N1xp) array holding the (multivariate) time-series 1
s1 a 2D (N2xp) array holding the (multivariate) time-series 2
out either an NxN matrix, or NULL (allocated in function)
optargs may contain many different arguments which are passed to the VectorDistanceFunction (see details there):
Returns:
a NxN matrix containing the distances (allocated in function, or out)

Definition at line 98 of file distances.c.

double** eeg_distmatrix ( EEG eeg,
VectorDistanceFunction  f,
double **  d,
OptArgList optargs 
)

Compute trial-to-trial distance matrix for all trials in eeg-struct. Average over all channels in the EEG-struct.

Parameters:
eeg the EEG-data
f the function used to compare two ERPs
d user allocated memory, or NULL -> own memory is alloc'ed
optargs may contain:

  • "progress=void*", a progress-bar function; default=NULL;
  • userdata is passed to the vectordist_*() function

Definition at line 269 of file distances.c.

Array* matrix_distmatrix ( VectorDistanceFunction  f,
const Array X,
Array D,
OptArgList optargs 
)

build a distance matrix D from data X which consists of n observations with p features each. Observations are compared using f.

Parameters:
f distance function
X data (nxp)
D output matrix or NULL -> own memory allocation
progress NULL or a progressbar
optargs may contain arguments for the vectordistancefunction f

  • progress=void* progress-bar function, called every now and then.
Returns:
the distance matrix between all columns in X

Definition at line 36 of file distances.c.

double pathdist_euclidean_dt ( WarpPath p1,
WarpPath p2 
)

computes the distance between two warping-functions.

It is assumed that both pathes have the same starting and endpoint.

Todo:
fix this! work with new warppath-arrays!

Algorithm:

     1. compute distance transform for p1
     2. line-integral of p2 through DT(p1) 

Definition at line 351 of file distances.c.

double** signaldist_euclidean ( double *  s1,
int  n1,
double *  s2,
int  n2,
double **  d,
OptArgList optargs 
)

pointwise distance matrix for euclidean metric.

Parameters:
optargs is ignored

Definition at line 412 of file distances.c.

double** signaldist_euclidean_derivative ( double *  s1,
int  n1,
double *  s2,
int  n2,
double **  d,
OptArgList optargs 
)

calculate

\[ d(s_1(t_1), s_2(t_2))_{{derivative}} := \theta_1|\tilde{s_1}(t_1) - \tilde{s_2}(t_2)| + \theta_2|\tilde{s_1}'(t_1) - \tilde{s_2}'(t_2)| \]

where

\[ \tilde{s}(t) := \frac{s(t) - \langle s(t) \rangle_t}{\sqrt{\langle s(t)^2 \rangle_t}} \]

Derivatives are approximated with s'(t) = s(t)-s(t-1)

Parameters:
s1,s2 
d matrix or NULL (alloc'd in function)
optargs can contain:

  • theta1=double, default is 1.0
  • theta2=double, default is 1.0
Returns:
d or NULL (error)

Definition at line 445 of file distances.c.

double** signaldist_stft ( double *  s1,
int  n1,
double *  s2,
int  n2,
double **  d,
OptArgList optargs 
)
Todo:
at the moment, N_time is required to be n, the number of sampling points in s1 and s2. This needs to be fixed by interpolation

calculate

\[ d_{{STFT}}(s_1(t_1), s_2(t_2)) := || STFT\{s_1(t_1)\} - STFT\{s_2(t_2)\}||_{\circ}. \]

where

\[ STFT\{s(t)\}(\omega) = \int_{-\infty}^{+\infty} s(t)w(t-\tau)e^{-i\omega t} dt \]

Parameters:
s1,s2 
d matrix or NULL (alloc'd in function)
optargs may contain:

  • sample_frequency=double of the signal, default is 500 (should really be provided!)
  • winfct=void* windowing function, default is window_hanning
  • winlength=int size of the window, default is MAX( SQR( sqrt(next_pow2( n ))-3 ), 5 )
  • N_freq=int number of frequency bins, default is winlength*4
  • N_time=int number of time bins, default is n
  • corner_freqs=double* (array with two double entries), default is (0.0,250.0)
Returns:
d or NULL (error)

Definition at line 523 of file distances.c.

double** vectordist_distmatrix ( VectorDistanceFunction  f,
const double **  X,
int  n,
int  p,
double **  D,
ProgressBarFunction  progress,
OptArgList optargs 
)

build a distance matrix D from data X which consists of n observations with p features each. Observations are compared using f.

Parameters:
f distance function
X data (nxp)
D output matrix or NULL -> own memory allocation
progress NULL or a progressbar
optargs may contain arguments for the vectordistancefunction f
Returns:
the distance matrix between all columns in X

Definition at line 642 of file distances.c.

double vectordist_dtw ( const double *  x1,
const double *  x2,
int  p,
OptArgList optargs 
)

Distance which sums the absolute deviation from the main diagonal of the dtw-warp-path computed on x1 and x2.

Parameters:
x1,x2 vectors of size p
optargs may contain:

  • pointdistance=void* a PointwiseDistanceFunction, default is signaldist_euclidean
  • optargs for the distfunction (optargs is passed 'as is' to this function)

Definition at line 221 of file distances.c.

double vectordist_euclidean ( const double *  x1,
const double *  x2,
int  p,
OptArgList optargs 
)

Euclidean distance between vector x1 and x2.

Parameters:
x1,x2 vectors of size p
optargs is ignored

Definition at line 160 of file distances.c.

double vectordist_euclidean_normalized ( const double *  x1,
const double *  x2,
int  p,
OptArgList optargs 
)

Euclidean distance between normalized vector x1 and x2.

Normalization is defined by

\[ \hat{x}_i = \frac{x_i-\langle x_i \rangle_i}{\sqrt{\langle x_i^2\rangle_i}} \]

Parameters:
x1,x2 vectors of size p
optargs is ignored

Definition at line 182 of file distances.c.

double vectordist_regularized_dtw ( const double *  x1,
const double *  x2,
int  p,
OptArgList optargs 
)