Random Signals for Engineers using MATLAB and Mathcad Copyright 1999 Springer Verlag NY Example 5.9 Correlation Function Computation In this example we will compute the mean and the correlation function of a Random Telegrapher wave by numerical methods. The analytical correlation function was computed in Example 5.7. We begin the numerical computation by generating one sample waveform and then converting this description to a function of time. We use time averaging techniques, which based upon the assumption that the waveform is ergodic in order to compute the correlation function. The sample waveform from the ensemble is generated by creating a time variable by summing the exponentially distributed time intervals as N=120; lam=1; z=-1/lam*log(rand(N)); x=cumsum(z); and the alternating function is given below y(1)=-1; for i=1:N-1 if y(i)==-1 y(i+1)=1; else y(i+1)=-1; end end A plot of the waveform is given below stairs(x(1:30),y(1:30)) axis([0 20 -1.5 1.5]) 1.5 1 0.5 0 -0.5 -1 -1.5 0 2 4 6 8 10 12 14 16 18 20 Next a computation is needed to convert the waveform to a function of t rather than the functional form used to plot the waveform. The time function can be sampled at a high rate to allow the correlation summation to be performed with samples that approach the continuous waveform. We use the Matlab find function to find the indices in the x array that has the value of x i greater than a value of time. Since the x array is monotonic, the minimum value of the index array is the value of the y array that corresponds to the x value. The previous value in the y array corresponds to the value of t and a t,y pair has be identified. To illustrate this process let us show values of the x y array and show that for a lookup value of t=2.5 the corresponding value of the y array is selected. [1:5;x(1:5);y(1:5)] ans = 1.0000 0.0512 -1.0000 2.0000 1.5159 1.0000 3.0000 2.0154 -1.0000 4.0000 2.7370 1.0000 5.0000 2.8520 -1.0000 Now the index array is found i=find(x>=2.5); i(1:5)' ans = 4 5 6 7 8 and minimum value of the index and the corresponding value of y ii=min(i) -y(ii) ii = 4 ans = -1 Examination of the table above show that the correct pair has been selected. We now apply this technique to the time values from 0 to 100. The waveform vy(tj) is compared to the one generated by the time function. This computation takes time. t=0:.1:100; vy=ones(1,length(t)); for j=1:length(t) i=find(x>=t(j)); ii=min(i); vy(j)=-y(ii); end stairs(x(1:30),y(1:30)) axis([0 20 -1.5 1.5]) hold on plot(t,vy+0.25,'r--') 1.5 1 0.5 0 -0.5 -1 -1.5 0 2 4 6 8 10 12 14 16 18 20 We notice that small spikes in the waveform less than the time increment used in the time computations may be missed by the time function. We now compute the correlation function by the discrete correlation formula. This formula sums the product of samples of the waveform and one delayed by k time increments, for each value of R(k). We use NP samples and compute the correlation function for 30 time increments figure NP=750; v=1:NP; for k=1:30 R(k)=1/length(v)*sum(vy(v+k-1).*vy(v)); end When we compare the computed correlation function with the theoretical one we get a deviation for larger values of . The cause of this effect is that the sample length used to compute the correlation function is not long enough to produce a zero average as expected from theory. This happens because we have used a small sample length due to the long computation time need to form the time function and then the correlation function. tp=0:.1:2.9; fp=exp(-2*tp); plot(tp,R,tp,fp,'r--') 1 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.5 1 1.5 2 2.5 3 The last point is best illustrated by removing the expected value computed by the correlation function from the plot of the function and renormalization of the correlation function. The numerically computed correlation function now compares favorably with the theoretical one. lr=length(R); RP=(R-R(lr))/(1-R(lr)); figure plot(tp,RP,tp,fp,'r--') 1.2 1 0.8 0.6 0.4 0.2 0 -0.2 0 0.5 1 1.5 2 2.5 3