function  [features] = feature_extraction(segment,config,startStop,minC0)
%%
%   Mobilab 2012-2013  
%
%%
if nargin < 4, minC0 = []; end,                                            %minC0 will be placed empty                                   
%if nargin < 3, startStop = [0 size(segment,1)/config.fs]; end,            %Default no segmentation
if nargin < 3, startStop = []; end,                                        %startSop is left empty, no segmentation occurs
%%
% Calculate the features
mel_feat = FE(segment, config);
logmel_feat = log(mel_feat);
[MM,Mstatic] = vec2featmat(config.melbands, config.cepOrder);
mfcc_static = Mstatic*logmel_feat;  
mfcc_d_dd = MM*stacklp(logmel_feat,4);

%Features
features.mel_feat=mel_feat;
features.logmel_feat=logmel_feat;
features.mfcc_static=mfcc_static;
features.mfcc_d_dd=mfcc_d_dd;

% Standard no segmentation and threholding
indicesEvent = [1:size(mel_feat,2)];
indicesSilence = [];
indicesAboveThreshold = [1:size(mel_feat,2)];
indicesBelowThreshold = [];

%Do a segmentation
if (~isempty(startStop))
    %Determine start and stop frame
    startFrame = 1 + floor((startStop(1)/(config.framestep_ms/1000)));
    stopFrame = startFrame + ceil((startStop(2)-startStop(1))/(config.framestep_ms/1000));
    if(stopFrame > size(mel_feat,2)),stopFrame = size(mel_feat,2); end,
    %These frame indices correspond to the event
    indicesEvent = [startFrame : stopFrame];
    %These frame indices correspond to the silence
    indicesSilence = [1:startFrame-1 stopFrame+1:size(mel_feat,2)];
    %Take only those event frames where C0 > minC0
    %Take only those silence frames where C0 <= minC0
    if (~isempty(minC0))
       indicesAboveThreshold=find(mfcc_static(end,:)>minC0);
       indicesBelowThreshold=find(mfcc_static(end,:)<minC0); 
    end,
end,
features.indicesEvent=indicesEvent;
features.indicesSilence=indicesSilence;
features.indicesAboveThreshold=indicesAboveThreshold;
features.indicesBelowThreshold=indicesBelowThreshold;
end

