Home > NoiseTools > nt_sgram.m

nt_sgram

PURPOSE ^

[s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags) - spectrogram

SYNOPSIS ^

function [s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags)

DESCRIPTION ^

[s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags) - spectrogram

 Without output arguments: plot cubic root of power spectrogram.
 With output arguments: return power spectrogram.
  s: spectrogram matrix (frequency X time)
  f: array of frequencies
  t: array of times

  x: data (if multidimensional, power is averaged over higher dimensions)
  window: size of hamming window in samples, if vector, shape of window
  noverlap: frame period in samples
  nfft: number of points in FFT (default 2^nextpow2(window size))
  sr: sampling rate.
  flags: 'nomean': remove mean of each window

  NoiseTools

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags)
0002 %[s,f,t]=nt_sgram(x,window,noverlap,nfft,sr,flags) - spectrogram
0003 %
0004 % Without output arguments: plot cubic root of power spectrogram.
0005 % With output arguments: return power spectrogram.
0006 %  s: spectrogram matrix (frequency X time)
0007 %  f: array of frequencies
0008 %  t: array of times
0009 %
0010 %  x: data (if multidimensional, power is averaged over higher dimensions)
0011 %  window: size of hamming window in samples, if vector, shape of window
0012 %  noverlap: frame period in samples
0013 %  nfft: number of points in FFT (default 2^nextpow2(window size))
0014 %  sr: sampling rate.
0015 %  flags: 'nomean': remove mean of each window
0016 %
0017 %  NoiseTools
0018 
0019 if nargin<1; error('!'); end
0020 if nargin<2; window=[]; end
0021 if nargin<3; noverlap=[]; end
0022 if nargin<4; nfft=[]; end
0023 if nargin<5; sr=[]; end
0024 if nargin<6; flags=[]; end
0025 
0026 if isempty(window)
0027     window=2^nextpow2(round(size(x,1)/8));
0028 end
0029 window=window(:);
0030 if numel(window)==1;
0031     window=hanning(window);
0032 end
0033 if isempty(noverlap)
0034     noverlap=floor(numel(window)/2);
0035 end
0036 if isempty(nfft)
0037     nfft=2^nextpow2(numel(window));
0038 end
0039 if isempty(sr)
0040     sr=1;
0041 end
0042 
0043 
0044 x=reshape(x,[size(x,1), prod(size(x))/size(x,1)]); % fold all higher dimesions
0045 
0046 nframes=floor( (size(x,1)-numel(window))/noverlap )  +  1;
0047 if nframes<=1; error('data too short for window size'); end
0048 
0049 s=zeros(nfft/2+1,nframes);
0050 
0051 for k=1:nframes
0052     start=(k-1)*noverlap;
0053     xx=nt_vecmult(x(start+1:start+size(window),:),window); 
0054     if ~isempty(flags) & strcmp(flags,'demean');
0055         xx=nt_demean(xx);
0056     end
0057     XX=abs(fft(xx,nfft)).^2;
0058     s(:,k)=sum(XX(1:(nfft/2+1),:),2);
0059 end
0060 
0061 f=(0:nfft/2)*sr/nfft;
0062 t= (numel(window)/2 + (0:nframes-1)*noverlap) / sr;
0063 
0064 if nargout==0;
0065     imagesc(s.^(1/5));
0066     ytick=niceticks(sr/2);
0067     set(gca,'ytick',1+ytick*nfft/sr, 'yticklabel',num2str(ytick'));
0068     ylabel('Hz');
0069     xtick=niceticks(size(x,1)/sr);
0070     set(gca,'xtick',1+xtick/noverlap*sr - nfft/2/noverlap, 'xticklabel',num2str(xtick'));
0071     xlabel('s');
0072     set(gca,'tickdir','out')
0073     s=[];t=[];f=[];
0074     axis xy
0075 end
0076 
0077

Generated on Mon 10-Nov-2014 14:40:42 by m2html © 2005