void TestCML (void) { const char hello[] = "Hello world!"; const char* phello = hello; // const storage is sometimes copied on pointing cmemlink a, b; a.link (phello, VectorSize(hello)); if (a.begin() != phello) cout.format ("a.begin() failed: %p != %p\n", a.begin(), phello); a.link (VectorRange (hello)); if (*(const char*)(a.begin() + 5) != hello[5]) cout.format ("begin()[5] failed: %c != %c\n", *(const char*)(a.begin() + 5), VectorElement(hello,5)); if (a.size() != VectorSize(hello)) cout << "link to VectorRange doesn't work\n"; if (0 != memcmp (a.begin(), hello, VectorSize(hello))) cout << "memcmp failed on cmemlink\n"; b.static_link (hello); WriteCML (a); WriteCML (b); if (!(a == b)) cout << "operator== failed on cmemlink\n"; b.resize (VectorSize(hello) - 5); a = b; WriteCML (a); }
void TestCML (void) { const char hello[] = "Hello world!"; const char* phello = hello; // const storage is sometimes copied on pointing cmemlink a, b; a.link (phello, VectorSize(hello)); if (a.begin() != phello) { cout << "a.begin() failed: " << ios::hex << uintptr_t(a.begin()); cout << " != " << uintptr_t(phello) << ios::dec << endl; } a.link (VectorRange (hello)); if (*(const char*)(a.begin() + 5) != hello[5]) { cout << "begin()[5] failed: " << *(const char*)(a.begin() + 5); cout << " != " << hello[5] << endl; } if (a.size() != VectorSize(hello)) cout << "link to VectorRange doesn't work" << endl; if (0 != memcmp (a.begin(), hello, VectorSize(hello))) cout << "memcmp failed on cmemlink" << endl; b.static_link (hello); WriteCML (a); WriteCML (b); if (!(a == b)) cout << "operator== failed on cmemlink" << endl; b.resize (VectorSize(hello) - 5); a = b; WriteCML (a); }
/// Returns a descriptive error message. fmt="%s stream %s: @%u: expected %u, available %u"; void stream_bounds_exception::info (string& msgbuf, const char* fmt) const throw() { char typeName [256]; strncpy (typeName, m_TypeName, VectorSize(typeName)); typeName[VectorSize(typeName)-1] = 0; if (!fmt) fmt = "%s stream %s: @0x%X: need %u bytes, have %u"; try { msgbuf.format (fmt, demangle_type_name (VectorBlock(typeName)), m_Operation, m_Offset, m_Expected, m_Remaining); } catch (...) {} }
/// Initializes the empty object. \p operation is the function that returned the error code. file_exception::file_exception (const char* operation, const char* filename) throw() : libc_exception (operation) { memset (m_Filename, 0, VectorSize(m_Filename)); set_format (xfmt_FileException); if (filename) { strncpy (m_Filename, filename, VectorSize(m_Filename)); m_Filename [VectorSize(m_Filename) - 1] = 0; } }
/* TriDiag2Vector: Copy diagonal from m into v */ void TriDiag2Vector(TriMat m, Vector v) { int i,size; if (TriMatSize(m) != (size=VectorSize(v))) HError(2090,"TriDiag2Vector: Covariance sizes differ %d vs %d", TriMatSize(m),VectorSize(v)); for (i=1; i<=size; i++) v[i] = m[i][i]; }
/* EXPORT-> FVec2Spectrum: cvt feature vector f to a spectrum, fzero is the value of the 0'th feature vector coefficient which is typically omitted by HSigP routines eg a0 = 1.0 for LPC */ void FVec2Spectrum (float fzero, Vector f, Vector s) { int i,p,n; p=VectorSize(f); n=VectorSize(s); s[1] = fzero; for (i=1;i<=p;i++) s[i+1] = f[i]; for (i=p+2;i<=n;i++) s[i] = 0.0; Realft(s); }
void TestHeapOperations (void) { static const int c_Values [31] = { // 31 values make a full 4-layer tree 93, 92, 90, 86, 83, 86, 77, 40, 72, 36, 68, 82, 62, 67, 63, 15, 26, 26, 49, 21, 11, 62, 67, 27, 29, 30, 35, 23, 59, 35, 29 }; vector<int> v; v.reserve (VectorSize(c_Values)); for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) { v.push_back (c_Values[i]); push_heap (v.begin(), v.end()); cout << "------------------------------------------------\n"; if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; PrintHeap (v); } cout << "------------------------------------------------\n"; cout << "make_heap on the full range:\n"; v.resize (VectorSize (c_Values)); copy (VectorRange(c_Values), v.begin()); make_heap (v.begin(), v.end()); PrintHeap (v); if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; cout << "------------------------------------------------\n"; cout << "pop_heap:\n"; pop_heap (v.begin(), v.end()); v.pop_back(); PrintHeap (v); if (!is_heap (v.begin(), v.end())) cout << "Is NOT a heap\n"; cout << "------------------------------------------------\n"; cout << "sort_heap:\n"; v.resize (VectorSize (c_Values)); copy (VectorRange(c_Values), v.begin()); make_heap (v.begin(), v.end()); sort_heap (v.begin(), v.end()); foreach (vector<int>::const_iterator, i, v) cout << *i; cout << endl; cout << "------------------------------------------------\n"; cout << "priority_queue push and pop:\n"; priority_queue<int> q; for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) q.push (c_Values[i]); while (!q.empty()) { cout << q.top(); q.pop(); } cout << endl; }
/* EXPORT->ASpec2LPCep: Perform IDFT to get autocorrelation values then produce autoregressive coeffs. and cepstral transform them */ void ASpec2LPCep (Vector as, Vector ac, Vector lp, Vector c, DMatrix cm) { float lpcGain, E; /* Do IDFT to get autocorrelation values */ E = MatrixIDFT(as, ac, cm); lp[VectorSize(lp)] = 0.0; /* init to make Purify et al. happy */ /* do Durbin recursion to get predictor coefficients */ lpcGain = Durbin(NULL,lp,ac,E,VectorSize(ac)-1); if (lpcGain<=0) HError(-5323,"ASpec2LPCep: Negative lpcgain"); LPC2Cepstrum(lp,c); c[VectorSize(c)] = (float) -log((double) 1.0/lpcGain); /* value forms C0 */ }
/* EXPORT-> Realft: apply fft to real s */ void Realft (Vector s) { int n, n2, i, i1, i2, i3, i4; double xr1, xi1, xr2, xi2, wrs, wis; double yr, yi, yr2, yi2, yr0, theta, x; n=VectorSize(s) / 2; n2 = n/2; theta = PI / n; FFT(s, FALSE); x = sin(0.5 * theta); yr2 = -2.0 * x * x; yi2 = sin(theta); yr = 1.0 + yr2; yi = yi2; for (i=2; i<=n2; i++) { i1 = i + i - 1; i2 = i1 + 1; i3 = n + n + 3 - i2; i4 = i3 + 1; wrs = yr; wis = yi; xr1 = (s[i1] + s[i3])/2.0; xi1 = (s[i2] - s[i4])/2.0; xr2 = (s[i2] + s[i4])/2.0; xi2 = (s[i3] - s[i1])/2.0; s[i1] = xr1 + wrs * xr2 - wis * xi2; s[i2] = xi1 + wrs * xi2 + wis * xr2; s[i3] = xr1 - wrs * xr2 + wis * xi2; s[i4] = -xi1 + wrs * xi2 + wis * xr2; yr0 = yr; yr = yr * yr2 - yi * yi2 + yr; yi = yi * yr2 + yr0 * yi2 + yi; } xr1 = s[1]; s[1] = xr1 + s[2]; s[2] = 0.0; }
/* EXPORT->FBank2MelSpec: convert log fbank to linear */ void FBank2MelSpec(Vector fbank) { int i; for (i=1; i<=VectorSize(fbank); i++) fbank[i] = exp(fbank[i]); }
ff = Str2Format(buf); if (!InfoPrinted() && NumArgs() == 0 && ff != HAUDIO)
VecArray reduceVecArraybymask(VecArray a, const intVector mask) { VecArray b; int i, m, j, n, c=0; g_assert(a); g_assert(mask); m = VecArrayVectorSize(a); g_assert(m == VectorSize(mask)); n = VecArraySize(a); for(i = 0; i < m; i++) if(mask[i] > 0) c++; b = newpopulatedVecArray(n, c); c = 0; for(i = 0; i < m; i++) if(mask[i]) { for(j = 0; j < n; j++) b[j][c] = a[j][i]; c++; } deleteVecArrayandVectors(a); return b; }
void SymbolsCloseBlock() { int current = 0; int last = (int)(intptr_t)VectorPop(blocks); for (current = VectorSize(symbols) - 1; current >= last; current--) VectorPop(symbols); }
/* EXPORT->Wave2FBank: Perform filterbank analysis on speech s */ void Wave2FBank(Vector s, Vector fbank, float *te, FBankInfo info) { const float melfloor = 1.0; int k, bin; float t1,t2; /* real and imag parts */ float ek; /* energy of k'th fft channel */ /* Check that info record is compatible */ if (info.frameSize != VectorSize(s)) HError(5321,"Wave2FBank: frame size mismatch"); if (info.numChans != VectorSize(fbank)) HError(5321,"Wave2FBank: num channels mismatch"); /* Compute frame energy if needed */ if (te != NULL){ *te = 0.0; for (k=1; k<=info.frameSize; k++) *te += (s[k]*s[k]); } /* Apply FFT */ for (k=1; k<=info.frameSize; k++) info.x[k] = s[k]; /* copy to workspace */ for (k=info.frameSize+1; k<=info.fftN; k++) info.x[k] = 0.0; /* pad with zeroes */ Realft(info.x); /* take fft */ /* Fill filterbank channels */ ZeroVector(fbank); for (k = info.klo; k <= info.khi; k++) { /* fill bins */ t1 = info.x[2*k-1]; t2 = info.x[2*k]; if (info.usePower) ek = t1*t1 + t2*t2; else ek = sqrt(t1*t1 + t2*t2); bin = info.loChan[k]; t1 = info.loWt[k]*ek; if (bin>0) fbank[bin] += t1; if (bin<info.numChans) fbank[bin+1] += ek - t1; } /* Take logs */ if (info.takeLogs) for (bin=1; bin<=info.numChans; bin++) { t1 = fbank[bin]; if (t1<melfloor) t1 = melfloor; fbank[bin] = log(t1); } }
/// Reads the exception from stream \p is. void file_exception::read (istream& is) { libc_exception::read (is); string filename; is >> filename; is.align (8); filename.copyto (filename, VectorSize(m_Filename)); }
/* UpdateParameters: in hmm using counts in accumulators */ void UpdateParameters(void) { HMMScanState hss; int size; StreamInfo *sti; WtAcc *wa; MuAcc *ma = NULL; VaAcc *va; TrAcc *ta; Boolean hFound = FALSE,shared; NewHMMScan(&hset,&hss); do if (hmmLink == hss.hmm){ hFound = TRUE; while (GoNextState(&hss,TRUE)) { while (GoNextStream(&hss,TRUE)) { sti = hss.sti; if (hss.M>1 && (uFlags&UPMIXES)){ wa = (WtAcc *)sti->hook; if (hset.hsKind == DISCRETEHS) UpDProbs(hss.i,hss.s,hss.M,wa,sti->spdf.dpdf); else UpWeights(hss.i,hss.s,hss.M,wa,sti); } if (hss.isCont && (uFlags&(UPMEANS|UPVARS)))/*PLAINHS or SHAREDHS*/ while (GoNextMix(&hss,TRUE)) { size = VectorSize(hss.mp->mean); if (!IsSeenV(hss.mp->mean)) { ma = (MuAcc *)GetHook(hss.mp->mean); if (ma->occ!=0.0) UpMeans(hss.i,hss.s,hss.m,size,ma,hss.mp->mean); /* NB old mean left in ma->mu */ TouchV(hss.mp->mean); } if (!IsSeenV(hss.mp->cov.var)) { if (uFlags&UPVARS) { va = (VaAcc *)GetHook(hss.mp->cov.var); shared = (GetUse(hss.mp->cov.var) > 1) ? TRUE:FALSE; if (va->occ!=0.0) UpVars(hss.i,hss.s,hss.m,size,va,ma->mu,hss.mp->mean,shared,hss.mp); } TouchV(hss.mp->cov.var); } } } } if (!IsSeenV(hmmLink->transP)) { if (uFlags&UPTRANS){ ta = (TrAcc *)GetHook(hmmLink->transP); UpTrans(ta,hmmLink->transP); } TouchV(hmmLink->transP); } } while (!hFound && GoNextHMM(&hss)); EndHMMScan(&hss); if (!hFound) HError(2129,"UpdateParameters: hmm not found"); }
/* EXPORT->IsTriMat: True if matrix is lower triangular */ Boolean IsTriMat(Matrix m) { int i,n; n=NumRows(m); for(i=1;i<=n;i++) if (VectorSize(m[i])!=i) return(FALSE); return(TRUE); }
/* InvDiagGConst: compute gConst for given inv variance */ static float InvDiagGConst(Vector iv) { float sum; int i,n; n=VectorSize(iv); sum = n*log(TPI); for (i=1; i<=n; i++) sum -= log(iv[i]); return sum; }
/* EXPORT->Ham: Apply Hamming Window to Speech frame s */ void Ham (Vector s) { int i,frameSize; frameSize=VectorSize(s); if (hamWinSize != frameSize) GenHamWindow(frameSize); for (i=1;i<=frameSize;i++) s[i] *= hamWin[i]; }
/* EXPORT-> SpecModulus: store modulus of s in m */ void SpecModulus(Vector s, Vector m) { int i,j; float x,y; for (i=1;i<=VectorSize(s)/2;i++) { j=i+i; x=s[j-1]; y=s[j]; m[i]=sqrt(x*x + y*y); } }
/* EXPORT->PreEmphasise: pre-emphasise signal in s */ void PreEmphasise (Vector s, float k) { int i; float preE; preE = k; for (i=VectorSize(s);i>=2;i--) s[i] -= s[i-1]*preE; s[1] *= 1.0-preE; }
AstDeclaration* SymbolsFind(char* identifier, int line) { int current = VectorSize(symbols) - 1; for (; current >= 0; current--) { Symbol* symbol = (Symbol*)VectorGet(symbols, current); if (symbol->identifier == identifier) return symbol->declaration; } ErrorL(line, "symbol '%s' is not declared", identifier); return NULL; }
/* EXPORT->MelSpec2FBank: convert lin mel spectrum to log fbank */ void MelSpec2FBank(Vector melspec) { int i; float x; for (i=1; i<=VectorSize(melspec); i++){ x = melspec[i]; if (x<1.0) x = 1.0; melspec[i] = log(x); } }
/* EXPORT-> SpecLogModulus: store log modulus of s in m */ void SpecLogModulus(Vector s, Vector m, Boolean invert) { int i,j; float x,y; for (i=1;i<=VectorSize(s)/2;i++) { j=i+i; x=s[j-1]; y=s[j]; x=0.5*log(x*x + y*y); m[i] = invert ? -x : x; } }
/* GenHamWindow: generate precomputed Hamming window function */ static void GenHamWindow (int frameSize) { int i; float a; if (hamWin==NULL || VectorSize(hamWin) < frameSize) hamWin = CreateVector(&sigpHeap,frameSize); a = TPI / (frameSize - 1); for (i=1;i<=frameSize;i++) hamWin[i] = 0.54 - 0.46 * cos(a*(i-1)); hamWinSize = frameSize; }
/// Installs OnSignal as handler for signals. static void InstallCleanupHandlers (void) { static const uint8_t c_Signals[] = { SIGINT, SIGQUIT, SIGILL, SIGTRAP, SIGABRT, SIGIOT, SIGBUS, SIGFPE, SIGSEGV, SIGTERM, SIGIO, SIGCHLD }; for (uoff_t i = 0; i < VectorSize(c_Signals); ++i) signal (c_Signals[i], OnSignal); std::set_terminate (Terminate); std::set_unexpected (OnUnexpected); }
/* EXPORT->FBank2C0: return zero'th cepstral coefficient */ float FBank2C0(Vector fbank) { int k,numChan; float mfnorm,sum; numChan = VectorSize(fbank); mfnorm = sqrt(2.0/(float)numChan); sum = 0.0; for (k=1; k<=numChan; k++) sum += fbank[k]; return sum * mfnorm; }
/* EXPORT->Wave2LPC: Calculate LPCoef in a & RefC in k */ void Wave2LPC (Vector s, Vector a, Vector k, float *re, float *te) { Vector thisA; /* Current LP filter coefficients */ Vector r; /* AutoCorrelation Sequence */ float E; /* Prediction Error */ int p,frameSize; if (a==NULL && k==NULL) HError(5320,"Wave2LPC: Null a and k vectors in WaveToLPC"); if (a!=NULL) p=VectorSize(a); else p=VectorSize(k); r = CreateVector(&gstack,p); thisA = (a!=NULL)?a:CreateVector(&gstack,p); frameSize=VectorSize(s); E = AutoCorrelate(s,r,p,frameSize); *te = E; *re = Durbin(k,thisA,r,E,p); FreeVector(&gstack,r); }
/* CloneSVector: return a clone of given matrix */ SVector CloneSVector(MemHeap *hmem, SVector s, Boolean sharing) { SVector t; /* the target */ if (s==NULL) return NULL; if (GetUse(s)>0 && sharing) { IncUse(s); return s; } t = CreateSVector(hmem,VectorSize(s)); CopyVector(s,t); return t; }
/* Matrix IDFT converts from auditory spectrum into autocorrelation values */ float MatrixIDFT(Vector as, Vector ac, DMatrix cm) { double acc; float E; int nAuto, nFreq; int i, j; nFreq = VectorSize(as); nAuto = VectorSize(ac); E=0.0; for (i=0; i<nAuto; i++) { acc = cm[i+1][1] * (double)as[1]; for (j=1; j<nFreq; j++) acc += cm[i+1][j+1] * (double)as[j+1]; if (i>0) ac[i] = (float)(acc / (double)(2.0 * (nFreq-1))); else E = (float)(acc / (double)(2.0 * (nFreq-1))); } return E; /* Return zero'th auto value separately */ }