common_avg_ref.m 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. function common_avg = common_avg_ref(filepath,varargin)
  2. %COMMON_AVG_REF Common average referencing.
  3. % AVG = COMMON_AVG_REF(PATH) calculates average of 32 open ephys
  4. % recording channels for performing offline common average referencing.
  5. %
  6. % AVG = COMMON_AVG_REF(PATH,CHANNEL_NUMBER,EXCLUDE_CHANNELS) takes
  7. % optional input arguments for number of recording channels (default, 32)
  8. % and any potential channels to exclude from averaging.
  9. %
  10. % See also LOAD_OPEN_EPHYS_DATA.
  11. % Panna Hegedüs, Balazs Hangya 2017/09
  12. % Default arguments
  13. prs = inputParser;
  14. addRequired(prs,'filepath',@(s)exist(s,'dir')) % data path
  15. addOptional(prs,'channel_number',32,@isnumeric) % number of recording channels
  16. addOptional(prs,'exclude_channels',[],@isnumeric) % exclude these channels from averaging
  17. addOptional(prs,'rawdatafiletag','',@ischar) % tags for raw data filenames
  18. parse(prs,filepath,varargin{:})
  19. g = prs.Results;
  20. % Calculate sum of all channels
  21. channelsum = [];
  22. for iC = 1:g.channel_number
  23. if ~ismember(iC,g.exclude_channels) % exclude channels
  24. data = load_open_ephys_data([filepath '\' '100_CH' num2str(iC) g.rawdatafiletag '.continuous']);
  25. pcs = horzcat(channelsum,data);
  26. channelsum = sum(pcs,2);
  27. end
  28. end
  29. % Calculate average
  30. NumChannels = g.channel_number - length(g.exclude_channels);
  31. common_avg = channelsum / NumChannels; % average
  32. common_avg = [common_avg common_avg common_avg common_avg]; % repeat four times to subtract from tetrode data