Anda di halaman 1dari 13

ANCP problem in real time mode

With the following program, we can get Figure 1 and 2.


% Adaptive Noise Cancellation
clear;
clc;
% The useful signal u(t) is a frequency and amplitude
% modulated sinusoid
f = 4e3 ; % signal frequency
fm = 300 ; % frequency modulation
fa = 200 ; % amplitude modulation
ts = 2e-5 ; % sampling time
N = 400 ; % number of sampling points
t = (0:N-1)*ts ; % 0 to 10 msec
signal = (1+0.2*sin(2*pi*fa*t)).*sin(2*pi*f*(1+0.2*cos(...
2*pi*fm*t)).*t) ;
% The noise is
noise = sawtooth(2*pi*1e3*t, 0.7) ;
% the filtered noise
b = [ 1 -0.6 -0.3] ;
n1 = filter(b, 1, noise) ;
% noisy signal
noisySignal = signal + n1 ;T=noisySignal;
figure(4)
subplot(2,1,1)
plot(1e3*t, signal, 1e3*t, noisySignal), grid, ...
title('Input signal(blue) and noisy input:noisySignal'),
xlabel('time -- msec')
subplot(2,1,2)
plot(1e3*t, noise, 1e3*t, n1), grid, ...
title('Noise(blue) and colored noise n1'),
xlabel('time -- msec')
p = 4 ; % dimensionality of the input space
% formation of the input matrix X of size p by N
P = convmtx(noise, p) ; P = P(:, 1:N) ;% delays=[0 1 2 3]
A1 = zeros(1,N) ; % memory allocation for y
errx = zeros(1,N) ; % memory allocation for uh = errx
LR = 0.05 ; % learning rate/gain
w = 2*(rand(1, p) -0.5) ; % Initialisation of the weight vector
w1=w;
% for c = 1:4
for n = 1:N % learning loop
A1(n) = w*P(:,n) ; % predicted output signal
errx(n) = noisySignal(n) - A1(n) ; % error signal
w = w + LR*errx(n)*P(:,n)' ;
end
%LR = 0.8*LR ;
% end
figure(5)
subplot(2,1,1)
plot(1e3*t, signal, 1e3*t, errx), grid, ...
title('Input:signal(blue) and estimated signal: restored'), ...
xlabel('time -- msec')
subplot(2,1,2)
plot(1e3*t(p:N), signal(p:N)-errx(p:N)), grid, ...
title('estimation error'), xlabel('time --[msec]')

Input signal(blue) and noisy input:noisySignal


2

-1

-2
0 1 2 3 4 5 6 7 8
time -- msec
Noise(blue) and colored noise n1
1

0.5

-0.5

-1
0 1 2 3 4 5 6 7 8
time -- msec

Figure 1.
NoisySigal is not different from original signal much as shown in
upper diagram of Figure 1, because the amplitude of colored noise
(n1) is relatively small (lower part of the graph). In general, ANC
problem is solved by the ON-LINE or real time LMS algorithm, instead
of off-line approach. Therefore the for loop (c) is not necessary.

Input:signal(blue) and estimated signal: restored


2

-1

-2
0 1 2 3 4 5 6 7 8
time -- msec
estimation error
2

-1
0 1 2 3 4 5 6 7 8
time --[msec]

Figure 2.

The error plot is not good enough (on-line mode) as shown in Figure
2. Of cause if off-line approach is used, the plot will be better in
performance than those shown in Figure 2.
The better way is to choose the random Gaussian or uniform random
number as noise source instead of saw tooth wave. The ANC solution is
apparent in this case.
[Modification of program]

% Adaptive Noise Cancellation


clear;
clc;
% The useful signal u(t) is a frequency and amplitude
% modulated sinusoid
f = 4e3 ; % signal frequency
fm = 300 ; % frequency modulation
fa = 200 ; % amplitude modulation
ts = 2e-5 ; % sampling time
%N = 400 ; % number of sampling points
N=1000; %% --------------------------------------------
t = (0:N-1)*ts ; % 0 to 10 msec
signal = (1+0.2*sin(2*pi*fa*t)).*sin(2*pi*f*(1+0.2*cos(...
2*pi*fm*t)).*t) ;
% The noise is
%noise = sawtooth(2*pi*1e3*t, 0.7) ;
noise = randn(size(t)) ; %% -------------------------------------

% the filtered noise


