Showing posts with label c program. Show all posts
Showing posts with label c program. Show all posts

Tuesday, August 24, 2010

vector_write

Digital Signal Processing Library

Voice Lab

vector_write


void vector_write(double *vector, int vector_width, FILE *out_file, int options)
{
int i, offset;

/*
** The idea of the options flag is to make the program more flexible.
** The first bit is reserved for the offset.
** The offset can be 0 or 1. Under normal circumstances it will be 0.
** In the case of arrays being 1 more than the order,
** and needing to disregard the first element, set offset to 1.
** The fifth bit (0x10) is reserverd for the mode.
** If mode is 0, each value will be on a separate line.
** If mode is 1, each vector will occupy only one line.
*/

offset = options & 0x01;

if(options & 0x10) {
for(i = offset; i < (vector_width+offset); i++) {
if(vector[i] >= 0) {
fprintf(out_file, " %lf ", vector[i]);
} else {
/* compensate for the minus sign */
fprintf(out_file, "%lf ", vector[i]);
}
}
fprintf(out_file, "\n");
} else {
for(i = offset; i < (vector_width+offset); i++) {
fprintf(out_file, "%lf\n", vector[i]);
}
}
return;
}


matrix_write

Digital Signal Processing Library

Voice Lab

matrix_write


void matrix_write(double **vector, int vector_width, \
int min, int max, char *file_name, int options)
{
int i;
FILE *out_file;

/* open output file for writing */
if((out_file = fopen(file_name,"w")) == NULL) {
fprintf(stderr, "Cannot open output file: %s\n", file_name);
exit(1);
}

for(i = min; i < max; i++) {
vector_write(vector[i], vector_width, out_file, options);
}

fclose(out_file);
return;
}


write_nsp_head

Digital Signal Processing Library

Voice Lab

write_nsp_head


void write_nsp_head(FILE *out_file, int data_size, int sample_rate, int data_length)
{
int header_length;
int size, i;
time_t now;
struct tm *times;
char month[20], date_string[30], txt[120];

fwrite("FORM", 1, sizeof("FORM")-1, out_file);
fwrite("DS16", 1, sizeof("DS16")-1, out_file);

/* This size should be the blocks from here to the end of the file */
/* it should be the HEDR chunk(40) + Note Chunk(8 + note_string) + SDA chunk(8 + data_length) */
/* Calculate size.... */
strcpy(txt, "Repackaged by RRB. File downsampled to 25000");
size = strlen(txt);
if(size%2) size++;
size += 40 + 8 + 8 + data_length;
write_data(out_file, size, 4);

fwrite("HEDR", 1, sizeof("HEDR")-1, out_file);

size = 32;
write_data(out_file, size, 4);

time(&now);
times = localtime(&now);
switch(times->tm_mon) {
case 0:
strcpy(month, "Jan");
break;
case 1:
strcpy(month, "Feb");
break;
case 2:
strcpy(month, "Mar");
break;
case 3:
strcpy(month, "Apr");
break;
case 4:
strcpy(month, "May");
break;
case 5:
strcpy(month, "Jun");
break;
case 6:
strcpy(month, "Jul");
break;
case 7:
strcpy(month, "Aug");
break;
case 8:
strcpy(month, "Sep");
break;
case 9:
strcpy(month, "Oct");
break;
case 10:
strcpy(month, "Nov");
break;
case 11:
strcpy(month, "Dec");
break;
}
sprintf(date_string, "%s %2d %2d:%2d:%2d %4d", month, times->tm_mday,
times->tm_hour, times->tm_min, times->tm_sec, 1900+times->tm_year);
fwrite(date_string, 20, sizeof(char), out_file);

printf("Date: %s\n", date_string);

write_data(out_file, sample_rate, 4);
printf("Sample Rate: %d\n", sample_rate);

write_data(out_file, data_size, 4);
write_data(out_file, 0xFFFF, 2);
write_data(out_file, 0xFFFF, 2);

fwrite("NOTE", 1, sizeof("NOTE")-1, out_file);

size = strlen(txt);
write_data(out_file, size, 4);

fwrite(txt, size, sizeof(char), out_file);
if(size%2) {
write_data(out_file, 0, 1);
}

fwrite("SDA_", 1, sizeof("SDA_")-1, out_file);

size = data_length;
write_data(out_file, size, 4);
printf("Data length: %d\n", data_length);

size = ftell(out_file);
}


