%--------------------------------------------------------------------------
% Spectr_G is a function developped by J.C. COMTE comtejc@gmail.com
% CNC UMR CNRS 5229.
% Last Release March. 2007
%
% This work is protected by the CeCILL-C Licence (see below).
%
%
% z : represents the signal s(t).
% Fs: is the sampling frequency.
% W_DT: is the Window size.
% W_dT: is the Overlap between two consecutive windows.
% v: is a parameter allowing ('y' or 'Y') to display the signal
% and its Spectrogram.
%
% F_Mb: Magnitude Time-Frequency Matrix (to calculate the power spectrum,
% calculate the square of F_Mb).
% F_Mlg: is the Log10 of F_Mb.
% f: is the frequency vector.
% t: is the time vector.
%
% This software is governed by the [CeCILL|CeCILL-B|CeCILL-C] license under French law and
% abiding by the rules of distribution of free software. You can use,
% modify and/ or redistribute the software under the terms of the [CeCILL|CeCILL-B|CeCILL-C]
% license as circulated by CEA, CNRS and INRIA at the following URL
% "http://www.cecill.info".
%
% As a counterpart to the access to the source code and rights to copy,
% modify and redistribute granted by the license, users are provided only
% with a limited warranty and the software's author, the holder of the
% economic rights, and the successive licensors have only limited
% liability.
%
% In this respect, the user's attention is drawn to the risks associated
% with loading, using, modifying and/or developing or reproducing the
% software by the user in light of its specific status of free software,
% that may mean that it is complicated to manipulate, and that also
% therefore means that it is reserved for developers and experienced
% professionals having in-depth computer knowledge. Users are therefore
% encouraged to load and test the software's suitability as regards their
% requirements in conditions enabling the security of their systems and/or
% data to be ensured and, more generally, to use and operate it in the
% same conditions as regards security.
%
% The fact that you are presently reading this means that you have had
% knowledge of the [CeCILL|CeCILL-B|CeCILL-C] license and that you accept its terms.
%
%--------------------------------------------------------------------------
function [t, f, F_Mb, F_Mlg]=Spectr_G(z,Fs,W_DT,W_dT,v)
[nl nc]=size(z);
if nl>nc
z=z';
n=nl; nl=nc; nc=n;
end;
N_dT=floor(W_dT*Fs);
N_DT=floor(W_DT*Fs);
N=floor(nc/(N_DT-N_dT));
stp=(N_DT-N_dT);
for i=0:N-1,
n=1+i*stp;
M(i+1,:)=z(1,n:n+N_DT);
end;
[Ml Mc]=size(M)
h=mean(M,2)*ones(1,Mc);
M=M-h;
h=ones(Ml,1)*hamming(Mc)';
M=M.*h;
k=2^(2+round(log(Mc)/log(2)));
n=k/2;
f=(0:n-1)*(Fs/k);
t=((0:Ml-1)/Ml)*(nc/Fs);
F_M=fftshift(fft(M,k,2),2);
F_M=sqrt(F_M.*conj(F_M));
F_Mb=2*(F_M(:,n+1:2*n))/Mc;
F_Mb=F_Mb';
F_Mlg=20*log10(F_Mb);
if (v=='y')||(v=='Y')
figure;
subplot(2,1,1);
plot([0:nc-1]/Fs,z);
subplot(2,1,2);
pcolor(t,f,F_Mlg);
shading interp;
lighting phong;
end;