Ejemplo n.º 1
0
void
realvec::appendRealvec(const realvec newValues)
{
  mrs_natural origSize = size_;

  stretch(origSize + newValues.getSize());

  for (mrs_natural i=0; i<newValues.getSize(); ++i)
    data_[origSize + i] = newValues.data_[i];
}
Ejemplo n.º 2
0
void
PeakConvert2::getShortBinInterval(realvec& interval, realvec& index, realvec& mag)
{
  const unsigned int maxLobeWidth = 6;
  unsigned int	nbP=index.getSize();
  unsigned int minIndex = 0,
               endLoop,
               length = mag.getSize ();

  // we could also use the instantaneous frequency here...?

  for(unsigned int i=0 ; i<nbP ; i++)
  {
    unsigned int idx = (unsigned int)(index(i)+.1);

    if (idx <= 0)
      continue;

    endLoop		= min(length,idx + maxLobeWidth);
    minIndex	= endLoop;
    // look for the next valley location upward
    for (unsigned int j = idx ; j < endLoop ; j++)
    {
      if(mag(j) < mag(j+1))
      {
        minIndex = j;
        break;
      }
    }

    interval(2*i+1) = minIndex;

    endLoop		= max((unsigned int)0,idx - maxLobeWidth);
    minIndex	= endLoop;

    // look for the next valley location downward
    for (unsigned int j= idx ; j > endLoop ; j--)
    {
      if(mag(j) < mag(j-1))
      {
        minIndex = j;
        break;
      }
    }

    interval(2*i) = minIndex;
  }
}
void
TranscriberExtract::getAllFromAudio(const std::string audioFilename, realvec&
                                    pitchList, realvec& ampList,
                                    realvec& boundaries)
{
	MarSystem* pitchSink = mng.create("RealvecSink", "pitchSink");
	MarSystem* ampSink = mng.create("RealvecSink", "ampSink");

	MarSystem* pnet = mng.create("Series", "pnet");
	mrs_real srate = addFileSource(pnet, audioFilename);
// TODO: double the number of observations?
//	pnet->updControl("SoundFileSource/src/mrs_natural/inSamples",256);
//	pnet->addMarSystem(mng.create("ShiftInput", "shift"));
//	pnet->updControl("ShiftInput/shift/mrs_natural/winSize",512);

	MarSystem* fanout = mng.create("Fanout", "fanout");
	fanout->addMarSystem(makePitchNet(srate, 100.0, pitchSink));
	fanout->addMarSystem(makeAmplitudeNet(ampSink));
	pnet->addMarSystem(fanout);

	while ( pnet->getctrl("mrs_bool/hasData")->to<mrs_bool>() )
		pnet->tick();

	pitchList = getPitchesFromRealvecSink(pitchSink, srate);
	ampList = getAmpsFromRealvecSink(ampSink);
	boundaries.create(2);
	boundaries(0) = 0;
	boundaries(1) = pitchList.getSize();
	delete pnet;
}
Ejemplo n.º 4
0
void
HWPS::harmonicWrap(mrs_real peak1Freq, mrs_real peak2Freq, realvec& peak1SetFreqs, realvec& peak2SetFreqs)
{

  // fundamental frequency estimate
  mrs_real hF;

  // Use the lowest in frequency highest amplitude
  // peak of the frames under consideration
  hF = min(peak1SetFreqs(0), peak2SetFreqs(0));

  // Original HWPS using the considered peaks for folding
  // hF = min(peak1Freq, peak2Freq);

  // mrs_real mhF = min(hF, abs(peak1Freq-peak2Freq));

  // shift frequencies
  peak1SetFreqs -= peak1Freq;
  peak2SetFreqs -= peak2Freq;


  /*
    MATLAB_PUT(peak1SetFreqs, "P1");
    MATLAB_PUT(peak2SetFreqs, "P2");
    MATLAB_EVAL("clf ; subplot(3, 1, 1);  hold ; stem(P1, A1); stem(P2, A2, 'r')");
  */

  // wrap frequencies around fundamental freq estimate
  peak1SetFreqs /= hF;
  peak2SetFreqs /= hF;

  for (mrs_natural k=0 ; k<peak1SetFreqs.getSize() ; k++)
  {
    peak1SetFreqs(k)=fmod(peak1SetFreqs(k), 1);
    //if(peak1SetFreqs(k)<0)
    while(peak1SetFreqs(k)<0)//replacing "if" in case of strongly negative (=> multiple wraps)
      peak1SetFreqs(k)+=1;
  }
  for (mrs_natural k=0 ; k<peak2SetFreqs.getSize() ; k++)
  {
    peak2SetFreqs(k)=fmod(peak2SetFreqs(k), 1);
    //if(peak2SetFreqs(k)<0)
    while(peak2SetFreqs(k)<0) //replacing "if" in case of strongly negative (=> multiple wraps)
      peak2SetFreqs(k)+=1;
  }
}
Ejemplo n.º 5
0
void MedianFilter::myProcess(realvec& inVec, realvec& outVec)
{
  // Each element in the input vector is replaced by the median of
  // the elements that fall in a window surrounding the element, defined as:
  // element index + [0,1.. N-1] - floor(N/2) with N, the size of the window.

  // Define moving window: once inserted,
  // the elements in the moving window are automatically sorted
  mrs_natural N = WindowSize_;
  multimap<mrs_real,mrs_natural,less<mrs_real> > theWindow;
  typedef multimap<mrs_real,mrs_natural,less<mrs_real> >::iterator iter;
  typedef pair<mrs_real,mrs_natural> element;

  // M = floor(N/2)
  mrs_natural M = (mrs_natural)floor((mrs_real)N/2.);

  // Initialize the moving window
  // For i=0, the window = [v(0), v(0),.. v(0),v(1),.. v(N-M)]
  for (int p=-M; p<=0; p++)
    theWindow.insert(element(inVec(0),p));
  for (int p=1; p<=N-M-1; p++)
    theWindow.insert(element(inVec(p),p));

  mrs_natural I = inVec.getSize();
  for (int i=0; i<I; ++i)
  {
    // Define to-be-inserted element
    element theNewOne(inVec(min(i+N-M,I-1)),i+N-M);

    // Search for median, insert and delete position
    int theOffset = -M;

    iter theMedian, theToDelete, theToInsert = theWindow.begin();
    for (iter theIter=theWindow.begin(); theIter!=theWindow.end(); theIter++)
    {
      if (theOffset == 0)
        theMedian = theIter;
      if (theIter->second == i-M)
        // Index i-M = first inserted element (oldest)
        theToDelete = theIter;
      if (theNewOne.first > theIter->first)
      {
        // Update theToInsert as long as theIter
        // is smaller than the to-be-inserted element
        theToInsert = theIter;
        theToInsert++;
      }
      theOffset++;
    }

    if (theToInsert == theToDelete)
      theToInsert++;

    outVec(i) = theMedian->first;
    theWindow.erase(theToDelete);
    theWindow.insert(theToInsert,theNewOne);
  }
}
Ejemplo n.º 6
0
void
PeakConvert::getShortBinInterval(realvec& interval, realvec& index, realvec& mag)
{
	mrs_natural k=0, start=0, nbP=index.getSize();
	mrs_natural minIndex = 0;

	// getting rid of padding zeros
	while(start<index.getSize() && !index(start))
		start++;

	for(mrs_natural i=start ; i<nbP ; ++i, ++k)
	{
		minIndex = 0;
		// look for the next valley location upward
		for (mrs_natural j= (mrs_natural)index(i) ; j<mag.getSize()-1 ; ++j)
		{
			if(mag(j) < mag(j+1))
			{
				minIndex = j;
				break;
			}
		}
// 		if(!minIndex) //arght!!! I hate using logic with integers!!! Makes code so difficult to read!!! [!]
// 		{
// 			cout << "pb while looking for bin intervals" << endl; //[?]
// 		}

		interval(2*k+1) = minIndex;

		// look for the next valley location downward
		for (mrs_natural j= (mrs_natural)index(i) ; j>1 ; --j)
		{
			if(mag(j) < mag(j-1))
			{
				minIndex = j;
				break;
			}
		}
// 		if(!minIndex) //arght!!! I hate using logic with integers!!! Makes code so difficult to read!!! [!]
// 		{
// 			cout << "pb while looking for bin intervals" << endl; //[?]
// 		}
		interval(2*k) = minIndex;
	}
}
Ejemplo n.º 7
0
void
WekaData::NormMaxMinRow(realvec& in)
{
  int ii;
  for(ii=0; ii<(int)in.getSize()-1; ++ii)
  {
    in(ii) =  (in(ii) - minimums_(ii)) / (maximums_(ii) - minimums_(ii));
  }
}
bool
MarxGLMultiBufferGraph::setBuffer( realvec& rv )
{
    if ( rv.getSize() == buffersize ) { 
     buffer = rv;
     updateGL();
      
    return true;
  }
  return false;
}
/*!
  Update the realvec buffer and redraw the widget.  Returns false if
  the realvec argument size and the buffersize of this object differ.

  \param realvec& the new buffer of data
  \return bool indicates if the buffer was updated
*/
bool 
MarxGL2Din3DSpaceGraph::setBuffer( realvec& rv )
{
  if ( rv.getSize() == buffersize ) { 
     *buffer = rv;

      updateGL();

    return true;
  }
  return false;
}
Ejemplo n.º 10
0
void
HWPS::discretize(const realvec& peakSetWrapFreqs, const realvec& peakAmps,
                 const mrs_natural& histSize, realvec& resultHistogram)
{
  mrs_natural index;

  resultHistogram.create(histSize);

  for (mrs_natural i=0 ; i<peakSetWrapFreqs.getSize() ; ++i)
  {
    index = (mrs_natural) fmod(floor(peakSetWrapFreqs(i)*histSize+.5), histSize);
    resultHistogram(index) += peakAmps(i);
  }
}
Ejemplo n.º 11
0
mrs_real
Metric2::logLikelihood(const realvec& Vi, const realvec& Vj, const realvec& covMatrix)
{
    mrs_real res1;
    mrs_real res = 0;
    mrs_real log2pi = log(2.0*PI);
    mrs_real logDet;
    if(covMatrix.getSize() == 0)
    {
        for(mrs_natural r=0; r<Vi.getSize(); ++r)
        {
            res1 = Vi(r)-Vj(r);
            res1 *= res1;
            res += res1;
        }
        res = 0.5*res + 0.5*Vi.getSize()*log2pi;
    }
    else
    {
        //logDet = log(covMatrix.det());
        logDet = 0;
        for(mrs_natural r=0; r<Vi.getSize(); ++r)
        {
            res1 = Vi(r)-Vj(r);
            res1 *= res1;
            if(covMatrix(r,r)>0)
            {
                res1 /= covMatrix(r,r);
                logDet += log(covMatrix(r,r));
            }
            res += res1;
        }
        res = 0.5*res + 0.5*(Vi.getSize()*log2pi+logDet);
    }
    return res;
}
Ejemplo n.º 12
0
void
TimeLine::segment(realvec segmentation, mrs_natural lineSize)
{
  mrs_natural i;
  mrs_natural peakCount=0;

  if (size_ != 0)
  {
    MRSERR("TimeLine::scan() - TimeLine has data already!");
    return;
  }

  size_ = segmentation.getSize();
  for (i=0; i<size_; ++i)
  {
    if (segmentation(i) == 1)
      peakCount++;
  }

  numRegions_ = peakCount-1; //[?]
  lineSize_ = lineSize;

  for (i=0; i < numRegions_; ++i)
  {
    TimeRegion region;
    regions_.push_back(region);
  }

  mrs_natural reg_index = 0;
  for (i=0; i<size_; ++i)
  {
    if (segmentation(i) == 1) //[?]
    {
      if (reg_index > 0)
        regions_[reg_index-1].end = i;
      if (reg_index == peakCount -1)
        break;
      regions_[reg_index].start = i;
      regions_[reg_index].classId = 0;
      reg_index++;
    }
  }
}
Ejemplo n.º 13
0
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_);
}
Ejemplo n.º 14
0
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);
	//      }
	//   }
}
Ejemplo n.º 15
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;
}
Ejemplo n.º 16
0
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; 
				}
	}
}