Array sa_of_string(String s) { // count number of commas in s int n = 0; // number of commas char *t = s; while (*t != '\0') { if (*t == ',') n++; t++; } n++; // n commas, n + 1 array elements String *a = xmalloc(n * sizeof(String)); t = s; int i = 0; char *start = s; while (*t != '\0') { while (*t != ',' && *t != '\0') t++; if (*t == '\0') break; // assert: *t is comma a[i++] = s_sub(start, 0, t - start); t++; // skip comma while (*t == ' ' || *t == '\t' || *t == '\n' || *t == '\r') t++; // skip whitespace start = t; } a[i++] = s_sub(start, 0, t - start); Array result = xmalloc(sizeof(ArrayHead)); result->n = n; result->s = sizeof(String); result->a = a; return result; }
void levinson_durbin( scalar R[], /* order+1 autocorrelation coeff */ scalar lpcs[], /* order+1 LPC's */ int order /* order of the LPC analysis */ ) { scalar a[order+1][order+1]; scalar sum, e, k; int i,j; /* loop variables */ scalar ZERO = fl_to_numb(0.0); e = R[0]; /* Equation 38a, Makhoul */ for(i=1; i<=order; i++) { sum = ZERO; for(j=1; j<=i-1; j++) sum = s_add(sum, s_mul(a[i-1][j],R[i-j])); k = s_mul(-1.0, s_div(s_add(R[i], sum),e)); /* Equation 38b, Makhoul */ if (s_abs(k) > fl_to_numb(1.0)) k = ZERO; a[i][i] = k; for(j=1; j<=i-1; j++) a[i][j] = s_add(a[i-1][j] , s_mul(k,a[i-1][i-j])); /* Equation 38c, Makhoul */ e = s_mul(e, s_sub(fl_to_numb(1.0),s_mul(k,k))); /* Equation 38d, Makhoul */ } for(i=1; i<=order; i++) lpcs[i] = a[order][i]; lpcs[0] = fl_to_numb(1.0); }
/* Return a truncated string of length n given a string of arbitrary length and n. */ String truncate_to_n(String input, int length) { if (s_length(input) <= length) { return input; } else { return s_sub(input, 0, length); } }
void pre_emp( scalar Sn_pre[], /* output frame of speech samples */ scalar Sn[], /* input frame of speech samples */ scalar *mem, /* Sn[-1]single sample memory */ int Nsam /* number of speech samples to use */ ) { int i; scalar ALPHA_ = fl_to_numb(ALPHA); for(i=0; i<Nsam; i++) { Sn_pre[i] = s_sub(Sn[i] , s_mul(ALPHA_ , mem[0])); mem[0] = Sn[i]; } }
void synthesis_filter( scalar res[], /* Nsam input residual (excitation) samples */ scalar a[], /* LPCs for this frame of speech samples */ int Nsam, /* number of speech samples */ int order, /* LPC order */ scalar Sn_[] /* Nsam output synthesised speech samples */ ) { int i,j; /* loop variables */ /* Filter Nsam samples */ for(i=0; i<Nsam; i++) { Sn_[i] = s_mul(res[i],a[0]); for(j=1; j<=order; j++) Sn_[i] = s_sub(Sn_[i], s_mul(Sn_[i-j],a[j])); } }
void hanning_window( scalar Sn[], /* input frame of speech samples */ scalar Wn[], /* output frame of windowed samples */ int Nsam /* number of samples */ ) { int i; /* loop variable */ scalar HALF = fl_to_numb(0.5); scalar PI2_ = fl_to_numb(2*PI); scalar Nsam_ = int_to_numb(Nsam-1); for(i=0; i<Nsam; i++){ scalar sc_cos = s_mul(HALF,s_cos(s_mul( PI2_, s_div(fl_to_numb((float)i),Nsam_)))); scalar sc_a = s_sub(HALF, sc_cos); Wn[i] = s_mul(Sn[i], sc_a); } }
void truncate_to_8_test() {// this is the testfunction check_expect_s(truncate_to_n("cumulus"), "cumulus"); //Exampel for String < n , with n = 14 check_expect_s(truncate_to_n("cumuluscumulus"), "cumuluscumulus"); //Exampel for String = n , with n = 14 check_expect_s(truncate_to_n("cumuluscumuluscumuluscumulus"), "cumuluscumulus"); //Exampel for String > n, with n = 14 // Compute the wage in cents given the number of hours worked. //This is the function: String truncate_to_n(string input) { if (s_length(input) <= n) {//when the string smaller n, then print the string return input; } else {//when the string bigger n, cut the string return s_sub(input, 0, n); } } int main(void) { truncate_to_8_test(); return 0; }
void *nlp_create( int m /* analysis window size */ ) { NLP *nlp; int i; assert(m <= PMAX_M); nlp = (NLP*)malloc(sizeof(NLP)); if (nlp == NULL) return NULL; nlp->m = m; scalar PI2_ = fl_to_numb(2*PI); scalar DEC_ = int_to_numb(m/DEC - 1); scalar HALF = fl_to_numb(.5); scalar ZERO = fl_to_numb(0.0); scalar a, b, c; for(i=0; i<m/DEC; i++) { a = s_div(s_mul(PI2_, int_to_numb(i) ) , DEC_); b = s_mul(HALF,s_cos(a)); nlp->w[i] = s_sub(HALF, b); //nlp->w[i] = 0.5 - 0.5*cosf(2*PI*i/(m/DEC-1)); } for(i=0; i<PMAX_M; i++) nlp->sq[i] = ZERO; nlp->mem_x = ZERO; nlp->mem_y = ZERO; for(i=0; i<NLP_NTAP; i++) nlp->mem_fir[i] = ZERO; nlp->fft_cfg = kiss_fft_alloc (PE_FFT_SIZE, 0, NULL, NULL); assert(nlp->fft_cfg != NULL); return (void*)nlp; }