-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
126 lines (109 loc) · 3.94 KB
/
main.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
%% Simulation properties
% The simulation will take the expected value of MSE, MSD, and EMSE
% by performing many realizations.
N = 10000; % Total samples
M = 10; % Filter length
R = 100; % Realizations Qty
%% Optimum filter definition
% A FIR filter needs to be used as optimum plant.
% To use MATLAB filter function, we need to pass the poles vector. Passing
% [1] just means the optimum filter doesn't have poles.
A = [1];
B = randn(M,1); % Create a random FIR filter
sig2v = 1e-3; % Noise level
%% Other definitons
mu = 0.05; % Adaptive filter step size
%% Global Accumulators
% These accumulators are responsible for storing desired metrics between
% realizations
% MSE: Mean Square Error
% EMSE: Excess Mean Square Error
% MSD: Mean Square Deviation
% FIR LMS Adaptive Filter accumulators
mse_lms = zeros(N,1);
emse_lms = zeros(N,1);
msd_lms = zeros(N,1);
% FIR NLMS Adaptive Filter accumulators
mse_nlms = zeros(N,1);
emse_nlms = zeros(N,1);
msd_nlms = zeros(N,1);
%% Run realizations
for r=1:R
%% Signal definitions
% Define input signal
u = randn(N,1);
% Define noise signal as white, with variance sig2v
v = sqrt(sig2v)*randn(N,1);
% Run input signal through optimum filter. This will be used in the
% filters to measure EMSE
yo = filter(B,A,u);
% Define desired signal by adding noise to the optimum filter output
d = yo + v;
%% Estimate Covariance Matrix
% The matrix covariance matrix Ru will later on be used to calculate
% the theoretical EMSE. It can be estimated by taking the mean of
% outer product of a sliding window with size M of the input signal.
ru = zeros(M,M);
u_reg = zeros(1,M);
for n = 1:N
u_reg =[u(n) u_reg(1:M-1)];
ru = ru + 1/N .* (u_reg' * u_reg);
end
%% Run the LMS Filter
[r_msd,r_mse,r_emse,w] = fir_lms(u,d,yo,B,M,N,mu,false);
mse_lms = mse_lms + (1/R) .* r_mse;
emse_lms = emse_lms + (1/R) .* r_emse;
msd_lms = msd_lms + (1/R) .* r_msd;
%% Run the NLMS Filter
[r_msd,r_mse,r_emse,w] = fir_lms(u,d,yo,B,M,N,mu,true);
mse_nlms = mse_nlms + (1/R) .* r_mse;
emse_nlms = emse_nlms + (1/R) .* r_emse;
msd_nlms = msd_nlms + (1/R) .* r_msd;
end
%% Asserts
% It is very easy to make a mistake with the accumulator vector dimensions.
% If that was the case, the accumulators would, at this point, be matrices
% instead of vectors. The assertions just make sure that all accumulators
% are actually column vectors with size N.
assertDimensions(mse_lms,zeros(N,1));
assertDimensions(emse_lms,zeros(N,1));
assertDimensions(msd_lms,zeros(N,1));
assertDimensions(mse_nlms,zeros(N,1));
assertDimensions(emse_nlms,zeros(N,1));
assertDimensions(msd_nlms,zeros(N,1));
%% Plot MSE
fig = figure;
hold all;
plot(10*log10(mse_lms),'DisplayName','LMS');
plot(10*log10(mse_nlms),'DisplayName','NLMS');
title('MSE');
xlabel('n')
ylabel('MSE (dB)')
legend('show')
y = 10*log10(sig2v);
line([0,N],[y,y],'Color','r','DisplayName','\sigma_v^2 (noise level)');
saveas(fig,'./img/mse.png')
%% Plot EMSE
fig = figure;
hold all;
plot(10*log10(emse_lms),'DisplayName','LMS');
plot(10*log10(emse_nlms),'DisplayName','NLMS');
title('EMSE');
xlabel('n')
ylabel('EMSE (dB)')
legend('show')
lms_theoretical_emse = 10*log10(mu*sig2v*trace(ru)/2);
line([0,N],[lms_theoretical_emse ,lms_theoretical_emse ],'Color','r','DisplayName','Theoretical EMSE');
saveas(fig,'./img/emse.png')
%% Plot MSD
fig = figure;
hold all;
plot(10*log10(msd_lms),'DisplayName','LMS');
plot(10*log10(msd_nlms),'DisplayName','NLMS');
title('MSD');
xlabel('n')
ylabel('MSD (dB)')
legend('show')
lms_theoretical_msd = 10*log10(mu*sig2v*M/2);
line([0,N],[lms_theoretical_msd ,lms_theoretical_msd ],'Color','r','DisplayName','Theoretical MSD');
saveas(fig,'./img/msd.png')