function [Epoch,Window_Length,NEpoch,Nbad_chan,bad_chan,reference,ravgcsdm,iavgcsdm] = rd_csdm(fid);
%[Epoch,Window_Length,NEpoch,Nbad_chan,bad_chan,reference,ravgcsdm,iavgcsdm]
%         = rd_csdm(fid);
% reads in csdm file
%
%fid = valid csdm fid must have already cheacked for version = -1
%Epoch = epochlength
%Window_Length = total samples (seconds) into average
%NEpoch = number of epochs
%Nbad_chan = # of bad channels
%bad_chan = bad_channels
%reference = reference type or channel listg
%avgcsdm = cross spectral density matrix
if nargin < 1
	error('wheres the fid')
end;

Epoch = fread(fid,1,'integer*2');
Window_Length =  fread(fid,1,'integer*2');
NEpoch = fread(fid,1,'integer*2');
Nbad_chan = fread(fid,1,'integer*2');
bad_chan = fread(fid,[1,Nbad_chan],'integer*2');
ref_flag = fread(fid,1,'integer*2');
if ref_flag == 1
	reference = 'average';
elseif ref_flag == 2
	reference = 'avgmast';
elseif ref_flag == 3
	reference = 'perimeter';
elseif ref_flag == 4
	reference = 'vertex';
elseif ref_flag == 5
	reference = 'laplacian';
elseif ref_flag == 6
	nchan = fread(fid,1,'integer*2');
	reference = fread(fid,[1,nchan],'integer*2');
elseif ref_flag == 7
	sigma_v = fread(fid,1,'integer*2');
	if sigma_v > 9
	reference = ['cortical' int2str(sigma_v)];
	else
	reference = ['cortical0' int2str(sigma_v)];
	end
end;
nfreq = fread(fid,1,'integer*2');
nchan2 = fread(fid,1,'integer*2');
ravgcsdm = fread(fid,[nchan2,nfreq],'real*4');
iavgcsdm = fread(fid,[nchan2,nfreq],'real*4');
ravgcsdm = ravgcsdm';
iavgcsdm = iavgcsdm';









