home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 8.ddi / SIGNAL.DI$ / SPECGRAM.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  2.4 KB  |  67 lines

  1. function yo = specgram(x,nfft,noverlap)
  2. %SPECGRAM Calculate spectrogram from signal.
  3. %    B = SPECGRAM(A,NFFT,NOVERLAP) calculates the spectrogram
  4. %    for the signal in vector A.  The signal is split into
  5. %    overlapping segments, each of which are windowed and then
  6. %    Fourier transformed to produce an estimate of the
  7. %    short-term frequency content of the signal.
  8. %    The columns of B contain spectrum estimates as a function of time.
  9. %    Time increases linearly across the columns, starting at column 1.
  10. %    Frequency increases linearly down the rows, starting at 0.
  11. %    If A is a length NX real-valued signal, B is a complex matrix
  12. %    with NFFT rows and FIX((NX-NOVERLAP)/(NFFT-NOVERLAP)) columns.  If A
  13. %    is real, only the first NFFT/2 rows are returned.
  14. %    We use the Hanning window, which you can replace with any
  15. %    other window of interest by editing this file.
  16. %    B = SPECGRAM(A) produces the spectrogram of the signal A using a
  17. %    window length which is the minimum of 256 and the length of the 
  18. %    signal, and uses 1/2 the length of the window for the amount of
  19. %    overlap.
  20. %    SPECGRAM(...) plots the absolute value of the spectrogram, using
  21. %    IMAGESC(B),AXIS XY, so the low frequency content of the first portion
  22. %    of the signal is displayed in the lower left corner of the axes.
  23.  
  24. %       L. Shure 1-1-91
  25. %       Copyright (c) 1991 by the MathWorks, Inc.
  26.  
  27. x = x(:); % make a column vector for ease later
  28. nx = length(x);
  29. if nargin == 1
  30.     nfft = min(nx,256);
  31. end
  32. if nargin < 3
  33.     noverlap = nfft/2;
  34. end
  35.  
  36. % Change the following line to change the window used.
  37. win = hanning(nfft);
  38.  
  39. % figure out number of columns for offsetting the signal
  40. % this may truncate the last portion of the signal since we'd
  41. % rather not append zeros unnecessarily - also makes the fancy
  42. % indexing that follows more difficult.
  43. ncol = fix((nx-noverlap)/(nfft-noverlap));
  44. y = zeros(nfft,ncol);
  45.  
  46. % now stuff x into columns of y with the proper offset
  47. % should be able to do this with fancy indexing!
  48. colindex = 1 + (0:(ncol-1))*(nfft-noverlap);
  49. rowindex = (1:nfft)';
  50. y(:) = x(rowindex(:,ones(1,ncol))+colindex(ones(nfft,1),:)-1);
  51. %y = x(rowindex(:,ones(1,ncol))+colindex(ones(nfft,1),:)-1);
  52.  
  53. % Apply the window to the array of offset signal segments.
  54. y(:) = win(:,ones(1,ncol)).*y;
  55.  
  56. % now fft y which does the columns
  57. y = fft(y);
  58. if ~any(any(imag(x)))
  59.     y = y(1:nfft/2,:);
  60. end
  61. % take abs, and use image to display results
  62. if nargout == 0
  63.     imagesc(abs(y));axis xy
  64. else
  65.     yo = y;
  66. end
  67.