1 <?xml version="1.0" encoding="UTF-8"?>
2 <refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink"
3 xmlns:svg="http://www.w3.org/2000/svg" xmlns:mml="http://www.w3.org/1998/Math/MathML"
4 xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org"
5 xml:lang="en" xml:id="corr">
7 <refname>corr</refname>
8 <refpurpose>correlation, covariance</refpurpose>
13 [cov,Mean] = corr(x,[y],nlags)
14 [cov,Mean] = corr('fft',xmacro,[ymacro],n,sect)
16 [w,xu] = corr('updt',x1,[y1],w0)
17 [w,xu] = corr('updt',x2,[y2],w,xu)
19 wk = corr('updt',xk,[yk],w,xu)
23 <title>Arguments</title>
28 <para>a real vector</para>
34 <para>a real vector, default value x.</para>
40 <para>integer, number of correlation coefficients desired.</para>
46 <para>a scilab external (see below).</para>
52 <para>a scilab external (see below), default value xmacro</para>
58 <para>an integer, total size of the sequence (see below).</para>
64 <para>size of sections of the sequence (see below).</para>
70 <para>a real vector</para>
76 <para>a real vector,default value xi.</para>
82 <para>real vector, the correlation coefficients</para>
88 <para>real number or vector, the mean of x and if given y</para>
94 <title>Description</title>
96 <literal>corr(x,y,…)</literal> computes
97 <latex alt="cov(m)=sum_{k=1}^{n-m} (x(k)-mean(x))(y(m+k)-mean(y)) / n">
98 cov(m)=\sum_{k=1}^{n-m} \left(x(k)-mean(x)\right)\left(y(m+k)-mean(y)\right) / n
100 for <literal>m = 0, …, nlag-1</literal>.
103 Note that if x and y sequences are differents corr(x,y,...) is
104 different with corr(y,x,...)
108 <term>Short sequences</term>
111 <literal>[cov,Mean]=corr(x,[y],nlags)</literal> returns the first nlags
112 correlation coefficients and Mean = <literal>mean(x)</literal>
113 (mean of <literal>[x,y]</literal> if <literal>y</literal> is an argument).
114 The sequence <literal>x</literal> (resp. <literal>y</literal>) is assumed real, and <literal>x</literal>
115 and <literal>y</literal> are of same dimension n.
120 <term>Long sequences</term>
123 <literal>[cov,Mean]=corr('fft',xmacro,[ymacro],n,sect)</literal>.
124 Here <literal>xmacro</literal> is either
129 a function of type <literal>[xx]=xmacro(sect,istart)</literal> which
130 returns a vector <literal>xx</literal> of dimension
131 <literal>nsect</literal> containing the part of the sequence with
132 indices from <literal>istart</literal> to
133 <literal>istart+sect-1</literal>.
138 a fortran subroutine or C procedure which performs the same
139 calculation. (See the source code of <literal>dgetx</literal> for an
145 <literal>n</literal> = total size of the sequence.
146 <literal>sect</literal> = size of sections of the sequence.
147 <literal>sect</literal> must be a power of 2.
148 <literal>cov</literal> has dimension <literal>sect</literal>.
149 Calculation is performed by FFT.
154 <term>Updating method</term>
156 <programlisting role=""><![CDATA[
157 [w,xu]=corr('updt',x1,[y1],w0)
158 [w,xu]=corr('updt',x2,[y2],w,xu)
160 wk=corr('updt',xk,[yk],w,xu)
163 With this syntax the calculation is updated at each
164 call to <literal>corr</literal>.
166 <programlisting role=""><![CDATA[
167 w0 = 0*ones(1,2*nlags);
171 <literal>x1,x2,...</literal> are parts of <literal>x</literal> such that
172 <literal>x=[x1,x2,...]</literal> and sizes of <literal>xi</literal> a power of
173 2. To get <literal>nlags</literal> coefficients a final fft must be
174 performed <literal>c=fft(w,1)/n</literal>; <literal>cov=c(1nlags)</literal>
175 (<literal>n</literal> is the size of <literal>x (y)</literal>). Caution: this
176 syntax assumes that <literal>xmean = ymean = 0</literal>.
183 <title>Examples</title>
184 <programlisting role="example"><![CDATA[
185 x=%pi/10:%pi/10:102.4*%pi;
186 rand('seed');rand('normal');
187 y=[.8*sin(x)+.8*sin(2*x)+rand(x);.8*sin(x)+.8*sin(1.99*x)+rand(x)];
189 for j=1:2,for k=1:2,c=[c;corr(y(k,:),y(j,:),64)];end;end;
190 c=matrix(c,2,128);cov=[];
191 for j=1:64,cov=[cov;c(:,(j-1)*2+1:2*j)];end;
194 rand('normal');x=rand(1,256);y=-x;
195 deff('[z]=xx(inc,is)','z=x(is:is+inc-1)');
196 deff('[z]=yy(inc,is)','z=y(is:is+inc-1)');
197 [c,mxy]=corr(x,y,32);
198 x=x-mxy(1)*ones(x);y=y-mxy(2)*ones(y); //centring
199 c1=corr(x,y,32);c2=corr(x,32);
201 [c3,m3]=corr('fft',xx,yy,256,32);
203 [c4,m4]=corr('fft',xx,256,32);
204 norm(m3,1),norm(m4,1)
205 norm(c3-c1,1),norm(c4-c2,1)
206 x1=x(1:128);x2=x(129:256);
207 y1=y(1:128);y2=y(129:256);
208 w0=0*ones(1:64); //32 coeffs
209 [w1,xu]=corr('u',x1,y1,w0);w2=corr('u',x2,y2,w1,xu);
210 zz=real(fft(w2,1))/256;c5=zz(1:32);
212 [w1,xu]=corr('u',x1,w0);w2=corr('u',x2,w1,xu);
213 zz=real(fft(w2,1))/256;c6=zz(1:32);
217 // test for Fortran or C external
219 deff('[y]=xmacro(sec,ist)','y=sin(ist:(ist+sec-1))');
221 [cc1,mm1]=corr(x,2^3);
222 [cc,mm]=corr('fft',xmacro,100,2^3);
223 [cc2,mm2]=corr('fft','corexx',100,2^3);
224 [max(abs(cc-cc1)),max(abs(mm-mm1)),max(abs(cc-cc2)),max(abs(mm-mm2))]
226 deff('[y]=ymacro(sec,ist)','y=cos(ist:(ist+sec-1))');
228 [cc1,mm1]=corr(x,y,2^3);
229 [cc,mm]=corr('fft',xmacro,ymacro,100,2^3);
230 [cc2,mm2]=corr('fft','corexx','corexy',100,2^3);
231 [max(abs(cc-cc1)),max(abs(mm-mm1)),max(abs(cc-cc2)),max(abs(mm-mm2))]
234 <refsection role="see also">
235 <title>See also</title>
236 <simplelist type="inline">
238 <link linkend="xcorr">xcorr</link>
241 <link linkend="xcov">xcov</link>
244 <link linkend="correl">correl</link>
247 <link linkend="cov">cov</link>
250 <link linkend="covar">covar</link>