spsvd.m 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. function [sv,sp,fm] = spsvd(data,params,mdkp)
  2. % Space frequency SVD of input data - continuous processes
  3. % Usage: [sv,sp,fm] = spsvd(data,params,mdkp)
  4. % Inputs:
  5. % data (data matrix in timexchannels form)-required
  6. % params structure containing parameters - params has the
  7. % following fields: tapers, Fs, fpass, pad
  8. % tapers : precalculated tapers from dpss or in the one of the following
  9. % forms:
  10. % (1) A numeric vector [TW K] where TW is the
  11. % time-bandwidth product and K is the number of
  12. % tapers to be used (less than or equal to
  13. % 2TW-1).
  14. % (2) A numeric vector [W T p] where W is the
  15. % bandwidth, T is the duration of the data and p
  16. % is an integer such that 2TW-p tapers are used. In
  17. % this form there is no default i.e. to specify
  18. % the bandwidth, you have to specify T and p as
  19. % well. Note that the units of W and T have to be
  20. % consistent: if W is in Hz, T must be in seconds
  21. % and vice versa. Note that these units must also
  22. % be consistent with the units of params.Fs: W can
  23. % be in Hz if and only if params.Fs is in Hz.
  24. % The default is to use form 1 with TW=3 and K=5
  25. %
  26. % Fs (sampling frequency) -- optional. Defaults to 1.
  27. % fpass (frequency band to be used in the calculation in the form
  28. % [fmin fmax])- optional.
  29. % Default all frequencies between 0 and Fs/2
  30. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  31. % -1 corresponds to no padding, 0 corresponds to padding
  32. % to the next highest power of 2 etc.
  33. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  34. % to 512 points, if pad=1, we pad to 1024 points etc.
  35. % Defaults to 0.
  36. % mdkp (number of dimensions to be kept)-optional. Default is the
  37. % maximum possible modes determined by taper parameters
  38. %
  39. % Outputs:
  40. % sv sp fm : singular values, space modes, frequency modes
  41. if nargin < 1; error('Need data'); end;
  42. if nargin < 2 || isempty(params); params=[]; end;
  43. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  44. clear err trialave params
  45. [N,NCHAN]=size(data);
  46. tapers=dpsschk(tapers,N,Fs);
  47. nfft=max(2^(nextpow2(N)+pad),N);% number of points in fft
  48. [N,K]=size(tapers);
  49. if nargin<3 || isempty(mdkp); mdkp=min(K,NCHAN);
  50. elseif mdkp > min(K,NCHAN); error('mdkp has to be less than both K and NCHAN');end;
  51. tvec=1/Fs *(1:N)';
  52. tvec=repmat(tvec,[1 K]);
  53. tvec=tvec*2*pi*i;
  54. f=getfgrid(Fs,nfft,fpass);
  55. nf=length(f);
  56. sp=zeros(NCHAN,nf,mdkp);
  57. sp=sp+i*sp;
  58. fm=zeros(K,nf,mdkp);
  59. fm=fm+i*fm;
  60. sv=zeros(nf,min([K,NCHAN]));
  61. for j=1:nf
  62. % for k=1:K
  63. % proj(:,k)=tapers(:,k).*exp(-f0*tvec');
  64. % end
  65. proj=tapers.*exp(-f(j)*tvec);
  66. tmp=data'*proj; % projected data
  67. [u,s,v]= svd(tmp,0); % svd
  68. for mk=1:mdkp,
  69. sp(:,j,mk)=u(:,mk)';
  70. fm(:,j,mk)=v(:,mk)';
  71. end
  72. sv(j,:)=diag(s);
  73. end;