r_data_v.m 7.11 KB
function r_data_v(nbF)
% R_FIGS_DATA - Produces data for r_figs_v and r_figs_summary_v.
%
% Syntax:  R_DATA_V
%
%
% Example:
%    r_data_v
%
% Other m-files required: R_CHOOSE_HT
% Subfunctions: none
% MAT-files required: list1.mat 
%                     figs_data_5f_$expNumber_$camNumber_$rowNumber.mat
%
% See also: R_FIGS_V, R_FIGS_SUMMARY_V
%
% Author: Marie-Pier Marquis, Dany Dumont
% UQAR/ISMER
% email: dany_dumont@uqar.ca
% Website: http://www.ismer.ca/dumont-dany
% July 2014, Last revision: October 2014
% ______________________________________________________________________
%% Don't forget to change parameters into r_data_v

nbFStr  = num2str(nbF);
% e1      = 201307241037; % same experiment as 201307241018
% e2      = 201307241056; % same experiment as 201307241018

 e1      = 201307251513; % same experiment as 201307251413
 e2      = 201307251532; % same experiment as 201307251413

rootdir   = r_root(nbF);
listfile2 = [rootdir, '/info.mat'];
listfile  = [rootdir,'/list1.mat'];
load(listfile) 
load(listfile2)

fps     = info.fps;
type    = info.waveType;
fpsStr  = num2str(fps);
res     = info.result;
ave     = info.average;  % from r_data_pos
aveStr  = num2str(ave);  % from r_data_pos
int1    = info.int1v;     
int2    = info.int2v;     
int3    = info.int3v;      
int4    = info.int4v;   % Data between int1 + int3*tpms and int2 + int4*tpms
dt      = info.dt;      %data between 50+5*fps and 75+75*fps
dtStr   = num2str(dt);
m       = 2;              % secon order speed
sumRes  = info.sumResultV;
resV    = info.resultV;
figsdata   = info.figsData;
figsDataV = info.figsDataV;



sumdir   = [rootdir,'/',fpsStr,'fps_',nbFStr,'f_',dtStr,'dt_',aveStr,'ave',sumRes]; 

if ~exist(sumdir,'dir')
    mkdir(sumdir)
end

%% Initialization
stdTkeDt  = nan.*ones(6,10,16);
stdTke    = nan.*ones(6,10,16);
meanTke   = nan.*ones(6,10,16);
meanTkeDt = nan.*ones(6,10,16);