Thursday, July 29, 2010

window_process

Digital Signal Processing Library

Voice Lab

window_process


void window_process(double *d_window_i, int window_type,\
int window_size, double **d_window_o)
{
int i;

/* calculate d_window_o */
switch(window_type) {
case 1:
/* rectangular window */
for(i = 0 ;i < window_size; i++)\
(*d_window_o)[i] = d_window_i[i];
break;
case 2:
/* hamming window */
for(i = 0; i %lt; window_size; i++) {
(*d_window_o)[i] = d_window_i[i]*\
(0.54 - 0.46*cos(2.0*PI*(i)/(window_size-1.0)));
}
break;
case 3:
/* Hann window */
for(i = 0 ;i %lt; window_size; i++) \
(*d_window_o)[i] = d_window_i[i]*\
(0.5 - 0.5*cos(2.0*PI*(i)/(window_size-1.0)));
break;
}
return;
}


save_plot

Digital Signal Processing Library

Voice Lab


save_plot



void save_plot(double **matrix, int order, int num_vectors, char *file_name, int options)
{
int i, j, offset;
FILE *out_file;

if((out_file = fopen(file_name,"w")) == NULL) {
fprintf(stderr, "Cannot open output file: %s\n", file_name);
exit(1);
}

offset = 0x01 & options;

for(i = 0; i < num_vectors; i++) {
for(j = offset; j < (order + offset); j++) {
fprintf(out_file, "%d, %d, %f\n", j, i, matrix[i][j]);
}
fprintf(out_file, "\n");
}
fclose(out_file);
}


check_wav

Digital Signal Processing Library

Voice Lab


check_wav



int check_wav(FILE *in_file_ptr, int *data_length, int *sample_rate)
{
int header_length;
int size;
char header[MAX_HEADER_LENGTH];

(*sample_rate) = -1;

/* check if the file has a WAVe header */

/* go to beginning of file */
fseek(in_file_ptr,0,SEEK_SET);

fread(&header, sizeof(char), 4, in_file_ptr);
if(strncmp(header, "RIFF", strlen("RIFF")) != 0) {
/* not wave header */
fseek(in_file_ptr,0,0);
return(0);
}

fread(&size, sizeof(char), 4, in_file_ptr);
fread(header, sizeof(char), 4, in_file_ptr);
if(strncmp(header, "WAVE", strlen("WAVE")) != 0) {
/* not wave header */
fseek(in_file_ptr,0,0);
return(0);
}
fread(header, sizeof(char), 4, in_file_ptr);
if(strncmp(header, "fmt ", strlen("fmt ")) != 0) {
/* not wave header */
fseek(in_file_ptr,0,0);
return(0);
}
fread(&size, sizeof(char), 4, in_file_ptr); /* size of wave section chunk (usually 16) */
fread(header, sizeof(char), 2, in_file_ptr); /* Type of WAVE format. */
fread(header, sizeof(char), 2, in_file_ptr); /* mono (0x01) or stereo (0x02). */
fread(&(*sample_rate), sizeof(char), 4, in_file_ptr); /* Sample rate. */
fread(&size, sizeof(char), 4, in_file_ptr); /* Bytes/Second. */

fread(header, sizeof(char), 2, in_file_ptr); /* Block alignment. */
(*data_length) = header[0];

fread(header, sizeof(char), 2, in_file_ptr); /* Bits/Sample. */
printf("Bits per sample: %d\n", header[0]);

fread(header, sizeof(char), 4, in_file_ptr);
if(strncmp(header, "data", strlen("data")) != 0) {
/* not valid wave header */
fseek(in_file_ptr,0,0);
(*sample_rate) = -1;
return(0);
}

fread(&size, sizeof(char), 4, in_file_ptr);

/* wave file found */
header_length = ftell(in_file_ptr);
return(header_length);
}



