Looping for find peak of signal

Hai...i have a problem to save looping data. For one row signal, my matlab is OK, but if my input more than one row signals it's a trouble. I want to find peak of signal but the peaks of every signal is diffent...please help me
clear erp
%Data
Ypeak = D3_Kiri_Bior35
%Ypeak = Ypeak(2:2,:)
[M1 M2] = size (Ypeak)
for M3 = 1:M1
Xpeak = Ypeak (M3:M3, 1 : M2)
%Xpeak = Ypeak
V = std(Xpeak)
M = mean(Xpeak)
S = 2*(M+V)
[maxmin] = find(Xpeak > S);
[minmax] = find(Xpeak <-S);
%Find ERP
Apeak = maxmin(1,:);
Bpeak = minmax(1,:);
Bpeak = Bpeak'
[ap1,bp1] = size(Xpeak)
[ap2,bp2] = size(Bpeak)
kurang = Bpeak(:,1:1)-30;
tambah = Bpeak(:,1:1)+30;
%Loop [Edit SCd: code formatting]
for ipeak = 1:ap2
if kurang(ipeak) <= 0
kurang(ipeak) = Bpeak(ipeak,1:1)-(Bpeak(ipeak,1:1)-1)
else kurang(ipeak) > 0
kurang(ipeak) = Bpeak(ipeak,1:1)-30
end
end
for kpeak = 1:ap2
if tambah(kpeak) >= M2
tambah(kpeak) = Bpeak(kpeak,1:1)+ (bp1- Bpeak(kpeak,1:1))
else tambah(kpeak) < M2
tambah(kpeak) = Bpeak(kpeak,1:1)+30
end
end
for iipeak = 1:ap2
erp=Ypeak(1:1,kurang(iipeak:iipeak,1:1):tambah(iipeak:iipeak,1:1))
end
end

Answers (2)

why can't you just use max or findpeaks?
More per comments
%data 1 valley, two peaks
x = rand(1,100)*10;
x(20) = -3;
x([52 72]) = 7*pi;
the_mean = mean(x);
stddev = std(x);
low_lim = the_mean - 2*stddev; %lower limit
up_lim = the_mean + 2*stddev;
idx_up = find(x>up_lim); %locations
idx_low = find(x<low_lim);
xup_parts = x(min(max(bsxfun(@plus,idx_up(:),-30:30),1),length(x))); %indices of parts
xlow_parts = x(min(max(bsxfun(@plus,idx_low(:),-30:30),1),length(x)));

12 Comments

ok...but how to use that for more than 2 row signal?
i mean if i have 2 signal that in every signal maybe have 2 until 3 peak
x = rand(2,100)*10;
haven't i use "for loop"?
That my problem
Yeah, just loop over every row:
for ii = 1:size(x,1)
do_stuff_with(x(ii,:))
end
%data 1 valley, two peaks
x = rand(2,100)*10;
x(:,20) = -3;
x(:,[52 72]) = 7*pi;
for ii = 1:size(x,1)
the_mean = mean(x(ii,:));
stddev = std(x(ii,:));
low_lim = the_mean - 2*stddev; %lower limit
up_lim = the_mean + 2*stddev;
idx_up = find(x(ii,:)>up_lim); %locations
idx_low = find(x(ii,:)<low_lim);
xup_parts = x(min(max(bsxfun(@plus,idx_up(:),-30:30),1),length(x))); %indices of parts
xlow_parts = x(min(max(bsxfun(@plus,idx_low(:),-30:30),1),length(x)));
end
xup_parts
xlow_parts
It have to be 2 xup_parts and 1 xlow_parts from first signal and 2 xup_parts and 1 xlow_parts from second signal. But i only get 2 xup_parts and 1 xlow_parts
why always only data from last loop that saved?
because you overwrite it on each iteration. You need to save to the current location. The easiest way would just to be
xup_parts = [xup_parts;new_x_up_parts];
Add the old one to the end.
??? Undefined function or variable "new_x_up_parts".
Error in ==> cobapeak1 at 24
xup_parts = [xup_parts;new_x_up_parts];
i'm sorry i'm beginner in matlab
that's my way of trying to hint that the new computation on each iteration of the for-loop. It wasn't working code.
that my problem...i don't know how to save each iteration to cuurent location so i save data from all looping
please help me
I do it for 2 week but i don't know how to save looping in current location

Sign in to comment.

Endro Yulianto
Endro Yulianto on 6 Oct 2011
I try to find peak of signal that amplitudo of the peak is above and under mean (+/-) 2*standart deviation and from that peak i segmenteg that signal (+/-) 30 data with peak is the center. The problem is when i try to find more than 2 row signal from looping, only last data is saved. Please help me

4 Comments

Calculate the std multiply by two and add/subtract from mean. Then use logical indexing to find what's outside that? I'm sorry, I'm having trouble understanding you.
For example : Mean= 2, std = 1. I try to find peak that have amplitudo above +(2 + 2(1)) and under -(+(2 + 2(1)). If i find peak at 100, i'll segmented data at (100 - 30) until (100 + 30) = 70 : 130.
Can you give us some MATLAB code to generate some sample data, that we can then use to create a demo for you?
x = rand(2,100)*10;
x(:,20) = -3;
x(:,[52 72]) = 7*pi;

Sign in to comment.

Categories

Asked:

on 6 Oct 2011

Community Treasure Hunt

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

Start Hunting!