function [toplot,toreuse,fftsize]=calcspec(x,fs,wintype,winms,shiftms)
%CALCSPEC(x,fs,wintype,winms,shiftms) 
%  Compute spectrogram for signal x using a window of size winms shifted by shiftms.


x=x(:)';
% winms = 30 is OK
% shiftms = 3-5 is good

winsamps=round((fs*winms)/1000);
shiftsamps=round((fs*shiftms)/1000);

halfwin=round(winsamps/2);
winsamps=2*halfwin+1;
centres=halfwin+1:shiftsamps:length(x)-halfwin;
fftsize=2.^ceil(log2(winsamps));
winvector=window(winsamps,wintype)';
numframes=length(centres);

toplot=zeros(numframes, round(fftsize/2)-2);
toreuse=zeros(numframes, fftsize);
sgrang=2:round(fftsize/2)-1;

for frm=1:numframes
  range=centres(frm)-halfwin:centres(frm)+halfwin;
  xfrag=x(range).*winvector;
  xf=fft(xfrag,fftsize);
  toreuse(frm,:)=abs(xf);
  toplot(frm,:)=abs(xf(sgrang));
end
toplot=toplot';
toreuse=toreuse';
