I have two signals of the same length that are fairly identical to each other as shown in the image below. Each signal can be named s1 and s2 respectively. I'm looking to time-align these two signals in such a way that s1 and s2 are out of phase with each other so that when they are added together, they have the greatest possible cancelation.
I've used the following code to time-align these signals,
[acor,lag] = xcorr(s2,s1);
[~,I] = max(abs(acor));
lagDiff = lag(I);
however, when I shift s1 in time using the value of lagDiff, s1 and s2 are not out-of-phase like I would like them to be. This is to be expected with my current code so I know that isn't the real problem. I've used for-loops in the past to try and get around this, but I've found they take up too much computation time and can be somewhat cumbersome with a high number of samples in a signal.
If there was a quicker way to more closely match the amplitudes of s1 and s2 than by using a for-loop, that would be great too.