Search code examples
matlabalignmentwaveform

Align right-shifted waveforms (action potentials)


enter image description here

I am having difficulty with my code aligning right shifted waveforms by the minimum peak point. In the left shifted, I copy the difference in indices between the desired minimum point and the given one to left of the waveform, and then delete those extra points after once this aligns the waveform. However, the same technique is not working for the right shifted ones. Any help would be much appreciated! Input (vals) is any n x 97 matrix.

function [vals] = align_wvs(wvs)
%Align_wvs - Align waveforms to minimum point
%
%align_wvs(wvs)
%
%wvs - matrix of waveforms
%
%Returns 'vals' - newly aligned matrix of waveforms

wvfrms = (wvs*10^6);                            %convert to microvolts
wvfrms = wvfrms(:,all(~isnan(wvfrms)));
min_pt = min(wvfrms(:));                     %find minimum point in wvs

[~,col] = find(wvfrms==min_pt);              %find index of min poin

if numel(col)>1
    col = col(1);
end
                                            %and that of other wvfrms

vals = zeros(size(wvfrms));               %matrix of size wvfrms, vals

for i = 1:size(vals,1)                     %for length of input
    vals(i,:) = wvfrms(i,:);                    %copy og wvfrm into vals
    nums = vals(i,:);                           %get second copy
    ind_min = min(nums);

    [~,colmin] = find(nums==ind_min);

    diff_col = col-colmin;

    if (diff_col~=0)         %if difference is not = 0  
        if (diff_col>0)      %if wvfrm is shifted to the left   
            inds = nums(1:diff_col);  %copy first n values of nums, where n is diff_rows
            new_length = length(nums)+length(inds); %extend wvfrm by amount ind
            new_vals = zeros(1,new_length);         %create new array of size new_length
            new_vals(1:(diff_col)) = inds;      %add inds to begining of new array
            new_vals(diff_col+1:end) = nums;    %add nums to rest of array

            new_vals(1:(diff_col)) = [];%delete diff_rows-1 values from         end
            vals(i,:) = new_vals;                   %add to values

        else                                    %if wvfrm is shifted to the right
            inds = nums(end+(diff_col+1):end);  %copy last n values of nums, where n is diff_rows

            new_length = length(nums)+length(inds); %extend wvfrm by amount ind
            new_vals = zeros(1,new_length);         %create new array of size new_length
            new_vals(end+(diff_col+1):end) = inds;%add inds to end of new array

            new_vals(1:(end+(diff_col))) = nums;%add nums to rest of array

            new_vals(1:(diff_col*-1)) = [];   %delete diff_rows-1 values from begining
            vals(i,:) = new_vals;                     %add to values

        end
    end
end

end

  • List item

Solution

  • Does replacing the if (diff_col~=0) block with the following work?

    if (diff_col~=0) 
        if (diff_col>0)
            vals(i,:) = [zeros(1,diff_col) nums(1:(end-diff_col))];   
        else
            vals(i,:) = [nums((-diff_col+1):end) zeros(1,-diff_col)];
        end
    end