void SpectralODFCOG::process(const MatrixXC& fft, MatrixXR* odfValue) { LOUDIA_DEBUG("SPECTRALODFCOG: Processing windowed"); const int rows = fft.rows(); (*odfValue).resize(rows, 1); LOUDIA_DEBUG("SPECTRALODFCOG: Processing the peaks"); _peaker.process(fft.array().abs(), &_peakStarts, &_peakPos, &_peakEnds, &_peakMag); _peakCoger.process(fft, _peakPos, &_cog); (*odfValue) = _cog.array().clipUnder().rowwise().sum(); LOUDIA_DEBUG("SPECTRALODFCOG: Finished Processing"); }
void SpectralODFPhase::phaseDeviation(const MatrixXC& spectrum, const MatrixXR& spectrumArg, MatrixXR* odfValue) { const int rows = spectrum.rows(); const int cols = spectrum.cols(); _phaseDiff = spectrumArg.block(1, 0, rows - 1, cols) - spectrumArg.block(0, 0, rows - 1, cols); _instFreq = _phaseDiff.block(1, 0, rows - 2, cols) - _phaseDiff.block(0, 0, rows - 2, cols); if (_weighted) _instFreq.array() *= spectrum.block(2, 0, rows - 2, cols).array().abs(); if (_normalize) { (*odfValue) = _instFreq.rowwise().sum().array() / (cols * spectrum.block(2, 0, rows - 2, cols).array().abs().rowwise().sum()); return; } (*odfValue) = _instFreq.rowwise().sum() / cols; return; }
void SpectralODFPhase::process(const MatrixXC& fft, MatrixXR* odfValue) { LOUDIA_DEBUG("SPECTRALODFPHASE: Processing windowed"); const int rows = fft.rows(); if ( rows < 3 ) { // Throw ValueError, it must have a minimum of 3 rows } (*odfValue).resize(rows - 2, 1); _unwrap.process(fft.array().angle(), &_unwrappedAngle); LOUDIA_DEBUG("SPECTRALODFPHASE: Processing unwrapped"); phaseDeviation(fft, _unwrappedAngle, odfValue); LOUDIA_DEBUG("SPECTRALODFPHASE: Finished Processing"); }
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"); }
void BandFilter::setup(){ LOUDIA_DEBUG("BANDFILTER: Setting up..."); _filter.setChannelCount( _channelCount, false ); LOUDIA_DEBUG("BANDFILTER: Getting zpk"); // Get the lowpass z, p, k MatrixXC zeros, poles; Real gain; switch( _filterType ){ case CHEBYSHEVI: chebyshev1(_order, _passRipple, _channelCount, &zeros, &poles, &gain); break; case CHEBYSHEVII: chebyshev2(_order, _stopAttenuation, _channelCount, &zeros, &poles, &gain); break; case BUTTERWORTH: butterworth(_order, _channelCount, &zeros, &poles, &gain); break; case BESSEL: bessel(_order, _channelCount, &zeros, &poles, &gain); break; } LOUDIA_DEBUG("BANDFILTER: zeros:" << zeros ); LOUDIA_DEBUG("BANDFILTER: poles:" << poles ); LOUDIA_DEBUG("BANDFILTER: gain:" << gain ); // Convert zpk to ab coeffs MatrixXC a; MatrixXC b; zpkToCoeffs(zeros, poles, gain, &b, &a); LOUDIA_DEBUG("BANDFILTER: Calculated the coeffs"); // Since we cannot create matrices of Nx0 // we have created at least one Zero in 0 if ( zeros == MatrixXC::Zero(zeros.rows(), zeros.cols()) ){ // Now we must remove the last coefficient from b MatrixXC temp = b.block(0, 0, b.rows(), b.cols()-1); b = temp; } // Get the warped critical frequency Real fs = 2.0; Real warped = 2.0 * fs * tan( M_PI * _lowFrequency / fs ); Real warpedStop = 2.0 * fs * tan( M_PI * _highFrequency / fs ); Real warpedCenter = sqrt(warped * warpedStop); Real warpedBandwidth = warpedStop - warped; // Warpped coeffs MatrixXC wa; MatrixXC wb; LOUDIA_DEBUG("BANDFILTER: Create the band type filter from the analog prototype"); switch( _bandType ){ case LOWPASS: lowPassToLowPass(b, a, warped, &wb, &wa); break; case HIGHPASS: lowPassToHighPass(b, a, warped, &wb, &wa); break; case BANDPASS: lowPassToBandPass(b, a, warpedCenter, warpedBandwidth, &wb, &wa); break; case BANDSTOP: lowPassToBandStop(b, a, warpedCenter, warpedBandwidth, &wb, &wa); break; } LOUDIA_DEBUG("BANDFILTER: Calculated the low pass to band pass"); // Digital coeffs MatrixXR da; MatrixXR db; bilinear(wb, wa, fs, &db, &da); LOUDIA_DEBUG("BANDFILTER: setup the coeffs"); // Set the coefficients to the filter _filter.setA( da.transpose() ); _filter.setB( db.transpose() ); _filter.setup(); LOUDIA_DEBUG("BANDFILTER: Finished set up..."); }
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"); }