How do I calculate the duration of a peak event for a cell activity signal?

15 次查看(过去 30 天)
I have a set of cell activity data plotted over time. I used the findpeaks function to detect peak "events" (in orange circles in screenshot below) which is measured as "dFF." I want to calculate how long each detected peak lasts for. Specifically, I want the code to take the time point of the lowest valley/point of the signal 2 seconds before and after the peak is detected and to subtract those time points to calculate the duration of the peak event. How would I be able to accomplish this?
Example code for the image below:
for i = 1:ncells % loops through each cell
fig_title = strcat('DFF, Cell # ', num2str(i));
[pks, locs] = findpeaks(z_dff(i,:), MinPeakProminence=2, MinPeakDistance=10); % detects peaks in cell activity
plot(timeSec, z_dff(i,:), timeSec(locs), pks, "o")
title(fig_title)
xlim([1, maxTime])
ylim ([(min(z_dff, [], "all")-2) (max(z_dff,[],"all"))])
xlabel('Time');
ylabel('dFF');
pause;
end

回答(1 个)

Vinay
Vinay 2024-9-3,11:14
编辑:Vinay 2024-9-3,11:15
Hii Esther,
As per my understanding you want to calculate the duration of the peak, which is defined as the time difference between the lowest valley before and after 2 seconds of the peak time.
The approach involves identifying the indices of the data points within the intervals [peakTime - 2, peakTime] and [peakTime, peakTime + 2], and then calculating the minimum values of "dff" within these intervals.
time = 0:0.1:10; % Example time vector
dFF = sin(time) + 1*randn(size(time)); % Example dFF signal with noise
% Find peaks
[peakValues, peakLocs] = findpeaks(dFF, time, MinPeakProminence=0.5, MinPeakDistance=2);
% Initialize array to store durations
peakDurations = zeros(length(peakLocs), 1);
% Loop through each peak to calculate duration
for i = 1:length(peakLocs)
peakTime = peakLocs(i);
% Find indices within the intervals
preIntervalIdx = find(time >= peakTime - 2 & time <= peakTime);
postIntervalIdx = find(time >= peakTime & time <= peakTime + 2);
% Ensure intervals are valid
if ~isempty(preIntervalIdx) && ~isempty(postIntervalIdx)
% Find minimum valley values in each interval
preValleyValue = min(dFF(preIntervalIdx));
postValleyValue = min(dFF(postIntervalIdx));
% Find corresponding times for these minimum values
preValleyTime = time(preIntervalIdx(dFF(preIntervalIdx) == preValleyValue));
postValleyTime = time(postIntervalIdx(dFF(postIntervalIdx) == postValleyValue));
% Calculate duration
peakDurations(i) = postValleyTime - preValleyTime;
else
% If no valid valleys are found, set duration to NaN
peakDurations(i) = NaN;
end
end
% Display results
disp('Peak Durations:');
disp(peakDurations);
% Plot results
figure;
plot(time, dFF);
hold on;
plot(peakLocs, peakValues, 'ro'); % Peaks
for i = 1:length(peakLocs)
if ~isnan(peakDurations(i))
plot([preValleyTime, postValleyTime], [preValleyValue, postValleyValue], 'go'); % Valleys
end
end
title('Peaks and Minimum Valleys');
xlabel('Time');
ylabel('dFF');
legend('Signal', 'Peaks', 'Valleys');
Kindly refer to the following documentation for “findpeaks”:
I hope this resolves the issue!

标签

Community Treasure Hunt

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

Start Hunting!

Translated by