How do I find the trough of my data with stated x limits where it should occur?

17 次查看(过去 30 天)
I have a matrix M where every row represents a cluster of data. I created a for loop to extract the negative peaks and positive troughs from the spike waveform of these clusters:
% identify negative peaks and corresponding (positive) troughs
for di = 1:size(M)
[pks(di,:),q(di,:)] = min(M(di,:));
[troughs(di,:),z(di,:)] = max(M(di,:));
end
Now do I want the loop to only find the trough after the location of the peak, when the peak is always located around x = 60.
I cannot use findpeaks because of weird results. The x-axis is set from 0 to 121 in the figure and the x-values are not stated explicitly in the M matrix, how could I then still add x-limits for finding of the troughs?
In the end, I want to use q and z to calculate spike duration and classify the neurons based on this duration.
Help would be very much appreciated, I am really new to matlab so don't know all the options!

回答(2 个)

Namnendra
Namnendra 2023-4-27
As far as I understand your issue, if the peak is always located around x=60, you can modify the code to only search for the troughs after x=60 by adding a condition inside the for loop. You can use the 'find' function to locate the index of x=60 in the matrix M and then use that index as the starting point for finding the troughs.
Here's an example of how you could modify your code:
% find index of x=60 in M
x_idx = find(M(1,:) == 60);
% identify negative peaks and corresponding (positive) troughs
for di = 1:size(M,1)
% find negative peaks after x=60
[pks(di,:),q(di,:)] = min(M(di,x_idx:end));
% add index of x=60 to the trough index to get the actual location
q(di,:) = q(di,:) + x_idx - 1;
% find positive troughs after negative peaks
[troughs(di,:),z(di,:)] = max(M(di,q(di,1):end));
% add index of x=60 to the trough index to get the actual location
z(di,:) = z(di,:) + q(di,1) - 1;
end
This code should find the negative peaks and positive troughs after x=60 for each row in the matrix M. The q and z matrices will contain the index of the peak and trough, respectively, for each row.
To calculate the spike duration, you can subtract the index of the peak from the index of the trough for each row:
spike_duration = z - q;
You can then use this value to classify the neurons based on their spike duration.

Star Strider
Star Strider 2023-4-27
The findpeaks function will not work optimally with NaN values in the vector, so if you suspect that, use the fillmissing function to eliminiate them.
Perhaps something like this —
T = 100;
Fs = 150;
x = linspace(0, T*Fs, T*Fs+1)/Fs;
y = fillmissing(sin(2*pi*(x-61)*0.1) ./ (2*pi*(x-61)*0.1), 'linear'); % 'sinc' Pulse
% START LOOP
[pks,plocs] = findpeaks( y); % Detect Peaks & Locations
[vys,vlocs] = findpeaks(-y); % Detect Valleys & Locations
maxpklocs = plocs(pks>=max(pks)); % Index Of Maximum Peak
nextvly = vlocs(find(vlocs>maxpklocs,1,'first')); % First Valley After The Peak
% END LOOP
figure
plot(x, y)
hold on
plot(x(nextvly), y(nextvly), 'sg')
hold off
grid
axis('padded')
The first valley after the major peak is marked with a green square. You can put this code in a loop to get all the required information. My code assumes row vectors.
.

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by