Skip to content

Commit

Permalink
Merge pull request #16 from gardner-lab/Liberti
Browse files Browse the repository at this point in the history
Liberti
  • Loading branch information
WALIII authored Aug 5, 2019
2 parents 605155d + f34cede commit 022c5b8
Show file tree
Hide file tree
Showing 27 changed files with 33,990 additions and 106 deletions.
67 changes: 35 additions & 32 deletions ActiveComm/Software/ActiveCommMk2/ActiveCommMk2.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,49 +4,52 @@
// WALIII

// Hall sensor controlled actove commutator, driven by a servo motor
// Inspired
//
// Inspired
//
#include <Servo.h>

Servo myservo;

int pos = 90;
const int analogInPin = A0;
//
#include <Servo.h>

Servo myservo;

int pos = 90;
const int analogInPin = A0;
int sensorValue = 0; // value read from the pot
int setpoint = 512;
int setpoint = 520;

void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
Serial.begin(9600);
pinMode(A0, INPUT_PULLUP);
pinMode(9, OUTPUT);
myservo.attach(9); // attaches the servo on pin 9 to the servo object

}

void loop() {


sensorValue = analogRead(analogInPin);

Serial.print("sensor = " );
Serial.println(sensorValue);


if(sensorValue> setpoint+15){
pos = pos-5;
myservo.write(pos);
delay(15);
}

if(sensorValue < setpoint-15){
pos = pos+5;

sensorValue = analogRead(analogInPin);

Serial.print("sensor = " );
Serial.println(sensorValue);


if (sensorValue > setpoint + 15) {
pos = pos - 15;
myservo.write(pos);
delay(15);
}
}

if(sensorValue > setpoint-15 && sensorValue < setpoint+15){
pos = 93;
myservo.write(pos);
}
else if (sensorValue < setpoint - 15) {
pos = pos + 15;
myservo.write(pos);
delay(15);
}

else if (sensorValue > setpoint - 15 && sensorValue < setpoint + 15) {
pos = 93;
myservo.write(pos);
}

delay(10);
delay(50);
}
93 changes: 45 additions & 48 deletions Analysis Pipeline/FS_AV_Parse.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function FS_AV_Parse(DIR,varargin)
% Parse Data from FreedomScopes
% Created: 2015/08/02
% By: WALIII
% Updated: 2017/09/23 % added gain offset.
% Updated: 2019/05/02 % added better wireless and large file handeling
% By: WALIII

% FS_AV_parse will do several things:
Expand All @@ -24,6 +24,8 @@ function FS_AV_Parse(DIR,varargin)
mat_dir=[pwd,'/mat'];
gif_dir=[pwd,'/gif'];
error_dir =[pwd,'/error'];
plot_spectrogram =0;
resize_factor = 1;

if exist(mat_dir,'dir') rmdir(mat_dir,'s'); end
if exist(gif_dir,'dir') rmdir(gif_dir,'s'); end
Expand Down Expand Up @@ -60,83 +62,82 @@ function FS_AV_Parse(DIR,varargin)
fprintf(1,formatstring,round((i/length(mov_listing))*100));
FILE = fullfile(DIR,mov_listing{i})

f = dir(FILE)
if f.bytes/1000000< 900.000
% Break into many smaller filles:
file_info = VideoReader(FILE);

%Extract Audio and video data...
try
[a_ts, a, v_ts, v] = extractmedia(FILE);
catch
v1 = VideoReader(FILE);
k = 1;
while hasFrame(v1)
v{k} = readFrame(v1);
k = k+1;
end
v = v';
a = 0;
a_ts = 0;
v_ts = 0;
end
clear k V1;

else
% v1 = VideoReader(FILE);
% k = 1;
% while hasFrame(v1)
% v{k} = readFrame(v1);
% k = k+1;
% end
% v = v';
% a = 0;
% a_ts = 0;
% v_ts = 0;
% end
% clear k V1;

disp('moving file- to large for batch processing... use FS_AV_Parse(pwd,large)');
%LargeDir = strcat(path,'/','LargeFiles');
movefile(FILE, error_dir)
continue;
end
% else
% disp('moving file- to large for batch processing... use FS_AV_Parse(pwd,large)');
% %LargeDir = strcat(path,'/','LargeFiles');
% movefile(FILE, error_dir)
% continue;
% end





