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.
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:


Apply a high pass filter in the region of 0-5Hz. This will remove the slow varying components and what you observe as "drift".
Select a number of heart beats, at random, from the filtered signal and create an average "beat". Use this as your template.


A little bit about how is this task usually done:

Picking up the heart beats from an ECG signal usually involves rectification of the signal and sliding window integration with the possible addition of some filtering to reduce the effect of artefacts. The "ideal" result of this is a time series that looks like a square pulse with the positive transition of the pulse almost aligned with the start of the QRS complex and the negative transition of the pulse aligned with the end of the QRS complex. But in practice, you might find that this signal is more "floating" due to noise and artefacts which means that you might also have to use an adaptive threshold technique to improve the detection performance.

For more information please see this link. For much more information, including signals, please see this link.

Hope this helps.