


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 (parameters for calculating tapers [NW,K]) - optional. Defaults to [3 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 (parameters for calculating tapers [NW,K]) - optional. Defaults to [3 5] 0013 % Fs (sampling frequency) -- optional. Defaults to 1. 0014 % fpass (frequency band to be used in the calculation in the form 0015 % [fmin fmax])- optional. 0016 % Default all frequencies between 0 and Fs/2 0017 % pad (padding factor for the FFT) - optional (can take values -1,0,1,2...). 0018 % -1 corresponds to no padding, 0 corresponds to padding 0019 % to the next highest power of 2 etc. 0020 % e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT 0021 % to 512 points, if pad=1, we pad to 1024 points etc. 0022 % Defaults to 0. 0023 % p (P-value to calculate error bars for) - optional. Defaults to 0.05/N where N is data length. 0024 % plt (y/n for plot and no plot respectively) - plots the 0025 % Fratio at all frequencies if y 0026 % f0 frequencies at which you want to remove the 0027 % lines - if unspecified the program 0028 % will compute the significant lines 0029 % 0030 % 0031 % Outputs: 0032 % datafit (linear superposition of fitted sine waves) 0033 % Amps (amplitudes at significant frequencies) 0034 % freqs (significant frequencies) 0035 % Fval (Fstatistic at all frequencies) 0036 % sig (significance level for F distribution p value of p) 0037 data=change_row_to_column(data); 0038 [N,C]=size(data); 0039 if nargin < 2 || isempty(params); params=[]; end; 0040 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params); 0041 clear pad fpass err trialave; 0042 if nargin < 3 || isempty(p);p=0.05/N;end; 0043 if nargin < 4 || isempty(plt); plt='n'; end; 0044 if nargin < 5; f0=[]; end; 0045 params.tapers=dpsschk(tapers,N,Fs); % calculate the tapers 0046 [Fval,A,f,sig] = ftestc(data,params,p,plt); 0047 if isempty(f0); 0048 fmax=findpeaks(Fval,sig); 0049 freqs=cell(1,C); 0050 Amps=cell(1,C); 0051 datafit=data; 0052 for ch=1:C; 0053 fsig=f(fmax(ch).loc); 0054 freqs{ch}=fsig; 0055 Amps{ch}=A(fmax(ch).loc,ch); 0056 Nf=length(fsig); 0057 % fprintf('The significant lines for channel %d and the amplitudes are \n',ch); 0058 % for nf=1:Nf; 0059 % fprintf('%12.8f\n',fsig(nf)); 0060 % fprintf('%12.8f\n',real(A(fmax(ch).loc(nf),ch))); 0061 % fprintf('%12.8f\n',imag(A(fmax(ch).loc(nf),ch))); 0062 % fprintf('\n'); 0063 % end; 0064 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)); 0065 end; 0066 else 0067 indx = zeros( length(f0) ); 0068 for n=1:length(f0); 0069 [fsig,indx(n)]=min(abs(f-f0(n))); 0070 end; 0071 fsig=f(indx); 0072 for ch=1:C; 0073 freqs{ch}=fsig; 0074 Amps{ch}=A(indx,ch); 0075 Nf=length(fsig); 0076 % fprintf('For channel %d the amplitudes and the Fstatistic at f=%f are \n',ch,f0); 0077 % fprintf('Fstatistic = %12.8f Fthreshold = %12.8f\n',Fval(indx),sig); 0078 % fprintf('Real part of amplitude = %12.8f\n',real(A(indx,ch))); 0079 % fprintf('Imaginary part of amplitude = %12.8f\n',imag(A(indx,ch))); 0080 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)); 0081 end; 0082 end; 0083