Gamma and X-ray energy spectra measured with semiconductor detectors suffer from various distortions, one of them being so-called “tailing” caused by an incomplete charge collection. Using the Hecht equation, a response matrix of size 321 × 321 was constructed which was used to correct the effect of incomplete charge collection. The correction matrix was constructed analytically for an arbitrary energy bin and the size of the energy bin thus defines the width of the spectral window. The correction matrix can be applied separately from other possible spectral corrections or it can be incorporated into an already existing response matrix of the detector. The correction was tested and its adjustable parameters were optimized on the line spectra of 57Co measured with a cadmium telluride (CdTe) detector in a spectral range from 0 up to 160keV. The best results were obtained when the values of the free path of holes were spread over a range from 0.4 to 1.0cm and weighted by a Gauss function. The model with the optimized parameter values was then used to correct the line spectra of 152Eu in a spectral range from 0 up to 530keV. An improvement in the energy resolution at full width at half maximum from 2.40 % ± 0.28 % to 0.96 % ± 0.28 % was achieved at 344.27keV. Spectra of “narrow spectrum series” beams, N120, N150, N200, N250 and N300, generated with tube voltages of 120kV, 150kV, 200kV, 250kV and 300kV respectively, and measured with the CdTe detector, were corrected in the spectral range from 0 to 160keV (N120 and N150) and from 0 to 530keV (N200, N250, N300). All the measured spectra correspond both qualitatively and quantitatively to the available reference data after the correction. To obtain better correspondence between N150, N200, N250 and N300 spectra and the reference data, lower values of the free paths of holes (range from 0.16 to 0.65cm) were used for X-ray spectra correction, which suggests energy dependence of the phenomenon.