In: Electrical Engineering
In this part of the assignment, you will need to calculate the cross-correlation of two random signals. In the Appendix: Part II, we include the function ccorr that gives you the crosscorrelation of two input vectors.
MATLAB provides other built-in function that may be useful for this part of the assignment
• max: gives you the maximum value of a vector. For more information type help max
find: gives you the index corresponding to a value of a vector. For more information type help find.
Finally, you will also need to use a linear filter in Exercise 3. You can use the function linfilt that you already used in MA2.
You can calculate the cross-correlation between two random signals in MATLAB by using the function ccorr given in the Appendix: Part II. As an example, consider two random signals x(t) and y(t), which are both modeled as white Gaussian random processes with mean 0 and variance 1. We plot the cross-correlation function Rxy(? ) of the random signals x(t), y(t) by applying the following MATLAB code:
t = -10:0.01:9.99;
x = randn(1, length(t));
y = randn(1, length(t));
[cc, tau] = ccorr(x,y,t);
plot(tau,cc);
In the same way you can plot the autocorrelation of signal x(t) [ac, tau] = ccorr(x,x,t); plot(tau, ac);
Now assume that y(t) is the output of a linear filter with impulse response h(t) = |sinc(t ? 2)|
where the input x(t) is a white Gaussian random signal with mean 0 and variance 1. Create an M-file to:
(a) Calculate y(t) using the function linfilt and plot x(t) and y(t) where t ranges from ?10 to 9.99, using 0.01 increments. NOTE: y(t) is the output signal of the filter, so if N is the length of the vector t, the length of y(t) is N + N ? 1 (convolution of two functions with length N). You will have to find the indices that correspond to t from ?10 to 9.99.
(b) Plot the cross-correlation function Rxy(? ).
(c) Plot the autocorrelations Rxx(? ) and Ryy(? ) of x(t) and y(t), respectively.
clc
clear all
close all
t=-10:0.01:9.99;
x=1*randn(1,length(t))+0; %input;
h=sinc(t-2); %filter
y=linfilt(x,h,length(t)); %function
subplot(211)
plot(t,h)
title('Input random signal')
xlabel('time')
ylabel('Amplitude')
subplot(212)
%plot(y)
t1=-20:0.01:19.98;
plot(t1,y)
title('Convoluted output')
xlabel('time')
ylabel('Amplitude')
axis([-10 9.99 min(y)-1 max(y)+1])
%Cross correlation
[C tau]=xcorr(x,y);
figure;
plot(tau,C)
title('Cross Correlation of x(t) & y(t)')
[A1 tau]=xcorr(x,x);
figure;
plot(tau,A1)
title('Auto Correlation of x(t)')
[A2 tau]=xcorr(y,y);
figure;
plot(tau,A2)
title('Auto Correlation of y(t)')
%linear filter function
function y=linfilt(x,h,t)
y=conv(x,h);
Output:
There is no ccorr function in matlab,instead we have xcorr to perform auto correlation and cross correlation.So I used those. If you are not intended to use this inbuilt function but only use user defined function of ccorr then let me know so that I can rewrite the code and submit.
h(t)=sinc(t-2) is what I have taken. linfilt function isjust convolution between input x(t) and filter h(t).
After convolution the time stampwill be from -20 to 19.98. In that we have to plot only for -10 to 9.99. So used axis command to display given specified range.
Please leave feedback for my improvement.
Thankyou.