MTCoherence - Compute LFP coherence by multi-taper estimation. The coherence is computed as the average coherogram, using the <a href="http://www.chronux.org">chronux</a> toolbox. USAGE [coherence,phase,f,sc,sp] = MTCoherence(lfp1,lfp2,<options>) lfp1,lfp2 unfiltered LFP <a href="matlab:help samples">samples</a> (one channel each). <options> optional list of property-value pairs (see table below) ========================================================================= Properties Values ------------------------------------------------------------------------- 'frequency' sampling rate (in Hz) (default = from timestamps if available, otherwise 1250Hz) 'range' frequency range (in Hz) (default = all) 'tapers' relative resolution and order of the tapers [NW K] (default = [3 5]) 'pad' FFT padding (see help for <a href="matlab:help coherencyc">coherencyc</a>) (default = 0) 'show' plot results (default = 'off') ========================================================================= NOTES The LFPs can be provided either as time stamped matrices (lists of time-voltage pairs), or as a voltage vectors - in which case the frequency must be specified. OUTPUT coherence coherence magnitude phase coherence phase f frequency bins sc error on magnitude sp error on phase DEPENDENCIES This function requires the <a href="http://www.chronux.org">chronux</a> toolbox. SEE See also MTCoherogram, MTSpectrum, MTSpectrogram, PlotMean.
0001 function [coherence,phase,f,sc,sp] = MTCoherence(lfp1,lfp2,varargin) 0002 0003 %MTCoherence - Compute LFP coherence by multi-taper estimation. 0004 % 0005 % The coherence is computed as the average coherogram, using the <a href="http://www.chronux.org">chronux</a> toolbox. 0006 % 0007 % USAGE 0008 % 0009 % [coherence,phase,f,sc,sp] = MTCoherence(lfp1,lfp2,<options>) 0010 % 0011 % lfp1,lfp2 unfiltered LFP <a href="matlab:help samples">samples</a> (one channel each). 0012 % <options> optional list of property-value pairs (see table below) 0013 % 0014 % ========================================================================= 0015 % Properties Values 0016 % ------------------------------------------------------------------------- 0017 % 'frequency' sampling rate (in Hz) (default = from timestamps if 0018 % available, otherwise 1250Hz) 0019 % 'range' frequency range (in Hz) (default = all) 0020 % 'tapers' relative resolution and order of the tapers [NW K] 0021 % (default = [3 5]) 0022 % 'pad' FFT padding (see help for <a href="matlab:help coherencyc">coherencyc</a>) (default = 0) 0023 % 'show' plot results (default = 'off') 0024 % ========================================================================= 0025 % 0026 % NOTES 0027 % 0028 % The LFPs can be provided either as time stamped matrices (lists of time-voltage 0029 % pairs), or as a voltage vectors - in which case the frequency must be specified. 0030 % 0031 % OUTPUT 0032 % 0033 % coherence coherence magnitude 0034 % phase coherence phase 0035 % f frequency bins 0036 % sc error on magnitude 0037 % sp error on phase 0038 % 0039 % DEPENDENCIES 0040 % 0041 % This function requires the <a href="http://www.chronux.org">chronux</a> toolbox. 0042 % 0043 % SEE 0044 % 0045 % See also MTCoherogram, MTSpectrum, MTSpectrogram, PlotMean. 0046 0047 % Copyright (C) 2010-2014 by Michaël Zugaro 0048 % 0049 % This program is free software; you can redistribute it and/or modify 0050 % it under the terms of the GNU General Public License as published by 0051 % the Free Software Foundation; either version 3 of the License, or 0052 % (at your option) any later version. 0053 0054 % Make sure chronux is installed and functional 0055 CheckChronux('coherencyc'); 0056 0057 % Defaults 0058 f = 1250; 0059 frequency = []; 0060 range = []; 0061 show = 'off'; 0062 tapers = [3 5]; 0063 pad = 0; 0064 0065 % Check number of parameters 0066 if nargin < 2 | mod(length(varargin),2) ~= 0, 0067 error('Incorrect number of parameters (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0068 end 0069 0070 % Check parameter sizes 0071 if size(lfp1,2) ~= 1 && size(lfp1,2) ~= 2, 0072 error('Parameter ''lfp1'' is not a vector or a Nx2 matrix (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0073 end 0074 if size(lfp2,2) ~= 1 && size(lfp2,2) ~= 2, 0075 error('Parameter ''lfp2'' is not a vector or a Nx2 matrix (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0076 end 0077 0078 % Parse parameter list 0079 v = {}; 0080 for i = 1:2:length(varargin), 0081 if ~ischar(varargin{i}), 0082 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).']); 0083 end 0084 switch(lower(varargin{i})), 0085 case 'frequency', 0086 frequency = varargin{i+1}; 0087 if ~isdscalar(frequency,'>0'), 0088 error('Incorrect value for property ''frequency'' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0089 end 0090 case 'range', 0091 range = varargin{i+1}; 0092 if ~isdvector(range,'#2','<','>=0'), 0093 error('Incorrect value for property ''range'' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0094 end 0095 case 'tapers', 0096 tapers = varargin{i+1}; 0097 if ~isivector(tapers,'#2','>0'), 0098 error('Incorrect value for property ''tapers'' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0099 end 0100 case 'pad', 0101 pad = varargin{i+1}; 0102 if ~isiscalar(pad,'>-1'), 0103 error('Incorrect value for property ''pad'' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0104 end 0105 case 'show', 0106 show = varargin{i+1}; 0107 if ~isastring(show,'on','off'), 0108 error('Incorrect value for property ''show'' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).'); 0109 end 0110 otherwise, 0111 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help MTCoherence">MTCoherence</a>'' for details).']); 0112 end 0113 if ~strcmp(varargin{i},'show'), v = {v{:},varargin{i:i+1}}; end 0114 end 0115 0116 % Compute spectrogram 0117 [coherogram,phase,~,f] = MTCoherogram(lfp1,lfp2,v{:}); 0118 0119 % Compute coherence 0120 coherogram = coherogram'; 0121 coherence = mean(coherogram); 0122 sc = sqrt(var(coherogram)); 0123 phase = phase'; 0124 [phase,sp] = CircularConfidenceIntervals(phase); 0125 0126 % Plot coherence 0127 if strcmp(lower(show),'on'), 0128 figure; 0129 subplot(2,1,1); 0130 PlotMean(f,coherence,coherence-sc,coherence+sc,':'); 0131 xlabel('Frequency (Hz)'); 0132 ylabel('Coherence'); 0133 title('Coherence Amplitude'); 0134 subplot(2,1,2); 0135 PlotMean(f,phase*180/pi,sp(1,:)*180/pi,sp(2,:)*180/pi,':'); 0136 xlabel('Frequency (Hz)'); 0137 ylabel('Phase (°)'); 0138 title('Coherence Phase'); 0139 end