In this paper, an elecrocardiogram (ECG) compression algorithm, called analysis by synthesis ECG compressor (ASEC), is introduced. The ASEC algorithm is based on analysis by synthesis coding, and consists of a beat codebook, long and short-term predictors, and an adaptive residual quantizer. The compression algorithm uses a defined distortion measure in order to efficiently encode every heartbeat, with minimum bit rate, while maintaining a predetermined distortion level. The compression algorithm was implemented and tested with both the percentage rms difference (PRD) measure and the recently introduced weighted diagnostic distortion (WDD) measure. The compression algorithm has been evaluated with the MIT-BIH Arrhythmia Database. A mean compression rate of approximately 100 bits/s (compression ratio of about 30:1) has been achieved with a good reconstructed signal quality (WDD below 4% and PRD below 8%). The ASEC was compared with several well-known ECG compression algorithms and was found to be superior at all tested bit rates. A mean opinion score (MOS) test was also applied. The testers were three independent expert cardiologists. As in the quantitative test, the proposed compression algorithm was found to be superior to the other tested compression algorithms.