from BufferSegment - Performs buffer command using pieces of its output by Sherif Omran
I wrote this code to buffer an audio signal and process it similar to a buffer command but using chu

Chunk=BufferSegment(signal,elementsincolumn,overlap);
function Chunk=BufferSegment(signal,elementsincolumn,overlap);

% Description
% Doing Buffer(signal,elements,elements-overlap,[]) for long signals take
% a huge memory and couldn't be done. 
% Target: to do it using Chunks similar to those could be taken from the buffer matrix 
% Each Chunk has a length = 2 x elementsincolumn (Design request)
%
% To check: Compare the following outputs
% BufferSegments([1:15],4,2) 
% Buffer([1:15],4,2,[])
% For use in real program, remove the last validation part
%
% Author: Sherif Omran
% Date: 23-June-08
% University Zurich - Inst. of Neuro Informatic
% This function is part of my PhD Thesis.
%
%

% number of buffer chunks
ChunkDataLength=2*elementsincolumn;
numofcols=ceil(length(signal)/(elementsincolumn-overlap));

if overlap>elementsincolumn,
    disp('Overlap is greater than column elements. Please Correct !! ');
    Chunk=[];
    return;
end


% number of columns per chunk
t=2;
while mod(numofcols,t)~=0
    t=t+1;
end
numofcolsperchunk=t;
% End number of columns per chunk

BufferChunks=numofcols/numofcolsperchunk;
% 
% BufferChunks=ceil(length(signal)/(ChunkDataLength));

            
for i=0: BufferChunks-1
    switch i
        case 0,



            numofcolsperchunk=numofcols/BufferChunks;

            %            numberofelements=numofcols*elementsincolumn;
            %            restelements=mod((numberofelements-overlap),(elementsincolumn-overlap));
            %            lastelementposition=ChunkDataLength*(i+1)+restelements;
            %

            lastelementindex=(elementsincolumn-overlap)*numofcolsperchunk;
            if lastelementindex>length(signal),        %Case only 1 chunk available
                lastelementindex=length(signal);
            end
            signalchunk=signal(1:lastelementindex);
            Chunk=buffer(signalchunk,elementsincolumn,overlap,[]);
        case BufferChunks-1,     %last Chunk
            Chunk=[Chunk buffer(signal(lastelementindex-overlap+1:end),elementsincolumn,overlap,'nodelay')];

        otherwise %middle Chunk
            % Middle chunk should have the same number of cols / chunk as the first one
            startelementindex=lastelementindex-overlap+1;
            lastelementindex=startelementindex+(elementsincolumn-overlap)*numofcolsperchunk+overlap-1;
            Chunk=[Chunk buffer(signal(startelementindex:lastelementindex),elementsincolumn,overlap,'nodelay')];
    end
end


% Validating the output with the output from a similar command using buffer
% For use in real program, comment these lines
BufferMatrix=buffer(signal,elementsincolumn,overlap,[]);
eq=BufferMatrix==Chunk;
if mean(mean(eq,1)==1) & mean(mean(eq,2))==1
%    disp('matrix validated OK !')
else
    uiwait(msgbox('Warning - Output is Not similar to the buffer, error in algorithm'));
end
% Commend up to here




end

Contact us at files@mathworks.com