b = [ 1 -0.6 -0.3] ;
n1 = filter(b, 1, noise) ;
% noisy signal
noisySignal = signal + n1 ;T=noisySignal;
figure(41)
subplot(2,1,1)
plot(1e3*t, signal,'r', 1e3*t, noisySignal), grid, ...
title('Input signal(red) and noisy input:noisySignal'),
xlabel('time -- msec')
subplot(2,1,2)
plot(1e3*t, noise, 1e3*t, n1), grid, ...
title('Noise(blue) and colored noise n1'),
xlabel('time -- msec')
p = 4 ; % dimensionality of the input space
% formation of the input matrix X of size p by N
P = convmtx(noise, p) ; P = P(:, 1:N) ;% delays=[0 1 2 3]
A1 = zeros(1,N) ; % memory allocation for y
errx = zeros(1,N) ; % memory allocation for uh = errx
LR = 0.05 ; % learning rate/gain
LR1=LR;
w = 2*(rand(1, p) -0.5) ; % Initialisation of the weight vector
w1=w;
for c = 1:4
for n = 1:N % learning loop
A1(n) = w*P(:,n) ; % predicted output signal
errx(n) = noisySignal(n) - A1(n) ; % error signal
w = w + LR*errx(n)*P(:,n)' ;
end
LR = 0.8*LR ;
end
figure(42)
subplot(2,1,1)
plot(1e3*t, signal, 1e3*t, errx), grid, ...
title('Input:signal(blue) and estimated signal: restored'), ...
xlabel('time -- msec')
subplot(2,1,2)
plot(1e3*t(p:N), signal(p:N)-errx(p:N)), grid, ...
title('estimation error'), xlabel('time --[msec]');
%axis([0 8 -4 4]);

fprintf('>>> On-line ANC algirithm!\n'); %% ----------------------

fprintf('>>> On-line ANC algirithm!\n');


pressEnter;
delays=[0 1 2 3];LR1=0.025;
[A2,err,W,MSE,Px]=F_adaptiveFilter(P,T,LR1,delays);
%restored=err;
figure(43);subplot(2,1,1)
plot(time, signal, time, err), grid, ...
title('Input:signal(blue) and estimated signal: restored'), ...
xlabel('time -- msec')
subplot(2,1,2)
plot(time(p:N), signal(p:N)-err(p:N)), grid, ...
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',On-line mode, time(msec)']);
ax=axis;

[w;W]
[mse2(signal);mse2(noisySignal);mse2(errx)]

fprintf('>>> Off-line ANC algirithm!\n'); %% <---------------------------


pressEnter;
[A3,err3,W,MSE,Px,Wlist]=F_adaptiveFilter(P,T,LR1,delays,[],40);
%restored=err;
figure(44);subplot(2,1,1)
plot(time, signal, time, err3); grid on;
title('Input:signal(blue) and estimated signal: restored'), ...
subplot(2,1,2)
plot(time(p:N), signal(p:N)-err3(p:N)); grid on;axis(ax);
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',Off-line mode, time(msec)']);

A4=sum(Wlist.*Px);
figure(45);subplot(211);plot(time,signal,time,T-A4)
title('Input:signal(blue) and estimated signal: restored');
grid on;subplot(212);plot(time,signal-(T-A4))
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',Off-line mode, time(msec)']);
axis(ax);grid on;
Input signal(red) and noisy input:noisySignal
5

-5
0 2 4 6 8 10 12 14 16 18 20
time -- msec
Noise(blue) and colored noise n1
4

-2

-4
0 2 4 6 8 10 12 14 16 18 20
time -- msec

Figure 3.

Figure 3 shows the off-line anc (adaptive noise cancellation)


solution. Noisy signal is much apparently larger signal, as shown in
the upper diagram.
Input:signal(blue) and estimated signal: restored
2

-1

-2
0 2 4 6 8 10 12 14 16 18 20
time -- msec
estimation error
1

0.5

-0.5

-1
0 2 4 6 8 10 12 14 16 18 20
Off-line mode,time --[msec]

Figure 4.
ANC solution is quite apparent as shown in Figure 4. Figure 5 shows
the on-line mode result. Figure 6 shows the off-line mode result.
Figure 5 shows the real-time case in normal ANC approach, while
Figure 6 is in off-line or non-real time mode. Off-line mode means
that once we have all waveforms (no restored yet), we do the
reconstruction of restored signal from noisy_signal by an adaptive
filter. Figure 7 is a duplicate of Figure 6 by calculating

A4=sum(Wlist.*Px);
Input:signal(blue) and estimated signal: restored
2

-2

-4
0 2 4 6 8 10 12 14 16 18 20
time -- msec
delays=[0 1 2 3],estimation error
1

0.5

-0.5

-1
0 2 4 6 8 10 12 14 16 18 20
MSE=0.5852,On-line mode, time(msec)

Figure 5.

Input:signal(blue) and estimated signal: restored


2

-1

-2
0 2 4 6 8 10 12 14 16 18 20

delays=[0 1 2 3],estimation error


