# An Efficient Method to Learn Overcomplete Multi-Scale Dictionaries of ECG Signals
## 3 Multi-Scale Dictionary Derivation
### Pre-Processing
#### 3.2.2 Resampling and Averaging  
To compute an average QRS complex for each individual, we need to work with QRS complexes that have a fixed length. (Note that extracting a single waveform per patient can be a limitation. However, since the recordings used in this work correspond to healthy patients and are rather short (less than 2 min), a single waveform is often enough to represent the average QRS complex for each patient. Developing an efficient method to extract multiple waveforms from each patient is a challenging issue that will be considered in future works.) The easiest solution to achieve this goal is resampling the extracted QRS complexes to the maximum length for each patient, \(L_{\max}^{q}=\max_{1\leq i\leq P_{q}}L_{q,i}\leq L_{\max}=\max_{1\leq q\leq Q^{ \prime}}L_{\max}^{q}=124\) samples. A change in the sampling rate of discrete time signals can be accomplished by means of interpolation and decimation [33]. If the \(i\)th QRS complex (\(i=1,\ldots,P_{q}\)) has a length \(L_{q,i}\leq L_{\max}^{q}\) samples and \(M_{q,i}=\text{GCD}(L_{q,i},L_{\max}^{q})\), with the Greatest Common Divisor (GCD) being the largest positive integer that divides each of the two integer numbers, then we need first to interpolate by a factor \(L_{\max}^{q}/M_{q,i}\) and then to decimate by a factor \(L_{q,i}/M_{q,i}\) (i.e., the fractional resampling rate is \(L_{\max}^{q}/L_{q,i}\geq 1\)). The aforementioned approach includes a digital lowpass antialiasing filter between the interpolator and the decimator, with a cut-off frequency \(\omega_{c}^{q,i}=\pi M_{q,i}/L_{\max}^{q}\) rad, which assumes that the sequence to process starts and ends with sequences of zeroes. Although the recorded ECGs have been initially bandpass filtered to remove baseline wander and other artifacts, the QRS complexes cannot be assumed to start and end with the required zero samples. In fact, the set of available QRS complexes (from the QRS onset to the QRS offset) always contain negative starting and ending values: from \(-0.0219\) to \(-0.2349\) mV for the initial sample, and from \(-0.0318\) to \(-0.1796\) mV for the final sample. As the starting and ending samples of all the QRS complexes are not equal to zero, resampling produces an undesired effect (_edge effect_), which consists on deviations from the expected values in the starting and ending values of the resampled signals. To remove the edge effect, we propose the following simple and effective approach:  
1. From the \(i\)th QRS complex signal, \(y_{q,i}[n]\) for \(n=0,1,\ldots,L_{q,i}-1\), we first constructed the following two sequences: \[y_{\ell}^{(q,i)}[n] =y_{q,i}[n]-y_{q,i}[0],\] \[y_{r}^{(q,i)}[n] =y_{q,i}[n]-y_{q,i}[L_{q,i}-1],\] which are not likely to be affected by the edge effect on their leftmost and rightmost samples, respectively. 2. We performed the resampling by the factor \(L_{q,i}/L_{\max}^{i}\) separately on \(y_{\ell}^{(q,i)}[n]\) and \(y_{r}^{(q,i)}[n]\), obtaining two resampled sequences \(\hat{y}_{\ell}^{(q,i)}[n]\) and \(\hat{y}_{r}^{(q,i)}[n]\). 3. The desired resampled sequence is finally given by \[\hat{y}_{q,i}[n] = \begin{cases}\hat{y}_{\ell}^{(q,i)}[n],&0\leq n\leq\left\lfloor \frac{N}{2}\frac{L_{\max}^{q}}{L_{q,i}}\right\rfloor-1;\\ \hat{y}_{r}^{(q,i)}[n],&\left\lfloor\frac{N}{2}\frac{L_{\max}^{q}}{L_{q,i}} \right\rfloor\leq n\leq\left\lfloor N\frac{L_{\max}^{q}}{L_{q,i}}\right\rfloor -1.\end{cases} \] (6)Figure 3 shows the leftmost and rightmost samples corresponding to one of the resampled QRS complexes (from patient 214 in the PTB database) when resampling is performed directly on \(y_{q,i}[n]\). The edge effect on the left and right parts of the signals (i.e., the deviation of the red line with respect to the desired values indicated by the black dots) is evident in this case.