%% Create data file for each row of each experiment.
for i = 1 : length(list1.exp) 
    if list1.nf(i)==nbF && list1.wt(i)==type && list1.exp(i)~=e1 &&...
            list1.exp(i)~=e2
        % Data from list1.
        exp       = num2str(list1.exp(i));      
        tp        = list1.tp(i);
        hs        = list1.hs(i);        
        [h,t]     = r_choose_ht(hs,tp);     % hs and tp are listed from 0 to
                                           % 6 (for hs) and 0 to 10 (tp)
        rowCounter   = 0;                     % counter of rows
        % Initialization
        expTke    = nan;
        expVy     = nan;
        expTkeDt  = nan;
        expVyDt   = nan;
        
        for c = 1:4
            cam = num2str(c);
            for r = 1:4                
                row       = num2str(r);
                rowCounter   = rowCounter + 1;
                
                expdir    = [rootdir,'/',exp,'_multicam'];
                resvdir   = [expdir,'/',fpsStr,'fps_',nbFStr,'f_',dtStr,...
                                  'dt_',aveStr,'ave',resV];   
                resdir  = [expdir,'/',fpsStr,'fps_',nbFStr,'f_',aveStr,res];
                datafile2 = [resdir,figsdata,fpsStr,'fps_',nbFStr,...
                                  'f_',aveStr,'_',exp,'_',cam,'_',row,'.mat'];                                               
                cd(expdir)                
                if ~exist(resvdir,'dir')
                    mkdir(resvdir)
                end
                if ~exist(resdir,'dir')
                    mkdir(resdir)
                end
                cd(resdir)
                if exist(datafile2,'file')
                    load(datafile2)
                        
                    %% (vx,vy)
                    vx = nan.*ones(nFrames, nObjects);  
                    vy = nan.*ones(nFrames, nObjects);
                    dt = 1/fps;
                    for k = 1:nObjects
                        for  n = 1:nFrames -1
                            vx(n, k) = abs((floeF(k,n+1) - floeF(k,n))/dt);
                            vy(n, k) = abs((floeF(k,n+1) - floeF(k,n))/dt);
                        end
                    end                    
                    
                    %% (vx,vy) m-order
                    vxm   = nan.*ones(nFrames, nObjects);
                    vym   = nan.*ones(nFrames, nObjects);
                    dtm   = m*(dt);
                    for k = 1:nObjects
                        for n = 1:nFrames-m
                            vxm(n,k) = abs((floeF(k,n+m) - floeF(k,n))/dtm);
                            vym(n,k) = abs((floeF(k,n+m) - floeF(k,n))/dtm);
                        end
                    end                       
                    
                    %% histogram of velosity for each row of each experiment    
                    rowVy  = nan.*ones(numel(vy),1);
                    for p = 1 : numel(vy)     
                       rowVy(p)  = vy(p);       
                    end
                    rowTke    = 0.5.*(rowVy.^2);
                    expVy   = [expVy, rowVy'];
                    expTke  = [expTke, rowTke']; 
                  
                    stdTke(h,t,rowCounter)  = nanstd(rowTke);
                    meanTke(h,t,rowCounter) = nanmean(rowTke);
                    %% histogram of velosity for delta t
                    
                    vy_dt  = nan.*ones(numel(int1+int3*fps:int2+...
                                                     int4*fps),nObjects); 
                    for k = 1:nObjects
                        vy_dt(:,k)    = vy(int1+int3*fps:int2+int4*fps,k);
                    end
                    rowVyDt  = nan.*ones(numel(vy_dt),1);
                    for p = 1 : numel(vy_dt)
                       rowVyDt(p)  = vy_dt(p);      
                    end
                    rowTkeDt   = 0.5.*(rowVyDt.^2);
                    expTkeDt = [expTkeDt, rowTkeDt'];
                    expVyDt   = [expVyDt, rowVyDt'];
                                
                    stdTkeDt(h,t,rowCounter)  = nanstd(rowTkeDt);
                    meanTkeDt(h,t,rowCounter) = nanmean(rowTkeDt);
                    %% Saving data    
                    disp(' Saving data ... ');                    
                    save([resvdir,'/',fpsStr,'fps_',nbFStr,'f_',dtStr,'dt_',...
                        aveStr,'ave',figsDataV,exp,'_',cam,'_',row], 'vx','vy','vxm',...
                        'vym','rowVy','rowTke','rowVyDt','rowTkeDt',...
                        'time','nObjects','m','-v7.3');
                    disp(' Done! ');
                    
                end
            end
        end
        
        %% Put together values by experiment
       
        disp(' Saving data ... ');
        save([resvdir,'/',fpsStr,'fps_',nbFStr,'f_',dtStr,'dt_',aveStr,'ave',figsDataV,exp],...
            'expVy','expTke','expTkeDt','expVyDt','time','nObjects','-v7.3');        
        disp(' Done! ');
        
        %% re-initialize variables
        for xx = 1 : numel(vx)
           vx(xx)  = nan;
           vy(xx)  = nan;
           vxm(xx) = nan;
           vym(xx) = nan;
        end
                
    end    
end

disp(' Saving summary data ... ');

save([sumdir,'/',fpsStr,'fps_',nbFStr,'f_',dtStr,'dt_',aveStr,'ave',sumRes],'meanTke',...
    'meanTkeDt','stdTkeDt','stdTke','-v7.3');        
disp(' Done! ');
end