Fundamental question about extracting a portion of repeating data over a large data set.
Show older comments
So I am going to do my best to explain this... but I have a sensor that is pusling on and off at a certain time interval Pt. After each pulse the signal decays for a period of time. I would like to extract and average the final 5 seconds for each pulse. The data is in the format of a 2 set of vectors. One being the signal, and the secod being time. I am a electrochemist, and dont have much formal training with programming, so must of the last week has been simply reading these forums trying to get a basic understanding. A brief example of the data format is below:
Signal Time
100 1
50 2
25 3
12.5 4
6.25 5
3.125 6
ect...
This goes for a few 100 seconds and i would like to only extract the average of the final 5 seconds. This then repeats for a few hundred cycles.
10 Comments
David Probst
on 5 Oct 2022
William Rose
on 5 Oct 2022
@David Probst, if you can add a comment in which you attach a sample file, using the paper clip icon, that could be helpful. The file can be just a text file with 2 columns of numbers and any separator. Column headers not required, but tolerated.
William Rose
on 5 Oct 2022
Is it, like, 300 seconds of data, and each cycle is T seconds long (like 20 s), and you want the last 5 sec of each cycle? Are the cycles all the same duration, and you know the duration? Or each cycle is different, and you want get the last 5 sec before each new peak? Or do the cycles all have the same duration, but you do not know the duration?
J. Alex Lee
on 5 Oct 2022
Is the cycle period fixed for each pulse, i.e., can you make a rule a priori about what time stamps correspond to the last 5 seconds of a pulse, or is it not fixed, i.e., do you additionally need to identify when each pulse ends? These would be different problems...
David Probst
on 5 Oct 2022
David Probst
on 5 Oct 2022
David Probst
on 5 Oct 2022
William Rose
on 6 Oct 2022
The first plot appears to have minutes on the horizontal axis, since one cycle has duration ~= 1. But actually the plotted duraiton is just a hair longer than 1. The second plot appears to have "sample number", not seconds, on the horizontal axis, since one cycle has duration ~=600.
I rquested a text file with numbers, but these plots are informative. I still would like a text file, to be more elpful to you.
The baseline, but not th top of the spikes, is drifting down over time. Do you really want to average the last 5 seconds of ALL the cycles, when the last 5 sec of cycle 1 is very different from the last 5 sec of the final cycle?
Maybe that is the problem you want to address: you want to find out how the basline is drifting down with time?
David Probst
on 6 Oct 2022
David Probst
on 6 Oct 2022
Accepted Answer
More Answers (2)
William Rose
on 6 Oct 2022
Edited: William Rose
on 6 Oct 2022
[Edit: fix typo in my comments: change "No" to "Now". Code is OK, I think.]
I will make some data, assuming Pt is constant, and assuming the cycles start at time 0:
dt=0.5; Tmax=200; %adjust as needed
Pt=20.9; %adjust as needed
t=0:dt:Tmax; %elapsed time
tcycle=mod(t,Pt); %time after each reset
tau=4; %decay time
y=exp(-tcycle/tau)+randn(size(t))/5; %decay, with noise
Let's plot the data, versus time and versus cycle time.
subplot(211); plot(t,y,'-b.')
grid on; xlabel('Elapsed Time')
subplot(212); plot(tcycle,y,'b.')
grid on; xlabel('Cycle Time')
Now let's average all the values that occur in the last 5 seconds of each cycle.
y5mean=mean(y(tcycle>=Pt-5))
The line above uses the indexing ability of Matlab. It finds the mean of all the y values whose tcycle value is >= Pt-5.
1 Comment
David Probst
on 6 Oct 2022
Adjusting my answer in light of your comments and in light of the figures you posted.
In the second figure you posted, the spikes at the end of the recording have slightly higher tops and lower baseline than at the start. The simulated data below reproduces those features.
dt=0.1; Tmax=1000; %sampling interval, total duration
Pt=60; %cycle time
t=0:dt:Tmax; %elapsed time
tau=20; %decay time
y=(0.28+t/2e4).*exp(-tcycle/tau)-t/3e4-.2; %decay, with noise
Plot the data, versus time.
plot(t,y,'-b')
grid on; xlabel('Time (s)')
Make an array with a separate column for each cycle. Discard any partial cycle at the end.
Nc=floor(Tmax/Pt); %number of complete cycles
Npc=Pt/dt; %number of points per cycle
yr=reshape(y(1:Nc*Npc),[Npc,Nc]); %reshape the data into columns for cycles
yend=yr(end-round(5/dt)+1:end,:); %array with last 5 seconds of each column
Now we can find the mean of the last 5 seconds of data, for every cycle.
yendmn=mean(yend); %vector: mean of each column
tendmn=(1:Nc)*Pt-2.5; %vector: mid-time of each mean
Plot the mean values.
hold on;
plot(tendmn,yendmn,'r+');
In the code above, I find the mean of the last 5 seconds separately for every cycle, since I assume you want to understand and perhaps compensate for the baseline drift which is evident in the second figure you posted.
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

