0001 function [coherogram,phase,t,f] = MTCoherogram(lfp1,lfp2,varargin)
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060 CheckChronux('cohgramc');
0061
0062
0063 f = 1250;
0064 frequency = [];
0065 window = 5;
0066 range = [];
0067 overlap = [];
0068 step = [];
0069 show = 'off';
0070 tapers = [3 5];
0071 pad = 0;
0072 cutoffs = [0 1];
0073
0074
0075 if nargin < 2 | mod(length(varargin),2) ~= 0,
0076 error('Incorrect number of parameters (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0077 end
0078
0079
0080 if size(lfp1,2) ~= 1 && size(lfp1,2) ~= 2,
0081 error('Parameter ''lfp1'' is not a vector or a Nx2 matrix (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0082 end
0083 if size(lfp2,2) ~= 1 && size(lfp2,2) ~= 2,
0084 error('Parameter ''lfp2'' is not a vector or a Nx2 matrix (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0085 end
0086 if any(size(lfp1) ~= size(lfp2)),
0087 error('Parameters ''lfp1'' and ''lfp2'' have different sizes (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0088 end
0089
0090
0091 for i = 1:2:length(varargin),
0092 if ~ischar(varargin{i}),
0093 error(['Parameter ' num2str(i+2) ' is not a property (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).']);
0094 end
0095 switch(lower(varargin{i})),
0096 case 'frequency',
0097 frequency = varargin{i+1};
0098 if ~isdscalar(frequency,'>0'),
0099 error('Incorrect value for property ''frequency'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0100 end
0101 case 'range',
0102 range = varargin{i+1};
0103 if ~isdvector(range,'#2','<','>=0'),
0104 error('Incorrect value for property ''range'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0105 end
0106 case 'window',
0107 window = varargin{i+1};
0108 if ~isdscalar(window,'>0'),
0109 error('Incorrect value for property ''window'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0110 end
0111 case 'overlap',
0112 overlap = varargin{i+1};
0113 if ~isdscalar(overlap,'>0'),
0114 error('Incorrect value for property ''overlap'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0115 end
0116 case 'step',
0117 step = varargin{i+1};
0118 if ~isdscalar(step,'>0'),
0119 error('Incorrect value for property ''step'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0120 end
0121 case 'tapers',
0122 tapers = varargin{i+1};
0123 if ~isivector(tapers,'#2','>0'),
0124 error('Incorrect value for property ''tapers'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0125 end
0126 case 'pad',
0127 pad = varargin{i+1};
0128 if ~isiscalar(pad,'>-1'),
0129 error('Incorrect value for property ''pad'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0130 end
0131 case 'show',
0132 show = varargin{i+1};
0133 if ~isastring(show,'on','off'),
0134 error('Incorrect value for property ''show'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0135 end
0136 case 'cutoffs',
0137 cutoffs = varargin{i+1};
0138 if ~isdvector(cutoffs,'#2','>=0','<'),
0139 error('Incorrect value for property ''cutoffs'' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0140 end
0141 otherwise,
0142 error(['Unknown property ''' num2str(varargin{i}) ''' (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).']);
0143 end
0144 end
0145
0146
0147 if size(lfp1,2) == 2 && any(lfp1(:,1) ~= lfp2(:,1)),
0148 error(['Parameters ''lfp1'' and ''lfp2'' have different timestamps (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).']);
0149 end
0150
0151
0152 if isempty(frequency),
0153 if size(lfp1,2) == 2,
0154 frequency = 1/median(diff(lfp1(:,1)));
0155 else
0156 frequency = f;
0157 end
0158 end
0159
0160
0161 if isempty(step),
0162 if isempty(overlap),
0163 overlap = window/2;
0164 end
0165 else
0166 if isempty(overlap),
0167 overlap = window-step;
0168 elseif overlap ~= window-step,
0169 error('Incompatible ''step'' and ''overlap'' parameters (type ''help <a href="matlab:help MTCoherogram">MTCoherogram</a>'' for details).');
0170 end
0171 end
0172
0173
0174 parameters.Fs = frequency;
0175 if ~isempty(range), parameters.fpass = range; end
0176 parameters.tapers = tapers;
0177 parameters.pad = pad;
0178 [coherogram,phase,~,~,~,t,f] = cohgramc(lfp1(:,2),lfp2(:,2),[window window-overlap],parameters);
0179 t = t'+lfp1(1,1);
0180 f = f';
0181 coherogram = coherogram';
0182
0183
0184 gaps = diff(lfp1(:,1)) > 2*nanmedian(diff(lfp1(:,1)));
0185 if any(gaps),
0186 n = length(lfp1(:,1));
0187 t = interp1(1:n,lfp1(:,1),linspace(1,n,length(t))');
0188 end
0189
0190
0191 phase = phase';
0192 if strcmp(lower(show),'on'),
0193 figure;
0194 subplot(2,1,1);
0195 PlotColorMap(coherogram,'x',t,'y',f,'cutoffs',cutoffs);
0196 xlabel('Time (s)');
0197 ylabel('Frequency (Hz)');
0198 title('Coherogram Amplitude');
0199 subplot(2,1,2);
0200 PlotColorMap(phase,'x',t,'y',f,'cutoffs',[-pi pi]);
0201 xlabel('Time (s)');
0202 ylabel('Frequency (Hz)');
0203 title('Coherogram Phase');
0204 end