% *************************************************************************
% Written for Dynamical Neuroscience XIV, 36th Annual Meeting of the Society for Neuroscience, 2006,
% Atlanta, GA., by K.Purpura, Dept. Neurology & Neuroscience, 
% Weill Medical College of Cornell University, NYC.
% *************************************************************************

%   Electrocorticography (EcoG) recordings obtained from a patient being
%   evaluated for possible surgical intervention for epilepsy (all
%   procedures involving patients were approved by the IRB of the Weill Medical
%   College of Cornell University). Seventy-six electrodes were implanted
%   in the patient. Sixty of the electrodes were in strips placed on the
%   surface of the cortex. There are six electrodes in each strip. Sixteen
%   of the electrodes are "depth" electrodes (microelectrodes) inserted
%   into the brain. The strips are arranged as spokes in a wheel centered
%   on the ventral termination of the central sulcus. One strip in each
%   hemisphere extends to the ventral pole of the frontal lobe. One strip in
%   each hemisphere extends to the dorsal aspect of the frontal lobe. One strip
%   in each hemiphere extends to the dorsal, anterior parietal lobe. Two
%   strips extend across the anterior and posterior temporal lobes, respectively of 
%   both hemispheres. The depth electrodes are inserted through the
%   temporal lobe into its ventral surface (fusiform gyrus) in both hemispheres.
%   
close all
clear all

global Fs
global ss;  global sschop
global ATL; global ATR;
global PTL; global PTR;
global DFL; global DFR;
global VFL; global VFR;
global APL; global APR;
global MTL; global MTR;
global frontal; global dfrontal;
global temporal; global parietal;
global MT; 

ss=get(0,'ScreenSize');
sschop=[round(ss(3)/2)-.2*round(ss(3)/2) 1 round(ss(3)/2)+.2*round(ss(3)/2) ss(4)];

load EcoG_dataV6

%   Sampling rate = 500 Hz
%   each time sample is 2 msec
%   Nyquist is 1/(2*.002)=250 Hz
Fs = 500;

ATL=montage.ATL; ATR=montage.ATR; 
PTL=montage.PTL; PTR=montage.PTR; 
DFL=montage.DFL; DFR=montage.DFR;  
VFL=montage.VFL; VFR=montage.VFR; 
APL=montage.APL; APR=montage.APR;
MTL=montage.MTL; MTR=montage.MTR;

figure                      %generate figure displaying all channels for the Left Hemisphere
subplot(3,3,3); plot(APL);
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
title('APL')
subplot(3,3,1); plot(DFL); title('DFL');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,5); plot(MTL); title('MTL');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,4); plot(VFL); title('VFL');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
ylabel('mV')
xlabel('seconds')
subplot(3,3,9); plot(PTL); title('PTL');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,8); plot(ATL); title('ATL');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
set(gcf,'Units','pixels','Position',sschop);
h1=gcf;

figure                      %generate figure displaying all channels for the Right Hemisphere    
subplot(3,3,1); plot(APR);
h=get(gca,'YLim'); 
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
ylabel('mV'); xlabel('seconds');
set(gca,'YLim',[h(1) h(2)]);
title('APR')
subplot(3,3,3); plot(DFR); title('DFR');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,5); plot(MTR); title('MTR');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,6); plot(VFR); title('VFR');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,7); plot(PTR); title('PTR');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
subplot(3,3,8); plot(ATR); title('ATR');
set(gca,'XTick',[0:1000:4000],'XTickLabel',[(0:1000:4000)/Fs]);
set(gcf,'Units','pixels','Position',sschop);
h2=gcf;
figure(h1);

display(['Type ''return'' when ready to continue']);
keyboard
close all
                                %generate figure displaying one channel
                                %from the Right Hemisphere
Single_Channel_from_Each_BrainRegion;       

display(['Hit UPARROW or type ''return'' when ready to continue']);
keyboard

frontal=[DFL VFL DFR VFR];      %group channels into larger regional sets
temporal=[PTL ATL PTR ATR ];
parietal=[APL APR];
MT=[MTL MTR];

Fs=500;                         %reiterate the sampling rate, 500 Hz
window=1.;                      %length of window (in seconds) for detrending
winstep=.5;                     %size of window step for detrending
movingwin=[window winstep];

                                %locdetrend will work across the channels
                                %(column vectors), so this is a more
                                %efficiet way to handle larger data sets
dfrontal=locdetrend(frontal,Fs,movingwin);  
dtemporal=locdetrend(temporal,Fs,movingwin);
dparietal=locdetrend(parietal,Fs,movingwin);
dMT=locdetrend(MT,Fs,movingwin);

                                 %figure displaying one channel
                                 %from the Right Hemisphere (DFR); 
                                 %comparing with detrended data using locdetrend
Single_Channel_BeforeAfter_Locdetrend;

clear montage;
return;