Digital Signal Processing LibraryVoice LabProgram 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:
Mathematical Description: 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); } |
English Version > Documents and tutorials > D.Sc. projects > Routines for DSP > vector_lib >