www.gusucode.com > signal 案例源码程序 matlab代码 > signal/CrossCorrelationOfTwoExponentialSequencesExample.m
%% Cross-Correlation of Two Exponential Sequences % Compute and plot the cross-correlation of two 16-sample exponential % sequences, $x_a=0.84^n$ and $x_b=0.92^n$, with $n\ge0$. % Copyright 2015 The MathWorks, Inc. N = 16; n = 0:N-1; a = 0.84; b = 0.92; xa = a.^n; xb = b.^n; r = xcorr(xa,xb); stem(-(N-1):(N-1),r) %% % Determine $c$ analytically to check the correctness of the result. Use a % larger sample rate to simulate a continuous situation. The % cross-correlation function of the sequences $x_a(n) = a^n$ and $x_b(n) = % b^n$ for $n\ge0$, with $0<a,b<1$, is % % $$c_{ab}(n) = \frac{1-(ab)^{N-|n|}}{1-ab} \times\cases{a^n, & $n>0$, % \cr1, & $n=0$, \cr b^{-n}, & $n<0$.\cr} $$ fs = 10; nn = -(N-1):1/fs:(N-1); cn = (1 - (a*b).^(N-abs(nn)))/(1 - a*b) .* ... (a.^nn.*(nn>0) + (nn==0) + b.^-(nn).*(nn<0)); %% % Plot the sequences on the same figure. hold on plot(nn,cn) xlabel('Lag') legend('xcorr','Analytic') %% % Verify that switching the order of the operands reverses the sequence. figure stem(-(N-1):(N-1),xcorr(xb,xa)) hold on stem(-(N-1):(N-1),fliplr(r),'--*') xlabel('Lag') legend('xcorr(x_b,x_a)','fliplr(xcorr(x_a,x_b))') %% % Generate the 20-sample exponential sequence $x_c=0.77^n$. Compute and % plot its cross-correlations with $x_a$ and $x_b$. Output the lags to make % the plotting easier. |xcorr| appends zeros at the end of the shorter % sequence to match the length of the longer one. xc = 0.77.^(0:20-1); [xca,la] = xcorr(xa,xc); [xcb,lb] = xcorr(xb,xc); figure subplot(2,1,1) stem(la,xca) subplot(2,1,2) stem(lb,xcb) xlabel('Lag')