function [u] = learn(acc, oldu)

%set base frequency in Hz for signals
base_freq = 10;

%declare the number of harmonic frequencies that you want to control, where
%each one is a multiple of the base frequency (e.g., 20Hz).
num_harmonics = 2;

T = 1/base_freq;  %period
dt = T/100;       %sampling time, 100 samples/wave
t = 0:dt:T-dt;    %time vector
k = .5;           %controller gain

% create desired acceleration signal. The one below is composed of just two
% frequencies/harmonics (at 10 and 20Hz).
amp1 = .25;
phi1 = 1;
amp2 = .125;
phi2 = 2;
acc_d = amp1*sin(2*pi*base_freq*t+phi1) + amp2*sin(2*pi*(2*base_freq)*t+phi2); %desired acceleration signal

%just a check.  basically prevents you from trying to control frequencies
%above the nyquist frequency and causing lots of problems
if num_harmonics > length(t)/2 - 1
    num_harmonics = length(t)/2 - 1;
end

% Experimental bode plot values
G_exp = 0.1*[.927 0.868]
G_exp = [1 G_exp];

%compute F, which is the inverse of G.  This is needed in the control
%loop to transform errors in acceleration into voltages: U = F*ACC
for n = 1:length(G_exp)
    F_exp(n) = 1/G_exp(n);
end

%FFT the desired acceleration signal
fft_acc_d = fft(acc_d);


tol = 10;
fft_u = fft(oldu);

%FFT the acceleration signal
fft_acc = fft(acc);

%compute error in Fourier domain
fft_acc_e = fft_acc_d - fft_acc;

%create new control signal in Fourier domain
for n = 1:num_harmonics
    fft_u(n+1) = fft_u(n+1) + k*F_exp(n+1)*fft_acc_e(n+1);
    fft_u(end-(n-1)) = conj(fft_u(n+1));
end

%convert control signal from FFT domain to time domain
%'u' is returned by the function
u = real(ifft(fft_u));

%PLOTTING
acc = real(ifft(fft_acc));
figure(1)
subplot(1,2,1)
title(['norm of error in FFT domain = ',num2str(norm(fft_acc_e))])
plot(t, acc_d,'b')
plot(t, acc,'r')
subplot(1,2,2)
plot(t, u, 'g')
subplot(1,2,1)
legend('Desired acceleration','Measured acceleration')
subplot(1,2,2)
legend('Control voltage sent to speaker')