Thursday, July 15, 2010

distortion

Digital Signal Processing Library

Voice Lab

distortion


double distortion(double **cb_vector, int cb_length, int vector_width, double **vector, int number_vectors, int *vector_index, int options)
{
int i, opt;
double total_distortion;

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

total_distortion = 0.0;

//if(opt == 0) distance = distance;
//if(opt == 2) distance = sqrt(distance);
//if(opt == 8) distance /= vector_width;
opt = 2;

for(i = 0; i < number_vectors; i++) {
total_distortion += vector_distance(vector[i], cb_vector[vector_index[i]], vector_width, opt);
number_adds++;
}

number_adds--;

/*
** Compensate the number of additions, subtractions, and multiplications
** that are not necessary for the total distortion calculation
*/

for(i = 0; i < (number_vectors*vector_width); i++) {
number_subs--;
number_muls--;
}

for(i = 0; i < (number_vectors*(vector_width-1)); i++) {
number_adds--;
}

if(options == 1) total_distortion /= number_vectors;

return(total_distortion);
}


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


vector_classify

Digital Signal Processing Library

Voice Lab

vector_classify


void vector_classify(double **cb_vector, int cb_length, int vector_width, double **vector, int number_vectors, int *vector_index)
{
int i, j, options;
double distance, distance_index;

//if(options == 0) distance = distance;
//if(options == 2) distance = sqrt(distance);
//if(options == 8) distance /= vector_width;
options = 2;

for(i = 0; i < number_vectors; i++) {
/* set initial distance equal to the first codebook vector */
distance_index = vector_distance(cb_vector[0], vector[i], vector_width, options);

/* set initial vector equal to first codebook vector */
vector_index[i] = 0;

/* for each additional codebook vecto, see if the distance is shorter */
for(j = 1; j < cb_length; j++) {
distance = vector_distance(cb_vector[j], vector[i], vector_width, options);
number_comps++;
if(distance < distance_index) {
distance_index = distance;
vector_index[i] = j;
}
}
}
return;
}


vector_split

Digital Signal Processing Library

Voice Lab

vector_split


void vector_split(double *vector1, double delta, int vector_length, double **vector_2, double **vector_3)
{
double aux;

aux = 1.0 + delta;
vector_multiply(vector1, aux, vector_length, vector_2);

aux = 1.0 - delta;
vector_multiply(vector1, aux, vector_length, vector_3);
return;
}


vector_multiply

Digital Signal Processing Library

Voice Lab

vector_multiply


void vector_multiply(double *vector1, double value, int vector_width, double **vector2)
{
int i;

if(vector_width > MAX_VECTOR_WIDTH) {
fprintf(stderr, "vector_width greater than MAX_VECTOR_WIDTH\n");
fprintf(stderr, "Please increase MAX_VECTOR_WIDTH in vector_lib.h and recompile.\n");
exit(-1);
}

for(i = 0; i < vector_width; i++){
(*vector2)[i] = vector1[i] * value;
number_muls++;
}
return;
}


vector_add

Digital Signal Processing Library

Voice Lab

vector_add


void vector_add(double *vector1, double *vector2, int vector_width, double **vector3)
{
int i;

if(vector_width > MAX_VECTOR_WIDTH) {
fprintf(stderr, "vector_width greater than MAX_VECTOR_WIDTH\n");
fprintf(stderr, "Please increase MAX_VECTOR_WIDTH in vector_lib.h and recompile.\n");
exit(-1);
}

for(i = 0; i < vector_width; i++){
(*vector3)[i] = vector1[i] + vector2[i];
number_adds++;
}
return;
}


Wednesday, July 14, 2010

vector_copy

Digital Signal Processing Library

Voice Lab

vector_copy


void vector_copy(double *vector1, int vector_width, double **vector2)
{
int i;

if(vector_width > MAX_VECTOR_WIDTH) {
fprintf(stderr, "vector_width greater than MAX_VECTOR_WIDTH\n");
fprintf(stderr, "Please increase MAX_VECTOR_WIDTH in vector_lib.h and recompile.\n");
exit(-1);
}

for(i = 0; i < vector_width; i++){
(*vector2)[i] = vector1[i];
}
return;
}


