-
Notifications
You must be signed in to change notification settings - Fork 1
/
AR.m
59 lines (53 loc) · 1.23 KB
/
AR.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
clear , close all ;
N = 10000; % number of samples
sigma_epsilon = 1; % noise variance
K = 20; % maximum lag
Rxx = zeros(1, K); % preallocate memory for autocorrelation function
figure();
for k = 0:K-1
Rxx(k+1) = (1/(1-0.6^2))*0.6^k*sigma_epsilon; % calculate autocorrelation for lag k
end
Amp = max(Rxx);
Rxx = Rxx / Amp;
lags = 0:K-1; % lags for plotting
subplot(211)
plot(lags, Rxx); % plot autocorrelation function
xlabel('k');
ylabel('R(k)');
legend('a1 = 0.6');
for k = 0:K-1
Rxx(k+1) = (1/(1-0.99^2))*0.99^k*sigma_epsilon; % calculate autocorrelation for lag k
end
Amp = max(Rxx);
Rxx = Rxx / Amp;
lags = 0:K-1; % lags for plotting
subplot(212)
plot(lags, Rxx); % plot autocorrelation function
xlabel('k');
ylabel('R(k)');
legend('a1 = 0.99');
% generate AR(1) signal
x = zeros(1, N); % pre-allocate memory for signal
x=x(1001:end);
x(1) = randn; % set initial value of signal
for n = 2:100
x(n) = .6 * x(n-1) + sigma_epsilon * randn;
end
% plot signal
figure
subplot(211)
plot(x(1:100));
xlabel('n');
ylabel('Xn');
title('AR Signal');
legend('a1 = 0.6');
for n = 2:100
x(n) = 0.99 * x(n-1) + sigma_epsilon * randn;
end
% plot signal
subplot(212)
plot(x(1:100));
xlabel('n');
ylabel('Xn');
title('AR Signal');
legend('a1 = 0.99');