1
<?xml version="1.0" encoding="UTF-8"?>
2
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" version="5.0-subset Scilab" xml:lang="en" xml:id="corr">
4
<refname>corr</refname>
5
<refpurpose> correlation, covariance</refpurpose>
8
<title>Calling Sequence</title>
9
<synopsis>[cov,Mean]=corr(x,[y],nlags)
10
[cov,Mean]=corr('fft',xmacro,[ymacro],n,sect)
12
[w,xu]=corr('updt',x1,[y1],w0)
13
[w,xu]=corr('updt',x2,[y2],w,xu)
15
[wk]=corr('updt',xk,[yk],w,xu)
19
<title>Arguments</title>
24
<para>a real vector</para>
30
<para>a real vector, default value x.</para>
36
<para>integer, number of correlation coefficients desired.</para>
42
<para>a scilab external (see below).</para>
48
<para>a scilab external (see below), default value xmacro</para>
54
<para>an integer, total size of the sequence (see below).</para>
60
<para>size of sections of the sequence (see below).</para>
66
<para>a real vector</para>
72
<para>a real vector,default value xi.</para>
78
<para>real vector, the correlation coefficients</para>
84
<para>real number or vector, the mean of x and if given y</para>
90
<title>Description</title>
94
<programlisting role=""><![CDATA[
98
cov(m) = > (x(k) - xmean) (y(m+k) - ymean) * ---
104
for m=0,..,<literal>nlag-1</literal> and two vectors <literal>x=[x(1),..,x(n)]</literal>
105
<literal>y=[y(1),..,y(n)]</literal>
108
Note that if x and y sequences are differents corr(x,y,...) is
109
different with corr(y,x,...)
113
<term>Short sequences</term>
116
<literal>[cov,Mean]=corr(x,[y],nlags)</literal> returns the first nlags
117
correlation coefficients and Mean = <literal>mean(x)</literal>
118
(mean of <literal>[x,y]</literal> if <literal>y</literal> is an argument).
119
The sequence <literal>x</literal> (resp. <literal>y</literal>) is assumed real, and <literal>x</literal>
120
and <literal>y</literal> are of same dimension n.
125
<term>Long sequences</term>
128
<literal>[cov,Mean]=corr('fft',xmacro,[ymacro],n,sect)</literal> Here <literal>xmacro</literal> is either
133
a function of type <literal>[xx]=xmacro(sect,istart)</literal> which
134
returns a vector <literal>xx</literal> of dimension
135
<literal>nsect</literal> containing the part of the sequence with
136
indices from <literal>istart</literal> to
137
<literal>istart+sect-1</literal>.
142
a fortran subroutine or C procedure which performs the same
143
calculation. (See the source code of <literal>dgetx</literal> for an
144
example). <literal>n</literal> = total size of the
145
sequence. <literal>sect</literal> = size of sections of the
146
sequence. <literal>sect</literal> must be a power of
147
2. <literal>cov</literal> has dimension
148
<literal>sect</literal>. Calculation is performed by FFT.
155
<term>Updating method</term>
157
<programlisting role=""><![CDATA[
158
[w,xu]=corr('updt',x1,[y1],w0)
159
[w,xu]=corr('updt',x2,[y2],w,xu)
161
wk=corr('updt',xk,[yk],w,xu)
164
With this calling sequence the calculation is updated at each
165
call to <literal>corr</literal>.
167
<programlisting role=""><![CDATA[
168
w0 = 0*ones(1,2*nlags);
172
<literal>x1,x2,...</literal> are parts of <literal>x</literal> such that
173
<literal>x=[x1,x2,...]</literal> and sizes of <literal>xi</literal> a power of
174
2. To get <literal>nlags</literal> coefficients a final fft must be
175
performed <literal>c=fft(w,1)/n</literal>; <literal>cov=c(1nlags)</literal>
176
(<literal>n</literal> is the size of <literal>x (y)</literal>). Caution: this
177
calling sequence assumes that <literal>xmean = ymean = 0</literal>.
184
<title>Examples</title>
185
<programlisting role="example"><![CDATA[
186
x=%pi/10:%pi/10:102.4*%pi;
187
rand('seed');rand('normal');
188
y=[.8*sin(x)+.8*sin(2*x)+rand(x);.8*sin(x)+.8*sin(1.99*x)+rand(x)];
190
for j=1:2,for k=1:2,c=[c;corr(y(k,:),y(j,:),64)];end;end;
191
c=matrix(c,2,128);cov=[];
192
for j=1:64,cov=[cov;c(:,(j-1)*2+1:2*j)];end;
195
rand('normal');x=rand(1,256);y=-x;
196
deff('[z]=xx(inc,is)','z=x(is:is+inc-1)');
197
deff('[z]=yy(inc,is)','z=y(is:is+inc-1)');
198
[c,mxy]=corr(x,y,32);
199
x=x-mxy(1)*ones(x);y=y-mxy(2)*ones(y); //centring
200
c1=corr(x,y,32);c2=corr(x,32);
202
[c3,m3]=corr('fft',xx,yy,256,32);
204
[c4,m4]=corr('fft',xx,256,32);
205
norm(m3,1),norm(m4,1)
206
norm(c3-c1,1),norm(c4-c2,1)
207
x1=x(1:128);x2=x(129:256);
208
y1=y(1:128);y2=y(129:256);
209
w0=0*ones(1:64); //32 coeffs
210
[w1,xu]=corr('u',x1,y1,w0);w2=corr('u',x2,y2,w1,xu);
211
zz=real(fft(w2,1))/256;c5=zz(1:32);
213
[w1,xu]=corr('u',x1,w0);w2=corr('u',x2,w1,xu);
214
zz=real(fft(w2,1))/256;c6=zz(1:32);
218
// test for Fortran or C external
220
deff('[y]=xmacro(sec,ist)','y=sin(ist:(ist+sec-1))');
222
[cc1,mm1]=corr(x,2^3);
223
[cc,mm]=corr('fft',xmacro,100,2^3);
224
[cc2,mm2]=corr('fft','corexx',100,2^3);
225
[max(abs(cc-cc1)),max(abs(mm-mm1)),max(abs(cc-cc2)),max(abs(mm-mm2))]
227
deff('[y]=ymacro(sec,ist)','y=cos(ist:(ist+sec-1))');
229
[cc1,mm1]=corr(x,y,2^3);
230
[cc,mm]=corr('fft',xmacro,ymacro,100,2^3);
231
[cc2,mm2]=corr('fft','corexx','corexy',100,2^3);
232
[max(abs(cc-cc1)),max(abs(mm-mm1)),max(abs(cc-cc2)),max(abs(mm-mm2))]
235
<refsection role="see also">
236
<title>See Also</title>
237
<simplelist type="inline">
239
<link linkend="fft">fft</link>