function [m]=my_filter_individualneurons(filename) coeffthreshold=10; time_conversion=20000; f=10000; %nyquist frequency totallength=0; spikes_sparse=[]; %flags plot_unfilteredfiltered_comparison=0; plot_filteredspike_comparison=0; flag_plotconvolutions=0; for numfile=1:1%numfiles load(sprintf('%s.mat',filename)); %discards intial data from each trial b/c this intial data shows a prominent transient behavior data=[]; for i=1:length(dataStruct.data) temp=dataStruct.data(i); temp=temp{:,:}; data=[data; temp(1001:end,:)]; end spikes=[]; numpoints=length(data); data_fft=zeros(f,1); for i=1:64 %%%%%%%%%%%%%%%%%%%%%%%%% %filter %%% %%%%%%%%%%%%%%%%%%%%%%%%% clear temp; temp=detrend(data(:,i)); data_fft=fft(temp, f); data_fft2=data_fft; %zero window around frequencies with coefficients above threshold for j=1:f if abs(data_fft(j,1)) > coeffthreshold if j-2<1 low=1; else low=j-2; end if j+2>f high=f; else high=j+2; end for k=low:high data_fft(k,1)=0; end end end %zero out frequencies above 5000Hz data_fft(round(5000*f/time_conversion+1):f-round(5000*f/time_conversion-1))=0; %zero out frequencies below 500Hz data_fft(1:round(500*f/time_conversion))=0; data_fft(f-round(500*f/time_conversion-1):f)=0; %compute inverse transform temp=real(ifft(data_fft,numpoints)); temp=temp-mean(temp); if plot_unfilteredfiltered_comparison==1 subplot(8,8,i); plot(dataStruct.timeVec,dataStruct.data(:,i), 'r', dataStruct.timeVec,temp, 'g'); xlabel('Time (ms)'); ylabel('Voltage'); title('Comparison of Filtered and Unfiltered Signals'); end %%%%%%%%%%%%%%%%%%%%%%%%% %extract spikes %%% %%%%%%%%%%%%%%%%%%%%%%%%% load template_spikes numspikes=zeros(10,1); template_shape2=zeros(10,length(temp)); for neuronline=1:10 %convolve spike shape for a given neuronal cell line with filtered data shapei=template_spikes(neuronline); shapei=shapei{:,1}; temp_shape=conv(shapei',temp'); %sets limits on part of convolution that is retained conv_min=round((length(shapei)-1)/2); conv_max=conv_min+length(temp)-1; %each row of temp_shape2 is the result of convolving the template shape with the filtered signal, the ends of the convolution are discarded. temp_shape2(neuronline,:)=temp_shape(conv_min:conv_max); %find spikes for convolved data %set threshold spikefactor=.1; spikethresh=shapei'*shapei*spikefactor*std(temp)/max(abs(shapei)); %set maximum spike time spikemax=2e-7; %holds spikes contained in convolved data holder=logical(zeros(1,length(temp))); %puts spike at maximum of signal for j=1:numpoints if (temp_shape2(neuronline,j) > spikethresh)|(temp_shape2(neuronline,j) < (-1*spikethresh)) maxpoint=j; for k=j:j+round(spikemax*time_conversion) if temp_shape2(neuronline,k) > temp_shape2(neuronline,j) maxpoint=k; end end holder(1,maxpoint)=1; j=k+1; end end temp_shape2(neuronline,:)=sparse(holder); numspikes(neuronline,1)=double(holder)*double(holder)'; end if flag_plotconvolutions==1 subplot(11,1,1); plot([1:length(temp)]'/20000,temp); xlabel('Time (s)','fontsize',8); ylabel('Voltage (mv)','fontsize',8); title(sprintf('Filtered Signal of Electrode %d, Threshold Factor=%s',i,spikefactor), 'fontweight','bold'); for neuronline=1:10 subplot(11,1,neuronline+1); plot([1:length(temp)]'/20000,temp_shape2(neuronline,:)'); title(sprintf('Convolution of Template Spike %d and the Filtered Signal',neuronline),'fontsize',8,'fontweight','bold'); end end %find neurons at a given electrode that have at least a threshold number of spikes spike_threshhold=100; for nueronline=1:10 if numspikes(neuronline,1) > spikethreshold spikes=sparse([spikes'; neuronline i temp_shape2(neuronline,:)]'); end end if plot_filteredspike_comparison==1 subplot(8,8,i) plot(dataStruct.timeVec,temp,'g', dataStruct.timeVec, .0005*spikes(1:numpoints,i),'b'); xlabel('Time (ms)'); ylabel('Voltage / Spike'); title('Comparison of Filtered Signal and Extracted Spikes'); end end end % save extracted spikes as sparse matrix save(sprintf('%s_spikesindivneurons.mat',filename),'spikes'); [m]=spikes;