rmlinesc.m 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. function data=rmlinesc(data,params,p,plt,f0)
  2. % removes significant sine waves from data (continuous data).
  3. %
  4. % Usage: data=rmlinesc(data,params,p,plt,f0)
  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 a single vector) - required.
  9. % params structure containing parameters - params has the
  10. % following fields: tapers, Fs, fpass, pad
  11. % tapers : precalculated tapers from dpss or in the one of the following
  12. % forms:
  13. % (1) A numeric vector [TW K] where TW is the
  14. % time-bandwidth product and K is the number of
  15. % tapers to be used (less than or equal to
  16. % 2TW-1).
  17. % (2) A numeric vector [W T p] where W is the
  18. % bandwidth, T is the duration of the data and p
  19. % is an integer such that 2TW-p tapers are used. In
  20. % this form there is no default i.e. to specify
  21. % the bandwidth, you have to specify T and p as
  22. % well. Note that the units of W and T have to be
  23. % consistent: if W is in Hz, T must be in seconds
  24. % and vice versa. Note that these units must also
  25. % be consistent with the units of params.Fs: W can
  26. % be in Hz if and only if params.Fs is in Hz.
  27. % The default is to use form 1 with TW=3 and K=5
  28. %
  29. % Fs (sampling frequency) -- optional. Defaults to 1.
  30. % fpass (frequency band to be used in the calculation in the form
  31. % [fmin fmax])- optional.
  32. % Default all frequencies between 0 and Fs/2
  33. % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...).
  34. % -1 corresponds to no padding, 0 corresponds to padding
  35. % to the next highest power of 2 etc.
  36. % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
  37. % to 512 points, if pad=1, we pad to 1024 points etc.
  38. % Defaults to 0.
  39. % p (P-value for F-test) - optional. Defaults to 0.05/N
  40. % where N is data length. This corresponds to a false detect
  41. % probability of approximately 0.05
  42. %
  43. % plt (y/n for plot and no plot respectively)
  44. % f0 frequencies at which you want to remove the
  45. % lines - if unspecified the program uses the f statistic
  46. % to determine appropriate lines.
  47. %
  48. % Outputs:
  49. % data (data with significant lines removed)
  50. %
  51. data=change_row_to_column(data);
  52. [N,C]=size(data);
  53. if nargin < 2 || isempty(params); params=[]; end;
  54. [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
  55. clear pad fpass err trialave
  56. user_specified_pval=0;
  57. if nargin < 3 || isempty(p);p=0.05/N; else; user_specified_pval=1; end;
  58. if nargin < 4 || isempty(plt); plt='n'; end;
  59. if nargin < 5; f0=[]; end;
  60. if isempty(f0) && user_specified_pval==1; p=p/N; end;
  61. [datafit,Amps,freqs,Fval,sig]=fitlinesc(data,params,p,'n',f0);
  62. datan=data-datafit;
  63. %params.tapers=dpsschk(tapers,N,Fs); % calculate the tapers
  64. % [Fval,A,f,sig] = ftestc(data,params,p,'n');
  65. % fmax=findpeaks(Fval,sig);
  66. % datasine=data;
  67. % for ch=1:C;
  68. % fsig=f(fmax(ch).loc);
  69. % Nf=length(fsig);
  70. % fprintf('The significant lines for channel %d and the amplitudes are \n',ch);
  71. % for nf=1:Nf;
  72. % fprintf('%12.8f\n',fsig(nf));
  73. % fprintf('%12.8f\n',real(A(fmax(ch).loc(nf),ch)));
  74. % fprintf('%12.8f\n',imag(A(fmax(ch).loc(nf),ch)));
  75. % fprintf('\n');
  76. % end;
  77. % datasine(:,ch)=exp(i*2*pi*(0:N-1)'*fsig/Fs)*A(fmax(ch).loc,ch)+exp(-i*2*pi*(0:N-1)'*fsig/Fs)*conj(A(fmax(ch).loc,ch));
  78. % end;
  79. % % subplot(211); plot(data); hold on; plot(datasine,'r');
  80. % datan=data-datasine;
  81. % subplot(212); plot(datan);
  82. if nargout==0 || strcmp(plt,'y');
  83. figure;
  84. [S1,f]=mtspectrumc(detrend(data),params);
  85. subplot(321); plot(f,10*log10(S1));xlabel('frequency Hz'); ylabel('Spectrum dB'); title('Original spectrum');
  86. subplot(323); plot(f,Fval); line(get(gca,'xlim'),[sig sig],'Color','r'); xlabel('frequency Hz');ylabel('F-statistic');
  87. [S2,f]=mtspectrumc(detrend(datan),params);
  88. subplot(325);plot(f,10*log10(S1),f,10*log10(S2));xlabel('frequency Hz'); ylabel('Spectrum dB'); title('Original and cleaned spectra');
  89. subplot(322); plot((1:size(data,1))/params.Fs,data); xlabel('time s'); title('Original data');
  90. subplot(324); plot((1:size(datan,1))/params.Fs,datan);xlabel('time s'); title('Cleaned data');
  91. end;
  92. data=datan;