
%  adlpr.m
%           6 March 2005 
% Adaptive Prediction with Adaline

clear

% Input signal x(t)

f1 = 2 ; % kHz
         % f2 = 4*f1 = 8kHz
         % fs = 10*f2 = 80 kHz -- sampling frequency
ts = 1/(40*f1) ; % 12.5 usec -- sampling time
N = 100 ;
t1 = (0:N)*4*ts ;
t2 = (0:2*N)*ts + 4*(N+1)*ts ;
t = [t1 t2] ;     % 0 to 7.5 sec
N = size(t, 2) ;  % N = 302

xt = [sin(2*pi*f1*t1) sin(2*pi*2*f1*t2)];
figure(1)
plot(t, xt), grid on, 
title('Signal to be predicted')

p = 4 ; % Number of synapses

% formation of the input matrix X of size  p by N
% use the convolution matrix. Try  convmtx(1:8, 5)

X = convmtx(xt, p) ; X = X(:, 1:N) ;

d = xt ;  % The target signal is equal to the input signal

 y  = zeros(size(d)) ;   % memory allocation for y
eps = zeros(size(d)) ;   % memory allocation for eps

eta = 0.4   ;    % learning rate/gain
w = rand(1, p) ; % Initialisation of the weight vector
w
for n = 1:N                % learning loop
  y(n) = w*X(:,n) ;        % predicted output signal
  eps(n) = d(n) - y(n) ;   % error signal
  w = w + eta*eps(n)*X(:,n)' ;
  if n== 99, w, end
end

w

figure(1), % myfigA(1, 10, 9.5)
plot(t, d, 'b', t, y, '-r'), grid on 
title('target and predicted signals'), xlabel('time [sec]')
% print -f1 -depsc2 cAdlpsg

figure(2), % myfigA(2, 10, 9.5)
plot(t, eps), grid on, 
title('prediction error'), xlabel('time [sec]')
%  print -f2 -depsc2 cAdlper