Thursday, July 08, 2010

mel_cepstral

Digital Signal Processing Library

Voice Lab

mel_cepstral


void mel_cepstral(double vector_i[], double vector_o[], int order, double gain)
{
int i, k, m, lpc_index, index;
double alpha;
double previous_am[MAX_ALLOC_SIZE];
double am[MAX_ALLOC_SIZE];
double K, sum;

/*
** Information for this routine taken from:
** "Recursive Calculation of Mel-Cepstrum from LP Coefficients"
** By Keiichi Tokuda, Takao Kobayashi and Satoshi Imai
** April 1994
*/

alpha = 0.455;

K = gain;

if((order+1) >= MAX_ALLOC_SIZE) {
fprintf(stderr, "Please increase MAX_ALLOC_SIZE in window_lib.h to at least %d\n", order+2);
exit(1);
}

/* In equations 17, 18 and 19 in the article, we see the following variables:
** a() = the array of LP coefficients denoted here by "vector_i"
** a_tilde() = the current line of the matrix denoted here by "am"
** a_tilde(i-1)() = the previous line of the matrix denoted here by "previous_am"
** alpha = a constant denoted here by "alpha" and set to 0.455
** K =
** c_tilde() = output coefficient vector denoted here by "vector_o"
** M = number of LP Coefficients denoted here by "p";
*/
for(i = 0; i <= order; i++) {
/* start with a zero vector */
vector_o[i] = 0.0;
previous_am[i] = 0.0;
}
for(i = -order; i <= 0; i++) {
DEBUG_WRITE2(16, " loop i=%d\n", i);
/* added p to i into index so that index is always positive */
index = i + order;
if(index < 0) {
fprintf(stderr, "Error with index (too small)\n");
exit(1);
}
if(index > order) {
fprintf(stderr, "Error with index (too large)\n");
exit(1);
}
if(i < 0) lpc_index = -i;
if(i == 0) lpc_index = 0;
if(lpc_index < 0) {
fprintf(stderr, "Error with lpc_index (too small)\n");
exit(1);
}
if(lpc_index > order) {
fprintf(stderr, "Error with lpc_index (too large)\n");
exit(1);
}
if(index == 0) {
am[0] = vector_i[lpc_index];
for(m = 1; m <= order; m++) {
am[0] = 0;
}
} else if(index == 1) {
am[0] = vector_i[lpc_index] + alpha * previous_am[0];
am[1] = (1 - (alpha * alpha)) * previous_am[0];
for(m = 2; m <= order; m++) {
am[m] = alpha * (-am[m-1]);
}
} else {
am[0] = vector_i[lpc_index] + alpha * previous_am[0];
am[1] = (1 - (alpha * alpha)) * previous_am[0] + alpha * previous_am[1];
for(m = 2; m <= order; m++) {
am[m] = previous_am[m-1] + alpha * (previous_am[m] - am[m-1]);
}
}
for(k = 0; k <= order; k++ ) {
previous_am[k] = am[k];
}
}

vector_o[0] = log(K/ am[0]);
for(m = 1; m <= order; m++) {
sum = 0.0;
for(k = 1; k <= (m - 1); k++) {
sum += ((double) k / (double) m) * vector_o[k] * am[m-k];
}
vector_o[m] = -am[m] - sum;
}
} /* End of the mel cepstral function */


weighted_cepstral

Digital Signal Processing Library

Voice Lab

weighted_cepstral


void weighted_cepstral(double ccep[], double ccepp[], int order, int type)
{
int n;
double aux, L;

for(n = 0; n <= order; n++) {
ccepp[n]=0.0;
}

L = (double) order;

switch (type) {
case 1:
for(n = 0; n <= order; n++) {
ccepp[n] = ccep[n];
}
break;
case 2:
for(n = 0; n <= order; n++) {
ccepp[n] = ccep[n] * n;
}
break;
case 3:
for(n = 0; n <= order; n++) {
aux = PI*(double)(n+1)/L;
ccepp[n] = ccep[n] * (1.0+((L/2.0)*sin(aux)));
}
break;
default:
fprintf(stderr, "Weighted Cepstral type not within range(1-3)\n");
exit(1);
break;
}
} /* End of the weighted cepstral function */


