Code covered by the BSD License  

Highlights from
Droplet Video Processor - code 3 of 3

image thumbnail

Droplet Video Processor - code 3 of 3

by

 

This code takes the signal generated by parts 1 & 2 and converts it to usable information.

CleanUpSig( Sig1, Sig2, WinO)
function [SigA, SigB, Controls] = CleanUpSig( Sig1, Sig2, WinO)
% This function is an attempt to organize all the adjustments I want to
% make to the signal into one organized function.  They are: shortening the
% signal, smoothing the signal,ect...


Controls = struct('Smooth', 0, 'SSpan', 0, 'shorten', 0, 'SStart',...
    0, 'SFinish', 0, 'Straighten', 0,  'Outlyers', 0);

start = input('Shorten Signal or not? [0/1)');
if start
   gate = 0;
   while ~gate
        plot(Sig1);
        first = input('First Cell? [1-?]');
        if first <= 0
            first = 1;
        end
        last = input('Last Cell?');
        if last >= size(Sig1,1)
            last = size(Sig1,1);
        end
        % the user is being silly. don't crop. 
        if first >= last
            first = 1;
            last = size(Sig1,1);
        end       
        disp(first);
        disp(last);
        gate = input('will this work? [0/1)'); 
   end
   SigA = MParse(Sig1, first, last);
   SigB = MParse(Sig2, first, last);
   Controls.SStart = first;
   Controls.SFinish = last;
   Controls.shorten = 1;  
else
   SigA = Sig1;
   SigB = Sig2;
end




start = input('Smooth Signal or not? [0/1)');
if start
    gate = 0;
    while ~gate
        plot(SigA);
        span = input('Smooth Size?');
        NewSig = smooth(SigA, span);
        plot(NewSig);
        gate = input('will this work? [0/1)'); 
    end
    SigA = smooth(SigA, span);
    SigB = smooth(SigB, span);    
    Controls.Smooth = 1;
    Controls.SSpan = span;
end




start = input('Straighten Signal or Not? [0/1)');
if start   
    NewSig = straighten(SigA, WinO);
    plot(NewSig);
    gate = input('will this work? [0/1)');
    if gate
        SigA = straighten(SigA, WinO);
        SigB = straighten(SigB, WinO);
        Controls.Straighten = 1;
    end    
end




start = input('Remove Outlyers or Not? [0/1)');
if start 
    gate = 0;
    while ~gate   
        NewSig = RemoveOutlyer(SigA);
        plot(NewSig);
        gate = input('will this work? [0/1)');
    end
    if gate
       SigA = RemoveOutlyer(SigA);
       SigB = RemoveOutlyer(SigB);
       Controls.Outlyers = 1; 
    end
end

end


function PracticeSig = RemoveOutlyer(ObjtSize)
% this function removes the outlyers from the passed array and returns the
% shortened array with outlyers removed. 

standev = std(ObjtSize);
average = mean(ObjtSize);
low = average - 2.99*standev;
high = average + 2.99*standev;
num = 0;

% figuring out how many points need removing....
% stupid isNaN function will not take doubles....
for m = 1:size(ObjtSize,1)  
    if (ObjtSize(m) <= high)&&(ObjtSize(m) >= low)
        num = num+1;
    end
end

PracticeSig = zeros(num,1);
num = 1;
for m = 1:size(ObjtSize,1)  
    if (ObjtSize(m) <= high)&&(ObjtSize(m) >= low)
        PracticeSig(num) = ObjtSize(m);
        num = num + 1;
    end
end

end


function NewSig = smooth(Sig, span)
% this function takes a signal and the span array and smooths it based on
% the size of the span array... 

   refrence = ones(span,1)/span;
   NewSig = convn(Sig, refrence, 'same');
   
end

function ObjtSize = straighten(ObjtSize, WinO)
% This function cleans up messy signals by recifying them.  This was 
% developed for the sawtooth signal. 

Frames = timeFinder(ObjtSize,WinO);
meansSig = meanSignal(ObjtSize,Frames);

overall_Ave = mean(ObjtSize);

    for m = 1:size(ObjtSize,1)
        A = ObjtSize(m) - meansSig(m);
        ObjtSize(m) = overall_Ave + A;
    end
 
end


function MeanSignal = meanSignal(ObjtSize,Frames)
% this function takes a messy signal (known as Object Size) and using the
% frames array returns a step function like signal - broken around where
% the droplet breaks off.

Frames = AddToFrames(Frames,size(ObjtSize,1));
MeanSignal = zeros(size(Frames,1)-1,1);

    for m = 2:size(Frames,1)  
      Array = MParse(ObjtSize,Frames(m-1),Frames(m));
      Mean = mean(Array); 
      
        for i = Frames(m-1):Frames(m)
            MeanSignal(i) = Mean;
        end    
    end   
 end


