Signal detection of primary users for cognitive radios enables spectrum use agility. In normal operation conditions, the sensed spectrum is nonflat, i.e., the power spectrum is not constant. A novel method proposes the segmentation of the measured spectra into regions where the flatness condition is approximately valid. As a result, an automatic detection of the significant spectral components together with an estimate of the magnitude of the spectral component and a measure of the quality of classification becomes available. In this paper, we optimize the methodology for signal detection in cognitive radios such that the probability that a spectral component was incorrectly classified is iteratively reduced. Simulation and measurement results show the advantages of the presented technique in different types of spectra.