mtfftpt.m 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. function [J,Msp,Nsp]=mtfftpt(data,tapers,nfft,t,f,findx)
  2. % Multi-taper fourier transform for point process given as times
  3. %
  4. % Usage:
  5. % [J,Msp,Nsp]=mtfftpt (data,tapers,nfft,t,f,findx) - all arguments required
  6. % Input:
  7. % data (struct array of times with dimension channels/trials;
  8. % also takes in 1d array of spike times as a column vector)
  9. % tapers (precalculated tapers from dpss)
  10. % nfft (length of padded data)
  11. % t (time points at which tapers are calculated)
  12. % f (frequencies of evaluation)
  13. % findx (index corresponding to frequencies f)
  14. % Output:
  15. % J (fft in form frequency index x taper index x channels/trials)
  16. % Msp (number of spikes per sample in each channel)
  17. % Nsp (number of spikes in each channel)
  18. if nargin < 6; error('Need all input arguments'); end;
  19. if isstruct(data); C=length(data); else C=1; end% number of channels
  20. K=size(tapers,2); % number of tapers
  21. nfreq=length(f); % number of frequencies
  22. if nfreq~=length(findx); error('frequency information (last two arguments) inconsistent'); end;
  23. H=fft(tapers,nfft,1); % fft of tapers
  24. H=H(findx,:); % restrict fft of tapers to required frequencies
  25. w=2*pi*f; % angular frequencies at which ft is to be evaluated
  26. Nsp=zeros(1,C); Msp=zeros(1,C);
  27. for ch=1:C;
  28. if isstruct(data);
  29. fnames=fieldnames(data);
  30. eval(['dtmp=data(ch).' fnames{1} ';'])
  31. indx=find(dtmp>=min(t)&dtmp<=max(t));
  32. if ~isempty(indx); dtmp=dtmp(indx);
  33. end;
  34. else
  35. dtmp=data;
  36. indx=find(dtmp>=min(t)&dtmp<=max(t));
  37. if ~isempty(indx); dtmp=dtmp(indx);
  38. end;
  39. end;
  40. Nsp(ch)=length(dtmp);
  41. Msp(ch)=Nsp(ch)/length(t);
  42. if Msp(ch)~=0;
  43. data_proj=interp1(t',tapers,dtmp);
  44. exponential=exp(-i*w'*(dtmp-t(1))');
  45. J(:,:,ch)=exponential*data_proj-H*Msp(ch);
  46. else
  47. J(1:nfreq,1:K,ch)=0;
  48. end;
  49. end;