clear all
close all

load('transient_oscillations.mat')

% generate data example
duration = max(t)
n = duration / dt+1
fmax=1/dt

figure(1)
subplot(2,1,1)
plot(t,final)
xlabel('time')
ylabel('data')


% figure(1)
% plot(w)

num_shift=n;
shifts=n/num_shift
prod=zeros(n,num_shift);

L=n/10;
w = hamming(L);

for kk = 1:num_shift
    k=(kk-1)*shifts+1;
    for j = 1:n
        %j-k
        if j>k & j-k<=L
            prod(j,kk)=final(j)*w(j-k);
        end
    end
end

ampprod=zeros(n,num_shift);
angleprod=zeros(n,num_shift);

for kk = 1:num_shift
    fftprod=fft(prod(:,kk));
    ampprod(:,kk)=abs(fftprod);
    angleprod(:,kk)=angle(fftprod);
end

P=ampprod;
T(1,:)=dt*linspace(0,n,num_shift);
F(:,1)=linspace(1/duration,fmax,n);
size(P), size(T)

subplot(2,1,2)
pcolor(T,F,P);
Pmax=max(max(P))
Pmin=min(min(P))
Tmax=max(max(T))
Tmin=min(min(T))
view(0,90);
caxis([-2,Pmax]);shading flat;
axis([0,dt*num_shift,0,2.0]);
colorbar();
ylabel('frequency')
xlabel('time [s]')