function period = findperiod(Signal,Timespan)

%Compute the gradient of the signal
FX = gradient(Signal);

%finding index with values between 0.1 and -0.1
[index] = find((FX <= (0.1)) & (FX >= (-0.1)));

%Set Ref_Check
if(FX(index(1)) > 0)
    Ref_Check = 1; %Positive
else
    Ref_Check = 0; %Negative
end
%Check for a change in sign
count = 1;
for i = 2: length(index)
    if(FX(index(i)) >= 0)
       Check = 1;
    else 
       Check = 0;
    end
    
    if(Check ~= Ref_Check)
        Save_index(count) = index(i);
        Ref_Check = Check;
        count = count + 1;
    end    
end
%Calculate the period
time_per_point = Timespan / length(Signal);
%Taking the mean period
if(length(Save_index) > 3)
    sum_period = 0;
    for j = 1 : length(Save_index)- 2
        temp_period = (Save_index(j+2)- Save_index(j)) * time_per_point;
        sum_period = sum_period + temp_period;
    end
end
period = sum_period / j;

