% Initialization steps.
format long g;
format compact;
fontSize = 10;
%% definice dat
Fc=[25 31 40 50 62 80 100 125 160 200 250 315 400 500 630 800 1000 1250 1600 2000 2500 3150 4000 5000 6400 8000 10000 12000 16000 20015],
fs=48000
dbmax=-5 %[dB]
dbmin=-25
pasmo=17
poradi_poklesu=2
obalka=OBALKY_500Hz(3,:)
maximum=find(obalka==0)
pocatek_obalky=min(maximum)
%eval(sprintf('obalka(1,1:length(obalka)-pocatek_obalky+1)=OBALKY_%dHz(poradi_poklesu,pocatek_obalky:length(obalka))',Fc(1,pasmo)))

hodnotymensi=find(obalka < dbmax);
pocatek=min(hodnotymensi)
hodnotyvetsi=find(obalka > dbmin);
konec=max(hodnotyvetsi)
pokles=obalka(1,pocatek:konec)

delkapoklesu=length(pokles)
n=delkapoklesu
caszkr=delkapoklesu/fs
diftime=caszkr/delkapoklesu

t2=0:diftime:caszkr-diftime;
x=t2;

y=pokles;
%% LINEARNI REGRESE
linearCoefficients = polyfit(x, y, 1)

rt=-60/linearCoefficients(1,1);

%% LINEARNI REGRESE
xFit = linspace(0, rt, fs*rt);
yFit = polyval(linearCoefficients, xFit)-5;
y=obalka(1,pocatek_obalky:pocatek_obalky+rt*fs*0.5)

x=0:diftime:rt*0.5;

%% GRAF
figure(2)
plot(x, y, 'b-', 'LineWidth', 0.7);
%cleanfigure('minimumPointsDistance',0.000001)
xlabel('t (s)', 'FontSize', fontSize);
ylabel('L (dB)', 'FontSize', fontSize);
	xlim([0 0.35]);
    ylim([-45 0]);
xlimits=xlim;
% cela obrazovka
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
%set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off') 
hold on;
plot(xFit(1,1:1000:length(xFit))+0.06, yFit(1,1:1000:length(yFit)), 'r', 'LineWidth', 0.7);
cleanfigure('minimumPointsDistance',0.001)
line([0,rt],[dbmin,dbmin],'Color',[.4,.4,.4],'LineWidth',.5);
line([0,rt],[dbmax,dbmax],'Color',[.4,.4,.4],'LineWidth',.5);
grid on;

legend('Casove integrovany prubeh', 'Regresni primka', 'Location', 'Northeast');
set(legend,'FontSize',7);
%casvmili=round(rt*1000);
%casvs=casvmili/1000;
%text(.6 * xlimits(2),-15,['T_2_0 (s) = ',num2str(rt,'%1.3f')],'FontSize',10,'color','k');
matlab2tikz('demonstrace_sum.tikz','height', '\figureheight', 'width', '\figurewidth');




