# rmlinesc

## PURPOSE

removes significant sine waves from data (continuous data).

## SYNOPSIS

function data=rmlinesc(data,params,p,plt,f0)

## DESCRIPTION

     removes significant sine waves from data (continuous data).

     Usage: data=rmlinesc(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 for F-test) - optional. Defaults to 0.05/N
            where N is data length. This corresponds to a false detect
            probability of approximately 0.05

           plt         (y/n for plot and no plot respectively)
           f0          frequencies at which you want to remove the
                       lines - if unspecified the program uses the f statistic
                       to determine appropriate lines.

      Outputs:
           data        (data with significant lines removed)

## CROSS-REFERENCE INFORMATION

This function calls:

- fitlinesc fits significant sine waves to data (continuous data).
- mtspectrumc Multi-taper spectrum - continuous process
- change_row_to_column Helper routine to transform 1d arrays into column vectors that are needed
- getparams Helper function to convert structure params to variables used by the

This function is called by:

## SOURCE CODE

    0001 function data=rmlinesc(data,params,p,plt,f0)
    0002 % removes significant sine waves from data (continuous data).
    0003 %
    0004 % Usage: data=rmlinesc(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 vector) - required.
    0009 %       params      structure containing parameters - params has the
    0010 %       following fields: tapers, Fs, fpass, pad
    0011 %           tapers : precalculated tapers from dpss or in the one of the following
    0012 %                    forms:
    0013 %                   (1) A numeric vector [TW K] where TW is the
    0014 %                       time-bandwidth product and K is the number of
    0015 %                       tapers to be used (less than or equal to
    0016 %                       2TW-1).
    0017 %                   (2) A numeric vector [W T p] where W is the
    0018 %                       bandwidth, T is the duration of the data and p
    0019 %                       is an integer such that 2TW-p tapers are used. In
    0020 %                       this form there is no default i.e. to specify
    0021 %                       the bandwidth, you have to specify T and p as
    0022 %                       well. Note that the units of W and T have to be
    0023 %                       consistent: if W is in Hz, T must be in seconds
    0024 %                       and vice versa. Note that these units must also
    0025 %                       be consistent with the units of params.Fs: W can
    0026 %                       be in Hz if and only if params.Fs is in Hz.
    0027 %                       The default is to use form 1 with TW=3 and K=5
    0028 %
    0029 %            Fs             (sampling frequency) -- optional. Defaults to 1.
    0030 %               fpass       (frequency band to be used in the calculation in the form
    0031 %                                   [fmin fmax])- optional.
    0032 %                                   Default all frequencies between 0 and Fs/2
    0033 %            pad            (padding factor for the FFT) - optional (can take values -1,0,1,2...).
    0034 %                    -1 corresponds to no padding, 0 corresponds to padding
    0035 %                    to the next highest power of 2 etc.
    0036 %                       e.g. For N = 500, if PAD = -1, we do not pad; if PAD = 0, we pad the FFT
    0037 %                       to 512 points, if pad=1, we pad to 1024 points etc.
    0038 %                       Defaults to 0.
    0039 %        p            (P-value for F-test) - optional. Defaults to 0.05/N
    0040 %        where N is data length. This corresponds to a false detect
    0041 %        probability of approximately 0.05
    0042 %
    0043 %       plt         (y/n for plot and no plot respectively)
    0044 %       f0          frequencies at which you want to remove the
    0045 %                   lines - if unspecified the program uses the f statistic
    0046 %                   to determine appropriate lines.
    0047 %
    0048 %  Outputs:
    0049 %       data        (data with significant lines removed)
    0050 %
    0051 data=change_row_to_column(data);
    0052 [N,C]=size(data);
    0053 if nargin < 2 || isempty(params); params=[]; end;
    0054 [tapers,pad,Fs,fpass,err,trialave,params]=getparams(params);
    0055 clear pad fpass err trialave
    0056 user_specified_pval=0;
    0057 if nargin < 3 || isempty(p);p=0.05/N; else; user_specified_pval=1; end;
    0058 if nargin < 4 || isempty(plt); plt='n'; end;
    0059 if nargin < 5; f0=[]; end;
    0060 if isempty(f0) && user_specified_pval==1; p=p/N; end;
    0061 [datafit,Amps,freqs,Fval,sig]=fitlinesc(data,params,p,'n',f0);
    0062 datan=data-datafit;
    0063 %params.tapers=dpsschk(tapers,N,Fs); % calculate the tapers
    0064
    0065 % [Fval,A,f,sig] = ftestc(data,params,p,'n');
    0066 % fmax=findpeaks(Fval,sig);
    0067 % datasine=data;
    0068 % for ch=1:C;
    0069 %     fsig=f(fmax(ch).loc);
    0070 %     Nf=length(fsig);
    0071 %     fprintf('The significant lines for channel %d and the amplitudes are \n',ch);
    0072 %     for nf=1:Nf;
    0073 %         fprintf('%12.8f\n',fsig(nf));
    0074 %         fprintf('%12.8f\n',real(A(fmax(ch).loc(nf),ch)));
    0075 %         fprintf('%12.8f\n',imag(A(fmax(ch).loc(nf),ch)));
    0076 %         fprintf('\n');
    0077 %     end;
    0078 %     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));
    0079 % end;
    0080 % % subplot(211); plot(data); hold on; plot(datasine,'r');
    0081 % datan=data-datasine;
    0082 % subplot(212); plot(datan);
    0083 if nargout==0 || strcmp(plt,'y');
    0084    figure;
    0085    [S1,f]=mtspectrumc(detrend(data),params);
    0086    subplot(321); plot(f,10*log10(S1));xlabel('frequency Hz'); ylabel('Spectrum dB'); title('Original spectrum');
    0087    subplot(323); plot(f,Fval); line(get(gca,'xlim'),[sig sig],'Color','r'); xlabel('frequency Hz');ylabel('F-statistic');
    0088    [S2,f]=mtspectrumc(detrend(datan),params);
    0089    subplot(325);plot(f,10*log10(S1),f,10*log10(S2));xlabel('frequency Hz'); ylabel('Spectrum dB'); title('Original and cleaned spectra');
    0090    subplot(322); plot((1:size(data,1))/params.Fs,data); xlabel('time s');  title('Original data');
    0091    subplot(324); plot((1:size(datan,1))/params.Fs,datan);xlabel('time s'); title('Cleaned data');
    0092 end;
    0093 data=datan;