function [Magx1,Magx2] = findmag(x1,x2,u)
%
% utility to find the magnitude from freq resp data:
%
% [Magx1,Magx2] = findmag(x1,x2,u)
%
% Determines the Bode magnitude ratio from a set of frequency response data
%   x1 = response of first mass (cm)
%   x2 = response of second mass (cm)
%    u = amplitude of input
%
% Assumes that the data set is 5000 samples long
% (Equivalent to 20s of ECP data under RTW)

% Find the means of the steady-state signals.
p1 = polyfit(0:200,x1(800:1000)',1);
p2 = polyfit(0:200,x2(800:1000)',1);

% Subtract the mean from the steady state signals.
Enc1cm = x1(800:1000) - (polyval(p1,0:200))';
Enc4cm = x2(800:1000) - (polyval(p2,0:200))';

% Compute the magnitude ratios.
Magx1 = max(Enc1cm)/u;
Magx2 = max(Enc4cm)/u;