Ystd = integrateDown(Y,intLength) This function integrates the data down. You pass it two arguments: Y - MxN matrix of data. The function operates on each column. intLength - Px1 vector of bin lengths, in terms of number of samples For each column in Y, this function bins the data into bins whose length is specified in intLength. It calculates the standard deviation of the binned data. It does this for each bin length specified in intLength. It returns the matrix: Ystd - PxN matrix. Row k corresponds to the standard deviation of the data when binned by intLength(k) samples. This is given for each column. ogk, 15 June 2011
0001 function Ystd = integrateDown(Y,intLength) 0002 % Ystd = integrateDown(Y,intLength) 0003 % 0004 % This function integrates the data down. You pass it two arguments: 0005 % Y - MxN matrix of data. The function operates on each column. 0006 % intLength - Px1 vector of bin lengths, in terms of number of samples 0007 % For each column in Y, this function bins the data into bins whose length 0008 % is specified in intLength. It calculates the standard deviation of the 0009 % binned data. It does this for each bin length specified in intLength. It 0010 % returns the matrix: 0011 % Ystd - PxN matrix. Row k corresponds to the standard deviation of the 0012 % data when binned by intLength(k) samples. This is given for each 0013 % column. 0014 % 0015 % ogk, 15 June 2011 0016 0017 % Y is a matrix. We want to apply this calculation to each column of Y. 0018 Nsamples = size(Y,1); 0019 intLength = intLength(:); % just in case it's a row vector instead of a column vector 0020 Ystd = zeros(size(intLength,1),size(Y,2)); 0021 0022 % For each entry in intLength, we want to bin that many samples together, 0023 % then calculate the standard deviation of the binned data. This standard 0024 % deviation data is returned in Ystd. 0025 0026 for k=1:length(intLength) 0027 Nbin = intLength(k); 0028 if Nbin <= Nsamples/2 0029 Nselect = Nbin*floor(Nsamples/Nbin); % select only these samples, throwing away the rest 0030 % Now use the power of MATLAB! 0031 Ystd(k,:) = std(reshape(mean(reshape(Y(1:Nselect,:),Nbin,Nselect/Nbin,size(Y,2)),1),Nselect/Nbin,size(Y,2),1),0,1); 0032 else 0033 % Cannot calculate a standard deviation, return NaN's 0034 disp('Error in function: integrateDown(Y,intLength).') 0035 fprintf('You have asked for bins of length %d, but there are only %d samples.\n',Nbin,Nsamples) 0036 disp('This makes it impossible to calculate a standard deviation. Sorry. Assigning NaNs.') 0037 Ystd(k,:) = nan; 0038 end 0039 end 0040 0041 end