MATLAB: Signal in and signal out need to be shifted to match one another…

allignment of datafast offsetting

Hello All, Well I have a signal in and a signal out, I would like to very quickly overlap(as best as possible) them. I just need to shift one to match (as best as possible) the x axes of the other. This could be just shifting one to match the other. I've tried to do this but seem to not be doing well at shifting them (not always finding the correct shifting value…). I also am doing this at higher frequency and am not doing this fast enough to meet the demand of the frequency were running the signal analysis program. Even a little pick up in the time it takes to do this will mean better data. The code I wrote is an example of the type of data coming in and out and how I handle it.
Any ideas or thoughts or comments that may help?
Thank You!

data = [0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 1;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0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0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 1;
0 0;
0 0;
0 0;
0 3;

Best Answer

Don't know that it'll help your run time unless you can minimize the lags over which you run it, but the basic way to overlay the two is...
c=xcorr(data(:,1),data(:,2),'coeff'); % xcorr for the two vectors
x3=[ceil(length(c)/2)+1:length(c)]'; % zero-th lag is in middle
[~,ic]=max(c(x3)); % max/location in positive lags
y=circshift(data(:,2),ic); % rotate second by that much
plot([1:length(data)]',[data y]) % show the results on the data
legend('1','2','shift') % identify who's who...
ADDENDUM 1: Certainly if the data as given are representative, one way to speed it up would be to eliminate the data where both are zero or even at/below the threshold level that you've set. Whether you need to keep those pre- and post-trigger lengths around to adjust the overall sample length in the end depends on the application, of course.
ALTERNATIVE: Again on the presumption the data are representative; w/o the cross-correlation bottleneck. This does depend upon the rise/fall times being as clean as are here and that one can find a suitable threshold for each pair expeditiously...
It needs must be above the early noise into the fast-rise/fall area and yet must not intersect the lower middle level of the lower=magnitude signal. Also it must be a value that isn't in the dataset to make the subsequent test robust. Since it appears that the data are integer-valued, one can assure that by using a fractional value as the threshold.
th=30.5; % see above notes
d=diff(sign(data-th));
% look for crossings and find them; look at result of sample case
id=[find(d(:,1)==2) find(d(:,2)==2);find(d(:,1)==-2) find(d(:,2)==-2)]
id =
610 437
715 542
shft=diff(id,[],2)
shft =
-173
-173
shft=floor(mean(diff(id,[],2))); % the actual shift averages
yd=circshift(data(:,2),abs(shft)); % will need the direction logic here
Quick, but more sensitive to noise by far...salt to suit! :)