Showing posts with label parameter_lib. Show all posts
Showing posts with label parameter_lib. Show all posts

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


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 19, 2010

lpc

Digital Signal Processing Library

Voice Lab

lpc


void lpc(double *rr, double am[], int order, double *gain)
{
int i, m, n, k;
/* Local arrays need to be at least order+1 size */
double kk[MAX_ALLOC_SIZE]; /* k(m) on page 343 of book cited below */
double e[MAX_ALLOC_SIZE]; /* E(m) on page 343 of book cited below */
double previous_am[MAX_ALLOC_SIZE]; /* am-1(m) on page 343 of book cited below */
double aux;

/* Information taken from Douglas O'Shaughnessy's book */
/* "Speech Communication, Human and Machine" 1990, pages 341-344 */
/* A few things to keep in mind while accessing information in the various arrays: */
/* C is very flexible, but leaves the index limit check up to the programmer */
/* Limits must be set up and tested so as not to go out of bounds */

/* the input rr is defined for indexes varying from 0 to order */
/* output array am[] should have info stored from index 0 thru p, this is what will be saved in outfile */
/* am[0] = 1.0 */

/* Test if space allocated for arrays is large enough */
/* Local arrays need to be at least order+1 size */
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);
}

for(i = 0; i <= order; i++) {
/* Start with a zero kk vector */
kk[i] = 0.0;
/* first "previous_am vector" set to zero */
previous_am[i] = 0.0;
/* first coefficient of LPC set to zero */
am[i] = 0.0;
}

//aux = rr[0];

/* Calculation of the LPC coefficients */
if(rr[0] <= MINFLOAT) {
/* no signal. Nothing to do */
} else {
e[0]=rr[0];
am[0] = 1.0;
/* Interval is 1 to order+1, but indexes in C are from 0 to order */
/* Care must be taken to not go beyond end of arrays */
for(m = 1; m <= order; m++) {
/* equation 8.16.a on page 343 */
aux=0.0;
/* formula interval goes from k = 1 to k = m, */
/* therefore k will always be less than order+1 and less than m+1 */
for(k = 1; k < m; k++) {
aux += previous_am[k]*rr[m-k];
}
kk[m] = (rr[m]-aux)/e[m-1];

/* equation 8.16.b on page 343 */
/* Indexes of am are in the interval between 0 and order */
am[m] = kk[m];

/* equation 8.16.c on page 343 */
/* formula interval goes from k = 1 to k = m - 1, */
/* therefore k will always be less than order */
for(k = 1; k < m; k++) {
/* Indexes of am are in the interval between 1 and order+1 */
am[k] = previous_am[k] - kk[m]*previous_am[m-k];
}

/* equation 8.16.d on page 343 */
e[m] = (1.0-kk[m]*kk[m])*e[m-1];

/* save current vector as previous_am vector */
/* Indexes of am are in the interval between 1 and order+1 */
for(k = 1; k <= m; k++) {
previous_am[k] = am[k];
}
}
}
aux = rr[0];
for(m = 1; m <= order; m++) {
aux -= am[m]*rr[m]; /* Equation 8.10 page 341 */
}
*gain = sqrt(aux);
} /* end of the Levinson-Durbin LPC routine */


autocorrelation

Digital Signal Processing Library

Voice Lab

autocorrelation


void autocorrelation(double *d_window_i, double **ac, int window_size, int order, int options)
{
int i, n;
double max, tmp;

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 37 equation 2.56 */
/* Calculation of autocorrelation coefficients */

/* One interesting fact is that there needs to be one more value than the order of the filter */
/* Therefore, the array needs to be dimensioned for order+1 locations */

/* Another interesting fact is that when i = 0 we have the sum of the squares */

max = 0.0;
for(i = 0; i <= order; i++) {
(*ac)[i] = 0.0; /* Start with a value of zero */
for(n = 0; n < (window_size-i); n++) {
/* then sum the rest of the values */
(*ac)[i] += d_window_i[n]*d_window_i[n+i];
}
if((*ac)[i] >= 0) {
/* value is positive or zero */
if(max < (*ac)[i]) max = (*ac)[i];
} else {
/* value is negative -> change sign */
if(max < (-(*ac)[i])) max = (-(*ac)[i]);
}
}

tmp = (*ac)[0];
if(options & 0x02) for(i = 0; i <= order; i++) (*ac)[i] /= tmp;
if(options & 0x04) for(i = 0; i <= order; i++) (*ac)[i] /= max;
if(options & 0x08) for(i = 0; i <= order; i++) (*ac)[i] /= window_size;
return;
}


zero_crossings

Digital Signal Processing Library

Voice Lab

zero_crossings


void zero_crossings(double *d_window_i, int window_size, FILE *outfile)
{
int l, sgn[MAX_WINDOW_SIZE+1];
int count=0;

if(window_size >= MAX_WINDOW_SIZE) {
fprintf(stderr, "Please increase MAX_WINDOW_SIZE in\
window_lib.h to at least %d\n", window_size+1);
exit(1);
}
for(l = 0; l < window_size; l++) {
if(d_window_i[l] >= 0.0) {
sgn[l] = 1;
} else {
sgn[l] = -1;
}
}
for(l = 1; l < window_size; l++) {
count += abs(sgn[l] - sgn[l-1]);
}

fprintf(outfile,"%d\n", count/2);
}