In verification it is often useful to align the output of a module with some reference vector for comparison. This program does that automatically. It reads the two vectors, correlates them then aligns them.
It has high success rate provided the matched length is not too short relative to vector length
%"align_run" is top level for testing "align" function function align_run for i = 1:1 L1 = 3400; %length of first vector L2 = 4023; %length of second vector %generate two random vectors x1 = randn(1,L1); x2 = randn(1,L2); %generate random index values for two identical segments r1 = round(rand(1)*L1); r2 = round(rand(1)*L2); r1(r1 == 0) = 1; r2(r2 == 0) = 1; %length of identical segments as percent of vector length n = round(50/100 * min([L1 L2])); n(n >= min([L1 L2])) = min([L1 L2])-1; while r1+n > L1, r1 = r1-1; end; while r2+n > L2, r2 = r2-1; end; %create identical segments at random location x1(r1:r1+n) = x2(r2:r2+n); %call align function [y1,y2] = align(x1,x2); %check alignment subplot(2,1,1); plot(y1);hold plot(y2,'r--') ; legend('y1','y2') subplot(2,1,2); plot(y1 - y2,'g') legend('(y1 - y2)') end return %%%%%%%%%%%%%%%%% align %%%%%%%%%%%%%%%%%% function [y1,y2] = align(x1,x2) %force a column orientation x1 = x1(:); x2 = x2(:); L1 = length(x1); L2 = length(x2); L = max([L1 L2]); %equalise vector lengths if L1 > L2, x2 = [x2; zeros(L1-L2,1)]; end; if L1 < L2, x1 = [x1; zeros(L2-L1,1)]; end; %apply correlation and find index of maximum output y = xcorr(x1,x2); n = find(y == max(y)) ; n = mod(n,L)+1; %align x1 vector with x2 y1 = [x1(n:end); x1(1:n-1)] ; y2 = x2; return