function [m]=my_filter(filename) coeffthreshold=10; time_conversion=20000; totallength=0; spikes_sparse=[]; %flags plot_unfilteredfiltered_comparison=0; plot_filteredspike_comparison=0; for numfile=1:1%numfiles load(sprintf('%s.mat',filename)); data=[]; for i=1:length(dataStruct.data) temp=dataStruct.data(i); temp=temp{:,:}; data=[data; temp(1001:end,:)]; end spikes=logical(zeros(size(data))); numpoints=length(data); f=numpoints; %nyquist frequency 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 %%% %%%%%%%%%%%%%%%%%%%%%%%%% %set threshold to be 3.2 standard deviations above mean spikethresh=3.2*std(temp); %set maximum spike time spikemax=2e-7; %puts spike at maximum of signal for j=1:numpoints if (temp(j)>spikethresh)|(temp(j)<(-1*spikethresh)) maxpoint=j; for k=j:j+round(spikemax*time_conversion) if temp(k) > temp(j) maxpoint=k; end end spikes(maxpoint,i)=1; j=k+1; 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 %Compress to spare matrix spikes_sparse = [spikes_sparse; sparse(spikes)]; totallength=totallength + numpoints; clear temp dataStruct spikes; end spikes=spikes_sparse; % save extracted spikes as sparse matrix save(sprintf('%s_spikes.mat',filename),'spikes'); [m]=spikes;