Ejemplo n.º 1
0
void SpectralNoiseSuppression::process(const MatrixXR& spectrum, MatrixXR* noise, MatrixXR* result){
  const int rows = spectrum.rows();
  const int cols = spectrum.cols();

  (*result).resize(rows, cols);
  
  (*result) = spectrum;

  //DEBUG("SPECTRALNOISESUPPRESSION: Calculate wrapped magnitude.");
  // Calculate the wrapped magnitude of the spectrum
  _g = (1.0 / (_k1 - _k0 + 1.0) * spectrum.block(0, _k0, rows, _k1 - _k0).cwise().pow(1.0/3.0).rowwise().sum()).cwise().cube();

  //cout << (_g) << endl;
  
  for ( int i = 0; i < cols; i++ ) {
    (*result).col(i) = (((*result).col(i).cwise() * _g.cwise().inverse()).cwise() + 1.0).cwise().log();
  }
  
  //cout << (*result) << endl;
  
  //DEBUG("SPECTRALNOISESUPPRESSION: Estimate spectral noise.");
  // Estimate spectral noise
  _bands.process((*result), noise);
  
  //DEBUG("SPECTRALNOISESUPPRESSION: Suppress spectral noise.");
  // Suppress spectral noise
  (*result) = ((*result) - (*noise)).cwise().clipUnder();
}
Ejemplo n.º 2
0
void PeakCOG::process(const MatrixXC& fft, const MatrixXR& peakPos, MatrixXR* peakCog) {
  LOUDIA_DEBUG("PEAKCOG: Processing windowed");
  const int rows = fft.rows();
  const int cols = fft.cols();
  const int halfCols = min((int)ceil(_fftLength / 2.0), cols);
  const int peakCount = peakPos.cols();
  
  LOUDIA_DEBUG("PEAKCOG: fft.shape " << fft.rows() << "," << fft.cols());
  _spectrumAbs2 = fft.block(0, 0, rows, halfCols).cwise().abs2();
  LOUDIA_DEBUG("PEAKCOG: Spectrum resized rows: " << rows << " halfCols: " << halfCols);
    
  unwrap(fft.block(0, 0, rows, halfCols).cwise().angle(), &_spectrumArg);
  derivate(_spectrumArg, &_spectrumArgDeriv);
  
  (*peakCog).resize(rows, peakCount);
  (*peakCog).setZero();

  for(int row = 0; row < rows; row++) {
    for(int i = 0; i < peakCount; i++){     
      if (peakPos(row, i) != -1) {
        int start = max(0, (int)floor(peakPos(row, i) - _bandwidth / 2));
        int end = min(halfCols, (int)ceil(peakPos(row, i) + _bandwidth / 2));

        if ( (end - start) > 0) {
          (*peakCog)(row, i) = ((-_spectrumArgDeriv).block(row, start, 1, end-start).cwise() * _spectrumAbs2.block(row, start, 1, end-start)).sum() / _spectrumAbs2.block(row, start, 1, end-start).sum();
        }
      }
      
    }
  }
  
  LOUDIA_DEBUG("PEAKCOG: Finished Processing");
}
Ejemplo n.º 3
0
void Window::setWindow( const MatrixXR& window, bool callSetup ){
  if (window.cols() != _inputSize || window.rows() != 1) {
    // Throw exception wrong window size
  }

  setWindowType(CUSTOM, false);
  _window = window;

  if ( callSetup ) setup();
}
Ejemplo n.º 4
0
Real PitchSaliency::saliency(Real period, Real deltaPeriod, Real tLow, Real tUp, const MatrixXR& spectrum){
  const int cols = spectrum.cols();
  Real sum = 0.0;
  
  for ( int m = 1; m < _numHarmonics; m++ ) {
    
    int begin = (int)round(m * _fftSize / (period + (deltaPeriod / 2.0)));
    int end = min((int)round(m * _fftSize / (period - (deltaPeriod / 2.0))), cols - 1);

    if (begin < end) sum += harmonicWeight(period, tLow, tUp, m) * spectrum.block(0, begin, 1, end - begin).maxCoeff();
  }

  return sum;
}
Ejemplo n.º 5
0
void SpectralReassignment::setup(){
  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Setting up...");
  
  // Setup the window so it gets calculated and can be reused
  _windowAlgo.setup();
  
  // Create the time vector
  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Creating time vector...");
  Real timestep = 1.0 / _sampleRate;

  // The unit of the vectors is Time Sample fractions
  // So the difference between one coeff and the next is 1
  // and the center of the window must be 0, so even sized windows
  // will have the two center coeffs to -0.5 and 0.5
  // This should be a line going from [-(window_size - 1)/2 ... (window_size - 1)/2]
  _time.resize(_frameSize, 1);
  for(int i = 0; i < _time.rows(); i++){
    _time(i, 0) = (i - Real(_time.rows() - 1)/2.0);
  }
  
  // Create the freq vector
  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Creating freq vector...");
  
  // The unit of the vectors is Frequency Bin fractions
  // TODO: Must rethink how the frequency vector is initialized
  // as we did for the time vector
  _freq.resize(1, _fftSize);
  range(0, _fftSize, _fftSize, &_freq);
  
  // Calculate and set the time weighted window
  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Calculate time weighted window...");
  MatrixXR windowInteg = _windowAlgo.window();
  windowInteg = windowInteg.cwise() * _time.transpose();
  _windowIntegAlgo.setWindow(windowInteg);

  // Calculate and set the time derivated window
  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Calculate time derivative window...");
  MatrixXR windowDeriv = _windowAlgo.window();
  for(int i = windowDeriv.cols() - 1; i > 0; i--){
    windowDeriv(0, i) = (windowDeriv(0, i) - windowDeriv(0, i - 1)) / timestep;
  }

  // TODO: Check what is the initial condition for the window
  // Should this be 0 or just the value it was originally * dt
  //windowDeriv(0, 0) = 0.0;
  _windowDerivAlgo.setWindow(windowDeriv);

  // Create the necessary buffers for the windowing
  _window.resize(1, _frameSize);
  _windowInteg.resize(1, _frameSize);
  _windowDeriv.resize(1, _frameSize);

  // Create the necessary buffers for the FFT
  _fftAbs2.resize(1, _fftSize);
  _fftInteg.resize(1, _fftSize);
  _fftDeriv.resize(1, _fftSize);
  
  // Setup the algos
  _windowIntegAlgo.setup();
  _windowDerivAlgo.setup();
  _fftAlgo.setup();

  LOUDIA_DEBUG("SPECTRALREASSIGNMENT: Finished set up...");
}
Ejemplo n.º 6
0
void PeakInterpolationComplex::process(const MatrixXC& input,
                              const MatrixXR& peakPositions, const MatrixXR& peakMagnitudes, const MatrixXR& peakPhases,
                              MatrixXR* peakPositionsInterp, MatrixXR* peakMagnitudesInterp, MatrixXR* peakPhasesInterp) {
  
  LOUDIA_DEBUG("PEAKINTERPOLATIONCOMPLEX: Processing");  
  Real leftMag, leftPhase;
  Real rightMag, rightPhase;
  Real mag, interpFactor;
  
  (*peakPositionsInterp).resize(input.rows(), peakPositions.cols());
  (*peakMagnitudesInterp).resize(input.rows(), peakPositions.cols());
  (*peakPhasesInterp).resize(input.rows(), peakPositions.cols());
  
  _magnitudes = input.cwise().abs();
  unwrap(input.cwise().angle(), &_phases);
  
  for ( int row = 0 ; row < _magnitudes.rows(); row++ ) {
  
    for ( int i = 0; i < peakPositions.cols(); i++ ) {
      
      // If the position is -1 do nothing since it means it is nothing
      if( peakPositions(row, i) == -1 ){

        (*peakMagnitudesInterp)(row, i) = peakMagnitudes(row, i);         
        (*peakPhasesInterp)(row, i) = peakPhases(row, i); 
        (*peakPositionsInterp)(row, i) = peakPositions(row, i);
        
      } else {
        
        // Take the center magnitude in dB
        mag = 20.0 * log10( peakMagnitudes(row, i) );

        // Take the left magnitude in dB
        if( peakPositions(row, i) <= 0 ){
          
          leftMag = 20.0 * log10( _magnitudes(row, (int)peakPositions(row, i) + 1) );
          
        } else {
          
          leftMag = 20.0 * log10( _magnitudes(row, (int)peakPositions(row, i) - 1) );
          
        }
        
        // Take the right magnitude in dB
        if( peakPositions(row, i) >= _magnitudes.row(row).cols() - 1 ){
          
          rightMag = 20.0 * log10( _magnitudes(row, (int)peakPositions(row, i) - 1) );
          
        } else {
          
          rightMag = 20.0 * log10( _magnitudes(row, (int)peakPositions(row, i) + 1) );
          
        }
                
        // Calculate the interpolated position
        (*peakPositionsInterp)(row, i) = peakPositions(row, i) + 0.5 * (leftMag - rightMag) / (leftMag - 2.0 * mag + rightMag);

        interpFactor = ((*peakPositionsInterp)(row, i) - peakPositions(row, i));

        // Calculate the interpolated magnitude in dB
        (*peakMagnitudesInterp)(row, i) = mag - 0.25 * (leftMag - rightMag) * interpFactor;

        // Calculate the interpolated phase
        leftPhase = _phases(row, (int)floor((*peakPositionsInterp)(row, i)));
        rightPhase = _phases(row, (int)floor((*peakPositionsInterp)(row, i)) + 1);
        
        interpFactor = (interpFactor >= 0) ? interpFactor : interpFactor + 1;
        
        (*peakPhasesInterp)(row, i) = (leftPhase + interpFactor * (rightPhase - leftPhase));
      }
    }
  }

  // Calculate the princarg() of the phase: remap to (-pi pi]
  (*peakPhasesInterp) = ((*peakPhasesInterp).cwise() != -1).select(((*peakPhasesInterp).cwise() + M_PI).cwise().modN(-2.0 * M_PI).cwise() + M_PI, (*peakPhasesInterp));
  
  LOUDIA_DEBUG("PEAKINTERPOLATIONCOMPLEX: Finished Processing");
}