% subrotina de estimativa dos autoespectros
% periodograma de Welch com 66% de sobreposição

close all;

% início do cálculo
if aviso_principal==0

aviso_ssi=0;
aviso_ar=0;
aviso_rdt=0;
aviso_fdd=0;
aviso_ssi_data=0;

if nout~=min(size(dados))
nout=min(size(dados));
clear string01;
for i=1:nout
string01(i)=cellstr(strcat('output#0',num2str(i)));
end
string01=cellstr(string01);
ni=1:nout;
end

clear yd dt;
R=fix(Fs/2/freq_util)
if R<1, freq_util=Fs/2;R=1;end
fc=0.8*(Fs/2)/R
fi_cwt=fc/2;

dt=1/(Fs/R);

%SNR=1000000;
%yr05=y05'+(std(y05)/SNR)*(2*rand(1,length(y05))-1.)';
%yr11=y11'+(std(y11)/SNR)*(2*rand(1,length(y11))-1.)';
%yr15=y15'+(std(y15)/SNR)*(2*rand(1,length(y15))-1.)';

%if R>1, yd(1,:)=(decimate(dados(1,:),R,8));else yd(1,:)=dados(1,:);end
%[Syy(1,:),f]=pwelch(yd(1:npf),nw,n_overlap,nw,Fs/R,'onesided');

for i=1:nout
if R>1, yd(i,:)=(decimate(dados(i,:),R,8));else yd(i,:)=dados(i,:);end
% [Syy(i,:)]=pwelch(yd(i,1:npf),nw,n_overlap,nw,Fs/R,'onesided');
end

%[Sxy]=cpsd(x(1:npf),yd05(1:npf),nw,n_overlap,nw,Fs/R,'onesided');
%plot(f,unwrap(angle(FRF05)),f,unwrap(angle(FRF11)),f,unwrap(angle(FRF15)));

aviso_principal=1;
aviso_espectro=1;
end
% fim do cálculo

if aviso_espectro==1
aviso_fdd=0;
clear Syy f1 f;
np=length(yd(1,:));
xlinha2=(np-1)*dt;
if nmedias==1
nw=np;
n_overlap=0;
else
nw=2^fix(log2(fix(3*np/(nmedias+2))))
n_overlap=fix(0.66*nw);
end
npdw=nw-n_overlap;
nmedias=fix((np-nw)/npdw+1)
npf=nw+(nmedias-1)*npdw;
df=Fs/R/nw
f1=0:df:Fs/R/2;
for i=1:nout
[Syy(i,:),f]=pwelch(yd(i,1:npf),nw,n_overlap,nw,Fs/R,'onesided');
end
aviso_espectro=0;
end

% desenha a figura01 - Espectro
figura01=figure(1);
%colorido=get(figura01,'Color');
colorido=[1 1 1];
cores=['b' 'r' 'g' 'c' 'm' 'y' 'k'];
for i=1:length(ni)
plot(f1,log10(abs(Syy(ni(i),:))),cores(1+mod(ni(i),7)));
hold on;
end
hold off;
axis tight;
graf01=gca;
%set(figura01,'Position', [200 100 800 500],'Color',colorido,'Name','Frequency Domain Analysis');
set(figura01,'Position', [20 20 800 500],'Color',colorido,'Name','Frequency Domain Analysis');
set(graf01,'OuterPosition', [0 0.2 0.8 0.8]);
xlabel('frequency (Hz)');
ylabel('log(Sy)');
title('Sy - Power Spectrum');
% fim da figura e gráfico

% Define a lista de outputs
comando_lista01='ni=get(lista01,''value'');principal;';
lista01=uicontrol(figura01,'style','listbox','position',[650 380 80 80],'callback',comando_lista01);
set(lista01,'string',string01);
set(lista01,'Max',10,'Min',1);
set(lista01,'value',ni);

% término da definição da lista de saídas

% text boxes
comando_box01a='freq_util=sscanf(get(textbox01a,''string''),''%f'');aviso_principal=0;principal;';
textbox01a=uicontrol(figura01,'style','edit','position',[690 350 40 20],'callback',comando_box01a,'HorizontalAlignment','right');
set(textbox01a,'string',num2str(freq_util,4));

comando_box01b='nmedias=sscanf(get(textbox01b,''string''),''%i'');aviso_espectro=1;principal;';
textbox01b=uicontrol(figura01,'style','edit','position',[690 320 40 20],'callback',comando_box01b,'HorizontalAlignment','right');
set(textbox01b,'string',int2str(nmedias));

% textos
uicontrol(figura01,'style','text','string','max freq(Hz)','position',[605 345 80 20],'BackgroundColor', colorido,'HorizontalAlignment','right');
uicontrol(figura01,'style','text','string','averages','position',[605 315 80 20],'BackgroundColor', colorido,'HorizontalAlignment','right');
uicontrol(figura01,'style','text','string',strcat('freq step= ',num2str(df,4),' Hz'),'position',[610 285 120 20],'BackgroundColor', colorido,'HorizontalAlignment','right');
uicontrol(figura01,'style','text','string',strcat('data points= ',int2str(npf)),'position',[610 255 120 20],'BackgroundColor', colorido,'HorizontalAlignment','right');
uicontrol(figura01,'style','text','string',strcat('points/window= ',int2str(nw)),'position',[610 225 120 20],'BackgroundColor', colorido,'HorizontalAlignment','right');

% botoes
comando_reseta='nout=0;aviso_principal=0;principal;';

bot01a=uicontrol(figura01,'string','Frequency Domain','position',[20 20 110 25],'callback','');
set(bot01a,'value',1);
set(bot01a,'enable','inactive');
bot01e=uicontrol(figura01,'string','Time signals','position',[130 20 90 25],'callback','stempo;');
%bot01b=uicontrol(figura01,'string','RD','position',[220 20 40 25],'callback','rdt;');
%bot01x=uicontrol(figura01,'string','FDD','position',[260 20 40 25],'callback','fdd;');
%bot01c=uicontrol(figura01,'string','AR','position',[300 20 40 25],'callback','ar_prony;');
%bot01d=uicontrol(figura01,'string','SSM','position',[340 20 40 25],'callback','ssi;');
%bot01y=uicontrol(figura01,'string','SSI','position',[380 20 40 25],'callback','ssi_data;');
%bot01f=uicontrol(figura01,'string','Reset','position',[660 180 50 25],'callback',comando_reseta);
%bot01cwt=uicontrol(figura01,'string','CWT','position',[420 20 40 25],'callback','cwta;');
%bot01hib=uicontrol(figura01,'string','Hybrid Estimation','position',[460 20 120 25],'callback','hibrido;');