function [FiltFileMat]=FiltNeuroscanFiles(FileMat,LowFiltStatus,LowFrequ,LowN, 
                       HighFiltStatus,HighFrequ,HighN,StopFiltStatus,StopFrequ,StopN)
%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.                                                                      
%=========================================================================================
% Filtering of cnt neuroscan data if all data fits in memory
% use FiltTransNeuroscanFiles for big data sets if all data of one sensor fits in memory
%=========================================================================================
if nargin<10; StopN=6; end
if nargin<9; StopFrequ=[49 51]; end %[LineFrequ-1 LineFrequ+1];
if nargin<8; StopFiltStatus=1; end
if nargin<7; HighN=6; end
if nargin<6; HighFrequ=2; end
if nargin<5; HighFiltStatus=1; end
if nargin<4; LowN=6; end
if nargin<3; LowFrequ=40; end
if nargin<2; LowFiltStatus=1; end
if nargin<1; FileMat=[]; end

[NFiles,FileMat]=ReadFileNames(FileMat,'*.cnt','Choose Neuroscan *.cnt file(s):');
if NFiles==0; return; end
FiltFileMat=FileNameExt2Mat(FileMat,'f.cnt',3);
for FileIndex=1:NFiles
    
    [FiltFile,FiltPath,FiltFilePath]=GetFileNameOfMat(FiltFileMat,FileIndex);
    [OrigFile,OrigPath,OrigFilePath]=GetFileNameOfMat(FileMat,FileIndex);
    
    NsStruct=ReadNeuroscanStruct(OrigFilePath);
    NsHeader=NsStruct.header;
    NsEvents=NsStruct.events;
    
    SampRate=NsHeader.rate;
    NChan=NsHeader.nchannels;
    NPoints=NsHeader.numsamples;
    DataStart=NsEvents(1).offset;
    
    Message=char('Copying file:');
    Message=char(Message,' ');
    Message=char(Message,['Orig file: ',OrigFilePath]);
    Message=char(Message,['Copy file: ',FiltFilePath]);
    hmsgbox=msgbox(Message,'Info:','help');
    [CopyStatus,CopyMsg]=copyfile(OrigFilePath,FiltFilePath,'writable');
    if ~CopyStatus
        hmsgbox=msgbox(CopyMsg,'Info:','help');
    end
    pause(.1)
    OrigFid=fopen(OrigFilePath,'r','l');
    OrigFidSeekStatus=fseek(OrigFid,DataStart,'bof');
    if OrigFidSeekStatus==-1; warning('OrigFid fseek error'); pause; end
    FiltFid=fopen(FiltFilePath,'r+','l');
    FiltFidSeekStatus=fseek(FiltFid,DataStart,'bof');
    if FiltFidSeekStatus==-1; warning('FiltFid fseek error'); pause; end
    
    Nyquist=SampRate./2;
    
    if HighFiltStatus
        [BHigh,AHigh]=butter(HighN,HighFrequ./Nyquist,'high');
    end
    if LowFiltStatus
        [BLow,ALow]=butter(LowN,LowFrequ./Nyquist,'low');
    end
    if StopFiltStatus
        [BLow,ALow]=butter(StopN,StopFrequ./Nyquist,'stop');
    end
    DataMat=zeros(NChan,NPoints);
    for i=1:NPoints
        DataMat(:,i)=fread(OrigFid,NChan,'int16');
    end
    for i=1:NChan
        if HighFiltStatus
            fprintf(1,'high ... Frequ: %g Hz; Order: %g; Sensor: %g\n',HighFrequ,HighN,i)
            DataMat(i,:)=filtfilt(BHigh,AHigh,DataMat(i,:));
        end
        if LowFiltStatus
            fprintf(1,'low ... Frequ: %g Hz; Order: %g; Sensor: %g\n',LowFrequ,LowN,i)
            DataMat(i,:)=filtfilt(BLow,ALow,DataMat(i,:));
        end
        if StopFiltStatus
            fprintf(1,'stop ... Frequ: %g Hz; Order: %g; Sensor: %g\n',StopFrequ,StopN,i)
            DataMat(i,:)=filtfilt(BStop,AStop,DataMat(i,:));
        end
    end
    for i=1:NPoints
        Count=fwrite(FiltFid,DataMat(:,i),'int16');
        if Count~=NChan; warning('Count~=NChan'); pause; end
    end
    fclose(OrigFid);
    fclose(FiltFid);
end
return;
