1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
function varargout = alignsignals(x,y,varargin)
//This function aligns the two input signals.
//
//Calling Sequence
//[Xa Ya] = ALIGNSIGNALS(X,Y)
//[Xa Ya] = ALIGNSIGNALS(X,Y,MAXLAG)
//[Xa Ya] = ALIGNSIGNALS(X,Y,MAXLAG,1)
//[Xa Ya D] = ALIGNSIGNALS(...)
//
//Description
//[Xa Ya] = ALIGNSIGNALS(X,Y) aligns the two vectors X and Y by estimating
//the delay D between the two. If Y is delayed with respect to X, D is
// positive , and X is delayed by D samples. If Y is advanced with respect
// to X, D is negative, and Y is delayed by -D samples.
//
// [Xa Ya] = ALIGNSIGNALS(X,Y,MAXLAG) considers MAXLAG be the maximum correlation
// window size which is used to calculate the estimated delay D between X and Y.
// MAXLAG is an integer-valued scalar. By default, MAXLAG is equal to MAX(LX,LY)-1.
// If MAXLAG is empty ([]),then default value is considered. If MAXLAG
// is negative, it is replaced by its absolute value.
//
// [Xa Ya] = ALIGNSIGNALS(X,Y,MAXLAG,1) keeps the lengths of Xa
// and Ya the same as those of X and Y, respectively.
// Here, 1 implies truncation of the intermediate vectors.
// Input argument 4 is 0 implies truncation_off (no truncation).
// D is positive implies D zeros are pre-pended to X, and the last D samples of X are truncated.
// D is negative implies -D zeros are pre-pended to Y, and the last -D samples
// of Y are truncated. That means, when D>=Length(X), all samples of X are lost.
// Similarly, when -D>=Length(Y), all samples of Y are lost.
// Avoid assigning a specific value to MAXLAG when using the truncate=1 option, set MAXLAG to [].
//
// [Xa Ya D] = ALIGNSIGNALS(...) returns the estimated delay D.
//
// Examples
// X = [0 0 0 1 2 3 ];
// Y = [1 2 3 ];
// [Xa,Ya] = alignsignals(X,Y,[],1)
//
// See also
// finddelay
//
// Authors
// Pola Lakshmi Priyanka, IIT Bombay//
// Check number of input arguments
[out_a,inp_a]=argn(0)
if inp_a<=1 | inp_a>4 then
error('comm:alignsignals: Invalid number of input arguments')
end
if out_a>3 then
error('comm:alignsignals: Invalid number of output arguments')
end
//Check input arguments
if (~or(type(x)==[1 5 8]) | ~isvector(x) )
error('comm:alignsignals:Input argument 1 should be a vector of numbers');
end
[row_x,col_x] = size(x);
len_x = length(x);
if (~or(type(y)==[1 5 8]) | ~isvector(y) )
error('comm:alignsignals:Input argument 2 should be a vector of numbers');
end
[row_y,col_y] = size(y);
len_y = length(y);
//Check for MaxLag
if inp_a==3 then
maxlag = varargin(1)
else
maxlag = max(len_x,len_y)-1; //Default value
end
if ~isempty(maxlag) then
if ( ~or(type(maxlag)==[1 5 8]) | ~isreal(maxlag) | length(maxlag)~=1 | ceil(maxlag)~=maxlag),
error('comm:alignsignals:Input argument 3 should be a scalar integer');
elseif (( isnan(maxlag)) | isinf(maxlag)),
error('comm:alignsignals:Input argument 3 can not be Inf or NAN');
end
else
maxlag = maxlag_default;
end
maxlag = double(abs(maxlag));
//Check for truncate
trunc_on=0;
if inp_a==4
if (varargin(2))
trunc_on=1;
end
end
// Estimate delay between X and Y.
if inp_a==2
d = finddelay(x,y);
else
d = finddelay(x,y,maxlag);
end
if d == 0 // X and Y are already aligned.
varargout(1) = x;
varargout(2) = y;
elseif d > 0 // Y is estimated to be delayed with respect to X.
if row_x>1 // X is entered as a column vector.
if trunc_on==0
varargout(1) = [zeros(d,1) ; x];
else
if d>=row_x
warning('comm:alignsignals:firstInputTruncated');
varargout(1) = zeros(row_x,1);
else
varargout(1) = [zeros(d,1) ; x(1:len_x-d)];
end
end
else // X is entered as a row vector.
if trunc_on==0
varargout(1) = [zeros(1,d) x];
else
if d>=col_x
warning('comm:alignsignals:firstInputTruncated');
varargout(1) = zeros(1,col_x);
else
varargout(1) = [zeros(1,d) x(1:len_x-d)];
end
end
end
varargout(2) = y;
else // X is estimated to be delayed with respect to Y.
varargout(1) = x;
if row_y>1 // Y is entered as a column vector.
if trunc_on==0
varargout(2) = [zeros(-d,1) ; y];
else
if (-d)>=row_y
warning('comm:alignsignals:secondInputTruncated');
varargout(2) = zeros(row_y,1);
else
varargout(2) = [zeros(-d,1) ; y(1:len_y-(-d))];
end
end
else // Y is entered as a row vector.
if trunc_on==0
varargout(2)= [zeros(1,-d) y];
else
if (-d)>=col_y
warning('comm:alignsignals:secondInputTruncated');
varargout(2) = zeros(1,col_y);
else
varargout(2) = [zeros(1,-d) y(1:len_y-(-d))];
end
end
end
end
varargout(3) = d;
endfunction
|