function[p,m,z] = peaks(data_mag, data_pha, peakspace)
% peaks takes a set of data and returns the local maximuma (in the column
% vector m), their respective positions (in the column vector p), and their
% corresponding phases (in column vector z).  To get the peaks of the data,
% the function looks at the current value and the values around it (as
% determined by the argument peakspace).  If that point is the max, then it
% is recorded as a peak.

% get the length of the data
len = length(data_mag);
% set some offsets
begin_offset = round(peakspace/2);
end_offset = len-round(peakspace/2);
% a counter
k = 1;

% find them peaks
[m(k),p(k)] = max(data_mag(1:begin_offset-1));
z(k) = data_pha(p(k));
k = k+1;
for i=begin_offset:end_offset,
	if(data_mag(i) == max(data_mag(i-floor(peakspace/2):i+floor(peakspace/2))) ...
				 & data_mag(i) > 0)
		p(k) = i;
		m(k) = data_mag(i);
		z(k) = data_pha(i);
		k = k+1;
	end % of if statement
end % of for loop

%convert p, m, and z to column matrices
p = p(:);
m = m(:);
z = z(:);