delta_cepstral

Digital Signal Processing Library

Voice Lab

delta_cepstral


void delta_cepstral(double **matrix, double vector[], int order, int num_vectors, int k, double G)
{
int i, j, index, contj;

/* start with a zero vector */
for(i = 0; i <= order; i++) {
vector[i] = 0.0;
}

if(k == 0) k = 2;
if(G == 0) G = 0.375;

for(contj = k; contj < (num_vectors - k); contj++) {
for(i = 0; i <= order; i++) {
for(j = 0; j < ((2 * k) + 1); j++) {
index = contj - (j - k);
if(index < 0) {
fprintf(stderr, "contj: %d, j = %d, k = %d\n", contj, j, k);
fprintf(stderr, "Index out of range(dcepstra1): %d\n", index);
exit(1);
} else if(index >= num_vectors) {
fprintf(stderr, "contj: %d, j = %d, k = %d\n", contj, j, k);
fprintf(stderr, "Index out of range(dcepstra2): %d\n", index);
exit(1);
} else {
vector[i] += (j-k)*matrix[index][i]*G;
}
}
}
}
} /* End of the delta cepstra function */


Sunday, July 04, 2010

energy

Digital Signal Processing Library

Voice Lab

energy


double energy(double *vector, int vector_width, int option)
{
int i;
double d_energy;

/* start with zero energy */
d_energy = 0.0;

for(i = 0; i < vector_width; i++){
/* add the square of each element of the vector */
d_energy += (vector[i] * vector[i]);
}

if(option & 0x02) return(sqrt(d_energy));
if(option & 0x08) return(d_energy/vector_width);
return(d_energy);
}


cut_off

Digital Signal Processing Library

Voice Lab

cut_off


int cut_off(double offset, int sample_rate, int window_size, int overlap)
{
int aux;

aux = offset * sample_rate * overlap / window_size;
return(aux);
}



Thursday, July 01, 2010

cepstral

Digital Signal Processing Library

Voice Lab

cepstral


void cepstral(double lpc[], double cep[], int order)
{
int n, k;
double sumation, ratio;

if(order >= MAX_ALLOC_SIZE) {
fprintf(stderr, "Please increase MAX_ALLOC_SIZE in window_lib.h to at least %d\n", order+1);
exit(1);
}

/* Information taken from Douglas O'Shaughnessy's book */
/* "Speech Communication, Human and Machine" 1990, page 356 equation 8.39 */
/* lpc[0] = 1.0, coming from lpc routine */
/* computed values stored from lpc[1] thru lpc[order] */

/* copy the 1.0 at lpc[0] */
cep[0] = lpc[0];

/* equation 8.39 page 356 */
for(n = 1; n <= order; n++) {
sumation = 0.0;
for(k = 1; k < n; k++) {
ratio = (double)(k)/(double)(n);
sumation += ratio*cep[k]*lpc[n-k];
}
cep[n] = lpc[n] + sumation;
}
} /* End of the Cepstra function */



Saturday, June 26, 2010

cat_vector

Digital Signal Processing Library

Voice Lab

cat_vector


int cat_vector(double ***vector, double **vector_data_ptr,\
int vector_width, int num_vectors_o, int *number_vectors_max,\
double **vector_i, int num_vectors_i)
{
int i, j;

for(i = 0; i < num_vectors_i; i++) {
if(num_vectors_o == (*number_vectors_max)) {
/* increase maximum number of vectors permitted */
matrix_reallocate(&(*vector), &(*vector_data_ptr),\
vector_width, number_vectors_max);
}
vector_copy(vector_i[i], vector_width,\
&((*vector)[num_vectors_o]));
num_vectors_o++;
}
return(num_vectors_o);
}