void StandardDeviation::myProcess(realvec& in, realvec& out) { mrs_natural t,o; //checkFlow(in,out); out.setval(0.0); for (o=0; o < inObservations_; o++) { for (t = 0; t < inSamples_; t++) { // Calculate mean obsrow_(t) = in(o,t); } out(o,0) = obsrow_.std(); } // VERY INEFFICIENT - LOTS OF MEMORY ALLOCATION AND COPYING // out = in.stdObs(); }
void PeakSynthOsc::myProcess(realvec& in, realvec& out) { out.setval(0); silence_ = true; pkGrp2Synth_ = ctrl_peakGroup2Synth_->to<mrs_natural>(); Nb_ = in.getSize()/peakView::nbPkParameters ; //number of peaks in the current frame nbH_ = ctrl_harmonize_->to<mrs_realvec>().getSize(); if(nbH_) for(mrs_natural j=0 ; j<(nbH_-1)/2 ; j++) { mulF_ = ctrl_harmonize_->to<mrs_realvec>()(1+j*2); mulA_ = ctrl_harmonize_->to<mrs_realvec>()(2+j*2); //cout << "mulF_" << mulF_ << "mulA_" << mulA_ << endl; for (mrs_natural i=0; i < Nb_; ++i) { // either synthesize peaks with a corresponding GroupID or all with a group ID >= 0 mrs_bool synthMe = (pkGrp2Synth_ < 0)? (in(i+peakView::pkGroup*Nb_) >= 0) : (in(i+peakView::pkGroup*Nb_) == pkGrp2Synth_); if( synthMe ) { sine(out, in(i)*mulF_, in(i+Nb_)*mulA_, in(i+2*Nb_)); silence_ = false; } } } else for (mrs_natural i=0; i < Nb_; ++i) { // either synthesize peaks with a corresponding GroupID or all with a group ID >= 0 mrs_bool synthMe = (pkGrp2Synth_ < 0)? (in(i+peakView::pkGroup*Nb_) >= 0) : (in(i+peakView::pkGroup*Nb_) == pkGrp2Synth_); if( synthMe ) { sine(out, in(i), in(i+Nb_), in(i+2*Nb_)); silence_ = false; } } //signal if at least one peak was synthesized or not ctrl_isSilence_->setValue(silence_); }
void SCF::myProcess(realvec& in, realvec& out) { mrs_natural i, k, bandwidth; double c, maxc; double aritMean ; //default SCF value = 1.0; (defines SCF=1.0 for silence) out.setval(1.0); //MPEG7 defines a grouping mechanism for the frequency bands above 1KHz //in order to reduce computational effort of the following calculation. //For now such grouping mechanism is not implemented... for(i = 0; i < nrValidBands_; ++i) { aritMean = 0.0; maxc = 0.0; bandwidth = ih_[i] - il_[i] + 1; for(k = il_[i]; k <= ih_[i]; k++) { c = in(k); //power spectrum coeff aritMean += c / bandwidth; if(c > maxc) maxc = c; } if (aritMean != 0.0) { out(i) = (float)(maxc/aritMean); } //else //mean power = 0 => silence... // out(i) = 1.0; //as used for the SFM (MPEG-7)... } //for freq bands above the nyquist freq //return SFM value defined in MPEG7 for silence //for(i = nrValidBands_; i < nrBands_; ++i) // out(i) = 1.0; }
void Krumhansl_key_finder::myProcess(realvec& in, realvec& out) { mrs_natural o,k; scores_.setval(0.0); // Correlate with each key profile for (k = 0; k < 12; k++) { for (o = 0; o < inObservations_; o++) { scores_(k) += in(o,0) * major_profile_((o+k)%12); } } mrs_real max_score = 0.0; mrs_natural max_index = 0; for (int k=0; k < 12; k++) { if (scores_(k) >= max_score) { max_score = scores_(k); max_index = k; } } ctrl_key_->setValue(max_index, NOUPDATE); ctrl_key_name_->setValue(key_names_[max_index], NOUPDATE); out.setval(0.0); out(max_index,0) = 1.0; }
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_) { 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++) for (t=0; t < onSamples_; t++) { out(o,t) = out(o,t); } } }
void EnhADRess::myProcess(realvec& in, realvec& out) { out.setval(0.0); for(mrs_natural t=0; t < inSamples_; ++t) { for (mrs_natural k=0; k < N4_; k++) { //get left channel spectrum bin if (k==0) //DC bin (i.e. 0) { rel_ = in(0,t); iml_ = 0.0; } else if (k == N4_-1) //Nyquist bin (i.e. N/2) { rel_ = in(1, t); iml_ = 0.0; } else //all other bins { rel_ = in(2*k, t); iml_ = in(2*k+1, t); } //get right channel spectrum bin if (k==0) //DC bin (i.e. 0) { rer_ = in(N2_,t); imr_ = 0.0; } else if (k == N4_-1) //Nyquist bin (i.e. N/2) { rer_ = in(N2_+1, t); imr_ = 0.0; } else //all other bins { rer_ = in(N2_ + 2*k, t); imr_ = in(N2_ + 2*k+1, t); } phaseL_ = atan2(iml_, rel_); //left channel phases phaseR_ = atan2(imr_, rer_); //right channel phases deltaPhase_ = abs(phaseL_ - phaseR_); //wrap phase into the 0~2*PI range deltaPhase_ = (mrs_real)fmod((double)deltaPhase_, (double)2*PI); //left amplitude value Lk_ = sqrt(rel_*rel_ + iml_*iml_); //right amplitude value Rk_ = sqrt(rer_*rer_ + imr_*imr_); if(deltaPhase_ < PI/2) { minLk_ = Lk_ * sin(deltaPhase_); minRk_ = Rk_ * sin(deltaPhase_); if(Lk_ < Rk_) // i.e. minLk < minRk --> sound panned right { //store magnitude to output mrs_real mag = Rk_ - minLk_; // bins with amplitude inferior to -100dB are discarded if(20.0*log10(mag*mag+0.000000001) > -100.0) { out(k,t) = mag; //store phase to output out(k+N4_,t) = phaseR_; //store azimuth to output out(k+N4_*2,t) = 1.0 - Lk_ * cos(deltaPhase_) / Rk_ ; //-1.0-> L; 0.0-> C; } } else if(Lk_ > Rk_) // i.e. minLk > minRk --> sound panned left { //store magnitude to output mrs_real mag = Lk_ - minRk_; // bins with amplitude inferior to -100dB are discarded if(20.0*log10(mag*mag+0.000000001) > -100.0) { out(k,t) = mag; //store phase to output out(k+N4_,t) = phaseL_; //store azimuth to output out(k+N4_*2,t) = Rk_ * cos(deltaPhase_) / Lk_ -1.0; //0.0 -> C; 1.0-> R; } } else if(Lk_ == Rk_) //sound panned at the CENTER { //store magnitude to output mrs_real mag = Lk_ - minRk_; // bins with amplitude inferior to -100dB are discarded if(20.0*log10(mag*mag+0.000000001) > -100.0) { out(k,t) = mag; //store phase to output out(k+N4_,t) = phaseL_; //could have been phaseR_... //store azimuth to output out(k+N4_*2,t) = 0.0; //0.0 -> C; } } } else { if(20.0*log10(Lk_*Lk_+0.000000001) < -100.0) Lk_ = 0.0; if(20.0*log10(Rk_*Rk_+0.000000001) < -100.0) Rk_ = 0.0; if(Lk_ > Rk_) { out(k,t) = Lk_; out(k+N4_,t) = phaseL_; out(k+N4_*2,t) = 0.0; //-1.0; } else if(Rk_ > Lk_) { out(k,t) = Rk_; out(k+N4_,t) = phaseR_; out(k+N4_*2,t) = 0.0; //1.0; } else if(Lk_ == Rk_ && Lk_ != 0.0) { out(k,t) = Lk_; out(k+N4_,t) = phaseL_; out(k+N4_*2,t) = 0.0; } } } } //MATLAB_PUT(out, "out"); //MATLAB_EVAL("figure(1);plot(out);figure(2)"); //MATLAB_EVAL("plot(out(length(out)/3*2+1:end))") }
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 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 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 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 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 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 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 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 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 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 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 NormCut::myProcess(realvec& in, realvec& out) { mrs_natural t,o; #ifdef MARSYAS_MATLAB #ifdef MTLB_DBG_LOG MATLAB_PUT(in, "in"); MATLAB_EVAL("figure(71),imagesc(in',[0 1]),colorbar"); #endif #endif //check if there is any data at the input, otherwise do nothing if(in.getSize() == 0 || numClusters_ == 0) { //MRSWARN("NormCut::myProcess - empty input!"); out.setval(-1.0); return; } if(in.getSize() == 1 || numClusters_ == 0) { //MRSWARN("NormCut::myProcess - empty input!"); out.setval(0); return; } out.setval(0); //should it be -1.0 instead? [!] FIXME //ncut( n, W, nbcluster, NcutEigenvectors, NcutEigenvalues, params ); //discretisation( n, nbcluster, NcutEigenvectors, NcutDiscrete, params ); ncut(inObservations_, in, numClusters_, nCutEigVectors_, nCutDiscrete_ ); discretisation(inObservations_, numClusters_, nCutEigVectors_, nCutDiscrete_ ); for( o=0 ; o<inObservations_ ; o++ ) { for( t=0 ; t<numClusters_ ; t++ ) { // Initialize NcutDiscrete as we go if( nCutDiscrete_(o*(numClusters_)+t) == 1.0 ) out(0,o) = t; //cout << "Peak " << o << " -> cluster = " << out(0,o) << endl; } } //cout << out << endl; // //get a local copy of the current gain control value // //(it will be used for this entire processing, even if it's // //changed by someone else, e.g. by a different thread) // mrs_real gainValue = ctrl_gain_->to<mrs_real>(); // // // It is important to loop over both observations // // and channels so that for example a gain can be // // applied to multi-channel signals // for (o=0; o < inObservations_; o++) // { // for (t = 0; t < inSamples_; t++) // { // //apply gain to all channels // out(o,t) = gainValue * in(o,t); // } // } }
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 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 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(:)'];"); }