% Format VIDEO DATA
[video.width, video.height, video.channels] = size(v{1});
video.times = v_ts% 0.1703*(day-1);
video.times = v_ts;% 0.1703*(day-1);

video.nrFramesTotal = size(v,1);
video.FrameRate = 1/mean(diff(v_ts));
for ii = 1: size(v,1)
est(:,:,:,ii) = v{ii};
end

%
try
disp('Performing Gain correction')
noise = squeeze(est(:,:,3,:)); % blue channel
noise = (((squeeze(mean(mean(noise(:,1:80,:),1))))));%+(squeeze(mean(mean(noise(1:10,:,:),2)))))/2);

sig = squeeze(est(:,:,2,:)); % green channel
sig = (squeeze(mean(mean(sig(:,1:80,:),1))));
video.gain = noise;
for ii = 1: size(v,1)
%video.frames(:,:,:,ii) = v{ii}-(noise(ii,:)-min(noise(30:end,:)));
video.frames(:,:,:,ii) = v{ii}; %-(noise(ii,:)-min(noise(30:end)));
temp = v{ii}; %-(noise(ii,:)-min(noise(30:end)));
video.frames(:,:,:,ii) = imresize(temp,resize_factor);
v{ii} = []; % empty the buffer
temp = [];
end
clear V %empty the buffer
try
disp('Performing Gain correction')
noise = squeeze(mean(mean(squeeze(video.frames(:,[1:30, video.height-30:video.height],3,:)),1),2)); % blue channel
noise2 = squeeze(mean(mean(squeeze(video.frames([1:30 video.width-30:video.width],:,3,:)),1),2)); % blue channel
noise = (noise+noise2)/2;
% sig = squeeze(est(:,:,2,:)); % green channel
% sig = (squeeze(mean(mean(sig(:,1:80,:),1))));
video.gain = noise;
clear noise;
clear est;
catch
video.gain = [];
end



% Format AUDIO DATA
audio.nrChannels = 1;
audio.nrChannels = size(a,2);
audio.bits = 16;
audio.nrFrames = length(a);
audio.data = double(a);
audio.rate = 48000;
audio.TotalDurration = audio.nrFrames/48000;
audio.times = a_ts;
mic_data = double(a);
fs = 48000;


[b,a]=ellip(5,.2,80,[500]/(fs/2),'high');
plot_data=mic_data./abs(max(mic_data));

if plot_spectrogram ==1;
[b,a]=ellip(5,.2,80,[500]/(fs/2),'high');
plot_data=mic_data./abs(max(mic_data));
try
[s,f,t]=fb_pretty_sonogram(filtfilt(b,a,mic_data./abs(max(mic_data))),fs,'low',2.5,'zeropad',0);

Expand All @@ -148,15 +149,11 @@ function FS_AV_Parse(DIR,varargin)
catch
disp('no audio... skipping spectrogram');
end
save(fullfile(mat_dir,[file '.mat']),'audio','video','-v7.3');
end
save(fullfile(mat_dir,[file,'.mat']),'audio','video','-v7.3');

% clear the buffer
clear video;
clear audio;
clear a_ts;
clear a;
clear v_ts;
clear v;
clear video audio a_ts a v_ts v;
end
fprintf(1,'\n');
%%
23 changes: 16 additions & 7 deletions Analysis Pipeline/FS_Plot_ROI.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
crop_correct=0;
counteri = 1;
ring = 0; % subtract ring around ROI ( local backgrounds)

resize = 0.3


nparams=length(varargin);
Expand Down Expand Up @@ -126,6 +126,8 @@

roi_n=length(ROIS.coordinates);
roi_t=zeros(roi_n,frames);
ring_t=zeros(roi_n,frames);

ave_time=0:1/ave_fs:length(mic_data)/fs;
[path,file,ext]=fileparts(mov_listing{i});
save_file=[ file '_roi' ];
Expand All @@ -147,14 +149,15 @@
fprintf(1,formatstring,round((j/roi_n)*100));

for k=1:frames
tmp=mov_data(ROIS.coordinates{j}(:,2),ROIS.coordinates{j}(:,1),k);
tmp=mov_data(round(ROIS.coordinates{j}(:,2)*resize),round(ROIS.coordinates{j}(:,1)*resize),k);

if ring == 1;

