int RSrefresh (AFILE *AFpI, long int offs, double x[], int Nx) { static long int lst = 0; static long int lnx = 0; int Nkeep, Nshift, Nout; /* Reset values */ if (Nx == 0) { lst = 0; lnx = 0; return 0; } /* Shift useful data that is already in the buffer */ if (offs < lnx && offs >= lst) { Nkeep = (int) MINV (lnx - offs, Nx); Nshift = (int) (offs - lst); VRdShift (x, Nkeep, Nshift); } else Nkeep = 0; /* Read more data */ Nout = AFdReadData (AFpI, offs + Nkeep, &x[Nkeep], Nx - Nkeep); /* Reset the pointers */ lst = offs; lnx = offs + Nx; return Nout; }
int STcatMax (const char Si[], char So[], int Maxchar) { char *so; int n, trunc; /* Save the initial output pointer */ so = So; /* Find the end of So */ for (n = 0; n < Maxchar && *so != '\0'; ++n) so++; trunc = (*so != '\0'); /* Copy to So */ for (; n < Maxchar && *Si != '\0'; ++n) *so++ = *Si++; /* Add a trailing null */ *so = '\0'; /* Check for truncation */ if (*Si != '\0' || trunc ) UTwarn ("STcatMax - %s: \"%.*s...\"", STM_StrTrunc, MINV (30, n), So); /* Return the number of characters in So */ return n; }
int AFfRdMulaw (AFILE *AFp, float Dbuff[], int Nreq) { int is, N, i, Nr; UT_uint1_t Buf[NBBUF/LW]; double g; for (is = 0; is < Nreq; ) { /* Read data from the audio file */ N = MINV (NBBUF / LW, Nreq - is); Nr = FREAD (Buf, LW, N, AFp->fp); /* Convert to float */ g = AFp->ScaleF; for (i = 0; i < Nr; ++i) { Dbuff[is] = (float) (g * Mutab[Buf[i]]); ++is; } if (Nr < N) break; } return is; }
int AFfRdI2 (AFILE *AFp, float Dbuff[], int Nreq) { int is, N, i, Nr; UT_int2_t Buf[NBBUF/LW2]; unsigned char *cp; unsigned char t; double g; for (is = 0; is < Nreq; ) { /* Read data from the audio file */ N = MINV (NBBUF / LW2, Nreq - is); Nr = FREAD (Buf, LW2, N, AFp->fp); /* Byte swap and convert to float */ g = AFp->ScaleF; for (i = 0; i < Nr; ++i) { if (AFp->Swapb == DS_SWAP) { cp = (unsigned char *) &Buf[i]; t = cp[1]; cp[1] = cp[0]; cp[0] = t; } Dbuff[is] = (float) (g * Buf[i]); ++is; } if (Nr < N) break; } return is; }
int AFfWrMulaw (AFILE *AFp, const float Dbuff[], int Nval) { int is, N, Nw, i; UT_uint1_t Buf[NBBUF/LW]; double Dv; /* Write data to the audio file */ is = 0; while (is < Nval) { N = MINV (NBBUF / LW, Nval - is); for (i = 0; i < N; ++i) { Dv = AFp->ScaleF * Dbuff[i+is]; if (Dv > AMAX || Dv < -AMAX) ++AFp->Novld; Buf[i] = Yq[SPdQuantL (Dv, Xq, NLEV)]; } Nw = FWRITE (Buf, LW, N, AFp->fp); is += Nw; if (Nw < N) break; } return is; }
int AFfWrI3 (AFILE *AFp, const float Dbuff[], int Nval) { int is, N, Nw, i, j, Hbo; UT_int4_t Iv; unsigned char Buf[NBBUF]; double g, Dv; unsigned char *cp; /* Write data to the audio file */ Hbo = UTbyteOrder (); cp = (unsigned char *) &Iv; is = 0; g = AFp->ScaleF; while (is < Nval) { N = MINV (NBBUF / LW3, Nval - is); for (i = 0, j = is; i < LW3*N; i += LW3, ++j) { Dv = g * Dbuff[j]; if (Dv >= 0.0) { Dv += 0.5; if (Dv >= UT_INT3_MAX + 1) { ++AFp->Novld; Dv = UT_INT3_MAX; } } else { Dv += -0.5; if (Dv <= UT_INT3_MIN - 1) { ++AFp->Novld; Dv = UT_INT3_MIN; } } if (Hbo == DS_EL) Iv = (UT_int4_t) Dv; /* DS_EL: X 2 1 0 */ else /* MSB LSB */ Iv = 256 * ((UT_int4_t) Dv); /* DS_EB: 0 1 2 X */ if (AFp->Swapb == DS_SWAP) { Buf[i] = cp[2]; Buf[i+1] = cp[1]; Buf[i+2] = cp[0]; } else { Buf[i] = cp[0]; Buf[i+1] = cp[1]; Buf[i+2] = cp[2]; } } Nw = FWRITE (Buf, LW3, N, AFp->fp); is += Nw; if (Nw < N) break; } return is; }
int AFreadHead (FILE *fp, void *Buf, int Size, int Nv, int Swapb) { double Lbuf[NBUF]; int Nvr, n, Nreq, status; if (Buf == NULL) { /* Skip data */ n = 0; if (FLseekable (fp)) { status = fseek (fp, Size * Nv, SEEK_CUR); if (status) { UTwarn ("AFreadHead: %s", AFM_FilePosErr); longjmp (AFR_JMPENV, 1); } n = Nv; } else { while (n < Nv) { Nreq = MINV (Nv - n, BSIZE (Lbuf) / Size); Nvr = FREAD (Lbuf, Size, Nreq, fp); n += Nvr; if (Nvr < Nreq) break; } } } else { /* Read the data in file byte order */ n = FREAD (Buf, Size, Nv, fp); /* Swap the data if necessary */ if (Size != 1 && UTswapCode (Swapb) == DS_SWAP) VRswapBytes (Buf, Buf, Size, n); } /* Error messages */ if (n < Nv) { if (ferror (fp)) { UTsysMsg ("AFreadHead - %s", AFM_ReadErr); longjmp (AFR_JMPENV, 2); } else { UTwarn ("AFreadHead - %s", AFM_UEoF); longjmp (AFR_JMPENV, 1); } } return (Size * n); }
int AFfRdI3 (AFILE *AFp, float Dbuff[], int Nreq) { int is, N, i, Nr, Hbo; UT_int4_t Iv; unsigned char Buf[NBBUF]; unsigned char *cp; unsigned char t; double g; Hbo = UTbyteOrder (); cp = (unsigned char *) &Iv; for (is = 0; is < Nreq; ) { /* Read data from the audio file */ N = MINV (NBBUF/LW3, Nreq - is); Nr = FREAD (Buf, LW3, N, AFp->fp); /* Byte swap and convert to float */ g = AFp->ScaleF; for (i = 0; i < LW3*Nr; i += LW3) { if (AFp->Swapb == DS_SWAP) { t = Buf[i+2]; Buf[i+2] = Buf[i]; Buf[i] = t; } if (Hbo == DS_EL) { cp[0] = 0; cp[1] = Buf[i]; cp[2] = Buf[i+1]; cp[3] = Buf[i+2]; /* Most significant byte */ } else { cp[0] = Buf[i]; /* Most significant byte */ cp[1] = Buf[i+1]; cp[2] = Buf[i+2]; cp[3] = 0; } Dbuff[is] = (float) (g * (Iv / 256)); ++is; } if (Nr < N) break; } return is; }
int AFfWrI4 (AFILE *AFp, const float Dbuff[], int Nval) { int is, N, Nw, i; UT_int4_t Buf[NBBUF/LW4]; double g, Dv; unsigned char *cp; unsigned char t; /* Write data to the audio file */ is = 0; g = AFp->ScaleF; while (is < Nval) { N = MINV (NBBUF / LW4, Nval - is); for (i = 0; i < N; ++i) { Dv = g * Dbuff[i+is]; if (Dv >= 0.0) { Dv += 0.5; if (Dv >= (double) UT_INT4_MAX + 1.) { ++AFp->Novld; Dv = UT_INT4_MAX; } } else { Dv += -0.5; if (Dv <= (double) (UT_INT4_MIN) - 1.) { ++AFp->Novld; Dv = UT_INT4_MIN; } } Buf[i] = (UT_int4_t) Dv; if (AFp->Swapb == DS_SWAP) { cp = (unsigned char *) &Buf[i]; t = cp[3]; cp[3] = cp[0]; cp[0] = t; t = cp[2]; cp[2] = cp[1]; cp[1] = t; } } Nw = FWRITE (Buf, LW4, N, AFp->fp); is += Nw; if (Nw < N) break; } return is; }
int main (int argc, const char *argv[]) { struct GN_FOpar FO; AFILE *AFp; FILE *fpinfo; int i, n, seed, Ftype, Dformat; long int k; double rms; float x[MAXBUF]; /* Get the input parameters */ GNoptions (argc, argv, &rms, &seed, &FO); /* If output is to stdout, use stderr for informational messages */ if (strcmp (FO.Fname, "-") == 0) fpinfo = stderr; else fpinfo = stdout; /* Open the output file */ Ftype = AOsetFtype (&FO); Dformat = AOsetDformat (&FO, NULL, 0); AOsetFOopt (&FO); if (strcmp (FO.Fname, "-") != 0) FLbackup (FO.Fname); AFp = AFopnWrite (FO.Fname, Ftype, Dformat, 1L, FO.Sfreq, fpinfo); /* Generate the noise samples */ MSrandSeed (seed); k = 0; while (k < FO.Nframe) { n = (int) MINV (FO.Nframe - k, MAXBUF); for (i = 0; i < n; ++i) x[i] = (float) MSfGaussRand (rms); k += n; AFfWriteData (AFp, x, n); } /* Close the audio file */ AFclose (AFp); return EXIT_SUCCESS; }
static int AF_rPstring (FILE *fp, char string[], int ncMax) { int offs, nc, ncP, nr; char slen[1]; offs = RHEAD_S (fp, slen); /* 1 byte length */ nc = (int) slen[0]; ncP = RNDUPV (nc + 1, 2); nr = MINV (nc, ncMax); offs += RHEAD_SN (fp, string, nr); string[nr] = '\0'; offs += RSKIP (fp, ncP - (nc + 1)); return offs; }
uintptr_t fa_resample_filter_init(int L, int M, float gain, win_t win_type) { fa_resample_filter_t *resflt = NULL; int lm_gcd; float ratio; resflt = (fa_resample_filter_t *)malloc(sizeof(fa_resample_filter_t)); ratio = ((float)L)/M; if ((ratio > FA_RATIO_MAX) || ((1./ratio) > FA_RATIO_MAX)) return -1; resflt->L = L; resflt->M = M; resflt->fc = MINV(1./L, 1./M); resflt->gain = gain; resflt->bytes_per_sample = 2; /*default we set to 2 bytes per sample */ resflt->out_index = 0; timevary_filter_init(&(resflt->tvflt), L, M, resflt->fc, L, win_type); lm_gcd = gcd(L, M); /*L*M/lm_gcd is the lowest multiplier of L&M*/ /*resflt->num_in = ((L*M)/lm_gcd)*RES_DEFAULT_NUM_IN;*/ resflt->num_in = ((L*M)/lm_gcd); while (resflt->num_in < FA_DEFAULT_FRAMELEN) resflt->num_in *= 2; resflt->num_out = (resflt->num_in*L)/M; resflt->bytes_in = resflt->bytes_per_sample * resflt->num_in; resflt->bytes_out = resflt->bytes_per_sample * resflt->num_out; resflt->buf_len = (resflt->tvflt.k + resflt->num_in ) * resflt->bytes_per_sample; resflt->buf = (unsigned char *)malloc(resflt->buf_len * sizeof(char)); memset(resflt->buf, 0, sizeof(char)*resflt->buf_len); return (uintptr_t)resflt; }
int Float2Short (short Buf[], const float Dbuff[], int Nval ) { int is, N, Nw, i; //short Buf[MAX_INPUT_CHANNELS * MAX_BUFFER_SIZE]; double g, Dv; unsigned char *cp; unsigned char t; is = 0; g = 32768.0; while (is < Nval) { N = MINV (DBUF, Nval - is); for (i = 0; i < N; ++i) { Dv = g * Dbuff[i+is]; if (Dv >= 0.0) { Dv += 0.5; if (Dv >= SHRT_MAX + 1) { //æ— æ•ˆå€¼; Dv = SHRT_MAX; } } else { Dv += -0.5; if (Dv <= SHRT_MIN - 1) { //æ— æ•ˆå€¼; Dv = SHRT_MIN; } } Buf[i] = (short) Dv; } Nw = N; //Nw = FWRITE (Buf, LW2, N, AFp->fp); is += Nw; if (Nw < N) break; } return is; }
int AFfWrI1 (AFILE *AFp, const float Dbuff[], int Nval) { int is, N, Nw, i; UT_int1_t Buf[NBBUF/LW1]; double g, Dv; /* Write data to the audio file */ is = 0; g = AFp->ScaleF; while (is < Nval) { N = MINV (NBBUF / LW1, Nval - is); for (i = 0; i < N; ++i) { Dv = g * Dbuff[i+is]; if (Dv >= 0.0) { Dv += 0.5; if (Dv >= UT_INT1_MAX + 1) { ++AFp->Novld; Dv = UT_INT1_MAX; } } else { Dv += -0.5; if (Dv <= UT_INT1_MIN - 1) { ++AFp->Novld; Dv = UT_INT1_MIN; } } Buf[i] = (UT_int1_t) Dv; } Nw = FWRITE (Buf, LW1, N, AFp->fp); is += Nw; if (Nw < N) break; } return is; }
static int AF_skipNVal (AFILE *AFp, long int N) { long int nl; int Lw, Nv, Nreq, ErrCode; double Buf[NBUF]; Lw = AF_DL[AFp->Format]; /* Read N values */ nl = 0L; while (nl < N) { Nreq = (int) MINV ((int) (sizeof Buf) / Lw, N - nl); Nv = FREAD (Buf, Lw, Nreq, AFp->fp); nl += Nv; if (Nv < Nreq) break; } AFp->Isamp += nl; ErrCode = 0; if (nl < N) { if (ferror (AFp->fp)) { UTsysMsg ("AFposition: %s %ld", AFM_ReadErrOffs, AFp->Isamp); ErrCode = AF_IOERR; } else if (AFp->Nsamp != AF_NSAMP_UNDEF) { UTwarn ("AFposition: %s %ld", AFM_UEoFOffs, AFp->Isamp); ErrCode = AF_UEOF; } else AFp->Nsamp = AFp->Isamp; } return ErrCode; }
char * STstrDots (const char Si[], int Maxchar) { int nc, i, N, M; static char Line[MAXM+1]; N = strlen (Si); M = MINV (MAXM, Maxchar); if (N <= M) { /* No truncation */ M = N; nc = N; } else if (M >= MINM) { nc = M - 3; for (i = M-4; i >= M-10; --i) { if (Si[i] == ' ') { nc = i + 1; break; } } } else if (M >= 6) nc = M-3; else nc = M; /* Copy characters */ ST_copyNtr (Si, Line, nc); if (nc + 3 <= M) STcopyMax ("...", &Line[nc], 3); return Line; }
int32_t svdcmp_c(int32_t m, double* a, double* w, double* v) { // C port of PLINK stats.cpp svdcmp(). // now thread-safe. double* rv1 = &(w[(uint32_t)m]); int32_t n = m; int32_t flag; int32_t l = 0; // suppress compile warning int32_t i,its,j,jj,k,nm; double anorm,c,f,g,h,s,scale,x,y,z; double temp; g=scale=anorm=0.0; for (i=0; i<n; i++) { l=i+2; rv1[i]=scale*g; g=s=scale=0.0; if (i < m) { for (k=i; k<m; k++) scale += fabs(a[k * m + i]); if (scale != 0.0) { for (k=i; k<m; k++) { a[k * m + i] /= scale; s += a[k * m + i]*a[k * m + i]; } f=a[i * m + i]; g = -SIGN(sqrt(s),f); h=f*g-s; a[i * m + i]=f-g; for (j=l-1; j<n; j++) { for (s=0.0,k=i; k<m; k++) s += a[k * m + i]*a[k * m + j]; f=s/h; for (k=i; k<m; k++) a[k * m + j] += f*a[k * m + i]; } for (k=i; k<m; k++) a[k * m + i] *= scale; } } w[i]=scale *g; g=s=scale=0.0; if (i+1 <= m && i+1 != n) { for (k=l-1; k<n; k++) scale += fabs(a[i * m + k]); if (scale != 0.0) { for (k=l-1; k<n; k++) { a[i * m + k] /= scale; s += a[i * m + k]*a[i * m + k]; } f=a[i * m + l-1]; g = -SIGN(sqrt(s),f); h=f*g-s; a[i * m + l-1]=f-g; for (k=l-1; k<n; k++) rv1[k]=a[i * m + k]/h; for (j=l-1; j<m; j++) { for (s=0.0,k=l-1; k<n; k++) s += a[j * m + k]*a[i * m + k]; for (k=l-1; k<n; k++) a[j * m + k] += s*rv1[k]; } for (k=l-1; k<n; k++) a[i * m + k] *= scale; } } anorm=MAXV(anorm,(fabs(w[i])+fabs(rv1[i]))); } for (i=n-1; i>=0; i--) { if (i < n-1) { if (g != 0.0) { for (j=l; j<n; j++) v[j * m + i]=(a[i * m + j]/a[i * m + l])/g; for (j=l; j<n; j++) { for (s=0.0,k=l; k<n; k++) s += a[i * m + k]*v[k * m + j]; for (k=l; k<n; k++) v[k * m + j] += s*v[k * m + i]; } } for (j=l; j<n; j++) v[i * m + j]=v[j * m + i]=0.0; } v[i * m + i]=1.0; g=rv1[i]; l=i; } for (i=MINV(m,n)-1; i>=0; i--) { l=i+1; g=w[i]; for (j=l; j<n; j++) a[i * m + j]=0.0; if (g != 0.0) { g=1.0/g; for (j=l; j<n; j++) { for (s=0.0,k=l; k<m; k++) s += a[k * m + i]*a[k * m + j]; f=(s/a[i * m + i])*g; for (k=i; k<m; k++) a[k * m + j] += f*a[k * m + i]; } for (j=i; j<m; j++) a[j * m + i] *= g; } else for (j=i; j<m; j++) a[j * m + i]=0.0; ++a[i * m + i]; } for (k=n-1; k>=0; k--) { for (its=0; its<30; its++) { flag=1; for (l=k; l>=0; l--) { nm=l-1; temp=fabs(rv1[l])+anorm; if (temp == anorm) { flag=0; break; } temp=fabs(w[nm])+anorm; if (temp == anorm) break; } if (flag) { c=0.0; s=1.0; for (i=l; i<k+1; i++) { f=s*rv1[i]; rv1[i]=c*rv1[i]; temp = fabs(f)+anorm; if (temp == anorm) break; g=w[i]; h=pythag(f,g); w[i]=h; h=1.0/h; c=g*h; s = -f*h; for (j=0; j<m; j++) { y=a[j * m + nm]; z=a[j * m + i]; a[j * m + nm]=y*c+z*s; a[j * m + i]=z*c-y*s; } } } z=w[k]; if (l == k) { if (z < 0.0) { w[k] = -z; for (j=0; j<n; j++) v[j * m + k] = -v[j * m + k]; } break; } if (its == 29) return 0; // cannot converge: multi-collinearity? x=w[l]; nm=k-1; y=w[nm]; g=rv1[nm]; h=rv1[k]; f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y); g=pythag(f,1.0); f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x; c=s=1.0; for (j=l; j<=nm; j++) { i=j+1; g=rv1[i]; y=w[i]; h=s*g; g=c*g; z=pythag(f,h); rv1[j]=z; c=f/z; s=h/z; f=x*c+g*s; g=g*c-x*s; h=y*s; y *= c; for (jj=0; jj<n; jj++) { x=v[jj * m + j]; z=v[jj * m + i]; v[jj * m + j]=x*c+z*s; v[jj * m + i]=z*c-x*s; } z=pythag(f,h); w[j]=z; if (z) { z=1.0/z; c=f*z; s=h*z; } f=c*g+s*y; x=c*y-s*g; for (jj=0; jj<m; jj++) { y=a[jj * m + j]; z=a[jj * m + i]; a[jj * m + j]=y*c+z*s; a[jj * m + i]=z*c-y*s; } } rv1[l]=0.0; rv1[k]=f; w[k]=x; } } return 1; }
static int lc_bus_consume_message_atomic(amqp_connection_state_t *conn, uint32_t flag, amqp_envelope_t *envelope, amqp_frame_t *frame, amqp_message_t *message, char *buf, int buf_len) { static struct timeval timeout = { tv_sec: RECV_TIMEOUT, tv_usec: 0 }; size_t body_remaining = 0, offset = 0; int res = LC_BUS_OK; amqp_basic_deliver_t *deliver = NULL; if (!envelope || !frame || !message) { return 0; } amqp_maybe_release_buffers(*conn); #if 0 amqp_consume_message(*conn, envelope, &timeout, 0); #endif res = amqp_simple_wait_frame_noblock(*conn, frame, &timeout); if (res) { LB_SYSLOG(LOG_ERR, "waiting for method frame, ret=%d.\n", res); if (res != AMQP_STATUS_TIMEOUT) { return -1; } return 0; } if (frame->frame_type != AMQP_FRAME_METHOD || frame->payload.method.id != AMQP_BASIC_DELIVER_METHOD) { LB_SYSLOG(LOG_WARNING, "got frame type 0x%X (expect AMQP_FRAME_METHOD " "0x%X) method 0x%X (expect AMQP_BASIC_DELIVER_METHOD 0x%X), " "ignore this message.\n", frame->frame_type, AMQP_FRAME_METHOD, frame->payload.method.id, AMQP_BASIC_DELIVER_METHOD); return 0; } LB_SYSLOG(LOG_INFO, "got frame type 0x%X method 0x%X.\n", frame->frame_type, frame->payload.method.id); deliver = (amqp_basic_deliver_t *)frame->payload.method.decoded; res = amqp_simple_wait_frame_noblock(*conn, frame, &timeout); if (res) { LB_SYSLOG(LOG_ERR, "waiting for header frame, ret=%d\n", res); if (res != AMQP_STATUS_TIMEOUT) { return -1; } return 0; } if (frame->frame_type != AMQP_FRAME_HEADER) { LB_SYSLOG(LOG_ERR, "got frame type 0x%X (expect " "AMQP_FRAME_HEADER 0x%X).\n", frame->frame_type, AMQP_FRAME_HEADER); return 0; } body_remaining = frame->payload.properties.body_size; LB_SYSLOG(LOG_INFO, "got frame type 0x%X (AMQP_FRAME_HEADER), " "AMQP_FRAME_BODY len %zd\n", frame->frame_type, body_remaining); while (body_remaining) { res = amqp_simple_wait_frame_noblock(*conn, frame, &timeout); if (res) { LB_SYSLOG(LOG_ERR, "waiting for body frame, ret=%d\n", res); if (res != AMQP_STATUS_TIMEOUT) { return -1; } return 0; } if (frame->frame_type != AMQP_FRAME_BODY) { LB_SYSLOG(LOG_ERR, "expected header, got frame type 0x%X\n", frame->frame_type); return 0; } LB_SYSLOG(LOG_DEBUG, "got body len %zd\n", frame->payload.body_fragment.len); memcpy(buf + offset, frame->payload.body_fragment.bytes, MINV(buf_len - offset, frame->payload.body_fragment.len)); if (buf_len - offset < frame->payload.body_fragment.len) { offset = buf_len; } else { offset += frame->payload.body_fragment.len; } body_remaining -= frame->payload.body_fragment.len; } if (flag & LC_BUS_CONSUME_WITH_ACK) { res = amqp_basic_ack( *conn, LC_BUS_CHANNEL, deliver->delivery_tag, 0 /* multiple */ ); if (res) { LB_SYSLOG(LOG_ERR, "basic ack, channel=%u ret=%d\n", LC_BUS_CHANNEL, res); return 0; } } if (buf_len == offset) { /* buffer is not enough */ return 0; } return offset; }
void PQadapt (const double *Ehs[2], const struct Par_Patt *Patt, double *EP[2], struct Mem_Adap *Adap) { int m, i, iL, iU, M1, M2; double sn, sd, s1, s2, CL; double R[2][PQ_MAXNC]; double **P = Adap->P; double **PC = Adap->PC; double *Rn = Adap->Rn; double *Rd = Adap->Rd; const int Nc = Patt->Nc; const double *a = Patt->a; const double *b = Patt->b; /* Smooth the excitation patterns */ /* Calculate the correlation terms */ sn = 0; sd = 0; for (m = 0; m < Nc; ++m) { P[0][m] = a[m] * P[0][m] + b[m] * Ehs[0][m]; P[1][m] = a[m] * P[1][m] + b[m] * Ehs[1][m]; sn += sqrt (P[1][m] * P[0][m]); sd += P[1][m]; } /* Level correlation */ CL = SQRV (sn / sd); for (m = 0; m < Nc; ++m) { /* Scale one of the signals to match levels */ if (CL > 1) { EP[0][m] = Ehs[0][m] / CL; EP[1][m] = Ehs[1][m]; } else { EP[0][m] = Ehs[0][m]; EP[1][m] = Ehs[1][m] * CL; } /* Calculate a pattern match correction factor */ Rn[m] = a[m] * Rn[m] + EP[1][m] * EP[0][m]; Rd[m] = a[m] * Rd[m] + EP[0][m] * EP[0][m]; assert (Rd[m] > 0 && Rn[m] > 0); if (Rn[m] >= Rd[m]) { R[0][m] = 1; R[1][m] = Rd[m] / Rn[m]; } else { R[0][m] = Rn[m] / Rd[m]; R[1][m] = 1; } } /* Average the correction factors over M channels and smooth with time */ M1 = Patt->M1; M2 = Patt->M2; for (m = 0; m < Nc; ++m) { iL = MAXV (m - M1, 0); iU = MINV (m + M2, Nc-1); s1 = 0; s2 = 0; for (i = iL; i <= iU; ++i) { s1 += R[0][i]; s2 += R[1][i]; } PC[0][m] = a[m] * PC[0][m] + b[m] * s1 / (iU-iL+1); PC[1][m] = a[m] * PC[1][m] + b[m] * s2 / (iU-iL+1); /* Final correction factor => spectrally adapted patterns */ EP[0][m] = EP[0][m] * PC[0][m]; EP[1][m] = EP[1][m] * PC[1][m]; } return; }
void FAfiltIIR (AFILE *AFpI, AFILE *AFpO, long int NsampO, const double h[][5], int Nsec, int Nsub, long int loffs) { double x[NBUF]; int mem, Nxmax, Nx; long int l, k, NyO; /* Notes: - The input signal d(.) is the data in the file, with d(0) corresponding to the first data value in the file. - Indexing: l is an offset into d(), referring to sample d(l). */ /* Batch processing - The data will be processed in batches by reading into a buffer x(.,.). The batches of input samples will be of equal size, Nx, except for the last batch. For batch j, x(j,l') = d(loffs+j*Nx+l'), for 0 <= l' < Nx, - The k'th output point y(k) is calculated at position d(loffs+k), that is the start of the impulse response, h(0), is aligned with d(loffs+k). y(k) --> h[0] <==> d(l), where l=loffs+k h[0] <==> x(j,l'). - For batch j=0, l = loffs - pointer to d(loffs), l' = 0 - pointer to x(0,0) = d(loffs), k = 0 - pointer to y(0). - For each batch, k and l' advance by Nx, k <- k + Nx, l' <- l' + Nx. - When the index l' for x(j,l') advances beyond Nx, we bring l' back into range by subtracting Nx from it and incrementing the batch number, */ /* Buffer allocation The buffer is allocated to filter memory (mem) and the input data (Nx). The output data will overlay the input data. */ mem = 2 * (Nsec + 1); Nxmax = NBUF - mem; if (Nxmax <= 0) UThalt ("%s: %s", PROGRAM, FAM_XIIRSect); NyO = (NsampO - 1) * Nsub + 1; /* Main processing loop */ /* if (l < loffs), processing warm-up points, no output */ VRdZero (x, mem); l = MINV (loffs, MAXV (0, loffs - MAXWUP)); k = 0; while (k < NyO) { /* Read the input data into the input buffer */ if (l < loffs) Nx = (int) MINV (Nxmax, loffs - l); else Nx = (int) MINV (Nxmax, NyO - k); AFdReadData (AFpI, l, &x[mem], Nx); /* Convolve the input samples with the filter response */ FIdFiltIIR (&x[mem-2], x, Nx, h, Nsec); /* Write the output data to the output audio file */ if (l >= loffs) { if (Nsub == 1) AFdWriteData (AFpO, &x[2], Nx); else FA_writeSubData (AFpO, k, Nsub, &x[2], Nx); k = k + Nx; } l = l + Nx; /* Update the filter memory */ VRdShift (x, mem, Nx); } return; }
static long int CP_comb1 (AFILE *AFpI, long int StartF, long int Nframe, const struct CP_Chgain *Chgain, long int MaxNframe, AFILE *AFpO) { int eof, i, k, m, NO, Nc, Nr, Ns, Nfv, Nfr; long int offr, offs, Nj, Nfrem; double g; double Dbuff[BFSIZE]; double *Dbuffi, *Dbuffo; NO = (int) AFpO->Nchan; Nj = AFpI->Nchan; assert (AFpO->Nchan == Chgain->NO); assert (Nj >= Chgain->NI); /* Split the buffer space up for the input and output buffers */ Nc = (int) MINV (MAXNI, Nj); Ns = BFSIZE / (Nc + NO); Dbuffi = Dbuff; Dbuffo = Dbuff + Nc * Ns; /* Main loop */ eof = (Nframe == AF_NFRAME_UNDEF); if (MaxNframe == AF_NFRAME_UNDEF) { if (eof) Nfrem = LONG_MAX; else Nfrem = Nframe; } else { if (eof) Nfrem = MaxNframe; else Nfrem = MINV (Nframe, MaxNframe); } offr = 0L; while (Nfrem > 0L) { Nfv = (int) MINV (Nfrem, Ns); for (k = 0; k < NO; ++k) { for (i = 0; i < Nfv; ++i) Dbuffo[i*NO+k] = Chgain->Offset[k]; /* dc offset */ } /* Read Nc channels (out of Nj) channels from the input file */ offs = Nj * (offr + StartF); if (Nc == Nj) { Nr = AFdReadData (AFpI, offs, Dbuffi, Nfv * Nc); Nfr = ICEILV (Nr, Nc); } else { for (i = 0; i < Nfv; ++i) { Nr = AFdReadData (AFpI, offs + i * Nj, &Dbuffi[i*Nc], Nc); if (Nr == 0 && eof) break; } Nfr = i; } if (Nfr < Nfv && eof) { Nfrem = Nfr; Nfv = Nfrem; } Nfrem -= Nfv; offr += Nfv; /* Add the contribution from the input buffer to the output buffer */ for (m = 0; m < Nc; ++m) { for (k = 0; k < NO; ++k) { g = Chgain->Gain[k][m]; if (g != 0.0) { for (i = 0; i < Nfv; ++i) Dbuffo[i*NO+k] += g * Dbuffi[i*Nc+m]; } } } /* Write the samples to the output file */ AFdWriteData (AFpO, Dbuffo, Nfv * NO); } return offr; }
static long int CP_combN (AFILE *AFp[], const long int StartF[], int Nifiles, long int Nframe, const struct CP_Chgain *Chgain, AFILE *AFpO) { int i, j, k, m, n, Nt, NI, NO, Nc, Ns, Nfr, NchanMax; long int offr, offs, Nj, Nrem; double g; double Dbuff[BFSIZE]; double *Dbuffi, *Dbuffo; assert (Nframe != AF_NFRAME_UNDEF); assert (AFpO->Nchan == Chgain->NO); /* Number of channels to be read (all files), maximum MAXNI */ NI = 0; for (j = 0; j < Nifiles; ++j) NI += (int) MINV (AFp[j]->Nchan, MAXNI - NI); assert (NI >= Chgain->NI); /* Maximum number of channels from any single file */ Nt = 0; NchanMax = 0; for (j = 0; j < Nifiles; ++j) { Nc = (int) MINV (AFp[j]->Nchan, NI - Nt); NchanMax = MAXV (NchanMax, Nc); Nt += Nc; } NO = (int) AFpO->Nchan; /* Split the buffer space up for the input and output buffers */ Ns = BFSIZE / (NchanMax + NO); Dbuffi = Dbuff; Dbuffo = Dbuff + NchanMax * Ns; /* The copying operation takes scaled samples from the input si(n,i) (channel n, sample i) and sums them to form so(k,i) (channel k, sample i) so(k,i) = SUM g(k,n) * si(n,i) n However, samples from different channels are interleaved. Thus so(k,i) is really so(i * No + k), where No is the number of output channels. For the input data, the channels appear in different files. The copying operation loops over the input channels n, the output channels k and the samples i. The inner loop is chosen to be over the samples, so that this loop can be skipped if the gain for an input/output channel combination is zero. The looping over input channels is actually two loops; one over files and the other for channels within a file. The loop over files is outermost, so that the data from the input files need only be read once. */ /* Main loop */ offr = 0L; Nrem = Nframe; while (Nrem > 0) { Nfr = (int) MINV (Nrem, Ns); for (k = 0; k < NO; ++k) { for (i = 0; i < Nfr; ++i) Dbuffo[i*NO+k] = Chgain->Offset[k]; /* dc offset */ } n = 0; Nt = 0; for (j = 0; j < Nifiles; ++j) { Nj = AFp[j]->Nchan; Nc = (int) MINV (NI - Nt, Nj); /* Read Nc channels (out of Nj) channels from file j */ offs = Nj * (offr + StartF[j]); if (Nc == Nj) AFdReadData (AFp[j], offs, Dbuffi, Nfr * Nc); else if (Nc > 0) { for (i = 0; i < Nfr; ++i) AFdReadData (AFp[j], offs + i * Nj, &Dbuffi[i*Nc], Nc); } Nt += Nc; /* Add the contribution from file j to the output */ for (m = 0; m < Nc; ++m) { for (k = 0; k < NO; ++k) { g = Chgain->Gain[k][n]; if (g != 0.0) { for (i = 0; i < Nfr; ++i) Dbuffo[i*NO+k] += g * Dbuffi[i*Nc+m]; } } ++n; } } /* Write the samples to the output file */ AFdWriteData (AFpO, Dbuffo, Nfr * NO); offr += Nfr; Nrem -= Nfr; } return Nframe; }
AFILE * AFrdEShead (FILE *fp) { AFILE *AFp; int NgI, Nv; long int offs; UT_float8_t STime; char Info[ES_MAXINFO]; struct ES_preamb Fpreamb; struct ES_fixhead FheadF; struct ES_FEAhead FheadV; char GenItems[ES_MAXGENERIC]; char str[20]; struct AF_read AFr; /* Set the long jump environment; on error return a NULL */ if (setjmp (AFR_JMPENV)) return NULL; /* Return from a header read error */ /* Defaults and inital values */ AFr = AFr_default; AFr.InfoX.Info = Info; AFr.InfoX.Nmax = ES_MAXINFO; /* Read selected preamble values */ /* We do not know the byte order until after we have read the file magic */ offs = RSKIP (fp, 8L); offs += RHEAD_V (fp, Fpreamb.Data_offset, DS_NATIVE); offs += RHEAD_V (fp, Fpreamb.Record_size, DS_NATIVE); offs += RHEAD_S (fp, Fpreamb.Magic); /* Check the preamble file magic */ if (SAME_CSTR (Fpreamb.Magic, FM_ESPS_BE)) AFr.DFormat.Swapb = DS_EB; else if (SAME_CSTR (Fpreamb.Magic, FM_ESPS_LE)) AFr.DFormat.Swapb = DS_EL; else { UTwarn ("AFrdEShead - %s", AFM_ES_BadId); return NULL; } /* Fix up the words we have already read */ if (UTswapCode (AFr.DFormat.Swapb) == DS_SWAP) { SWAPB (Fpreamb.Data_offset); SWAPB (Fpreamb.Record_size); } /* Read selected values from the fixed part of the header */ offs += RSKIP (fp, 32 - offs); offs += RHEAD_V (fp, FheadF.Type, AFr.DFormat.Swapb); offs += RSKIP (fp, 2); offs += RHEAD_S (fp, FheadF.Magic); offs += AFrdTextAFsp (fp, 26, "date: ", &AFr.InfoX, 1); offs += AFrdTextAFsp (fp, 8, "header_version: ", &AFr.InfoX, 1); offs += AFrdTextAFsp (fp, 16, "program_name: ", &AFr.InfoX, 1); offs += AFrdTextAFsp (fp, 8, "program_version: ", &AFr.InfoX, 1); offs += AFrdTextAFsp (fp, 26, "program_compile_date: ", &AFr.InfoX, 1); offs += RHEAD_V (fp, FheadF.Ndrec, AFr.DFormat.Swapb); offs += RSKIP (fp, 4); offs += RHEAD_V (fp, FheadF.Ndouble, AFr.DFormat.Swapb); offs += RHEAD_V (fp, FheadF.Nfloat, AFr.DFormat.Swapb); offs += RHEAD_V (fp, FheadF.Nlong, AFr.DFormat.Swapb); offs += RHEAD_V (fp, FheadF.Nshort, AFr.DFormat.Swapb); offs += RHEAD_V (fp, FheadF.Nchar, AFr.DFormat.Swapb); offs += RSKIP (fp, 8); offs += AFrdTextAFsp (fp, 8, "user: "******"AFrdEShead - %s: \"%d\"", AFM_ES_UnsType, (int) FheadF.Type); return NULL; } if (! SAME_CSTR (FheadF.Magic, Fpreamb.Magic)) { UTwarn ("AFrdEShead - %s", AFM_ES_IdMatch); return NULL; } if (FheadV.Fea_type != ES_FEA_SD) { UTwarn ("AFrdEShead - %s: \"%d\"", AFM_ES_UnsFea, (int) FheadV.Fea_type); return NULL; } /* Determine the data format */ if (FheadF.Nshort != 0) { AFr.NData.Nchan = FheadF.Nshort; AFr.DFormat.Format = FD_INT16; } else if (FheadF.Nlong != 0) { AFr.NData.Nchan = FheadF.Nlong; AFr.DFormat.Format = FD_INT32; } else if (FheadF.Nfloat != 0) { AFr.NData.Nchan = FheadF.Nfloat; AFr.DFormat.Format = FD_FLOAT32; } else if (FheadF.Ndouble != 0) { AFr.NData.Nchan = FheadF.Ndouble; AFr.DFormat.Format = FD_FLOAT64; } else { UTwarn ("AFrdEShead - %s", AFM_ES_UnsData); return NULL; } if (Fpreamb.Record_size != AF_DL[AFr.DFormat.Format] * AFr.NData.Nchan) { UTwarn ("AFrdEShead - %s", AFM_ES_UnsEncod); return NULL; } /* Get the sampling frequency */ if (! AF_getGeneric (GenItems, NgI, "record_freq", AFr.DFormat.Swapb, 1, ES_DOUBLE, &AFr.Sfreq)) { UTwarn ("AFrdEShead - %s", AFM_ES_NoSFreq); return NULL; } /* Other Generic Items */ if (AF_getGeneric (GenItems, NgI, "start_time", AFr.DFormat.Swapb, 1, ES_DOUBLE, &STime)) { Nv = sprintf (str, "%.7g", STime); AFaddAFspRec ("start_time: ", str, Nv, &AFr.InfoX); } /* Pick up "max_value" only if it is a single value */ if (AF_getGeneric (GenItems, NgI, "max_value", AFr.DFormat.Swapb, 1, ES_DOUBLE, &STime)) { Nv = sprintf (str, "%.7g", STime); AFaddAFspRec ("max_value: ", str, Nv, &AFr.InfoX); } /* Set the parameters for file access */ if (FheadF.Ndrec != 0) AFr.NData.Nsamp = FheadF.Ndrec * AFr.NData.Nchan; AFp = AFsetRead (fp, FT_ESPS, &AFr, AF_FIX_NSAMP_LOW | AF_FIX_NSAMP_HIGH); return AFp; }