# Baseline correlation for template matching

In normal cross-correlation or convolution between a signal and a template(say a small portion of the signal) the template is moved along length of the signal without changing the amplitude offset of the template. But is there any function (in MATLAB or general) that alters the amplitude offset of the template according to the changes in the amplitude of the signal during cross-correlation or convolution?

For example, consider the following ECG signal which is has a large amount of baseline wander due to motion artifacts. The sharp peaks and trough are the heartbeat signals. The beat signal contained in the red box is taken as a template to perform cross-correlation with the full ECG signal.

Due to the large baseline wander, after doing normal cross-correlation, the heartbeat peaks are getting de-emphasized.But, there is strong possibility that the heart-beats will be emphasized if the amplitude offset(offset along y axis) of the template follows the signal amplitude during correlation. In other words, the template must be more or less alighted with the heart-beat in both time and amplitude during convolution or xcross operation. I hope the above illustration adds clarity to the question and the requirement. If there is any alternate method for template matching that will give better results for the above scenario, please do suggest the same.

• On the hindsight, it appears that it is not a good idea to do a baseline-following correlation. Any alternatives for template matching? Commented Jun 9, 2015 at 20:33

This would be a cumbersome way to detect heart beats (or the QRS complex), if that is what you are trying to do ultimately.

A little bit about what you are trying to do currently:

Your observations are correct and to these I would like to add that no two heart beats are the same and therefore, strictly speaking, your template will be aligning just with similar heart beats. What if the subject started running in the mean time? That would be quite a different heart beat template to compare against.

If you absolutely must use a template matching technique and apply a hard threshold on the peaks of the cross correlation, here is a set of points that might help you:

1. Apply a high pass filter in the region of 0-5Hz. This will remove the slow varying components and what you observe as "drift".

2. Select a number of heart beats, at random, from the filtered signal and create an average "beat". Use this as your template.