How to stitch together subsets of data from acceleration time histories?

2 views (last 30 days)
I have large time histories of data (in array) with the first colomn being time and the second column being acceleration (g). I need to find the peaks that are above a certain threshold and then create a subset of data that starts say 1 second before and ends 2 seconds after the peak. After that I need the subsets stitched together to create a "final" time history. Any help would be greatly appreciated!!!

Answers (1)

Voss
Voss on 28 Feb 2024
One thing you have to consider is what to do if a peak is within 2 seconds of another peak, so that the two peaks' 3-second-long intervals overlap: do you want to combine those intervals into a single interval (so that there's no data repeating in the final output), or do you want to extract the 3-second interval around each peak independently (so that there may be data repeated in the final output)? In the code below, I chose to combine overlapping intervals.
% parameters
peak_threshold = 0.6;
time_before_peak = 1;
time_after_peak = 2;
% random data
t = linspace(600,3600,30001);
g = 0.18*randn(size(t));
% g = 0.22*randn(size(t));
data = [t;g].';
% plot the data
plot(data(:,1),data(:,2))
xlim(data([1 end],1))
% find peaks above peak_threshold
% [p,idx] = findpeaks(data(:,2),'MinPeakHeight',peak_threshold); % if you have the Signal Processing Toolbox, you can use findpeaks
idx = islocalmax(data(:,2)) & data(:,2) > peak_threshold;
p = data(idx,2);
tp = data(idx,1);
% plot the peaks
hold on
plot(tp,p,'ro')
% construct the start and end times of each interval to extract
ts_te = tp(:)+[-time_before_peak time_after_peak];
% combine overlapping intervals:
idx = 1+find(ts_te(2:end,1) <= ts_te(1:end-1,2));
for ii = numel(idx):-1:1
ts_te(idx(ii)-1,2) = ts_te(idx(ii),2);
ts_te(idx(ii),:) = [];
end
% plot a green line to indicate the start of each interval
xline(ts_te(:,1),'g')
% and a red line to indicate the end of each interval
xline(ts_te(:,2),'r')
% extract the data between each start and end time into the cell array new_data
N = size(ts_te,1);
new_data = cell(N,1);
for ii = 1:N
start_idx = find(data(:,1) >= ts_te(ii,1), 1);
end_idx = find(data(:,1) <= ts_te(ii,2), 1, 'last');
new_data{ii} = data(start_idx:end_idx,:);
end
% combine all the intervals' data
new_data = vertcat(new_data{:});
% plot the extracted acceleration data vs extracted time
figure
plot(new_data(:,1),new_data(:,2))
xlim(data([1 end],1))
% or plot the extracted acceleration data vs its sample index (i.e.,
% eliminating the time gaps between extracted intervals)
figure
plot(new_data(:,2))

Products

Community Treasure Hunt

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

Start Hunting!