rmlinesmovingwinc.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. function [datac,datafit,Amps,freqs]=rmlinesmovingwinc(data,movingwin,tau,params,p,plt,f0)
  2. % fits significant sine waves to data (continuous data) using overlapping windows.
  3. %
  4. % Usage: [datac,datafit]=rmlinesmovingwinc(data,movingwin,tau,params,p,plt)
  5. %
  6. % Inputs:
  7. % Note that units of Fs, fpass have to be consistent.
  8. % data (data in [N,C] i.e. time x channels/trials or as a single vector) - required.
  9. % movingwin (in the form [window winstep] i.e length of moving
  10. % window and step size)
  11. % Note that units here have
  12. % to be consistent with
  13. % units of Fs - required
  14. % tau parameter controlling degree of smoothing for the amplitudes - we use the
  15. % function 1-1/(1+exp(-tau*(x-Noverlap/2)/Noverlap) in the region of overlap to smooth
  16. % the sinewaves across the overlap region. Noverlap is the number of points
  17. % in the overlap region. Increasing tau leads to greater overlap smoothing,
  18. % typically specifying tau~10 or higher is reasonable. tau=1 gives an almost
  19. % linear smoothing function. tau=100 gives a very steep sigmoidal. The default is tau=10.
  20. % params structure containing parameters - params has the
  21. % following fields: tapers, Fs, fpass, pad
  22. % tapers : precalculated tapers from dpss or in the one of the following
  23. % forms:
  24. % (1) A numeric vector [TW K] where TW is the
  25. % time-bandwidth product and K is the number of
  26. % tapers to be used (less than or equal to
  27. % 2TW-1).
  28. % (2) A numeric vector [W T p] where W is the
  29. % bandwidth, T is the duration of the data and p
  30. % is an integer such that 2TW-p tapers are used. In
  31. % this form there is no default i.e. to specify
  32. % the bandwidth, you have to specify T and p as
  33. % well. Note that the units of W and T have to be
  34. % consistent: if W is in Hz, T must be in seconds
  35. % and vice versa. Note that these units must also
  36. % be consistent with the units of params.Fs: W can
  37. % be in Hz if and only if params.Fs is in Hz.
  38. % The default is to use form 1 with TW=3 and K=5
  39. % Note that T has to be equal to movingwin(1).
  40. %
  41. % Fs (sampling frequency) -- optional. Defaults to 1.
  42. % fpass (frequency band to be used in the calculation in the form
  43. % [fmin fmax])- optional.
  44. % Default all frequencies between 0 and Fs/2
  45. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  46. % -1 corresponds to no padding, 0 corresponds to padding
  47. % to the next highest power of 2 etc.
  48. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  49. % to 512 points, if pad=1, we pad to 1024 points etc.
  50. % Defaults to 0.
  51. % p (P-value to calculate error bars for) - optional.
  52. % Defaults to 0.05/Nwin where Nwin is length of window which
  53. % corresponds to a false detect probability of approximately 0.05.
  54. % plt (y/n for plot and no plot respectively) - default no
  55. % plot.
  56. % f0 frequencies at which you want to remove the
  57. % lines - if unspecified the program uses the f statistic
  58. % to determine appropriate lines.
  59. %
  60. % Outputs:
  61. % datafit (fitted sine waves)
  62. % datac (cleaned up data)
  63. if nargin < 2; error('Need data and window parameters'); end;
  64. if nargin < 4 || isempty(params); params=[]; end;
  65. if length(params.tapers)==3 & movingwin(1)~=params.tapers(2);
  66. error('Duration of data in params.tapers is inconsistent with movingwin(1), modify params.tapers(2) to proceed')
  67. end
  68. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params); % set defaults for params
  69. clear err trialave
  70. if nargin < 6; plt='n'; end;
  71. %
  72. % Window,overlap and frequency information
  73. %
  74. data=change_row_to_column(data);
  75. [N,C]=size(data);
  76. Nwin=round(Fs*movingwin(1)); % number of samples in window
  77. Nstep=round(movingwin(2)*Fs); % number of samples to step through
  78. Noverlap=Nwin-Nstep; % number of points in overlap
  79. %
  80. % Sigmoidal smoothing function
  81. %
  82. if nargin < 3 || isempty(tau); tau=10; end; % smoothing parameter for sigmoidal overlap function
  83. x=(1:Noverlap)';
  84. smooth=1./(1+exp(-tau.*(x-Noverlap/2)/Noverlap)); % sigmoidal function
  85. smooth=repmat(smooth,[1 C]);
  86. %
  87. % Start the loop
  88. %
  89. if nargin < 5 || isempty(p); p=0.05/Nwin; end % default for p value
  90. if nargin < 7 || isempty(f0); f0=[]; end; % empty set default for f0 - uses F statistics to determine the frequencies
  91. params.tapers=dpsschk(tapers,Nwin,Fs); % check tapers
  92. winstart=1:Nstep:N-Nwin+1;
  93. nw=length(winstart);
  94. datafit=zeros(winstart(nw)+Nwin-1,C);
  95. Amps=cell(1,nw);
  96. freqs=cell(1,nw);
  97. for n=1:nw;
  98. indx=winstart(n):winstart(n)+Nwin-1;
  99. datawin=data(indx,:);
  100. [datafitwin,as,fs]=fitlinesc(datawin,params,p,'n',f0);
  101. Amps{n}=as;
  102. freqs{n}=fs;
  103. datafitwin0=datafitwin;
  104. if n>1; datafitwin(1:Noverlap,:)=smooth.*datafitwin(1:Noverlap,:)+(1-smooth).*datafitwin0(Nwin-Noverlap+1:Nwin,:);end;
  105. datafit(indx,:)=datafitwin;
  106. end;
  107. datac=data(1:size(datafit,1),:)-datafit;
  108. if strcmp(plt,'y');
  109. [S,f]=mtspectrumsegc(data,movingwin(1),params);
  110. [Sc,fc]=mtspectrumsegc(datac,movingwin(1),params);
  111. plot(f,10*log10(S),fc,10*log10(Sc));
  112. end;