mrs_realvec Delay::seconds2Samples (mrs_realvec seconds) { for (mrs_natural i = 0; i < seconds.getSize (); ++i) seconds(i) = seconds(i) * israte_; return seconds; }
mrs_realvec Delay::samples2Seconds (mrs_realvec samples) { for (mrs_natural i = 0; i < samples.getSize (); ++i) samples(i) = samples(i)/ israte_; return samples; }
void TimeFreqPeakConnectivity::InitMatrix (mrs_realvec &Matrix, unsigned char **traceback, mrs_natural rowIdx, mrs_natural colIdx) { mrs_natural i,j, numRows = Matrix.getRows (), numCols = Matrix.getCols (); mrs_natural iCol; Matrix.setval(0); traceback[rowIdx][colIdx] = kFromLeft; // left of point of interest for (i = 0; i < numRows; i++) { for (j = 0; j < colIdx; j++) { Matrix(i,j) = costInit; traceback[i][j] = kFromLeft; } } //cout << Matrix << endl; //upper left corner for (i = 0; i < rowIdx; i++) { iCol = MyMin (rowIdx - i + colIdx, numCols); for (j = colIdx; j < iCol; j++) { Matrix(i,j) = costInit; traceback[i][j] = kFromLeft; } } //cout << Matrix << endl; // lower left corner for (i = rowIdx + 1; i < numRows; i++) { iCol = MyMin (i - rowIdx + colIdx, numCols); for (j = colIdx; j < iCol; j++) { Matrix(i,j) = costInit; traceback[i][j] = kFromLeft; } } //cout << Matrix << endl; }
void PeakConvert2::ComputePeaker (mrs_realvec in, realvec& out) { #ifdef ORIGINAL_VERSION peaker_->updControl("mrs_real/peakStrength", 0.2);// to be set as a control [!] #else peaker_->updControl("mrs_real/peakStrength",1e-1); peaker_->updControl("mrs_real/peakStrengthRelMax" ,1e-2); peaker_->updControl("mrs_real/peakStrengthAbs",1e-10 ); peaker_->updControl("mrs_real/peakStrengthTreshLpParam" ,0.95); peaker_->updControl("mrs_real/peakStrengthRelThresh" , 1.); #endif peaker_->updControl("mrs_real/peakSpacing", 2e-3); // 0 peaker_->updControl("mrs_natural/peakStart", downFrequency_); // 0 peaker_->updControl("mrs_natural/peakEnd", upFrequency_); // size_ peaker_->updControl("mrs_natural/inSamples", in.getCols()); peaker_->updControl("mrs_natural/inObservations", in.getRows()); peaker_->updControl("mrs_natural/onSamples", out.getCols()); peaker_->updControl("mrs_natural/onObservations", out.getRows()); peaker_->process(in, out); }
void TimeFreqPeakConnectivity::CalcDp (mrs_realvec &Matrix, mrs_natural startr, mrs_natural startc, mrs_natural stopr, mrs_natural stopc) { mrs_natural i,j, numRows = Matrix.getRows (), numCols = Matrix.getCols (); mrs_real prevCost[kNumDirections] = {0,0,0}; costMatrix_.stretch ( numRows, numCols); // initialize cost and traceback matrix // upper and lower left corner InitMatrix (costMatrix_, tracebackIdx_, startr, startc); costMatrix_(startr, startc) = Matrix(startr, startc); // compute cost matrix for (j = startc+1; j <= stopc; j++) { mrs_natural rowLoopStart = MyMax (0, startr - (j - startc)), rowLoopStop = MyMin (numRows, startr + (j-startc) + 1); for (i = rowLoopStart; i < rowLoopStop; i++) { prevCost[kFromLeft] = costMatrix_(i,j-1); prevCost[kFromUp] = (i >= numRows-1) ? costInit : costMatrix_(i+1, j-1); // the if isn't very nice here.... prevCost[kFromDown] = (i <= 0) ? costInit : costMatrix_(i-1, j-1); // assign cost costMatrix_(i,j) = Matrix(i,j) + dtwFindMin (prevCost, tracebackIdx_[i][j]); } } // compute path i = stopr; for (j = stopc; j >= startc; j--) { path_[j-startc] = i; i -= (kFromLeft - tracebackIdx_[i][j]); // note: does work only for kFromUp, kFromLeft, kFromDown } }
static void FreqSmear (mrs_realvec &spectrum) { mrs_natural length = spectrum.getSize (); mrs_real buf[3] = {0,0,0}, coeff[3] = {.25, .5, .25}; spectrum(0) = spectrum(length-1) = 0; for (mrs_natural i = 0; i < length-1; i++) { buf[(i+1)%3] = spectrum(i+1); spectrum(i) = coeff[0]*buf[(i-1+3)%3] + coeff[1]*buf[(i)%3] + coeff[2]*buf[(i+1)%3]; } return; }
void Peaker::compLpThresh (const mrs_realvec input, mrs_realvec &output) { mrs_natural i, len = input.getCols (); mrs_real buf = input(0); for (i = 0; i < len; ++i) { buf = input(i) + lpCoeff_ * (buf - input(i)); output(i) = buf; } // do reverse filtering to ensure zero group delay for (i = len-1; i >= 0; i--) { buf = output(i) + lpCoeff_ * (buf - output(i)); output(i) = buf; } }
// used inside myProcess mrs_real HarmonicStrength::quadratic_interpolation(mrs_real best_bin, mrs_realvec& in, mrs_natural t) { if ((best_bin == 0) || (best_bin == in.getRows()-1)) { // don't try to interpolate using data that's // outside of the spectogram // TODO: find some convincing DSP thing to do in this case return in( (mrs_natural)best_bin, t); } // https://ccrma.stanford.edu/~jos/sasp/Quadratic_Interpolation_Spectral_Peaks.html // a = alpha, b = beta, g = gamma mrs_real a = in( (mrs_natural)best_bin - 1, t); mrs_real b = in( (mrs_natural)best_bin + 0, t); mrs_real g = in( (mrs_natural)best_bin + 1, t); mrs_real p = 0.5 * (a-g)/(a-2*b+g); // avoid some NaNs if ((p < -0.5) || (p > 0.5)) { return b; } mrs_real yp = b - 0.25*(a-g)*p; // avoid all NaNs if (yp < b) { // I think this happens because the search window doesn't // encompass the entire spectrum, so the "highest" bin // might not actually be the highest one, if it was on the // edge of search window. // TODO: find some convincing DSP thing to do in this case return b; } return yp; }
void PeakDistanceHorizontality::myProcess(realvec& in, realvec& out) { mrs_natural i; const mrs_natural numInputs = getctrl ("mrs_natural/numInputs")->to<mrs_natural>(); const mrs_realvec isHoriz = ctrl_horizvert_->to<mrs_realvec>(); const mrs_real range[2] = {ctrl_rangeX_->to<mrs_real>(), ctrl_rangeY_->to<mrs_real>()}; out = in; MRSASSERT(range[0] > 0 && range[1] > 0); if (isHoriz.getSize () != numInputs) { MRSWARN("PeakDistanceHorizontality: dimension mismatch"); MRSASSERT(false); out.setval(0); return; } if (getctrl("mrs_bool/bypass")->to<mrs_bool>()) { weights_.setval(1.); setctrl ("mrs_realvec/weights", weights_); return; } for (i = 0; i < inSamples_; i++) { for (mrs_natural j = i; j < inSamples_; j++) { mrs_natural k; mrs_real horizontality = ComputeHorizontality ( std::abs(in(1,i)-in(1,j))/range[0], std::abs(in(0,i)-in(0,j))/range[1]), norm = 0; for (k = 0; k < numInputs; k++) { mrs_real weight = horizontality; if (abs(isHoriz(k) - 2) < kIdentityThresh) weight = .5; // input is both horizontal and vertical else if (abs(isHoriz(k)) < kIdentityThresh) weight = 1.-weight; // input is vertical norm += weight; weights_(k*inSamples_ + i, j) = weight; weights_(k*inSamples_ + j, i) = weight; // symmetry } if (norm != 0) norm = 1./norm; for (k = 0; k < numInputs; k++) { weights_(k*inSamples_ + i, j) *= norm; if (i != j) weights_(k*inSamples_ + j, i) *= norm; // symmetry } } } setctrl ("mrs_realvec/weights", weights_); #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(weights_, "weights"); MATLAB_EVAL("figure(1);imagesc(weights),colorbar;"); #endif #endif }
void SimulMaskingFft::CalcSpreading (mrs_realvec &bandLevels, mrs_realvec &result) { // this is level dependent adapted from ITU-R BS.1387 mrs_natural iBarkj, // Masker iBarkk; // Maskee mrs_real fTmp1, fTmp2, fSlope, fScale = sqrt(8./3.), fBRes = barkRes_,//hertz2bark (.5*audiosrate_, h2bIdx)/numBands_, *pfEnPowTmp = processBuff_.getData (), *pfSlopeUp = helpBuff_.getData (); mrs_real *pfSlope = slopeSpread_.getData (), *pfNorm = normSpread_.getData (); // initialize pfResult result.setval(0); fSlope = exp ( -fBRes * 2.7 * 2.302585092994045684017991454684364207601101488628772976033); fTmp2 = 1.0 / (1.0 - fSlope); for (iBarkj = 0; iBarkj < numBands_; iBarkj++) { pfSlopeUp[iBarkj] = pfSlope[iBarkj] * pow (bandLevels(iBarkj)*fScale, .2 * fBRes); fTmp1 = (1.0 - IntPow (fSlope, iBarkj+1)) * fTmp2; fTmp2 = (1.0 - IntPow(pfSlopeUp[iBarkj], numBands_ - iBarkj)) / (1.0 - pfSlopeUp[iBarkj]); if (bandLevels(iBarkj) < 1e-20) { pfSlopeUp[iBarkj] = 0; pfEnPowTmp[iBarkj] = 0; continue; } pfSlopeUp[iBarkj] = exp (0.4 * log (pfSlopeUp[iBarkj])); pfEnPowTmp[iBarkj] = exp (0.4 * log (bandLevels(iBarkj)/(fTmp1 + fTmp2 -1))); } fSlope = exp ( 0.4 * log (fSlope)); // lower slope result(numBands_-1) = pfEnPowTmp[numBands_-1]; for (iBarkk = numBands_-2; iBarkk >= 0; iBarkk--) result(iBarkk) = pfEnPowTmp[iBarkk] + result(iBarkk + 1) * fSlope; // upper slope for (iBarkj = 0; iBarkj < numBands_-1; iBarkj++) { fSlope = pfSlopeUp[iBarkj]; fTmp1 = pfEnPowTmp[iBarkj]; for (iBarkk = iBarkj+1; iBarkk < numBands_; iBarkk++) { mrs_real dTmp1 = fTmp1 * fSlope; fTmp1 = (dTmp1 < 1e-30)? 0 : (mrs_real)dTmp1; result(iBarkk) += fTmp1; } } // normalization for (iBarkk = 0; iBarkk < numBands_; iBarkk++) { mrs_real dTmp = result(iBarkk); result(iBarkk) = sqrt(dTmp) * dTmp * dTmp *pfNorm[iBarkk]; //result(iBarkk) = sqrt (result(iBarkk)); //result(iBarkk) *= result(iBarkk)*result(iBarkk)*result(iBarkk)*result(iBarkk); //result(iBarkk) *= pfNorm[iBarkk]; } return; }