function Y = synchronized_segmentation(y, fs, num_per)

% Y = synchronized_segmentation(y, fs, num_per)
% 
% This function provides the pitch-synchronized segmentation using the
% algorithm pitch_marks written by W. Goncharoff (goncharo@ece.uic.edu).
% 
% y         - input signal in column vector
% fs        - sampling frequency
% num_per   - number of periods in each segment (default: 3)
% Y         - output cell array with segments

%% Paths and variables
if ((nargin < 3) || (isempty(num_per)))
    num_per = 3;
end

%% Find the pitch marks
pitch_marks = find_pmarks(y, fs);

%% Segment the signal according to periods
Y = cell(1, fix((length(pitch_marks) - 1)/num_per));

for i = 1:size(Y,2)
    brd_left  = num_per*(i - 1) + 1;
    brd_right = brd_left + num_per;
    
    Y{1, i} = y(pitch_marks(brd_left):pitch_marks(brd_right) - 1);
end