vector_distance

Digital Signal Processing Library

Voice Lab

Program name: vector_distance

Language: C

In file: vector_lib.c

Objective: Calculate distance between two vectors

Usage: double vector_distance(double *vector1, double *vector2, int vector_width, int options);

Parameters:

  • vector1 - The pointer to the first vector
  • vector2 - The pointer to the second vector
  • vector_width - Number of data points in each vactor
  • options - The normalization options
    • if(options == 2) distance = sqrt(distance);
    • if(options == 8) distance /= vector_width;
Return
  • distance between vectors

Mathematical Description:

Comments:

User Comments

Code:

double vector_distance(double *vector1, double *vector2, int vector_width, int options)
{
        int i, offset;
        double distance, diff;

        /* Euclidean distance is defined as the square root of the square of the diferences of each element */
        /* For this routine to return the Euclidean distance between two vectors, OPTIONS must be set to two */

        /* Information taken from Douglas O'Shaughnessy's book */
        /*   "Speech Communication, Human and Machine" 1990, page 315 equation 7.31 */

        /* start with zero distance */
        distance = 0.0;

        offset = options & 0x01;

        for(i = offset; i < (vector_width+offset); i++) {
                /* add the square of the distance between each element of the vectors */
                diff = (vector1[i] - vector2[i]);
                distance += (diff * diff);
                number_subs++;
                number_muls++;
                number_adds++;
        }
        number_adds--; /* compensate the first add with zero ??? */

        if(options & 0x02)  distance = sqrt(distance);
        if(options & 0x08)  distance /= vector_width;

        return(distance);
}