function NewArray = AddToFrames(Array,last)
% This function Adds 1 and last value to the frames array, if it needs it
% and returns the lengthend array. 

    if (Array(1) == 1)
        AddOne = 0;
    else
        AddOne = 1;
    end
   
    if (last <= Array(size(Array,1)))
        AddLast = 0;
    else
        AddLast = 1;
    end
     
NewArray = zeros(size(Array,1)+AddLast+AddOne,1);

   for m = (1+AddOne):(size(NewArray,1)-AddLast)
       NewArray(m) = Array(m-AddOne);
   end
   
   if AddOne
       NewArray(1) = 1;
   end

   if AddLast
       NewArray(size(NewArray,1)) = last;
   end
 
end


function Frames = timeFinder(Array, WinO)
% This function takes the OjectSize Array, and the framerate, and converts
% it to seconds.
    
    % Check for Needed Data
    if size(Array,1) <= 12 
       disp('error, not enough data - This will yield bad information'); 
    end
    
    % this is an oil in water situation, the data needs to be flipped. (YES IT REALLY DOES)
    
    if ~WinO
        high = max(Array);
        low = min(Array);
            for m = 1:size(Array,1)
                Array(m) = abs(high-Array(m))+low;
            end
    end
    clear('high','low');
   
    sample = NaN(size(Array,1),1);
    needToExamin = zeros(size(Array,1),1);
    nearEnd = 0;
    negativNum = 0;
    Flag = 0;
    
     % fill sample array
     for m = 1:size(Array,1)
          if m >= 5
            sample(m) = Array(m) - Array(m-4);  
          end
     end
     
     
     for m = 5:size(Array,1)
       % Find out if the number is negative
         if  sample(m) <=0
             negativNum = 1;
         else
             negativNum = 0;
         end
                
       % find out where we are in the array
         if m >=size(Array,1)-3
             nearEnd = 1; 
             A = [sample(m), sample(m-1),sample(m-2),sample(m-3)];    
         else
             nearEnd = 0;
             A = [sample(m), sample(m+1),sample(m+2),sample(m+3)];
         end
               
       % find out if the number needs flagging
         if negativNum
            Flag = isFlag(A, nearEnd);
         else
            Flag = 0;
         end
         needToExamin(m) = Flag;
     end
     clear('negativNum','nearEnd','Flag','A');
     
     
     NumOfDrop = 0;
     for m = 2:size(needToExamin,1)
        % This is a reset action, everytime a new block of 1's come up, we
        % have hit a frame of interest.
        if needToExamin(m) == 1  && needToExamin(m-1) == 0 
            NumOfDrop = NumOfDrop+1;
        end
     end
     
     Frames = zeros(NumOfDrop,1);
    
     v = 1;
     for m = 2:size(needToExamin,1)
        if needToExamin(m) == 1  && needToExamin(m-1) == 0 
            Frames(v) = m;
            v = v+1;
        end
     end
     clear('needToExamin', 'v'); 
    

    for m = 1:size(Frames,1) 
       r = Frames(m);
       if r <= 6
B = [Array(1), Array(2), Array(3), Array(4), Array(5),Array(6)];           
       else
B = [Array(r), Array(r-1), Array(r-2), Array(r-3), Array(r-4),Array(r-5)];
       end
       
       
       [~,r] = max(B);
       Frames(m) = Frames(m)-r+1;
    end
    
end

function bool = isFlag(ArrayB, back)
% This function checks around the negative value to ensure it is a flag of
% interes, the length is 4 

    % if negative run 1
    logical = zeros(4,1);
    
    for m = 1:4
        if ArrayB(m) <= 0 
            logical(m) = 1;
        else
            logical(m) = 0;
        end
    end
    
    if ~back
        if logical(1,1) == 1 && logical(2,1) == 1 && logical(3,1) == 1 
            bool = 1;
        else
            bool = 0;
        end
    else
        if logical(4,1)
            if logical(3,1)
                bool = 0;
            else
                bool = 1;
            end
        else
            bool = 0;
        end 
    end
end

function arrayB = MParse(arrayA, start, finish)
% this function parses up ArrayA from the starting index to the finishing
% index and returns the sample array.

    if (start == 0)||(finish - start) == 1
        arrayB = arrayA(finish);
    else
        if (finish == start)
            arrayB = arrayA(start);
        else    
            arrayB = zeros((finish - start),1);
            n = 1; 
            for m = start:finish
                arrayB(n) = arrayA(m);
                n = n+1; 
            end
        end
    end
end

Contact us