Skip to content

Instantly share code, notes, and snippets.

@alcantarar
Last active May 18, 2020 23:08
Show Gist options
  • Save alcantarar/8a6a7d5879c01a68fca183760fb3a367 to your computer and use it in GitHub Desktop.
Save alcantarar/8a6a7d5879c01a68fca183760fb3a367 to your computer and use it in GitHub Desktop.

Data Discontinuity

Kenzie, you've got a fun situation because in POE2_Data_Discontinuity.xls, it's not just the signal exceeding the +/- 180 range. It's a signal exceeding 2x, 3x... the +/-180 range! I wasn't able to fix it completely with the time I currently have, but I think a place to start is breaking the trial into slices based on the "jumps" occurring in the signal. After you break the trial into slices, you need to shift signals by a certain amount and in a particular direction. That amount will depend on two things:

  1. the first value in that slice
  2. the number of times the signal has consecutively exceeded the range in the same direction.

What I thought would work is puting some shifting code into a while loop that keeps checking and shifting the slices until diff(y) > 20 returns nothing. That would imply that there are no more big jumps. However, we don't know how much to shift the signal or in which direction. We know that the shift amount is some multiple of the first value, but you'll have to think about point #2 above. For the example you sent, it works to shift by 2x the amount and then 4x the amount (while loop passes #1 and #2). But I don't think this will work for every signal. It might?

Another way to go about it might be to determe the shift multiplier at every loop.

There's an issue with my plot though, if you look early slices, the shifted slices match up well, but if you look at later slices, you'll see that it looks like they're like 1 frame off. Not sure why. But take a look at frames 2397-8 and 2435-6, Anyways, here's what I used to produce the plot:

y = data(:,2);
idx = [1,NaN, length(y)];
col = [0.1216, 0.4667, 0.7059;...
    214/255, 39/255, 40/255;...
    44/255, 160/255, 44/255] ; %use tableau colors
c = 1; %color counter
hold on
grid on
plot(y, 'Color', col(c,:), 'LineWidth', 1.5) %original signal
shift = 2; %shift multiplier
while length(idx) > 2
    c = c+ 1;
    d = diff(y); %find jumps in signal
    frames = 1:length(y);
    idx = [1, frames(abs(d) > 100)+1, length(y)];%frames of signal jumps
    slice = cell(length(idx),1); %initialize 
    flip = repmat([0,1], 1, ceil(length(idx)/2)); %use logical array to determine if shift needed. assummed first slice ok (0).
    flip = flip(1:length(idx)); %might need odd number of slices
    for i = 1:length(idx)-1 %loop through slices
        slice{i,1} = y(idx(i):(idx(i+1)-1)); %store slice in column 1
        slice{i,2} = flip(i)*y(idx(i))*shift; %store shift amount in column 2
    end
    
    f = cellfun(@(a,b) a - b, slice(:,1), slice(:,2),'UniformOutput', false); %add shift to slices
    y = vertcat(f{:,1}); %cell array -> matrix
    if length(frames(abs(d) > 100)) > 0 %check to see if we got rid of all jumps
            plot(y, 'Color', col(c,:), 'LineStyle', '--', 'LineWidth', 1.5) %if not, plot most recent attempt
        shift = shift*2; % update shift amount. THIS MIGHT NOT WORK IN ALL CASES.
    else
    end
end

legend({'original', '1st shift', '2nd shift'}, 'Location', 'northwest');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment