function sampled=sample_data(in, samplerate) % This function simulates sampling a data stream by creating equally % spaced sample points from timestamped data. If no points lie in one % sample period in the original data, then the last sample values are % used again. If there are multiple original data points within one % sample period, their arithmetic mean is used as sample value. % % Rene Mayrhofer, 2006 % % Parameters: % in: The originial time stamped data with the timestamp contained in the % first column and data vectors in the following columns. Timestamps should % be in seconds and may be floating point. % samplerate: The samplerate in Hz that should be used. % Return value: % sampled: The sampled data vector starting at the time marked by the % first timestamp in the original data with all following rows % being spaced by 1/samplerate seconds. Timestamps are not put % into this output vector, only the data points. if nargin<2 error('Need two argument'); end if size(in,2) < 2 error('Data needs at least a timestamp column and one data column'); end if size(in,1) < 1 error('Need at least one data point (one row)'); end if samplerate <= 0 error('Invalid sample rate, can not be <= 0'); end curtime=in(1,1); width=1/samplerate; fprintf('Data is starting at time %f sec, simulating sample width of %f sec\n', curtime, width); nextinpos=1; nextoutpos=1; lastvector = zeros(1,size(in,2)-1); while nextinpos <= size(in,1) curvector = zeros(1,size(in,2)-1); numdatapoints = 0; while nextinpos <= size(in,1) && in(nextinpos,1) < curtime+width curvector = curvector + in(nextinpos,2:size(in,2)); numdatapoints = numdatapoints + 1; nextinpos = nextinpos + 1; end if numdatapoints > 0 lastvector = curvector/numdatapoints; numdatapoints = 0; end out(nextoutpos,1:size(lastvector,2)) = lastvector; nextoutpos = nextoutpos + 1; curtime = curtime + width; end; sampled=out;