fits significant sine waves to data (continuous data). Usage: [datafit,Amps,freqs,Fval,sig]=fitlinesc(data,params,p,plt,f0) Inputs: Note that units of Fs, fpass have to be consistent. data (data in [N,C] i.e. time x channels/trials or a single vector) - required. params structure containing parameters - params has the following fields: tapers, Fs, fpass, pad tapers : precalculated tapers from dpss or in the one of the following forms: (1) A numeric vector [TW K] where TW is the time-bandwidth product and K is the number of tapers to be used (less than or equal to 2TW-1). (2) A numeric vector [W T p] where W is the bandwidth, T is the duration of the data and p is an integer such that 2TW-p tapers are used. In this form there is no default i.e. to specify the bandwidth, you have to specify T and p as well. Note that the units of W and T have to be consistent: if W is in Hz, T must be in seconds and vice versa. Note that these units must also be consistent with the units of params.Fs: W can be in Hz if and only if params.Fs is in Hz. The default is to use form 1 with TW=3 and K=5 Fs (sampling frequency) -- optional. Defaults to 1. fpass (frequency band to be used in the calculation in the form [fmin fmax])- optional. Default all frequencies between 0 and Fs/2 pad (padding factor for the FFT) - optional (can take values -1,0,1,2...). -1 corresponds to no padding, 0 corresponds to padding to the next highest power of 2 etc. e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT to 512 points, if pad=1, we pad to 1024 points etc. Defaults to 0. p (P-value to calculate error bars for) - optional. Defaults to 0.05/N where N is data length. plt (y/n for plot and no plot respectively) - plots the Fratio at all frequencies if y f0 frequencies at which you want to remove the lines - if unspecified the program will compute the significant lines Outputs: datafit (linear superposition of fitted sine waves) Amps (amplitudes at significant frequencies) freqs (significant frequencies) Fval (Fstatistic at all frequencies) sig (significance level for F distribution p value of p)
0001 function [datafit,Amps,freqs,Fval,sig]=fitlinesc(data,params,p,plt,f0) 0002 % fits significant sine waves to data (continuous data). 0003 % 0004 % Usage: [datafit,Amps,freqs,Fval,sig]=fitlinesc(data,params,p,plt,f0) 0005 % 0006 % Inputs: 0007 % Note that units of Fs, fpass have to be consistent. 0008 % data (data in [N,C] i.e. time x channels/trials or a single 0009 % vector) - required. 0010 % params structure containing parameters - params has the 0011 % following fields: tapers, Fs, fpass, pad 0012 % tapers : precalculated tapers from dpss or in the one of the following 0013 % forms: 0014 % (1) A numeric vector [TW K] where TW is the 0015 % time-bandwidth product and K is the number of 0016 % tapers to be used (less than or equal to 0017 % 2TW-1). 0018 % (2) A numeric vector [W T p] where W is the 0019 % bandwidth, T is the duration of the data and p 0020 % is an integer such that 2TW-p tapers are used. In 0021 % this form there is no default i.e. to specify 0022 % the bandwidth, you have to specify T and p as 0023 % well. Note that the units of W and T have to be 0024 % consistent: if W is in Hz, T must be in seconds 0025 % and vice versa. Note that these units must also 0026 % be consistent with the units of params.Fs: W can 0027 % be in Hz if and only if params.Fs is in Hz. 0028 % The default is to use form 1 with TW=3 and K=5 0029 % 0030 % Fs (sampling frequency) -- optional. Defaults to 1. 0031 % fpass (frequency band to be used in the calculation in the form 0032 % [fmin fmax])- optional. 0033 % Default all frequencies between 0 and Fs/2 0034 % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...). 0035 % -1 corresponds to no padding, 0 corresponds to padding 0036 % to the next highest power of 2 etc. 0037 % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT 0038 % to 512 points, if pad=1, we pad to 1024 points etc. 0039 % Defaults to 0. 0040 % p (P-value to calculate error bars for) - optional. 0041 % Defaults to 0.05/N where N is data length. 0042 % plt (y/n for plot and no plot respectively) - plots the 0043 % Fratio at all frequencies if y 0044 % f0 frequencies at which you want to remove the 0045 % lines - if unspecified the program 0046 % will compute the significant lines 0047 % 0048 % 0049 % Outputs: 0050 % datafit (linear superposition of fitted sine waves) 0051 % Amps (amplitudes at significant frequencies) 0052 % freqs (significant frequencies) 0053 % Fval (Fstatistic at all frequencies) 0054 % sig (significance level for F distribution p value of p) 0055 data=change_row_to_column(data); 0056 [N,C]=size(data); 0057 if nargin < 2 || isempty(params); params=[]; end; 0058 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params); 0059 clear pad fpass err trialave; 0060 if nargin < 3 || isempty(p);p=0.05/N;end; 0061 if nargin < 4 || isempty(plt); plt='n'; end; 0062 if nargin < 5; f0=[]; end; 0063 params.tapers=dpsschk(tapers,N,Fs); % calculate the tapers 0064 [Fval,A,f,sig] = ftestc(data,params,p,plt); 0065 if isempty(f0); 0066 fmax=findpeaks(Fval,sig); 0067 freqs=cell(1,C); 0068 Amps=cell(1,C); 0069 datafit=data; 0070 for ch=1:C; 0071 fsig=f(fmax(ch).loc); 0072 freqs{ch}=fsig; 0073 Amps{ch}=A(fmax(ch).loc,ch); 0074 Nf=length(fsig); 0075 % fprintf('The significant lines for channel %d and the amplitudes are \n',ch); 0076 % for nf=1:Nf; 0077 % fprintf('%12.8f\n',fsig(nf)); 0078 % fprintf('%12.8f\n',real(A(fmax(ch).loc(nf),ch))); 0079 % fprintf('%12.8f\n',imag(A(fmax(ch).loc(nf),ch))); 0080 % fprintf('\n'); 0081 % end; 0082 datafit(:,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)); 0083 end; 0084 else 0085 indx = zeros( length(f0) ); 0086 for n=1:length(f0); 0087 [fsig,indx(n)]=min(abs(f-f0(n))); 0088 end; 0089 fsig=f(indx); 0090 for ch=1:C; 0091 freqs{ch}=fsig; 0092 Amps{ch}=A(indx,ch); 0093 Nf=length(fsig); 0094 % fprintf('For channel %d the amplitudes and the Fstatistic at f=%f are \n',ch,f0); 0095 % fprintf('Fstatistic = %12.8f Fthreshold = %12.8f\n',Fval(indx),sig); 0096 % fprintf('Real part of amplitude = %12.8f\n',real(A(indx,ch))); 0097 % fprintf('Imaginary part of amplitude = %12.8f\n',imag(A(indx,ch))); 0098 datafit(:,ch)=exp(i*2*pi*(0:N-1)'*fsig/Fs)*A(indx,ch)+exp(-i*2*pi*(0:N-1)'*fsig/Fs)*conj(A(indx,ch)); 0099 end; 0100 end; 0101