534 Practical MATLAB® Applications for Engineers
Observe that the magnitude spectrum for f(n) and f(n − 32) are similar. The phase-error plot
shows variations that are proportional to the shift (32). Note that the error in time is large;
but in frequency, approximately 1000 Hz, the magnitude and phase errors are small.Example 5.11This example explores and verifi es some properties that relate the correlation with the
convolution process.
Let f 1 (n) = sin(2πn/16) and f 2 (n) = cos(2πn/16), over the range n = 0, 1, 2, 3, 4, ..., 16.
Create the script fi le conv_corr that returns the following:
a. fi gure(1): f 1 (n) versus n and f 2 (n) versus n, over the range 0 ≤ n ≤ 31 , using the stairs
command
b. fi gure(2), explore the conv and xcorr functions in time by obtaining and comparing
the following plots:
i. [corr = xcorr(f1n, f2n)] versus n
ii. [conv1 = conv(f1n, f2(−n))] versus n
iii. [conv2 = conv(f1(−n), f2n)] versus n
c. fi gure(3) verifi es that the maximum point occurs at the midrange, and this point
corresponds to R 11 (0) and R 22 (0), by obtaining the following plots:
i. [R 11 = aut_corr1 = xcorr(f1n, f1n)] versus n
ii. [R 1 = aut_xcorr(f1n)] versus n
iii. [R 22 = aut_corr2 = xcorr(f2n, f2n)] versus n
Observe that R 1 ≠ R 22 , but R 11 = R 22.
d. fi gure(4), normalized frequency analysis (verify that the two following plots are
identical)
i. abs[conj[fft(f1n)] * fft(f2n)] versus W
ii. abs[f ft(xcorr(f 1(n), f 2(n)))] versus W
e. fi gure(5), error analysis in time
i. [error1(n) = xcorr(f 1(n), f 2(n)) − conv(f1(n), f2(−n))] versus n and
ii. [error2(n) = abs(abs(xcorr(f1(n), f2(n))) − abs(conv(f1(−n), f 2(n))))] versus nMATLAB Solution
% Script file: conv _ corr
n=0:1:31;
f1n=sin(2*pi*n./16);
f2n=cos(2*pi*n./16);
f1n _ rev=fliplr(f1n);
f2n _ rev=fliplr(f2n);figure(1) % discrete time plots of f1(n) and f2(n)
subplot(2,1,1)
stairs(n,f1n), axis([0 33 -1.1 1.1]),
title(‘ Stair plot of f1(n) vs. n’);
ylabel(‘Amplitude ’);
subplot(2,1,2)
stairs(n,f2n); axis([0 33 -1.1 1.1]),
title(‘ Stair plot of f2(n) vs. n’);
xlabel(‘time index n’);ylabel(‘Amplitude’);