Spectrum Spectrum::convolve(const Spectrum& other, double threshold) const { if (this->isConvolutionUnit()) return other; if (other.isConvolutionUnit()) return *this; const auto& p1 = *this, p2 = other; size_t n1 = p1.size(), n2 = p2.size(); assert(isNormalized(p1)); assert(isNormalized(p2)); Spectrum result; for (size_t i = 0; i < n1; i++) for (size_t j = 0; j < n2; j++) { auto abundance = p1.intensities[i] * p2.intensities[j]; if (abundance > threshold) { result.masses.push_back(p1.masses[i] + p2.masses[j]); result.intensities.push_back(abundance); } else { if (j == 0) break; n2 = j; } } return result.normalize(); }