Hi, I have this signal and I want to find out the mean between the peaks. I know the x and y values at the peaks. this is my code
y1=locs+10; %wants few points before and after peaks to avoid mean being affected by the peak values thus +10
y2=locs-10; %locs is the y position of the peaks
for k= 1:length(locs)-1
ROI=transpose(y1(k):y2(k+1)); %transpose is the whole signal, trying to extract each region between peaks
mean2=mean(ROI);
end
The code works for one loop (i.e.k=1) but wont repeat it. But if I use 'ROI=transpose(y1(k):y2(k+1))' in the command window and physically type k=2 and 3 it will work. so I guess i'm missing a step in the for loop. any suggestions would be appreciated. Thanks in advancepks per revolution.jpg

 Akzeptierte Antwort

Star Strider
Star Strider am 7 Dez. 2019

0 Stimmen

We o not have your entire code. Note that the transpose call is not necessary, since the mean funciton also accepts a dimension argument, where 1 is down the rows for each column (the default) and 2 is across columns, taking the mean of each row.

4 Kommentare

Aleena James
Aleena James am 7 Dez. 2019
it was giving me error without the transpose
Star Strider
Star Strider am 7 Dez. 2019
Use the ‘dim’ argumnent in the mean function:
as I already described. It should work then.
Try this demonstration code:
N = 100;
tv = linspace(0, 10, N);
signal = randn(1, N);
[pks,locs] = findpeaks(signal, 'MinPeakHeight',1, 'MinPeakDistance',5);
for k = 1:numel(locs)-1
locv{k} = locs(k):locs(k+1);
ROI(k) = mean(signal(locs(k):locs(k+1)), 2);
end
figure
plot(tv, signal)
hold on
plot(tv(locs), pks, 'vb')
for k = 1:numel(locv)
plot(tv(locv{k}), ones(size(locv{k}))*ROI(k), '-k')
text(median(tv(locv{k})), ROI(k), sprintf('\\mu = %.3f', ROI(k)), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',7)
end
hold off
grid
Make necessary changes to get the result you want.
Aleena James
Aleena James am 7 Dez. 2019
thank you so much it works:)
Star Strider
Star Strider am 7 Dez. 2019
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by