static mrs_real Std (const realvec& beatHistogram) { return sqrt (beatHistogram.var ()); }
void SOM::myProcess(realvec& in, realvec& out) { mrs_string mode = getctrl("mrs_string/mode")->to<mrs_string>(); mrs_natural o,t; mrs_real geom_dist; mrs_real geom_dist_gauss; int px; int py; MarControlAccessor acc_grid(ctrl_gridmap_); realvec& grid_map = acc_grid.to<mrs_realvec>(); if (mode == "train") { mrs_real dx; mrs_real dy; mrs_real adj; for (t=0; t < inSamples_; t++) { px = (int) in(inObservations_-2, t); py = (int) in(inObservations_-1, t); if ((px == -1.0)&&(px == -1.0)) { find_grid_location(in, t); px = (int) grid_pos_(0); py = (int) grid_pos_(1); } out(0,t) = px; out(1,t) = py; out(2,t) = in(inObservations_-3,t); for (int x=0; x < grid_width_; x++) for (int y=0; y < grid_height_; y++) { dx = px-x; dy = py-y; geom_dist = sqrt((double)(dx*dx + dy*dy)); geom_dist_gauss = gaussian( geom_dist, 0.0, neigh_std_, false); // subtract map vector from training data vector adj = alpha_ * geom_dist_gauss; for (o=0; o < inObservations_-3; o++) { adjustments_(o) = in(o,t) - grid_map(x * grid_height_ + y, o); adjustments_(o) *= adj; grid_map(x * grid_height_ + y, o) += adjustments_(o); } } } alpha_ *= getctrl("mrs_real/alpha_decay_train")->to<mrs_real>(); neigh_std_ *= getctrl("mrs_real/neighbourhood_decay_train")->to<mrs_real>(); } if (mode == "init") { mrs_real dx; mrs_real dy; mrs_real adj; mrs_real std_ = getctrl("mrs_real/std_factor_init")->to<mrs_real>(); neigh_std_ = ((0.5*(grid_width_+grid_height_)) * std_); for (t=0; t < inSamples_; t++) { // no need to find grid locations, just read from the file px = (int) in( in.getRows() - 2, t); py = (int) in( in.getRows() - 1, t); for(int i =0; i < inObservations_ - 3; ++i) { grid_map(px * grid_height_ + py, i) = in(i); } for (int x=0; x < grid_width_; x++) for (int y=0; y < grid_height_; y++) { dx = px-x; dy = py-y; geom_dist = sqrt((double)(dx*dx + dy*dy)); geom_dist_gauss = gaussian( geom_dist, 0.0, neigh_std_, false); // subtract map vector from training data vector adj = alpha_ * geom_dist_gauss; for (o=0; o < inObservations_-3; o++) { adjustments_(o) = in(o,t) - grid_map(x * grid_height_ + y, o); adjustments_(o) *= adj; grid_map(x * grid_height_ + y, o) += adjustments_(o); } } } //WARNING: UGLY HACK // Last two rows of init features are x,y locations // so we want to set all the coresponding rows in the SOM to 0 // to prevent randomness from creaping in. for (int x=0; x < grid_width_; x++) { for (int y=0; y < grid_height_; y++) { grid_map(x * grid_height_ + y, grid_map.getCols() - 2) = 0; grid_map(x * grid_height_ + y, grid_map.getCols() - 1) = 0; cout << "x: " << x << " y: " << y << endl; } } alpha_ *= getctrl("mrs_real/alpha_decay_init")->to<mrs_real>(); neigh_std_ *= getctrl("mrs_real/neighbourhood_decay_init")->to<mrs_real>(); } if (mode == "predict") { for (t=0; t < inSamples_; t++) { find_grid_location(in, t); px = (int) grid_pos_(0); py = (int) grid_pos_(1); out(0,t) = px; out(1,t) = py; out(2,t) = in(inObservations_-3,t); } } }
void GMMClassifier::myProcess(realvec& in, realvec& out) { mrs_string mode = ctrl_mode_->to<mrs_string>(); // reset if ((prev_mode_ == "predict") && (mode == "train")) { //just drop all accumulated feature vectors and //copy take the new ones from the input trainMatrix_ = in; } if (mode == "train") { MRSASSERT(trainMatrix_.getRows() == inObservations_); //stretch to acommodate input feat Vecs mrs_natural storedFeatVecs = trainMatrix_.getCols(); trainMatrix_.stretch(inObservations_, storedFeatVecs + inSamples_); //append input data for(mrs_natural c=0; c < inSamples_; ++c) for(mrs_natural r = 0; r < inObservations_; ++r) trainMatrix_(r, c+storedFeatVecs) = in(r,c); } if (mode == "predict") { mrs_real maxProb = 0.0; mrs_natural maxClass = 0; mrs_real prob; mrs_real dist; realvec vec; realvec means; realvec covars; MRSASSERT(trainMatrix_.getRows() == inObservations_); for(mrs_natural t=0; t < inSamples_; ++t) { in.getCol(t, vec); for (mrs_natural cl=0; cl < classSize_; cl++) { for (mrs_natural k=0; k < nMixtures_; k++) { means_[cl].getCol(k, means); covars_[cl].getCol(k, covars); dist = NumericLib::mahalanobisDistance(vec, means, covars); likelihoods_(cl,k) = weights_[cl](k) / dist; } prob = 0.0; for (mrs_natural k=0; k < nMixtures_; k++) { prob += likelihoods_(cl,k); } if (prob > maxProb) { maxProb = prob; maxClass = cl; } } out(0,t) = in(labelRow_, t); out(1,t) = (mrs_real)maxClass; //FIXME: what about he maxProb (i.e. Confidence)? } } prev_mode_ = mode; }
void ADRessSpectrum::myProcess(realvec& in, realvec& out) { mrs_natural t; out.setval(0.0); //output spectrum of the "selected" source, given d and H mrs_natural H = (mrs_natural)(beta_* ctrl_H_->to<mrs_natural>()); if(H < 0) { H = 0; ctrl_H_->setValue(0.0); } if(H > beta_) { H = beta_; ctrl_H_->setValue(1.0); } mrs_natural H2 = H/2; mrs_natural d = (mrs_natural)(beta_*ctrl_d_->to<mrs_real>()); if(d < 0) { d = 0; ctrl_d_->setValue(0.0); } if(d > beta_) { d = beta_; ctrl_d_->setValue(1.0); } mrs_real mag = 0; mrs_real phase = 0; mrs_real azim = 0; for(mrs_natural k=0; k < N2_; ++k) { //get magnitude, phase and azimuth of bin k from input mag = 0.0; for(t=0; t <= beta_; ++t) { //search for non-zero values in azimuth plane azim = -1; if(in(k,t+1) > 0.0) { azim = t; mag = in(k,t+1); phase = in(k, 0); break; } if(in(k+N2_,t+1) > 0.0) { azim = beta_*2-t; mag = in(k+N2_,t+1); phase = in(k+N2_, 0); break; } } if(azim < 0) { //no sound at this bin, //so do not send anything to output continue; } //check if bin is inside specified range, //otherwise, send nothing to output if(abs(d-azim) <= H2) { //convert back to rectangular form re_ = mag*cos(phase); im_ = mag*sin(phase); //write bin to output if (k==0) { out(0,0) = re_; //DC } else if (k == N2_-1) { out(1, 0) = re_; //Nyquist } else { out(2*k, 0) = re_; //all other bins out(2*k+1, 0) = im_; } } } }
void PeakConvert::myProcess(realvec& in, realvec& out) { mrs_natural o; mrs_real a, c; mrs_real b, d; mrs_real phasediff; out.setval(0); peakView pkViewOut(out); for(mrs_natural f=0 ; f < inSamples_; ++f) { //we should avoid the first empty frames, //that will contain silence and consequently create //discontinuities in the signal, ruining the peak calculation! //only process if we have a full data vector (i.e. no zeros) if(frame_ >= skip_) { // handle amplitudes from shifted spectrums at input for (o=0; o < size_; o++) { if (o==0) //DC bins { a = in(0,f); b = 0.0; c = in(N_, f); d = 0.0; } else if (o == size_-1) //Nyquist bins { a = in(1, f); b = 0.0; c = in(N_+1, f); d = 0.0; } else //all other bins { a = in(2*o, f); b = in(2*o+1, f); c = in(N_+2*o, f); d = in(N_+2*o+1, f); } // computer magnitude value //mrs_real par = lobe_value_compute (0, 1, 2048); //[?] // compute phase phase_(o) = atan2(b,a); // compute precise frequency using the phase difference lastphase_(o)= atan2(d,c); if(phase_(o) >= lastphase_(o)) phasediff = phase_(o)-lastphase_(o); else phasediff = phase_(o)-lastphase_(o)+TWOPI; if(prec_) frequency_(o) = phasediff * factor_ ; else frequency_(o) = o*fundamental_; // compute precise amplitude mag_(o) = sqrt((a*a + b*b))*2; //*4/0.884624;//*50/3); // [!] mrs_real mag = lobe_value_compute((o * fundamental_-frequency_(o))/factor_, 1, N_); magCorr_(o) = mag_(o)/mag; // computing precise frequency using the derivative method // use at your own risk [?] /* mrs_real lastmag = sqrt(c*c + d*d); mrs_real rap = (mag_(o)-lastmag)/(lastmag*2); f=asin(rap); f *= (getctrl("mrs_real/israte")->to<mrs_real>()*inObservations/2.0)/PI; */ // rough frequency and amplitude //frequency_(o) = o * fundamental_; //magCorr_(o) = mag_(o); if(lastfrequency_(o) != 0.0) deltafrequency_(o) = (frequency_(o)-lastfrequency_(o))/(frequency_(o)+lastfrequency_(o)); deltamag_(o) = (mag_(o)-lastmag_(o))/(mag_(o)+lastmag_(o)); // remove potential peak if frequency too irrelevant if(abs(frequency_(o)-o*fundamental_) > 0.5*fundamental_) frequency_(o)=0.0; lastfrequency_(o) = frequency_(o); lastmag_(o) = mag_(o); } // select bins with local maxima in magnitude (--> peaks) realvec peaks_ = mag_; realvec tmp_; peaker_->updControl("mrs_real/peakStrength", 0.2);// to be set as a control [!] peaker_->updControl("mrs_natural/peakStart", downFrequency_); // 0 peaker_->updControl("mrs_natural/peakEnd", upFrequency_); // size_ peaker_->updControl("mrs_natural/inSamples", mag_.getCols()); peaker_->updControl("mrs_natural/inObservations", mag_.getRows()); peaker_->updControl("mrs_natural/onSamples", peaks_.getCols()); peaker_->updControl("mrs_natural/onObservations", peaks_.getRows()); if(pick_) peaker_->process(mag_, peaks_); else { //peaks_ = mag_; for (o = 0 ; o < downFrequency_ ; o++) peaks_(o)=0.0; for (o = upFrequency_ ; o < (mrs_natural)peaks_.getSize() ; ++o) peaks_(o)=0.0; } //discard bins whose frequency was set as irrelevant... for(o=0 ; o < size_ ; o++) { if(frequency_(o) == 0) peaks_(o) = 0.0; } // keep only the frameMaxNumPeaks_ highest amplitude local maxima if(ctrl_frameMaxNumPeaks_->to<mrs_natural>() != 0) //?????????????????? is this check needed?!? See myUpdate { tmp_.stretch(frameMaxNumPeaks_*2); max_->updControl("mrs_natural/nMaximums", frameMaxNumPeaks_); } else //?????????????????? is this check needed?!? See myUpdate { tmp_.stretch((upFrequency_-downFrequency_)*2); max_->updControl("mrs_natural/nMaximums", upFrequency_-downFrequency_); } max_->setctrl("mrs_natural/inSamples", size_); max_->setctrl("mrs_natural/inObservations", 1); max_->update(); max_->process(peaks_, tmp_); nbPeaks_=tmp_.getSize()/2; realvec index_(nbPeaks_); //[!] make member to avoid reallocation at each tick! for (mrs_natural i=0 ; i<nbPeaks_ ; ++i) index_(i) = tmp_(2*i+1); realvec index2_ = index_; index2_.sort(); // search for bins interval realvec interval_(nbPeaks_*2); //[!] make member to avoid reallocation at each tick! interval_.setval(0); if(pick_) getShortBinInterval(interval_, index2_, mag_); // fill output with peaks data /* MATLAB_PUT(mag_, "peaks"); MATLAB_PUT(peaks_, "k"); MATLAB_PUT(tmp_, "tmp"); MATLAB_PUT(interval_, "int"); MATLAB_EVAL("figure(1);clf;hold on ;plot(peaks);stem(k);stem(tmp(2:2:end)+1, peaks(tmp(2:2:end)+1), 'r')"); MATLAB_EVAL("stem(int+1, peaks(int+1), 'k')"); */ interval_ /= N_*2; for (mrs_natural i = 0; i < nbPeaks_; ++i) { pkViewOut(i, peakView::pkFrequency, f) = frequency_((mrs_natural) index_(i)); pkViewOut(i, peakView::pkAmplitude, f) = magCorr_((mrs_natural) index_(i)); pkViewOut(i, peakView::pkPhase, f) = -phase_((mrs_natural) index_(i)); pkViewOut(i, peakView::pkDeltaFrequency, f) = deltafrequency_((mrs_natural) index_(i)); pkViewOut(i, peakView::pkDeltaAmplitude, f) = deltamag_((mrs_natural) index_(i)); pkViewOut(i, peakView::pkFrame, f) = frame_; pkViewOut(i, peakView::pkGroup, f) = 0.0; //This should be -1!!!! [TODO] pkViewOut(i, peakView::pkVolume, f) = 1.0; pkViewOut(i, peakView::pkBinLow, f) = interval_(2*i); pkViewOut(i, peakView::pkBin, f) = index_(i); pkViewOut(i, peakView::pkBinHigh, f) = interval_(2*i+1); pkViewOut(i, peakView::pkTrack, f) = -1.0; //null-track ID if(useStereoSpectrum_) pkViewOut(i, peakView::pkPan, f) = in((mrs_natural)index_(i)+2*N_, f); else pkViewOut(i, peakView::pkPan, f) = 0.0; } } else //if not yet reached "skip" number of frames... { for(mrs_natural i=0; i< frameMaxNumPeaks_; ++i) { //pkViewOut(i, peakView::pkFrequency, f) = 0; //pkViewOut(i, peakView::pkAmplitude, f) = 0; //pkViewOut(i, peakView::pkPhase, f) = 0; //pkViewOut(i, peakView::pkDeltaFrequency, f) = 0; //pkViewOut(i, peakView::pkDeltaAmplitude, f) = 0; pkViewOut(i, peakView::pkFrame, f) = frame_; //pkViewOut(i, peakView::pkGroup, f) = -1; //pkViewOut(i, peakView::pkVolume, f) = 0; //pkViewOut(i, peakView::pkPan, f) = 0; //pkViewOut(i, peakView::pkBinLow, f) = 0; //pkViewOut(i, peakView::pkBin, f) = 0; //pkViewOut(i, peakView::pkBinHigh, f) = 0; } } frame_++; } //count the total number of existing peaks (i.e. peak freq != 0) ctrl_totalNumPeaks_->setValue(pkViewOut.getTotalNumPeaks()); // MATLAB_PUT(out, "peaks"); // MATLAB_PUT(frameMaxNumPeaks_, "k"); // MATLAB_EVAL("figure(1);clf;plot(peaks(6*k+1:7*k));"); }
void BeatHistogram::myProcess(realvec& in, realvec& out) { if (reset_) { out.setval(0.0); reset_ = false; setctrl("mrs_bool/reset", false); } //out.setval(0.0); mrs_natural bin=0; mrs_real amp; mrs_real srate = getctrl("mrs_real/israte")->to<mrs_real>(); mrs_natural count = 1; mrs_natural prev_bin =endBin_-1; mrs_natural pprev_bin =endBin_-1; mrs_real sumamp = 0.0; mrs_real tempo_weight = 0.0; #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(in, "acr"); MATLAB_EVAL("figure(1);plot(acr),grid on"); #endif #endif for (mrs_natural o=0; o < inObservations_; o++) { for (mrs_natural t = 1; t < inSamples_; t++) { bin = (mrs_natural)(( (2*srate) * 60.0 * factor_ / (t+1)) + 0.5); amp = in(o,t); // optional tempo weight if (getctrl("mrs_bool/tempoWeighting")->to<mrs_bool>()) { tempo_weight = 5.0 * log10((t+1) * 400.0 / (srate * 60.0 * factor_)) * log10((t+1) * 400.0 / (srate * 60.0 * factor_)); tempo_weight = exp(0.5 * tempo_weight * tempo_weight); } else { tempo_weight = 1.0; } amp = tempo_weight * amp; if (amp < 0.0) amp = 0.0; if ((bin > 40)&&(bin < endBin_)) { if (prev_bin == bin) { sumamp += amp; count++; } else { sumamp += amp; out(o,prev_bin) += ((sumamp / count)); count = 1; sumamp = 0.0; } // linear interpolation of the "not-set" bins... if (pprev_bin-prev_bin > 1) { // prev is x0, pprev is x1 mrs_real x0 = prev_bin; mrs_real x1 = pprev_bin; mrs_real y0 = out(o, prev_bin); mrs_real y1 = out(o, pprev_bin); for (mrs_natural k = prev_bin+1; k < pprev_bin; k++) out (o,k) = y0 + (y1-y0)*(k-x0)/(x1-x0); } pprev_bin = prev_bin; prev_bin = bin; } } } #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(out, "bh"); MATLAB_EVAL("figure(2);plot(bh),grid on"); #endif #endif }
void AutoCorrelation::myProcess(realvec& in, realvec& out) { mrs_natural t,o; k_ = ctrl_magcompress_->to<mrs_real>(); // Copy to output to perform inplace fft and zeropad to double size scratch_.create(fftSize_); //scratch_ needs to be reset every time for (o=0; o < inObservations_; o++) { for (t=lowSamples_; t < (lowSamples_+numSamples_); t++) { scratch_(t-(lowSamples_)) = in(o,t); } //zeropad for(t=(lowSamples_+numSamples_); t < fftSize_; t++) scratch_(t) = 0.0; //get pointer to data (THIS BREAKS ENCAPSULATION! FIXME [!]) mrs_real *tmp = scratch_.getData(); //compute forward FFT (of size fftSize_) myfft_->rfft(tmp, fftSize_/2, FFT_FORWARD); //rfft() takes as second argument half of the desired FFT size (see fft.cpp) // Special case for zero and Nyquist/2, // which only have real part if (k_ == 2.0) { re_ = tmp[0]; tmp[0] = re_ * re_; re_ = tmp[1]; tmp[1] = re_ * re_; } else { re_ = tmp[0]; re_ = sqrt(re_ * re_); tmp[0] = pow(re_, k_); re_ = tmp[1]; re_ = sqrt(re_ * re_); tmp[1] = pow(re_, k_); } // Compress the magnitude spectrum and zero // the imaginary part. for (t=1; t < fftSize_/2; t++) { re_ = tmp[2*t]; im_ = tmp[2*t+1]; if (k_ == 2.0) am_ = re_ * re_ + im_ * im_; else { am_ = sqrt(re_ * re_ + im_ * im_); am_ = pow(am_, k_); } tmp[2*t] = am_; tmp[2*t+1] = 0; } // Take the inverse Fourier Transform (of size fftSize_) myfft_->rfft(tmp, fftSize_/2, FFT_INVERSE); // Copy result to output if(normalize_) { cout << "NORM Normalization happening" << endl; for (t=0; t < onSamples_; t++) { out(o,t) = scratch_(t)*norm_(t); } } else for (t=0; t < onSamples_; t++) { // out(o,t) = 0.1 * scratch_(t) + 0.99 * out(o,t); out(o,t) = 1.0 * scratch_(t) + 0.0 * out(o,t); // out(o,t) = 0.5 * scratch_(t) + 0.5 * out(o,t); // out(o,t) += scratch_(t); } } if (ctrl_makePositive_->to<mrs_bool>()) { out -= out.minval(); } if(octaveCost_) //is there a reference for this octaveCost computation [?] { for (o=0; o < inObservations_; o++) { mrs_real maxOut = 0; for (t=1 ; t<onSamples_/2 ; t++) if (out(o, t)> out(o, t+1) && out(o, t) > out(o, t-1) && out(o, t)>maxOut) maxOut = out(o, t) ; //cout << maxOut/out(o, 0)<< " " << 1+voicing_ << << endl; if(maxOut && maxOut/out(o, 0) > 1-voicing_) for (t=1; t < onSamples_; t++) out(o, t) += octaveMax_-octaveCost_*log(36.0*t); else out.setval(0); } } if (ctrl_setr0to1_->to<mrs_bool>()) { // out -= out.minval(); /* for (o=0; o < onObservations_; o++) for (t=0; t< onSamples_-1; t++) { out(o,t) = out(o,t) / (onSamples_ - 1 - t); if (t > onSamples_-1-100) out(o,t) = 0.0; } */ // mrs_real myNorm = out(0,0); // if (myNorm > 0) // out /= myNorm; } if (ctrl_setr0to0_->to<mrs_bool>()) { // for (o=0; o < onObservations_; o++) // out(o,0) = 0.0; for (o=0; o < onObservations_; o++) for (t=0; t < onSamples_; t++) { out(o,t) = out(o,t); } } /* MATLAB_PUT(in, "corr_in"); MATLAB_PUT(out, "corr"); MATLAB_EVAL("subplot(211)"); MATLAB_EVAL("plot(corr_in)"); MATLAB_EVAL("subplot(212)"); MATLAB_EVAL("plot(corr)"); */ }
mrs_real McAulayQuatieri::peakTrack(realvec& vec, mrs_natural frame, mrs_natural grpOne, mrs_natural grpTwo) { mrs_real dist; mrs_natural candidate; mrs_natural lastMatched = -1; mrs_natural matchedTracks = 0; mrs_real delta = ctrl_delta_->to<mrs_real>(); if(frame+1 >= vec.getCols()) { MRSERR("McAulayQuatieri::peakTrack - frame index is bigger than the input vector!"); return -1.0; } peakView tmpPeakView(vec); //get the trackID for any future track to be born (in STEP 3 - see below) mrs_natural nextTrack = tmpPeakView.getFrameNumPeaks(0, grpOne); //iterate over peaks in current frame for(mrs_natural n = 0; n < tmpPeakView.getFrameNumPeaks(frame, grpOne); ++n) { mrs_real lastdist = MAXREAL; candidate = -1; // STEP 1 // find a candidate match on the next frame for each peak (i.e. track) in current frame for(mrs_natural m = lastMatched + 1; m < tmpPeakView.getFrameNumPeaks(frame+1, grpTwo); ++m) { //set track parameter of all peaks of next frame to -1 so we know later //which ones were not matched (=> BIRTH of new tracks) tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) = -1.0; dist = abs(tmpPeakView(n, peakView::pkFrequency, frame, grpOne) - tmpPeakView(m, peakView::pkFrequency, frame+1, grpTwo)); if (dist < delta && dist < lastdist) { //found a candidate! lastdist = dist; candidate = m; } } // STEP 2 // must confirm candidate (if any) if(candidate >= 0) //check if a candidate was found { //confirm if this is not the last peak in current frame if(n < tmpPeakView.getFrameNumPeaks(frame, grpOne)-1) { //check the next remaining peak in current frame and see if it is a better match for the found candidate dist = abs(tmpPeakView(n+1, peakView::pkFrequency, frame, grpOne) - tmpPeakView(candidate, peakView::pkFrequency, frame+1, grpTwo)); if(dist < lastdist) { // it is a better match! Check two additional conditions: // 1. an unmatched lower freq candidate should exist // 2. it is inside the frequency interval specified by delta if(candidate - 1 > lastMatched) { if(abs(tmpPeakView(n, peakView::pkFrequency, frame, grpOne) - tmpPeakView(candidate-1, peakView::pkFrequency, frame+1, grpTwo)) < delta) { //found a peak to continue the track -> confirm candidate! tmpPeakView(candidate-1, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne); lastMatched = candidate-1; matchedTracks++; } } } else { //no better match than this one, so confirm candidate! tmpPeakView(candidate, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne); lastMatched = candidate; matchedTracks++; } } else { //if this was the last peak in current frame, so inherently it was the best match. //Candidate is therefore automatically confirmed and can be propagated. tmpPeakView(candidate, peakView::pkTrack, frame+1, grpTwo) = tmpPeakView(n, peakView::pkTrack, frame, grpOne); lastMatched = candidate; matchedTracks++; } } } //end of loop on peaks of current frame // STEP 3 // check for any unmatched peaks in the next frame and give BIRTH to new tracks! for(mrs_natural m = 0; m < tmpPeakView.getFrameNumPeaks(frame+1, grpTwo); ++m) { if(tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) == -1.0) tmpPeakView(m, peakView::pkTrack, frame+1, grpTwo) = nextTrack++; //BIRTH of new track } return matchedTracks; }
void Filter::myProcess(realvec& in, realvec& out) { //checkFlow(in,out); mrs_natural i,j,c; mrs_natural size = in.getCols(); mrs_natural stateSize = state_.getCols(); mrs_natural channels = in.getRows(); mrs_real gain = getctrl("mrs_real/fgain")->to<mrs_real>(); // State array holds the various delays for the difference equation // of the filter. Similar implementation as described in the manual // for MATLAB Signal Processing Toolbox. State corresponds to // the z, num_coefs to the b and denom_coefs to the a vector respectively // in_window is the input x(n) and out_window is the output y(n) //dcoeffs_/=10; // state_.setval(0); if (norder_ == dorder_) { for (c = 0; c < channels; ++c) { for (i = 0; i < size; ++i) { out(c,i) = ncoeffs_(0) * in(c,i) + state_(c,0); for (j = 0; j < stateSize - 1; j++) { state_(c,j) = ncoeffs_(j+1) * in(c,i) + state_(c,j+1) - dcoeffs_(j+1) * out(c,i); } state_(c,stateSize - 1) = ncoeffs_(order_-1) * in(c,i) - dcoeffs_(order_-1) * out(c,i); } } } else if (norder_ < dorder_) { for (c = 0; c < channels; ++c) { for (i = 0; i < size; ++i) { out(c,i) = ncoeffs_(0) * in(c,i) + state_(c,0); for (j = 0; j < norder_ - 1; j++) { state_(c,j) = ncoeffs_(j+1) * in(c,i) + state_(c,j+1) - dcoeffs_(j+1) * out(c,i); } for (j = norder_ - 1; j < stateSize - 1; j++) { state_(c,j) = state_(c,j+1) - dcoeffs_(j+1) * out(c,i); } state_(c,stateSize - 1) = -dcoeffs_(order_ - 1) * out(c,i); } } } else { for (c = 0; c < channels; ++c) { for (i = 0; i < size; ++i) { out(c,i) = ncoeffs_(0) * in(c,i) + state_(c,0); for (j = 0; j < dorder_ - 1; j++) { state_(c,j) = ncoeffs_(j+1) * in(c,i) + state_(c,j+1) - dcoeffs_(j+1) * out(c,i); } for (j = dorder_ - 1; j < stateSize - 1; j++) { state_(c,j) = ncoeffs_(j+1) * in(c,i) + state_(c,j+1); } state_(c,stateSize - 1) = ncoeffs_(order_-1) * in(c,i); } } } out *= gain; // MATLAB_PUT(in, "Filter_in"); // MATLAB_PUT(out, "Filter_out"); // MATLAB_PUT(ncoeffs_, "ncoeffs_"); // MATLAB_PUT(dcoeffs_, "dcoeffs_"); // MATLAB_EVAL("MAT_out = filter(ncoeffs_, dcoeffs_, Filter_in)"); // // MATLAB_EVAL("spec_in = abs(fft(Filter_in));"); // MATLAB_EVAL("spec_out = abs(fft(Filter_out));"); // MATLAB_EVAL("spec_mat = abs(fft(MAT_out));"); // // MATLAB_EVAL("subplot(2,1,1);plot(Filter_in);hold on; plot(Filter_out, 'r'); plot(MAT_out, 'g');hold off"); // MATLAB_EVAL("subplot(2,1,2);plot(spec_in(1:end/2));hold on; plot(spec_out(1:end/2),'r');plot(spec_mat(1:end/2),'g');hold off;"); // MATLAB_EVAL("h = abs(fft([1 -.97], length(Filter_in)));"); // MATLAB_EVAL("hold on; plot(h(1:end/2), 'k'); hold off"); // //MATLAB_GET("MAT_out", out) // }
void PeakerOnset::myProcess(realvec& in, realvec& out) { mrs_natural o,t; (void) o; ctrl_onsetDetected_->setValue(false); ctrl_confidence_->setValue(0.0); out.setval(0.0); mrs_natural w = ctrl_lookAheadSamples_->to<mrs_natural>(); if(w == 0) return; //point to check for an onset mrs_natural checkPoint = inSamples_-1-w; mrs_real checkPointValue = in(checkPoint); mrs_bool isOnset = true; //check first condition mrs_natural interval = mrs_natural(2.0/3.0*w); //for (t = inSamples_-1; t >= inSamples_-1-2*w ; t--) for(t=checkPoint-interval; t <= checkPoint+interval; t++) { if(checkPointValue < in(t)) { isOnset = false; MRSDIAG("PeakerOnset::myProcess() - Failed 1st condition!"); break; } } // //new check proposed by Fabien Gouyon // mrs_real ww = w/2; // mrs_real maxVal = MINREAL; // for(t = inSamples_-1-ww; t > checkPoint; t--) // { // if(in(t) > maxVal) // { // maxVal = in(t); // } // else // { // isOnset = false; // //cout << "failed 1st condition!" << endl; // break; // } // } // maxVal = MINREAL; // for(t = inSamples_-1-2*ww; t < checkPoint; t++) // { // if(in(t) > maxVal) // { // maxVal = in(t); // } // else // { // isOnset = false; // //cout << "failed 1st condition!" << endl; // break; // } // } /* Last version (by lgmartins) -> corrected (below) for not being strict to the window size defined by the precede ShiftInput //check second condition mrs_real m = 0.0; for(t=0; t < inSamples_; t++) m += in(t); m /= inSamples_; */ mrs_natural mul = 3; //multiplier proposed in Dixon2006 mrs_real m = 0.0; for(t=checkPoint-(mul*w); t < inSamples_; t++) m += in(t); m /= (w*4+1); //checkPoint value should be higher than the window mean and mean should //be a significant value (otherwise we most probably are in a silence segment, //and we do not want to detect onsets on segments!) if(checkPointValue <= (m * ctrl_threshold_->to<mrs_real>()) || m < 10e-20) { isOnset = false; MRSDIAG("PeakerOnset::myProcess() - Failed 2nd condition!"); } //third condition from Dixon2006 (DAFx paper) is not implemented //since it was found on that paper that its impact is minimal... if(isOnset) { ctrl_onsetDetected_->setValue(true); //ctrl_confidence_->setValue(1.0); //[!] must still find a way to output a confidence... ctrl_confidence_->setValue(checkPointValue/100.0); // ad-hoc value which should still have more meaning than a pure 1.0 vs. 0.0. out.setval(1.0); MRSDIAG("PeakerOnset::myProcess() - Onset Detected!"); } //used for toy_with_onsets.m (DO NOT DELETE! - COMMENT INSTEAD) //t_++; //if(t_ == 0) // MATLAB_PUT(in, "PeakerOnset_inWIN"); //MATLAB_PUT(in, "PeakerOnset_in"); //if(t_ <= 431) // MATLAB_EVAL("PK_TS2 = [PK_TS2 PeakerOnset_in]"); //MATLAB_EVAL("plot(PK_TS, 'r');"); //MATLAB_EVAL("plot(FluxTS); hold on; plot(PK_TS, 'r');"); //MATLAB_EVAL("plot(PeakerOnset_in,'r');hold on; plot(ShiftInput_out); hold off"); //MATLAB_PUT(out,"PeakerOnset_out"); //MATLAB_EVAL("onsetTS = [onsetTS, PeakerOnset_out];"); }
void McAulayQuatieri::myProcess(realvec& in, realvec& out) { mrs_natural t,o,c; t=0; o=0; c=0; realvec* outPtr; out(o,t) = in(o,t); // ?????? //if we want to use memory and we already have data from //past inputs (i.e. memory is not empty)... if(ctrl_useMemory_->to<mrs_bool>() && memory_.getSize() != 0) { //concatenate memory column vector with current input //so we can continue peak tracking from previous input tmp_.stretch(onObservations_, onSamples_+1); for(o = 0; o < onObservations_; ++o) tmp_(o, 0) = memory_(o); for(o = 0; o < onObservations_; ++o) for(c = 0; c < onSamples_; ++c) tmp_(o,c+1) = in(o,c); outPtr = &tmp_; //attempt matching of groups between the frame in memory //and the first frame from current input if(ctrl_useGroups_->to<mrs_bool>()) { peakView inPV(in); mrs_realvec inFirstFrame; in.getCol(0, inFirstFrame); peakView inFirstFramePV(inFirstFrame); peakView memPV(memory_); peakView tmpPV(tmp_); //mrs_natural numInGroups = inPV.getNumGroups(); mrs_natural numInFirstFrameGroups = inFirstFramePV.getNumGroups(); mrs_natural numMemGroups = memPV.getNumGroups(); //we must update the group numbers of the groups //in tmp realvec (i.e. [mem|current input]) //so they do not clash with the previous ones if(nextGroup_ > 0) for(mrs_natural f=1; f < tmpPV.getNumFrames(); ++f) for(mrs_natural p = 0; p < tmpPV.getFrameNumPeaks(f); ++p) tmpPV(p, peakView::pkGroup, f) = tmpPV(p, peakView::pkGroup, f) + nextGroup_; // Try matching previous groups (in memory from last input) // with groups in current input // create a tmp copy of the frame in memory and the first frame // of current input, so we can do the group matching without // destroying the input values realvec frames2Match(inObservations_, 2); // calculate the matching score for all pairs of groups under matching realvec matchScores(numInFirstFrameGroups, numMemGroups); for(mrs_natural mg=0; mg < numMemGroups; ++mg) { for(mrs_natural ig = nextGroup_; ig < nextGroup_ + numInFirstFrameGroups; ++ig) { //since peakTrack(...) is destructible, we must reset frames2Match everytime... [!] for(o=0; o<inObservations_; ++o) for(c=0; c < 2; ++c) frames2Match(o, c) = tmp_(o, c); //use McAulay-Quatieri num of successful peak continuations as a score //for the group matching (may be replaced by some other metric in future) matchScores(ig-nextGroup_, mg) = peakTrack(frames2Match, 0, ig, mg); } } //Given the matchScores, try to find the optimal assignment //of the groups coming from previous input (stored in memory) //and the new groups in the current input //(using, for e.g. the hungarian method) realvec assignedGrp(numInFirstFrameGroups); //convert matchScores to costs mrs_real maxScore = matchScores.maxval(); for(o=0; o < matchScores.getRows(); ++o) for(c=0; c < matchScores.getCols(); ++ c) matchScores(o,c) = maxScore - matchScores(o,c); NumericLib::hungarianAssignment(matchScores, assignedGrp); //!!!!!!!!!!!!!!! [TODO][!] // given the assignments, try to propagate the group IDs // to the groups in the current input mrs_natural ig; for(mrs_natural f=1; f < tmpPV.getNumFrames(); ++f) { for(mrs_natural p = 0; p < tmpPV.getFrameNumPeaks(f); ++p) { //get input group ID (converted to the range [0:...]) ig = (mrs_natural)(tmpPV(p, peakView::pkGroup, f)) - nextGroup_; if(assignedGrp(ig) > -1) //a match was found for this group (ig) { //check if match is higher than the specified threshold if((maxScore - matchScores(ig, (mrs_natural)assignedGrp(ig))) / memPV.getFrameNumPeaks(0,(mrs_natural)assignedGrp(ig)) > ctrl_matchThres_->to<mrs_real>()) { //match confirmed --> propagate group ID tmpPV(p, peakView::pkGroup, f) = assignedGrp(ig); } else //match below threshold --> set as new group { tmpPV(p, peakView::pkGroup, f) = nextGroup_; assignedGrp(ig) = nextGroup_; nextGroup_++; } } else //no match found for this group! --> set as new group { tmpPV(p, peakView::pkGroup, f) = nextGroup_; assignedGrp(ig) = nextGroup_; nextGroup_++; } } } } } else { //no need to concatenate memory information with //current input. Just do it inplace in the output realvec (avoid extra copy)! outPtr = &out; } peakView tmpPeakView(*outPtr); ///////////////////////////////////////////////////////////////////////////// mrs_natural numGroups; mrs_natural g; if(ctrl_useGroups_->to<mrs_bool>()) { numGroups = tmpPeakView.getNumGroups(); g = 0; } else { numGroups = 0; g = -1; } //iterate over groups (if any or enabled) for(; g < numGroups; ++g) { //if no memory being used (or no memory stored yet), we must use peaks //in first frame to give birth to new tracks if(!ctrl_useMemory_->to<mrs_bool>() || memory_.getSize() == 0) { for(mrs_natural n = 0; n < tmpPeakView.getFrameNumPeaks(0, g); ++n) tmpPeakView(n, peakView::pkTrack, 0) = (mrs_real) n; } //iterate over input frames for(mrs_natural f=0; f < tmpPeakView.getNumFrames()-1; ++f) peakTrack(*outPtr, f, g, g); } //if using memory... if(ctrl_useMemory_->to<mrs_bool>()) { if(memory_.getSize() != 0) { //if using a non-empty memory, we should now fill the trackID and GroupID parameters //computed above (and stored in the tmp realvec) into the actual output peakView outPeakView(out); for(mrs_natural f=0; f < outPeakView.getNumFrames(); ++f) for(mrs_natural p = 0; p < outPeakView.getFrameNumPeaks(f); ++p) { outPeakView(p, peakView::pkTrack, f) = tmpPeakView(p, peakView::pkTrack, f+1); outPeakView(p, peakView::pkGroup, f) = tmpPeakView(p, peakView::pkGroup, f+1); } } //store the last frame of current output for next time memory_.stretch(onObservations_, 1); for(o = 0; o < onObservations_; ++o) memory_(o, 0) = out(o, onSamples_-1); } }
void SelfSimilarityMatrix::myProcess(realvec& in, realvec& out) { if(this->getctrl("mrs_natural/mode")->to<mrs_natural>() == SelfSimilarityMatrix::outputDistanceMatrix) { //check if there are any elements to process at the input //(in some cases, they may not exist!) - otherwise, do nothing //(i.e. output will be zeroed out) if(inSamples_ > 0) { unsigned int child_count = marsystems_.size(); if(child_count == 1) { mrs_natural nfeats = in.getRows(); //normalize input features if necessary if(ctrl_normalize_->to<mrs_string>() == "MinMax") in.normObsMinMax(); // (x - min)/(max - min) else if(ctrl_normalize_->to<mrs_string>() == "MeanStd") in.normObs(); // (x - mean)/std //calculate the Covariance Matrix from the input, if defined if(ctrl_calcCovMatrix_->to<mrs_natural>() & SelfSimilarityMatrix::fixedStdDev) { //fill covMatrix diagonal with fixed value (remaining values are zero) MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); covMatrix.create(inObservations_, inObservations_); mrs_real var = ctrl_stdDev_->to<mrs_real>(); var *= var; for(mrs_natural i=0; i< inObservations_; ++i) { covMatrix(i,i) = var; } } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & SelfSimilarityMatrix::diagCovMatrix) { in.varObs(vars_); //FASTER -> only get the vars for each feature mrs_natural dim = vars_.getSize(); //fill covMatrix diagonal with var values (remaining values are zero) MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); covMatrix.create(dim, dim); for(mrs_natural i=0; i< dim; ++i) { covMatrix(i,i) = vars_(i); } } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & SelfSimilarityMatrix::fullCovMatrix) { MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); in.covariance(covMatrix); //SLOWER -> estimate the full cov matrix } else if(ctrl_calcCovMatrix_->to<mrs_natural>() == SelfSimilarityMatrix::noCovMatrix) { ctrl_covMatrix_->setValue(realvec()); } for(mrs_natural i=0; i < in.getCols(); ++i) { in.getCol(i, i_featVec_); for(mrs_natural j=0; j <= i; ++j) { in.getCol(j, j_featVec_); //stack i and j feat vecs for(mrs_natural r=0; r < nfeats; ++r) { stackedFeatVecs_(r, 0) = i_featVec_(r); stackedFeatVecs_(r+nfeats, 0) = j_featVec_(r); } //do the metric calculation for these two feat vectors //and store it in the similarity matrix (which is symmetric) marsystems_[0]->process(stackedFeatVecs_, metricResult_); out(i,j) = metricResult_(0,0); //metric should be symmetric! out(j, i) = out(i, j); } } } else { out.setval(0.0); if(child_count == 0) { MRSWARN("SelfSimilarityMatrix::myProcess - no Child Metric MarSystem added - outputting zero similarity matrix!"); } else { MRSWARN("SelfSimilarityMatrix::myProcess - more than one Child MarSystem exists (i.e. invalid metric) - outputting zero similarity matrix!"); } } } //MATLAB_PUT(out, "simMatrix"); //MATLAB_EVAL("figure(1);imagesc(simMatrix);"); //MATLAB_PUT(out, "simMat"); //MATLAB_EVAL(name_+"=["+name_+",simMat(:)'];"); } else if(this->getctrl("mrs_natural/mode")->to<mrs_natural>() == SelfSimilarityMatrix::outputPairDistance) { if(inSamples_ == 2) //we always need two column vector instances at input { unsigned int child_count = marsystems_.size(); if(child_count == 1) { MarControlAccessor acc(ctrl_instanceIndexes_); realvec& instIdxs = acc.to<mrs_realvec>(); mrs_natural i = mrs_natural(instIdxs(0)); mrs_natural j = mrs_natural(instIdxs(1)); //check for out of bound indexes (which could have been set //by someone outside changing the value of the ctrl_instanceIndexes control) mrs_natural nInstances = ctrl_nInstances_->to<mrs_natural>(); if(i >= nInstances || j >= nInstances) ctrl_done_->setValue(true); if(!ctrl_done_->isTrue()) { mrs_natural nfeats = in.getRows(); //COMPUTE DISTANCE between the two column vector at input in.getCol(0, i_featVec_); in.getCol(1, j_featVec_); //stack i and j feat vecs for(mrs_natural r=0; r < nfeats; ++r) { stackedFeatVecs_(r, 0) = i_featVec_(r); stackedFeatVecs_(r+nfeats, 0) = j_featVec_(r); } //do the metric calculation for these two feat vectors //and send it to the output marsystems_[0]->process(stackedFeatVecs_, out); //out(0) = metricResult_(0,0); } else { //Self Similarity has completed all pair-wise similarity computations //so, it will just send zero valued values and a warning out(0) = 0.0; MRSWARN("SelfSimilarityMatrix::myProcess - no more pairwise similarity computations to be performed - outputting zero similarity value!") } //Select indexes for next pair of instances for distance computation //Similarity matrix is tringular simetric, so we should just compute //half of it (including diagonal). These indexes are to be used by some //source MarSystem that has a control linked to ctrl_instanceIndexes (e.g. WekaSource) if (i < j) ++i; else { ++j; i = 0; } if (j >= nInstances) { ctrl_done_->setValue(true); j = -1; //used to signal that there are no more instance pairs to compute i = -1; //used to signal that there are no more instance pairs to compute } else ctrl_done_->setValue(false); //set indexes into the ctrl_instanceIndexes_ control instIdxs(0) = i; instIdxs(1) = j; } else {
void OneRClassifier::myProcess(realvec& in, realvec& out) { cout << "OneRClassifier::myProcess" << endl; cout << "in.getCols() = " << in.getCols() << endl; cout << "in.getRows() = " << in.getRows() << endl; //get the current mode, either train of predict mode bool trainMode = (getctrl("mrs_string/mode")->to<mrs_string>() == "train"); row_.stretch(in.getRows()); if (trainMode) { if(lastModePredict_ || instances_.getCols()<=0) { mrs_natural nAttributes = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); cout << "nAttributes = " << nAttributes << endl; instances_.Create(nAttributes); } lastModePredict_ = false; //get the incoming data and append it to the data table for (mrs_natural ii=0; ii< inSamples_; ++ii) { mrs_real label = in(inObservations_-1, ii); instances_.Append(in); out(0,ii) = label; out(1,ii) = label; }//for t }//if else {//predict mode cout << "OneRClassifier::predict" << endl; if(!lastModePredict_) { //get the number of class labels and build the classifier mrs_natural nAttributes = getctrl("mrs_natural/inObservations")->to<mrs_natural>(); cout << "BUILD nAttributes = " << nAttributes << endl; Build(nAttributes); }//if lastModePredict_ = true; cout << "After lastModePredict" << endl; //foreach row of predict data, extract the actual class, then call the //classifier predict method. Output the actual and predicted classes. for (mrs_natural ii=0; ii<inSamples_; ++ii) { //extract the actual class mrs_natural label = (mrs_natural)in(inObservations_-1, ii); //invoke the classifier predict method to predict the class in.getCol(ii,row_); mrs_natural prediction = Predict(row_); cout << "PREDICTION = " << prediction << endl; cout << "row_ " << row_ << endl; //and output actual/predicted classes out(0,ii) = (mrs_real)prediction; out(1,ii) = (mrs_real)label; }//for t }//if }//myProcess
void PeakInObservation::myProcess(realvec& inVec, realvec& outVec) { // (!!) Should be simplified outVec.setval(0.f); //int nmin = 0; mrs_real vmin = inVec(0); int nmax = 0; mrs_real vmax = inVec(0); int nthresh = 0; bool theValid = true; bool theMaxFlag = true; for (mrs_natural n = 1; n < inVec.getSize(); n++){ if (theMaxFlag) if (inVec(n) > vmax){ // Zone 1: [min hysteresis, max] vmax = inVec(n); nmax = n; nthresh = n; theValid = true; vmin = vmax; //nmin = nmax; }else{ if (inVec(n)<vmax/HystFactor_ && nmax!=0){ // Zone 3: [max hysteresis, min] if ((mrs_natural)n > nthresh + HystLength_){ // Maximum was WIDE ENOUGH if (theValid){ outVec(nmax) = vmax; theMaxFlag = false; }else{ //Search for new maximum vmax = inVec(n); nmax = n; nthresh = n; theValid = true; vmin = vmax; //nmin = nmax; } }else{ // Maximum was TOO SMALL if (inVec(n) < vmin){ vmin = inVec(n); //nmin = n; } } }else{ // Zone 2: [max, max hysteresis] if (nthresh != (mrs_natural)n-1){ theValid = false; if ((mrs_natural)n > nthresh + HystLength_){ // Search for new maximum vmax = inVec(n); nmax = n; nthresh = n; theValid = true; vmin = vmax; //nmin = nmax; } }else nthresh = n; } } else if (inVec(n) < vmin){ vmin = inVec(n); //nmin = n; }else if (inVec(n) > vmin*HystFactor_){ vmax = inVec(n); nmax = n; nthresh = 0; vmin = vmax; //nmin = nmax; theValid = true; theMaxFlag = true; } } }
void BeatHistoFeatures::beatHistoFeatures(realvec& in, realvec& out) { mrs_real sum = 0; for (mrs_natural o=0; o < inObservations_; o++) for (mrs_natural t = 0; t < inSamples_; t++) { sum += in(o,t); } mrs_real result[2]; mrs_natural i,startIdx = 200; // zero-out below 50BPM for (i=0; i < startIdx; i++) in(i) = 0; for (i = startIdx; i < in.getCols (); i++) if (in(i) < 0) in(i) = 0; pkr1_->process(in, pkres1_); mxr_->process(pkres1_,mxres_); vector<mrs_real> bpms; bpms.push_back(mxres_(0,1)); bpms.push_back(mxres_(0,3)); bpms.push_back(mxres_(0,5)); sort(bpms.begin(), bpms.end()); out(0,0) = sum; for (unsigned int i=0; i<bpms.size(); i++) for (unsigned int j =0; j < bpms.size(); j++) { if (bpms[i] == mxres_(0,2*j+1)) out(i+1,0) = mxres_(0,2*j); } out(4,0) = bpms[0] /4.0; out(5,0) = bpms[1] /4.0; out(6,0) = bpms[2] /4.0; out(7,0) = out(4,0) / out(5,0); NormInPlace (in); #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(in, "beathist"); MATLAB_EVAL("figure(1);plot((201:800)/4, beathist(201:800)),grid on"); #endif #endif MaxAcf (result[0], result[1],in, flag_, startIdx, 600); out(8,0) = result[0]; out(9,0) = result[1]; out(10,0) = MaxHps (in, startIdx); out(11,0) = SpectralFlatness (in, startIdx); out(12,0) = Std(in); out(13,0) = PeriodicCentroid(in, false, startIdx); out(14,0) = PeriodicCentroid(in, true, startIdx); out(15,0) = PeriodicSpread(in, out(13,0), false, startIdx); out(16,0) = PeriodicSpread(in, out(14,0), true, startIdx); out(17,0) = NumMax(in); }
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 TimeFreqPeakConnectivity::myProcess(realvec& in, realvec& out) { // get pitch resolution const mrs_real reso = ctrl_reso_->to<mrs_real>(); const mrs_bool isInBark = getctrl("mrs_bool/inBark")->to<mrs_bool>(); // a matrix indicating where peaks are in the time frequency plane MRSASSERT(textWinSize_ >= in(1,inSamples_-1)-in(1,0)); peakMatrix_.stretch(numBands_, textWinSize_); // init peakMatrix_.setval (1); for (t = 0; t < textWinSize_; t++) for (o=0; o < numBands_; o++) peakIndices_[o][t] = -1; // initialized pseudo spectrogram representation for (t = 0; t < inSamples_; t++) { mrs_natural row = (isInBark)? Freq2RowIdx (in(0,t),reso) : Freq2RowIdx (bark2hertz(in(0,t)),reso), // input is in bark frequency, so we might have to convert it back col = (mrs_natural)(in(1,t)-in(1,0)+.1); MRSASSERT(col >= 0 && col < textWinSize_); MRSASSERT(row >= 0 && row < numBands_); // check whether more than one peak are at that matrix pos, i.e. we already have set the entry if (peakIndices_[row][col] != -1) { multipleIndices->AddIndex (row,col,peakIndices_[row][col]); multipleIndices->AddIndex (row,col,t); peakIndices_[row][col] = -2; } else peakIndices_[row][col] = t; // a matrix indicating which matrix bin corresponds to which input index peakMatrix_(row, col) = 0; } // initialize output out.setval (costInit); #ifdef MATLAB_DBG_OUT #ifdef MARSYAS_MATLAB MATLAB_PUT(peakMatrix_, "peakMatrix"); MATLAB_EVAL ("figure(1),imagesc(peakMatrix),colorbar"); #endif #endif // iteration over all pairs for (t = 0; t < inSamples_; t++) { for (o=inSamples_-1; o >= t;o--) { // don't compute distance if we already have it if (out(t,o) != costInit) continue; // get peak matrix indices mrs_natural rowt = (isInBark)? Freq2RowIdx (in(0,t),reso) : Freq2RowIdx (bark2hertz(in(0,t)),reso), // input is in bark frequency, so we might have to convert it back rowo = (isInBark)? Freq2RowIdx (in(0,o),reso) : Freq2RowIdx (bark2hertz(in(0,o)),reso), // input is in bark frequency, so we might have to convert it back colt = (mrs_natural)(in(1,t)-in(1,0)+.1), colo = (mrs_natural)(in(1,o)-in(1,0)+.1), pathLength; MRSASSERT(colt >= 0 && colt < textWinSize_); MRSASSERT(colo >= 0 && colo < textWinSize_); MRSASSERT(rowt >= 0 && rowt < numBands_); MRSASSERT(rowo >= 0 && rowo < numBands_); // self similarity and similarity with overlapping peaks if ((t == o) || (rowt == rowo && colt == colo)) { SetOutput(out, 0, rowt, colt, rowo, colo); continue; } // check if path calculation makes sense with the current dp step size if (abs(rowt - rowo) > abs(colt-colo)) { SetOutput(out, 1, rowt, colt, rowo, colo); continue; } // let's calculate only from left to right if (colo < colt) continue; // dynamic programming CalcDp (peakMatrix_, rowt, colt, rowo, colo); pathLength = colo-colt+1; #ifdef MATLAB_DBG_OUT #ifdef MARSYAS_MATLAB MATLAB_PUT(costMatrix_, "cost"); MATLAB_EVAL ("figure(2),imagesc(cost,[0 10]),colorbar"); #endif #endif // set cost for this path and all subpaths for (mrs_natural i = 0; i < pathLength; i++) { if (peakMatrix_(path_[i],colt + i) > 0) { MRSASSERT(i>0); continue; } for (mrs_natural j = i; j < pathLength; j++) { if (peakMatrix_(path_[j],colt + j) > 0) continue; // this path entry is not a peak mrs_real cost = (costMatrix_(path_[j],colt + j) - costMatrix_(path_[i],colt + i)) / (j-i+1); MRSASSERT (cost >= 0 && cost <=1); // assign this (and related) output SetOutput(out, cost, path_[i], colt + i, path_[j], colt + j); } } } } multipleIndices->Reset (); // scale output because of RBF without std?? //out *= 10.; // convert distance to similarity //out *= -1; //out += 1; #ifdef SANITY_CHECK for (t=0; t < inSamples_;t++) for (o=0; o < inSamples_;o++) MRSASSERT (out(t,o) >= 0 && out(t,o) <=1); #endif #ifdef MATLAB_DBG_OUT #ifdef MARSYAS_MATLAB MATLAB_PUT(out, "out"); MATLAB_EVAL ("figure(3),imagesc(out),colorbar"); #endif #endif }
void PeakSynthOscBank::myProcess(realvec& in, realvec& out) { mrs_natural t,c; out.setval(0.0); if (P_ > 1.0) NP_ = (mrs_natural)(N_/P_); else NP_ = N_; Iinv_ = (mrs_real)(1.0 / I_); Pinc_ = P_ * L_ / R_; nextamp_.setval(0); nextfreq_.setval(0); nextindex_.setval(0); // FIXME This line defines a (possibly) unused variable // bool flag = false; if(nbH_) { for(mrs_natural j=0 ; j<nbH_ ; j++) { mrs_real mulF = ctrl_harmonize_->to<mrs_realvec>()(1+j*2); mrs_real mulA = ctrl_harmonize_->to<mrs_realvec>()(2+j*2); for (t=0; t < NP_; t++) { mrs_natural index = (mrs_natural) ceil(in(t)/R_*2048*2+0.5); if (in(t) == 0.0 || index >= 2048) break; index+=j*2048; /* save current values for next iteration */ if(nextfreq_(index)) { cout << "PROBLEM"<<endl; } nextamp_(index) = in(t+NP_)*mulA; nextfreq_(index) = in(t)*Pinc_*mulF; } } } for (mrs_natural t=0; t < nextamp_.getSize(); t++) { // cout << endl << index << endl; if(lastfreq_(t) && nextfreq_(t)) { f_ = lastfreq_(t); finc_ = (nextfreq_(t) - f_)*Iinv_; } else if(nextfreq_(t)) { f_ = nextfreq_(t); finc_=0; } else { f_ = lastfreq_(t); finc_=0; } a_ = lastamp_(t); ainc_ = (nextamp_(t) - a_)*Iinv_; address_ = index_(t); /* avoid extra computing */ if ((a_ != 0.0 || ainc_!=0.0)) { // accumulate I samples from each oscillator // into output slice for (c=0; c < I_; ++c) { naddress_ = (mrs_natural)address_ % L_; out(0, c) += a_ * table_(naddress_); address_ += f_; while (address_ >= L_) address_ -= L_; while (address_ < 0) address_ += L_; a_ += ainc_; f_ += finc_; } // move down one parenthesis } nextindex_(t) = address_; } lastamp_ = nextamp_; lastfreq_ = nextfreq_; index_ = nextindex_; }
void PeakViewMerge::myProcess(realvec& in, realvec& out) { peakView *In[kNumMatrices], Out (out); mrs_natural i, rowIdx = 0, numPeaks[kNumMatrices], outputIdx = 0; const mrs_bool discNegGroups = ctrl_noNegativeGroups_->to<mrs_bool>(); out.setval(0.); for (i = 0; i < kNumMatrices; i++) { mrs_natural numRows = (i==kMat1)? ctrl_frameMaxNumPeaks1_->to<mrs_natural>() : ctrl_frameMaxNumPeaks2_->to<mrs_natural>(); numRows *= peakView::nbPkParameters; if (numRows == 0) // if the controls have not been set assume both matrixes to be of equal size numRows = in.getRows ()/kNumMatrices; peakViewIn_[i].stretch (numRows, in.getCols ()); in.getSubMatrix (rowIdx, 0, peakViewIn_[i]); rowIdx += numRows; In[i] = new peakView(peakViewIn_[i]); numPeaks[i] = In[i]->getTotalNumPeaks (); } if (ctrl_mode_->to<mrs_string>() == "OR") { // write all entries of the second peakView to output for (i = 0; i < numPeaks[1]; i++) { if (discNegGroups && (*In[1])(i,peakView::pkGroup) < 0) continue; WriteOutput (Out, In[1], i, outputIdx); outputIdx++; } // write all entries of the first peakView to output except duplicates for (i = 0; i < numPeaks[0]; i++) { mrs_natural Idx; if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0) continue; for (mrs_natural k = 1; k < kNumMatrices; k++) Idx = FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]); if (Idx < 0) { WriteOutput (Out, In[0], i, outputIdx); outputIdx++; } } } else if (ctrl_mode_->to<mrs_string>() == "AND") { // find duplicates and write only them to output for (i = 0; i < numPeaks[0]; i++) { mrs_natural Idx; if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0) continue; for (mrs_natural k = 1; k < kNumMatrices; k++) Idx = FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]); if (Idx >= 0) { if (discNegGroups && (*In[1])(Idx,peakView::pkGroup) < 0) continue; WriteOutput (Out, In[0], i, outputIdx); outputIdx++; } } } else if (ctrl_mode_->to<mrs_string>() == "ANDOR") { // keep the input[0] peaks that are not in input[1] for (i = 0; i < numPeaks[0]; i++) { mrs_natural Idx; if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0) continue; for (mrs_natural k = 1; k < kNumMatrices; k++) Idx = FindDuplicate (In[k], (*In[0])(i, peakView::pkFrequency), numPeaks[k]); if (Idx < 0) { WriteOutput (Out, In[0], i, outputIdx); outputIdx++; } } } else if (ctrl_mode_->to<mrs_string>() == "XOR") { // find duplicates and write only residual to output for (i = 0; i < numPeaks[0]; i++) { if (discNegGroups && (*In[0])(i,peakView::pkGroup) < 0) continue; mrs_natural Idx = FindDuplicate (In[1], (*In[0])(i, peakView::pkFrequency), numPeaks[1]); if (Idx < 0) { WriteOutput (Out, In[0], i, outputIdx); outputIdx++; } } // find duplicates and write only residual to output for (i = 0; i < numPeaks[1]; i++) { if (discNegGroups && (*In[1])(i,peakView::pkGroup) < 0) continue; mrs_natural Idx= FindDuplicate (In[0], (*In[1])(i, peakView::pkFrequency), numPeaks[0]); if (Idx < 0) { WriteOutput (Out, In[1], i, outputIdx); outputIdx++; } } } else { MRSERR("PeakViewMerfe::myProcess() : illegal mode string: " << ctrl_mode_->to<mrs_string>()); } for (i = 0; i < kNumMatrices; i++) { delete In[i]; } ctrl_totalNumPeaks_->setValue(outputIdx); }
void PeakConvert2::myProcess(realvec& in, realvec& out) { mrs_natural o,i; out.setval(0); peakView pkViewOut(out); const mrs_bool useMasking = getctrl("mrs_bool/useMasking")->to<mrs_bool>(); const mrs_real probThresh = getctrl("mrs_real/probabilityTresh")->to<mrs_real>(); max_->updControl("mrs_natural/nMaximums", frameMaxNumPeaks_); max_->setctrl("mrs_natural/inSamples", size_); max_->setctrl("mrs_natural/inObservations", 1); max_->update(); tmp_.stretch(frameMaxNumPeaks_*2); for(mrs_natural f=0 ; f < inSamples_; ++f) { //we should avoid the first empty frames, //that will contain silence and consequently create //discontinuities in the signal, ruining the peak calculation! //only process if we have a full data vector (i.e. no zeros) if(frame_ >= skip_) { // get pair of ffts in.getCol (f, tmpBuff_); // compute magnitude, phase, and instantaneous frequency this->ComputeMagnitudeAndPhase (tmpBuff_); // compute masking threshold if (useMasking && pick_) ComputeMasking (tmpBuff_); else masked_.setval(10.); // select bins with local maxima in magnitude (--> peaks) peaks_ = mag_; if(pick_) this->ComputePeaker (mag_, peaks_); else { for (o = 0 ; o < downFrequency_ ; o++) peaks_(o)=0.0; for (o = upFrequency_ ; o < (mrs_natural)peaks_.getSize() ; o++) peaks_(o)=0.0; } if (lpCoeff_ > 0) FreqSmear (lpPeakerRes_); //compute the probability of a peak being a peak for(o=0 ; o < size_ ; o++) { if (peaks_(o) <= 0) { frequency_(o) = .0; //lastmag_(o) = .0; lastfrequency_(o) = .0; // time smearing if no new peak lpPeakerRes_(o) *=lpCoeff_; continue; } #ifdef ORIGINAL_VERSION // probability of peak being a masker peakProb_(0) = 0; // probability of peak being stationary peakProb_(1) = 0; // probability of peak being tonal peakProb_(2) = (abs(frequency_(o)/fundamental_-o) > .5)? 0 : 1; #else // probability of peak being a masker peakProb_(0) = max((mrs_real).1, (mrs_real).5 * (mrs_real)(log10(masked_(o)) +1.)); // probability of peak being stationary peakProb_(1) = max((mrs_real).1, (mrs_real)lpPeakerRes_(o)); // probability or peak being tonal peakProb_(2) = GaussianPdf (frequency_(o)/fundamental_-o, gaussianStd); #endif // reset lpPeakerRes with peaker results lpPeakerRes_(o) = 1; peakProb_ *= peakProbWeight_; if ((peakProb_.sum() < probThresh) && pick_) { peaks_(o) = .0; frequency_(o) = .0; //lastmag_(o) = .0; lastfrequency_(o) = .0; } } // keep only the frameMaxNumPeaks_ highest amplitude local maxima tmp_.setval(0.); max_->process(peaks_, tmp_); nbPeaks_=tmp_.getSize()/2; realvec index_(nbPeaks_); //[!] make member to avoid reallocation at each tick! for (i=0 ; i<nbPeaks_ ; i++) index_(i) = tmp_(2*i+1); // search for bins interval realvec interval_(nbPeaks_*2); //[!] make member to avoid reallocation at each tick! interval_.setval(0); if(pick_) getShortBinInterval(interval_, index_, mag_); else { for (i=0 ; i<nbPeaks_ ; i++) interval_(2*i+1) = index_(i); } #ifdef LOG2FILE for (i=0 ; i<nbPeaks_ ; i++) { mrs_real value = frequency_((mrs_natural) (index_(i)+.1)); pFDbgFile << std::scientific << std::setprecision(4) << value << "\t"; } pFDbgFile << std::endl; #endif #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(mag_, "peaks"); MATLAB_PUT(peaks_, "k"); MATLAB_PUT(tmp_, "tmp"); MATLAB_PUT(interval_, "int"); MATLAB_PUT(frequency_, "freq"); // MATLAB_EVAL("figure(1);clf;hold on ;plot(peaks);stem(k);stem(tmp(2:2:end)+1, peaks(tmp(2:2:end)+1), 'r')"); // MATLAB_EVAL("stem(int+1, peaks(int+1), 'k')"); MATLAB_EVAL("figure(1);hold on ;stem(freq(tmp(2:2:end)+1), peaks(tmp(2:2:end)+1), 'r');hold off"); #endif #endif // fill output with peaks data interval_ /= N_; for (i = 0; i < nbPeaks_; i++) { mrs_natural index = (mrs_natural) (index_(i)+.1); pkViewOut(i, peakView::pkFrequency, f) = frequency_(index); pkViewOut(i, peakView::pkAmplitude, f) = magCorr_(index); pkViewOut(i, peakView::pkPhase, f) = -phase_(index); pkViewOut(i, peakView::pkDeltaFrequency, f) = deltafrequency_(index); pkViewOut(i, peakView::pkDeltaAmplitude, f) = /*abs*/(deltamag_(index)); pkViewOut(i, peakView::pkFrame, f) = frame_; pkViewOut(i, peakView::pkGroup, f) = 0.;//(pick_)?-1.:0.; //This should be -1!!!! [TODO] pkViewOut(i, peakView::pkVolume, f) = 1.0; pkViewOut(i, peakView::pkBinLow, f) = interval_(2*i); pkViewOut(i, peakView::pkBin, f) = index_(i); pkViewOut(i, peakView::pkBinHigh, f) = interval_(2*i+1); pkViewOut(i, peakView::pkTrack, f) = -1.0; //null-track ID MRSASSERT((index_(i) <= interval_(2*i)) || (interval_(2*i+1) <= index_(i))); if(useStereoSpectrum_) pkViewOut(i, peakView::pkPan, f) = in((mrs_natural)index_(i)+2*N_, f); else pkViewOut(i, peakView::pkPan, f) = 0.0; } } else //if not yet reached "skip" number of frames... { for(mrs_natural i=0; i< frameMaxNumPeaks_; ++i) { //pkViewOut(i, peakView::pkFrequency, f) = 0; //pkViewOut(i, peakView::pkAmplitude, f) = 0; //pkViewOut(i, peakView::pkPhase, f) = 0; //pkViewOut(i, peakView::pkDeltaFrequency, f) = 0; //pkViewOut(i, peakView::pkDeltaAmplitude, f) = 0; pkViewOut(i, peakView::pkFrame, f) = frame_; //pkViewOut(i, peakView::pkGroup, f) = -1; //pkViewOut(i, peakView::pkVolume, f) = 0; //pkViewOut(i, peakView::pkPan, f) = 0; //pkViewOut(i, peakView::pkBinLow, f) = 0; //pkViewOut(i, peakView::pkBin, f) = 0; //pkViewOut(i, peakView::pkBinHigh, f) = 0; } } frame_++; } //count the total number of existing peaks (i.e. peak freq != 0) ctrl_totalNumPeaks_->setValue(pkViewOut.getTotalNumPeaks()); }
void Peaker::myProcess(realvec& in, realvec& out) { mrs_natural t,o; const mrs_natural peakSpacing = (mrs_natural)(inSamples_ *getctrl("mrs_real/peakSpacing")->to<mrs_real>() + .5); mrs_real peakStrengthRelRms, peakStrengthRelMax, peakStrengthRelThresh, peakStrengthAbs; //mrs_real peakGain; mrs_bool peakHarmonics; mrs_bool rmsNormalize; mrs_natural peakStart; mrs_natural peakEnd; mrs_natural interpolationMode; mrs_natural peakNeighbors; peakStrengthRelRms = getctrl("mrs_real/peakStrength")->to<mrs_real>(); peakStrengthRelMax = getctrl("mrs_real/peakStrengthRelMax")->to<mrs_real>(); peakStrengthRelThresh = getctrl("mrs_real/peakStrengthRelThresh")->to<mrs_real>(); lpCoeff_ = getctrl("mrs_real/peakStrengthThreshLpParam")->to<mrs_real>(); peakStrengthAbs = getctrl("mrs_real/peakStrengthAbs")->to<mrs_real>(); peakStart = getctrl("mrs_natural/peakStart")->to<mrs_natural>(); peakEnd = getctrl("mrs_natural/peakEnd")->to<mrs_natural>(); interpolationMode = getctrl("mrs_natural/interpolation")->to<mrs_natural>(); //peakGain = getctrl("mrs_real/peakGain")->to<mrs_real>(); peakHarmonics = getctrl("mrs_bool/peakHarmonics")->to<mrs_bool>(); rmsNormalize = getctrl("mrs_bool/rmsNormalize")->to<mrs_bool>(); peakNeighbors = getctrl("mrs_natural/peakNeighbors")->to<mrs_natural>(); if (peakEnd == 0) peakEnd = inSamples_; // FIXME This line defines an unused variable // mrs_real srate = getctrl("mrs_real/israte")->to<mrs_real>(); out.setval(0.0); //peakStrengthRelRms = 0.0; for (o = 0; o < inObservations_; o++) { rms_ = 0.0; max_ = -1e37; for (t=peakStart; t < peakEnd; t++) { rms_ += in(o,t) * in(o,t); if (max_ < in(o,t)) max_ = in(o,t); } if (rms_ != 0.0) rms_ /= (peakEnd - peakStart); rms_ = sqrt(rms_); mrs_real max; mrs_natural maxIndex; bool peakFound = false; if (peakStrengthRelThresh > .0) { in.getRow (o,lpThresh_); compLpThresh (lpThresh_, lpThresh_); // do it inplace to avoid another copy... } for (t=peakStart; t < peakEnd; t++) { peakFound = true; // peak has to be larger than neighbors for (int j = 1; j < peakNeighbors; j++) { mrs_natural index=t-j; if (index<0) index=0; if (in(o,index) >= in(o,t)) { peakFound = false; break; } index=t+j; if (index>=inSamples_) index=inSamples_-1; if (in(o,index) >= in(o,t)) { peakFound = false; break; } } if (peakFound) { currThresh_ = lpThresh_(t); peakFound = doThresholding (in(o,t), peakStrengthRelRms, peakStrengthRelMax, peakStrengthRelThresh, peakStrengthAbs); } if (peakFound) { // check for another peak in the peakSpacing area max = in(o,t); maxIndex = t; for (int j=0; j < peakSpacing; j++) { if (t+j < peakEnd-1) if (in(o,t+j) > max) { max = in(o,t+j); maxIndex = t+j; } } t += peakSpacing; if (rmsNormalize) { out(o,maxIndex) = in(o,maxIndex) / rms_; if(interpolationMode && maxIndex > 0 && maxIndex < inSamples_) { out(o,maxIndex-1) = in(o,maxIndex-1) /rms_; out(o,maxIndex+1) = in(o,maxIndex+1) / rms_; } } else { out(o,maxIndex) = in(o,maxIndex); if(interpolationMode && maxIndex > 0 && maxIndex < inSamples_) { out(o,maxIndex-1) = in(o,maxIndex-1); out(o,maxIndex+1) = in(o,maxIndex+1); } } // peakNeighbors = 0; // rms_ = 1.0; if (peakHarmonics) { twice_ = 2 * maxIndex; half_ = (mrs_natural) (0.5 * maxIndex + 0.5); triple_ = 3 * maxIndex; third_ = (mrs_natural) (0.33 * maxIndex + 0.5); if (twice_ < (peakEnd - peakStart)) { peakFound = true; // peak has to be larger than neighbors for (int j = 1; j < peakNeighbors; j++) { if (in(o,twice_-j) >= in(o,twice_)) { peakFound = false; break; } if (in(o,twice_+j) >= in(o,twice_)) { peakFound = false; break; } } if (peakFound) { out(o, maxIndex) += (in(o, twice_)/rms_); out(o, twice_) = in(o,twice_)/rms_ + 0.5 * out(o, maxIndex); } } if (half_ < (peakEnd - peakStart)) { peakFound = true; // peak has to be larger than neighbors for (int j = 1; j < peakNeighbors; j++) { if (in(o,half_-j) >= in(o,half_)) { peakFound = false; break; } if (in(o,half_+j) >= in(o,half_)) { peakFound = false; break; } } if (peakFound) { out(o, maxIndex) += (in(o, half_)/rms_); out(o, half_) = in(o,half_)/rms_ + 0.5 * out(o, maxIndex); } } if (triple_ < (peakEnd - peakStart)) { peakFound = true; // peak has to be larger than neighbors for (int j = 1; j < peakNeighbors; j++) { if (in(o,triple_-j) >= in(o,triple_)) { peakFound = false; break; } if (in(o,triple_+j) >= in(o,triple_)) { peakFound = false; break; } } if (peakFound) { out(o, maxIndex) += (in(o, triple_)/rms_); out(o, triple_) = in(o,triple_)/rms_ + 0.5 * out(o, maxIndex); } } if (third_ < (peakEnd - peakStart)) { peakFound = true; // peak has to be larger than neighbors for (int j = 1; j < peakNeighbors; j++) { if (in(o,third_-j) >= in(o,third_)) { peakFound = false; break; } if (in(o,third_+j) >= in(o,triple_)) { peakFound = false; break; } } if (peakFound) { out(o, maxIndex) += (in(o, third_)/rms_); out(o, third_) = in(o,third_)/rms_ + 0.5 * out(o, maxIndex); } } } peakFound = true; } } } }
void Yin::myProcess(realvec& in, realvec& out) { // The tolerance for the yin algorithm const mrs_real tol = ctrl_tolerance_->to<mrs_real>(); // get pointers to avoid excessive (long,long) lookups mrs_real *yin_buffer = yin_buffer_realvec_.getData(); const mrs_natural yin_buffer_size = yin_buffer_realvec_.getSize(); // ASSUME: only one channel mrs_real *input = in.getData(); mrs_real pitch = -1.0; // cout << "yin.getSize()=" << yin.getSize() << endl; // cout << "tol=" << tol << endl; const mrs_real freq_max = ctrl_frequency_max_->to<mrs_real>(); const mrs_real freq_min = ctrl_frequency_min_->to<mrs_real>(); // yes, low_sample comes from the highest pitch mrs_natural low_sample = 4; if (freq_max > 0) { low_sample = israte_ / freq_max; } mrs_natural high_sample = yin_buffer_size; if (freq_min > 0) { high_sample = israte_ / freq_min; } // Calculate the pitch with the Yin method //mrs_natural c=0; mrs_real cmndf = 0.; // cumulative mean normalized difference function std::fill(yin_buffer, yin_buffer + yin_buffer_size, 0.0); yin_buffer[0] = 1.; //for (mrs_natural tau=1; tau < yin_size_; tau++) for (mrs_natural tau=1; tau < high_sample; tau++) { // d_t( tau ) for (mrs_natural j=0; j < yin_buffer_size;j++) { const mrs_real delta = input[j] - input[j+tau]; yin_buffer[tau] += delta * delta; } cmndf += yin_buffer[tau]; yin_buffer[tau] *= tau / cmndf; if (tau > low_sample) { const mrs_natural period = tau-3; //if(tau > 4 && (yin_buffer_(c,period) < tol) && // (yin_buffer_(c,period) < yin_buffer_(c,period+1))) if((yin_buffer[period] < tol) && (yin_buffer[period] < yin_buffer[period+1])) { pitch = vec_quadint_min(&yin_buffer_realvec_,period,1); break; } } } if (pitch < 0) { pitch = vec_quadint_min(&yin_buffer_realvec_, vec_min_elem(&yin_buffer_realvec_),1); } if (pitch !=0) out(0,0) = ctrl_osrate_/pitch; else out(0,0) = 0.0; }
void PeakConvert::getLargeBinInterval(realvec& interval, realvec& index, realvec& mag) { mrs_natural k=0, start=0, nbP=index.getSize(); // handling the first case mrs_real minVal = HUGE_VAL; mrs_natural minIndex = 0; // getting rid of padding zeros while(!index(start)) start++; for (mrs_natural j=0 ; j<index(start) ; j++) //is this foor loop like this?!?!?!?!?!?!?!?! [!] { if(minVal > mag(j)) { minVal = mag(j); minIndex = j; } } // if(!minIndex) // { // cout << "pb while looking for minimal bin intervals" << endl; //[WTF] // } interval(0) = minIndex; for(mrs_natural i=start ; i<nbP-1 ; ++i, k++) { minVal = HUGE_VAL; minIndex = 0; // look for the minimal value among successive peaks for (mrs_natural j= (mrs_natural) index(i) ; j<index(i+1) ; j++) // is this for loop like this?!?!?! [?] { if(minVal > mag(j)) { minVal = mag(j); minIndex = j; } } // if(!minIndex) // { // cout << "pb while looking for bin intervals" << endl; //[WTF] // } interval(2*k+1) = minIndex-1; interval(2*(k+1)) = minIndex; } // handling the last case minVal = HUGE_VAL; minIndex = 0; for (mrs_natural j= (mrs_natural)index(nbP-1) ; j<mag.getSize()-1 ; ++j) { if(minVal > mag(j)) { minVal = mag(j); minIndex = j; } // consider stopping the search at the first valley if(minVal<mag(j+1)) break; } // if(!minIndex) // { // cout << "pb while looking for maximal bin intervals" << endl; //[WTF] // } interval(2*(k)+1) = minIndex; }
void PlotSink::myProcess(realvec& in, realvec& out) { out = in; mrs_natural t,o; //if using MATLABengine, plot the input data in MATLAB #ifdef MARSYAS_MATLAB if(ctrl_matlab_->isTrue()) { MATLAB_PUT(in, type_+"_"+name_+"_indata") MATLAB_EVAL(ctrl_matlabCommand_->to<mrs_string>()); } #endif counter_++; if (ctrl_sequence_->isTrue()) { //save current input to a sequence of numbered output files ostringstream oss; oss << ctrl_filename_->to<mrs_string>() << setfill('0') << setw(4) << counter_ << ".plot"; cout << "name = " << name_ << " " << oss.str() << endl; MRSMSG("Writing " << oss.str() << endl); in.write(oss.str()); } if (ctrl_single_file_->isTrue()) { for (o=0; o < inObservations_; o++) { for (t = 0; t < inSamples_; t++) { //(*single_file_) << counter_ << " " << t << " "; (*single_file_) << std::setprecision(20) << in(o,t); (*single_file_) << std::endl; //cout << in(o,t); } } if (ctrl_no_ticks_->isTrue()) { } else { (*single_file_) << std::endl; } } if(ctrl_messages_->isTrue()) { mrs_string sep =ctrl_separator_->to<mrs_string>(); //ostringstream oss; //output input content as a Marsyas Message (stdout by default) // for (t = 0; t < inSamples_; t++) // { // for (o=0; o < inObservations_; o++) // { // if (o < inObservations_ - 1) // { // oss << out(o,t) << sep; // } // else // { // oss << out(o,t); // } // } // mrs_string s = oss.str(); // MRSMSG(s << endl); // }//FIXME: confirm that code below is correct and remove commented code above for (o=0; o < inObservations_; o++) { ostringstream oss; for (t = 0; t < inSamples_; t++) { if (t < inSamples_ - 1) { oss << out(o,t) << sep; } else { oss << out(o,t); } } mrs_string s = oss.str(); MRSMSG(s << endl); } } }
void PeakerAdaptive::myProcess(realvec& in, realvec& out) { mrs_natural t,o; mrs_real peakSpacing; mrs_real peakStrength; mrs_real peakGain; mrs_natural peakStart; mrs_natural peakEnd; mrs_natural peakStrengthReset; mrs_real peakDecay; peakSpacing = getctrl("mrs_real/peakSpacing")->to<mrs_real>(); peakStrength = getctrl("mrs_real/peakStrength")->to<mrs_real>(); peakStart = getctrl("mrs_natural/peakStart")->to<mrs_natural>(); peakEnd = getctrl("mrs_natural/peakEnd")->to<mrs_natural>(); peakGain = getctrl("mrs_real/peakGain")->to<mrs_real>(); peakStrengthReset = getctrl("mrs_natural/peakStrengthReset")->to<mrs_natural>(); peakDecay = getctrl("mrs_real/peakDecay")->to<mrs_real>(); out.setval(0.0); MRSMSG("peakEnd = " << peakEnd); for (o = 0; o < inObservations_; o++) { peakSpacing = (mrs_real)(peakSpacing * inSamples_); mrs_real max; mrs_natural maxIndex = 0; bool peakFound = false; for (t=peakStart+1; t < peakEnd-1; t++) { if (fabs(in(o,t)) > rms_) rms_ = fabs(in(o,t)); } for (t=peakStart+1; t < peakEnd-1; t++) { // peak has to be larger than neighbors if ((in(o,t -1) < in(o,t)) && (in(o,t+1) < in(o,t)) && (fabs(in(o,t)) > peakStrength * rms_) ) { max = in(o,t); maxIndex = t; for (int j=0; j < (mrs_natural)peakSpacing; j++) { if (t+j < peakEnd-1) if (in(o,t+j) > max) { max = in(o,t+j); maxIndex = t+j; } } t += (mrs_natural)peakSpacing; if ((peakHysterisis_ > peakStrengthReset) || (peakHysterisis_ == 0) ) { out(o,maxIndex) = fabs(in(o,maxIndex)); peakHysterisis_ = 1; } rms_ = fabs(in(o,maxIndex)); peakFound = true; } } if (!peakFound) { rms_ *= peakDecay; setctrl("mrs_bool/peakFound", false); } else setctrl("mrs_bool/peakFound", true); peakHysterisis_ ++; } }
void SimilarityMatrix::myProcess(realvec& in, realvec& out) { //check if there are any elements to process at the input //(in some cases, they may not exist!) - otherwise, do nothing //(i.e. output is also an empty vector) mrs_natural i, j, k, l; if(inSamples_ > 0) { child_count_t child_count = marsystems_.size(); if(child_count == 1) { mrs_natural nfeats = in.getRows()/sizes_.getSize(); // calculate hte Covariance Matrix from the input, if defined mrs_natural obs = 0; for(i=0; i<sizes_.getSize(); ++i) { for(j=0; j<sizes_(i); j++) { for(k=0; (mrs_natural)k<invecs_[i].getRows(); k++) { invecs_[i](k, j) = in(k+obs, j); } } obs += invecs_[i].getRows(); } // normalize input features if necessary if(ctrl_normalize_->to<mrs_string>() == "MinMax") for(i=0; i<sizes_.getSize(); ++i) { invecs_[i].normObsMinMax(); // (x - min)/(max - min) } else if(ctrl_normalize_->to<mrs_string>() == "MeanStd") for(i=0; i<sizes_.getSize(); ++i) { invecs_[i].normObs(); // (x - mean)/std } if(ctrl_calcCovMatrix_->to<mrs_natural>() & SimilarityMatrix::fixedStdDev) { MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); covMatrix.create(inObservations_/sizes_.getSize(), inObservations_/sizes_.getSize()); mrs_real var = ctrl_stdDev_->to<mrs_real>(); var *= var; for(i=0; i< inObservations_/sizes_.getSize(); ++i) { covMatrix(i,i) = var; } } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & SimilarityMatrix::diagCovMatrix) { invecs_[0].varObs(vars_); // Faster mrs_natural dim = vars_.getSize(); // fill covMatrix diagonal with var values (remaining values are zero) MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); covMatrix.create(dim, dim); for(i=0; i<(mrs_natural)dim; ++i) { covMatrix(i,i) = vars_(i); } } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & SimilarityMatrix::fullCovMatrix) { MarControlAccessor acc(ctrl_covMatrix_); realvec& covMatrix = acc.to<mrs_realvec>(); invecs_[0].covariance(covMatrix); // Slower } else if(ctrl_calcCovMatrix_->to<mrs_natural>() & SimilarityMatrix::noCovMatrix) { ctrl_covMatrix_->setValue(realvec()); } for(i=0; i<sizes_(0); ++i) { obs = 0; invecs_[0].getCol(i, i_featVec_); for(l=0; l<(mrs_natural)nfeats; l++) { stackedFeatVecs_(l,0) = i_featVec_(l); } for(j=1; j<sizes_.getSize(); j++) { for(k=0; k<sizes_(j); k++) { invecs_[j].getCol(k, j_featVec_); // stack i and j feat vectors for(l=0; l<(mrs_natural)nfeats; l++) { stackedFeatVecs_(l+nfeats,0) = j_featVec_(l); } marsystems_[0]->process(stackedFeatVecs_, metricResult_); out(k+obs,i) = metricResult_(0,0); } obs += (mrs_natural)sizes_(j); } } } else { out.setval(0.0); if(child_count == 0) { MRSWARN("SimilarityMatrix::myProcess - no Child Metric MarSystem added - outputting zero similarity matrix!"); } else { MRSWARN("SimilarityMatrix::myProcess - more than one Child MarSystem exists (i.e. invalid metric) - outputting zero similarity matrix!"); } } } //MATLAB_PUT(out, "simMat"); //MATLAB_EVAL(name_+"=["+name_+",simMat(:)'];"); }