[yCoordinates, xCoordinates] = GetRing(ROIS.coordinates{j},rows,columns);
[yCoordinates, xCoordinates] = GetRing(round(ROIS.coordinates{j}*resize),rows,columns);
annul=mov_data(yCoordinates,xCoordinates,k);
roi_t(j,k)=mean(tmp(:))-mean(annul(:));
else
roi_t(j,k)=mean(tmp(:));
ring_t(j,k) = mean(annul(:));
else
roi_t(j,k)=mean(tmp(:));

end
Expand All @@ -174,7 +177,7 @@

%------[ Background and Neuropil]--------%

[Bgnd, Npil] = FS_neuropil(mov_data,ROIS);
[Bgnd, Npil] = FS_neuropil(mov_data,ROIS,resize);

roi_ave.Bgnd{counteri}=interp1(timevec,Bgnd,ave_time,'spline');
roi_ave.Npil{counteri}=interp1(timevec,Npil,ave_time,'spline');
Expand All @@ -185,8 +188,14 @@
for j=1:roi_n
clear tmp; clear dff; clear yy2; clear yy;


if ring ==1;
% tmp=roi_t(j,:) - ring_t(j,:);
tmp = roi_t(j,:)-smooth(ring_t(j,:),12)';
tmp(:,1:10) = roi_t(j,1:10)-ring_t(j,1:10);
else
tmp=roi_t(j,:);
end

tmp = tmp(:,(1:size(timevec,2)));
if baseline==0
norm_fact=mean(tmp,3);
Expand Down
37 changes: 25 additions & 12 deletions Analysis Pipeline/FS_tiff.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ function FS_tiff(videodata,varargin)




filename = 'G.tiff';
format_switch = 1;
nparams=length(varargin);

% if mod(nparams,2)>0
% error('Parameters must be specified as parameter/value pairs');
% end
filename = 'G.tiff';

[files, n] = FS_Format(videodata,1);
for i=1:2:nparams
switch lower(varargin{i})
case 'colors'
Expand All @@ -43,22 +41,37 @@ function FS_tiff(videodata,varargin)
files = convn(files, single(reshape([1 1 1] / b, 1, 1, [])), 'same');
case 'fname'
filename=varargin{i+1};



case 'format'
format_switch=varargin{i+1};
end

end


if format_switch == 1;
[files, n] = FS_Format(videodata,1);
else
files = videodata;
end


% if rm_artifacts ==1;
%

% d = files2;
% files2 = ((d-min(d(:))) ./ (max(d(:)-min(d(:)))))*255;

% % scale data
% files = normalize(files,'range',[0,1])*255;
% maxV = max(files(:));
% minV = min(files(:));
%files = (files - min(files(:))) / (max(files(:)) - min(files(:)))*255;

% Normalize data:
%files = mat2gray(files)*256;

imwrite(uint8(files(:,:,1)),filename);
imwrite(uint8(files(:,:,1)),[filename,'.tif']);
if size(size(videodata),2) ==4
imwrite(uint8(videodata(:,:,:,1)),'RGB.tif');
imwrite(uint8(videodata(:,:,:,1)),[filename,'_','RGB.tif']);
end

%imwrite(bitshift(uint16(files(:,:,1)), 8),'G_16.tif');
Expand All @@ -75,10 +88,10 @@ function FS_tiff(videodata,varargin)
%G = filter2(fspecial('average',3),files2(:,:,i));
%GG = medfilt2(G);

imwrite(uint8(K),filename,'WriteMode','append');
imwrite(uint8(K),[filename,'.tif'],'WriteMode','append');
if size(size(videodata),2) ==4

imwrite(uint8(K2),'RGB.tif','WriteMode','append');
imwrite(uint8(K2),[filename,'_','RGB.tif'],'WriteMode','append');
end
end

Expand Down
3 changes: 1 addition & 2 deletions Analysis Pipeline/GetRing.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
G = boundary(ROI_dat);
G2 = round(G*1);

scalF = 1.1;
scalF = 1.5;
scalF2 = scalF-1;


Expand All @@ -14,7 +14,6 @@
% figure(); imagesc(h3)



[yCoordinates, xCoordinates] = find(h3);

% figure(); plot(ROI.coordinates{1}(:,1),ROI.coordinates{1}(:,2)); hold on; plot(xCoordinates,yCoordinates,'*');
Expand Down
Loading

0 comments on commit 022c5b8

Please sign in to comment.