Skip to content. | Skip to navigation

Personal tools
Log in
Sections
You are here: Home Resources Behavior Matlab functions local_realignment

local_realignment

The function locally realigns rows in a matrix according to maximal crosscorrelations between the locally selected frames. The algorithm tracks locally identified frames from the last row to the firs row in the matrix. The function was written for the purpose of realigning acoustic features of syllables in zebra finch song. Each row of the input data was an acoustic feature, sampled with millisecond resolution.

local_realignment.m — Objective-C source code, 3 kB (3903 bytes)

File contents

% local_realignment 

% Written by Primoz Ravbar Sep 2011

% The function locally realigns rows in a matrix according to maximal
% crosscorrelations between the locally selected frames. The algorithm
% tracks locally identified frames from the last row to the firs row in the
% matrix. The function was written for the purpose of realigning acoustic
% features of syllables in zebra finch song. Each row of the input data was
% an acoustic feature, sampled with millisecond resolution. 

% functions called: crosscorrelate

% The function was designed for locally realigning acoustic features of
% syllables (rows in the matrix). 

% The inputs: 
% data -- matrix of rows that need to get locally realigned;

% initial_left_cut:
% for local realignment select initial_left_cut at least 5 milliseconds after the
% beginning of each row of the data (initial_left_cut=5); 

% initial_right_cut:
% 5 milliseconds before the end of the
% rows you wish to realign (length(row)-5)

% outputs: slice_realigned -- locally realigned matrix; 
% frame -- marker points used to perform the local realignment. The 'frame'
% could be applied to the raw matrix to compute quick realignemnt without
% recomputing the marker points (skiping the cross-correlation step). 


function [slice_realigned, max_r, frame]=local_realignment(initial_left_cut, initial_right_cut, data);

clear lags;
clear r;
clear max_r;
clear sample;
clear frame;
clear slice_realigned;





left_cut=initial_left_cut;
right_cut=initial_right_cut;

frame_width=right_cut-left_cut;

s_size=size(data);

initial_index=1;

index=initial_index;
index2=1;
index3=1;

lags=zeros(1,s_size(1));
slice_template=zeros(1,right_cut-left_cut+1);
slice_realigned=zeros(1,right_cut-left_cut+1);
slice_template_aver=mean(data(s_size(1)-100:s_size(1),left_cut:right_cut));


for i=initial_index:s_size(1)-1;
    
      
   if left_cut-lags(s_size(1)-i+1)>0;       % redefine cuts
       left_cut=left_cut-lags(s_size(1)-i+1);
       right_cut=right_cut-lags(s_size(1)-i+1);
   end
   % end
   if right_cut>s_size(2)-5;
      right_cut=s_size(2)-5;
      left_cut=right_cut-frame_width;
   end
   
   if left_cut<5;
      left_cut=5;
      right_cut=left_cut+frame_width;
   end
    
    
    
    frame(s_size(1)-i,:)=[left_cut, right_cut];
    
    
    sample(s_size(1)-i,:)=data(s_size(1)-i,left_cut:right_cut);
   
    slice_template(s_size(1)-i,:)=data(s_size(1)-i,left_cut:right_cut);
    
      if index2>100;
          slice_template_aver=mean(slice_template(s_size(1)-i:s_size(1)-i+99,:));
      end 
      index2=index2+1;
      
    if index>1;
      
      [max_r(s_size(1)-index),lags(s_size(1)-index),r(s_size(1)-index,:)]=crosscorrelate(slice_template_aver,slice_template(s_size(1)-index,:));
      
      if left_cut-lags(s_size(1)-i)>0 & right_cut-lags(s_size(1)-i)<s_size(2)-1;
      
         slice_realigned(s_size(1)-i,:)=data(s_size(1)-i,left_cut-lags(s_size(1)-i):right_cut-lags(s_size(1)-i));
         frame_metrix(s_size(1)-i,initial_left_cut)=left_cut-lags(s_size(1)-i);
      end
      
      if left_cut-lags(s_size(1)-i)<1
         slice_realigned(s_size(1)-i,:)=data(s_size(1)-i,1:frame_width+1);
         frame_metrix(s_size(1)-i,initial_left_cut)=1;
      end
      
      if right_cut-lags(s_size(1)-i)>=s_size(2)-1
         slice_realigned(s_size(1)-i,:)=data(s_size(1)-i,s_size(2)-frame_width:s_size(2));
         frame_metrix(s_size(1)-i,initial_left_cut)=s_size(2)-frame_width;
      end
    
      
      plot_it=0;
      if plot_it==1;
      if lags(s_size(1)-i)~=0;
        plot(slice_realigned(s_size(1)-i,:)','Color',[i/s_size(1),0,1-i/s_size(1)]);hold on;
        plot(slice_template(s_size(1)-i,:)','r');hold on;
      end
      end
    end
    
    index=index+1;
    
end