MATLAB Answers

signal in and signal out need to be shifted to match one another...

4 views (last 30 days)
Chris E.
Chris E. on 6 Aug 2013
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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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;
0 0;
0 0;
0 0;
0 0;
0 1;
0 0;
0 0;
0 0;
0 3;
0 4;
0 7;
0 8;
0 9;
0 11;
0 12;
0 12;
0 13;
0 11;
0 19;
0 7;
0 12;
0 9;
0 9;
0 12;
0 13;
0 14;
0 5;
0 18;
0 14;
0 18;
0 16;
0 17;
0 29;
0 18;
0 36;
0 61;
0 77;
0 91;
0 101;
0 119;
0 128;
0 128;
0 128;
0 131;
0 151;
0 148;
0 148;
0 160;
0 163;
0 172;
0 152;
0 162;
0 148;
0 146;
0 142;
0 144;
0 138;
0 129;
0 130;
0 126;
0 131;
0 135;
0 127;
0 128;
0 127;
0 127;
0 116;
0 117;
0 124;
0 127;
0 123;
0 122;
0 115;
0 119;
0 124;
0 120;
0 117;
0 126;
0 122;
0 119;
0 123;
0 120;
0 122;
0 116;
0 119;
0 122;
0 120;
0 121;
0 121;
1 114;
0 114;
0 112;
0 111;
0 119;
0 124;
0 122;
0 118;
0 110;
0 113;
0 120;
0 119;
0 115;
0 121;
0 124;
0 125;
0 116;
0 118;
0 123;
0 115;
0 112;
0 118;
0 114;
0 102;
0 108;
0 100;
0 104;
0 96;
0 116;
0 111;
0 114;
0 109;
0 122;
0 129;
0 122;
0 99;
0 118;
0 122;
0 141;
0 115;
0 99;
0 97;
0 109;
0 112;
0 102;
0 89;
0 81;
0 70;
0 68;
0 47;
0 30;
0 24;
0 20;
0 8;
0 5;
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;
2 0;
0 0;
0 0;
0 0;
0 0;
1 0;
3 0;
11 0;
9 0;
18 0;
14 0;
14 0;
13 0;
14 0;
16 0;
21 0;
15 0;
15 0;
10 0;
10 0;
13 0;
18 0;
14 0;
19 0;
16 0;
13 0;
16 0;
17 0;
19 0;
18 0;
22 0;
33 0;
40 0;
63 0;
65 0;
60 0;
65 0;
62 0;
52 0;
57 0;
58 0;
60 0;
67 0;
67 0;
64 0;
58 0;
57 0;
54 0;
53 0;
56 0;
55 0;
54 0;
51 0;
39 0;
39 0;
44 0;
40 0;
40 0;
44 0;
38 0;
37 0;
36 0;
40 0;
42 0;
39 0;
44 0;
38 0;
39 0;
38 0;
35 0;
42 0;
38 0;
42 0;
41 0;
40 0;
40 0;
38 0;
38 0;
39 0;
41 0;
40 0;
36 0;
35 0;
39 0;
37 0;
38 0;
39 0;
37 0;
37 0;
37 0;
38 0;
40 0;
35 0;
36 0;
36 0;
37 0;
34 0;
38 0;
36 0;
37 0;
34 0;
36 0;
34 0;
33 0;
34 0;
34 0;
34 0;
34 0;
33 0;
34 0;
37 0;
35 0;
39 0;
40 0;
39 0;
37 0;
39 0;
37 0;
38 0;
38 0;
37 0;
34 0;
34 0;
37 0;
45 0;
53 0;
74 0;
89 0;
102 0;
101 0;
101 0;
78 0;
74 0;
63 0;
56 0;
51 0;
25 0;
19 0;
22 0;
15 0;
10 0;
2 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
0 0;
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]
%uncomment below to test "else" part of if...
% F = circshift(data(:,1),[-300 -300])
% data = [F data(:,2)];
plot(data)
p = data > 3;
u1 = min(find(p(:,1)));
u2 = min(find(p(:,2)));
if u1 > u2
d = u1-u2;
B = circshift(data(:,2),[d d]);
j = [data(:,1) B];
else
d = u2-u1;
B = circshift(data(:,1),[d d]);
j = [data(:,2) B];
end
plot(j)

Accepted Answer

dpb
dpb on 6 Aug 2013
Edited: dpb on 6 Aug 2013
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! :)
  7 Comments
dpb
dpb on 6 Aug 2013
Ok...thanks for the update.
OH, one more thought on speed w/ robustness--use method B) first, then A) on those two pairs -- should need at most three or for lags to tune in if it is off a little.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!