centroid

Digital Signal Processing Library

Voice Lab

Program name: centroid

Language: C

In file: vector_lib.c

Objective: Find centroid of group of vectors

Usage: void centroid(double **cb_vector, int cb_length, int vector_width, double **vector, int number_vectors, int *vector_index);

Parameters:

  • cb_vector - The pointer to the codebook vector
  • cb_length - The number of vectors in the codebook
  • vector_width - Number of data points in each vactor
  • vector - array of vectors to classify
  • number_vectors - number of vectors in array
  • vector_index - index of closest codebook vector for each vector in array
Return value
  • void

Mathematical Description:

Comments:

User Comments

Code:

void centroid(double **cb_vector, int cb_length, int vector_width, double **vector, int number_vectors, int *vector_index)
{
        int i, j;
        int *number_class; /* stores the number of vectors in each class */
        double *temp_vector, **temp_matrix, *temp_matrix_data_ptr;
        int temp_vectors_max;

        // vector_classify(cb_vector, cb_length, vector_width, vector, number_vectors, vector_index);

        /* Allocate temporary vector */
        vector_allocate(&temp_vector, vector_width);

        /* Allocate temporary matrix data pointer for temporary codebook */
        temp_vectors_max = cb_length;
        matrix_allocate(&temp_matrix, &temp_matrix_data_ptr, vector_width, &temp_vectors_max);

        /* Clear matrix */
        for(i = 0; i < cb_length; i++) {
                for(j = 0; j < vector_width; j++) {
                        temp_matrix[i][j] = 0.0;
                }
        }

        /* add the vectors in each cloud */
        for(i = 0; i < number_vectors; i++) {
                vector_add(temp_matrix[vector_index[i]], vector[i], vector_width, &temp_matrix[vector_index[i]]);
        }

        /* Compensate for the vector_add routine */
        for(i = 0; i < cb_length; i++) {
                for(j = 0; j < (vector_width - 1); j++) {
                        number_adds--;
                }
        }

        /* Allocate number_class array */
        number_class = malloc(cb_length * sizeof(int));
        if(number_class == NULL) {
                fprintf(stderr, "Error allocating memory (centroid - number_class)\n");
                exit(1);
        }

        /* clear class_number array */
        for(i = 0; i < cb_length; i++) {
                number_class[i] = 0;
                number_adds--;
        }

        /* count vector in each cloud */
        for(i = 0; i < number_vectors; i++) {
                number_class[vector_index[i]]++;
                number_adds++;
        }

        /* divide each cloud sum, by number of vectors in each cloud */
        for(i = 0; i < cb_length; i++) {
                for(j = 0; j < vector_width; j++){
                        temp_vector[j] = temp_matrix[i][j];
                        if(number_class[i] != 0) {
                                temp_matrix[i][j] = temp_vector[j]/number_class[i];
                        }
                        number_divs++;
                }
        }

        for(i = 0; i < cb_length; i++) {
                if(number_class[i] != 0) {
                        /* copy temp_matrix to cb_vector */
                        vector_copy(temp_matrix[i], vector_width, &cb_vector[i]);
                }
        }
        free(temp_vector);
        free(number_class);
        free(temp_matrix_data_ptr);
        free(temp_matrix);
        return;
}