Shifting data in time

Hello! I have a data set containing several trials as shown in the figure. I now want to take the average of these trials, but as you can see, the trials are shifted, so when I take the average, it is not a good representation. I now want to shift the trials such that their maximum is at the same position so I can compute the average maximum.
Is there anyone with a good suggestion for this?

1 Comment

KL
KL on 30 Oct 2017
It would be easier if you provided some sample data.

Sign in to comment.

Answers (3)

Roos Bulthuis
Roos Bulthuis on 30 Oct 2017

0 votes

Thanks for your quick replies. I am actually looking for some lines of code that I can incorporate within a Matlab function, is that possible with the Simulink suggestion you gave? I have not used Simulink that much to know all the possibilities.
A sample data set is attached to this post. As you can see, it is a matrix with 5 entries of length 683. As the entries do not all have the same length, they are padded with NaN.
So far I thought I could find the position of the maximum for each entry, then find the position that is furthest in time and find the difference between this position and the position of the maximum for the other entries. And then delay the signal by this amount. But how can I do that? Can I just pad each entry with NaN in the front?
KL
KL on 30 Oct 2017
I suppose you want to synchronize all the maximums, try this in that case,
m = max(temp_data);
[a b c] = find(temp_data==m);
new_data = temp_data;
new_data(:,2) = circshift(new_data(:,2),a(1)-a(2));
new_data(:,3) = circshift(new_data(:,3),a(1)-a(3));
new_data(:,4) = circshift(new_data(:,4),a(1)-a(4));
new_data(:,5) = circshift(new_data(:,5),a(1)-a(5));
plot(new_data)

4 Comments

This seems like a good suggestion, thanks! The function however adds the beginning of the function to the end, which makes the graph a bit odd, as each entry is padded with NaN, and the beginning of the graph is added after these NaN. I added a figure of a different data set to show. When computing the average however, this part is gone (the first entry has NaN in place of those parts, so then the average is automatically NaN). But that means the average is a much shorter vector than the original ones. That might be a problem when further processing the average.
I hope you understand what I mean, so I will continue trying different options I think.
KL
KL on 30 Oct 2017
Edited: KL on 30 Oct 2017
why don't you trim down the parts you don't want? After you're done aligning, something like,
new_data = new_data(101:end-100,:);
This one trims down the first and last 100 values. Another idea is to interpolate instead of padding with nan. https://de.mathworks.com/help/matlab/ref/interp2.html
When computing the average it actually already trims down the signal. But I am just wondering whether this will be a problem for further analysis as I do not know yet whether a shorter signal will provide me with different results. But I think anyways that your suggestion is best, so thanks a lot!
KL
KL on 30 Oct 2017
Pleasure, good luck solving the next steps.

Sign in to comment.

Hi, you can use Signal Processing Toolbox (https://de.mathworks.com/help/signal/ug/align-signals-with-different-start-times.html).
At first find highest delay between signals with by itereating through every pair of signals :
delaynm = finddelay(signalN, signalM);
Then allign them to the maximal delayed signal maxDelSignal:
[maxDelSignal, signalN] = alignsignals(maxDelSignal, signalN);
Pad with NaNs or zeros to the signal with maximal size:
signalN(mSaxize) = NaN;
Now you have alligned signals and can simply compute mean value of the signals and find max.

Asked:

on 30 Oct 2017

Answered:

on 7 Feb 2019

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!