1

0.5

-0.5

-1
0 2 4 6 8 10 12 14 16 18 20
MSE=0.5518,Off-line mode, time(msec)

Figure 6.
Input:signal(blue) and estimated signal: restored
2

-1

-2
0 2 4 6 8 10 12 14 16 18 20

delays=[0 1 2 3],estimation error


1

0.5

-0.5

-1
0 2 4 6 8 10 12 14 16 18 20
MSE=0.5518,Off-line mode, time(msec)

Figure 7.

[Final program listing]


%% AdaptiveNoiseCancellation

%% PenChen Chou, 2016-8-26.

% Adaptive Noise Cancellation


clear;
clc;
% The useful signal u(t) is a frequency and amplitude
% modulated sinusoid
f = 4e3 ; % signal frequency
fm = 300 ; % frequency modulation
fa = 200 ; % amplitude modulation
ts = 2e-5 ; % sampling time
N = 400 ; % number of sampling points
N=1000;
t = (0:N-1)*ts ; % 0 to 10 msec
signal = (1+0.2*sin(2*pi*fa*t)).*sin(2*pi*f*(1+0.2*cos(...
2*pi*fm*t)).*t) ;
% The noise is
noise1 = sawtooth(2*pi*1e3*t, 0.7) ;
noise = noise1 + randn(size(t)) ;
time=1e3*t;
% the filtered noise
b = [ 1 -0.6 -0.3] ;
n1 = filter(b, 1, noise) ;
% noisy signal
noisySignal = signal + n1 ;T=noisySignal;
figure(41);subplot(2,1,1)
plot(time, signal,'r', time, noisySignal), grid, ...
title('Input signal(red) and noisy input:noisySignal'),
xlabel('time -- msec')
subplot(2,1,2)
plot(time, noise, time, n1), grid, ...
title('Noise(blue) and colored noise n1'),
xlabel('time -- msec')
p = 4 ; % dimensionality of the input space
% formation of the input matrix X of size p by N
P = convmtx(noise, p) ; P = P(:, 1:N) ;% delays=[0 1 2 3]
A1 = zeros(1,N) ; % memory allocation for y
errx = zeros(1,N) ; % memory allocation for uh = errx
LR = 0.05 ; % learning rate/gain
LR1=LR;
w = 2*(rand(1, p) -0.5) ; % Initialisation of the weight vector
w1=w;
for c = 1:4
for n = 1:N % learning loop
A1(n) = w*P(:,n) ; % predicted output signal
errx(n) = noisySignal(n) - A1(n) ; % error signal
w = w + LR*errx(n)*P(:,n)' ;
end
LR = 0.8*LR ;
end
figure(42);subplot(2,1,1)
plot(time, signal, time, errx), grid, ...
title('Input:signal(blue) and estimated signal: restored'), ...
xlabel('time -- msec')
subplot(2,1,2)
plot(time(p:N), signal(p:N)-errx(p:N)), grid, ...
title('estimation error'),
xlabel('Off-line mode,time --[msec]');
ax=axis;

fprintf('>>> On-line ANC algirithm!\n');


pressEnter;
delays=[0 1 2 3];LR1=0.025;
[A2,err,W,MSE,Px]=F_adaptiveFilter(P,T,LR1,delays);
%restored=err;
figure(43);subplot(2,1,1)
plot(time, signal, time, err), grid, ...
title('Input:signal(blue) and estimated signal: restored'), ...
xlabel('time -- msec')
subplot(2,1,2)
plot(time(p:N), signal(p:N)-err(p:N)), grid, ...
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',On-line mode, time(msec)']);
axis(ax);grid on;

[w;W]
[mse2(signal);mse2(noisySignal);mse2(errx)]

fprintf('>>> Off-line ANC algirithm!\n');


pressEnter;
[A3,err3,W,MSE,Px,Wlist]=F_adaptiveFilter(P,T,LR1,delays,[],40);
%restored=err;
figure(44);subplot(2,1,1)
plot(time, signal, time, err3); grid on;
title('Input:signal(blue) and estimated signal: restored'), ...
subplot(2,1,2)
plot(time(p:N), signal(p:N)-err3(p:N)); grid on;axis(ax);
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',Off-line mode, time(msec)']);
axis(ax);grid on;
A4=sum(Wlist.*Px);
figure(45);subplot(211);plot(time,signal,time,T-A4)
title('Input:signal(blue) and estimated signal: restored');
grid on;subplot(212);plot(time,signal-(T-A4))
title(['delays=[' num2str(delays) '],estimation error']);
xlabel(['MSE=' num2str(MSE) ',Off-line mode, time(msec)']);
axis(ax);grid on;

Anda mungkin juga menyukai