function energy = get_energy(SIGNAL, FRAME_LEN, TIME_OVERLAP)
% get_energy computes the energy in SIGNAL
%   SIGNAL = input signal
%   FRAME_LEN = length of window over which the energy is summed
%   TIME_OVERLAP = number of samples for frame windows to overlap
%   energy is an array of length length(signal)/(frame_len - time_overlap)
% Harriet Fell 2004

% Calculate energy
window_len = FRAME_LEN - TIME_OVERLAP;
signal_len = length(SIGNAL);

% Remove the components around d.c.
signal = filtfilt([1 -1],[1 -.99], SIGNAL);

if (FRAME_LEN > signal_len)
    disp(['Please use a frame_len < ',int2str(signal_len)]);
else
    num_windows = floor(signal_len/window_len);
    average = zeros(1, num_windows);

    % Find start and end for next sample
    for z = 1:num_windows
        sample_start = (z-1)*window_len + 1;
        if (sample_start + FRAME_LEN -1 > signal_len)
            sample_end = signal_len;
        else
            sample_end = sample_start + FRAME_LEN - 1;
        end
        
        frame = signal(sample_start:sample_end);
        energy(z) = sum(frame.^2)/length(frame);
    end
end

return;
