Thursday, July 15, 2010

centroid

Digital Signal Processing Library

Voice Lab

centroid


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;
}


No comments:

Post a Comment