MATLAB: Count Peaks in a Graph

signal processing

Hello all,
I'm writing a code in matlab which will count the peaks, by (1) determining the intersection with a threshold and (2) a positive slope of the signal z(n)-z(n-1)>0
but the code isn't working. I'm stuck at counting the intersection points beteween the signal and the threshold.
P.S. I'm trying to avoid MATLAB's Specific functions, and to use basic code. because I'll convert this code to MathScript later on.
Thank you
Here's my code:
clc;
clear all;
rise=0;
%specs
f=1;
Amp1=1;
Amp2=0.5;
ts=1/8000;
T=6;
t=0:ts:T;
%signals
y=Amp1*sin(2*pi*f*t);
x=-Amp2*sin(2*pi*f*t);
%rectification and summation
y(y<0)=0;
x(x<0)=0;
z=x+y;
%thresh
thL=0.1*max(z);
for n=1:(length(z)-1)
if (z == thL) % crossing thresh
rise = rise + 1;
end
end
disp(rise);
plot(z, 'b')
hold
line(xlim, [thL,thL], 'Color', 'r')
legend('Signal', 'Threshold');

Best Answer

Is your signal always this clean? if so, this would work
for n=3:length(z)
if z(n-1) - z(n-2) > 0 && z(n)-z(n-1) < 0 && z(n-1) >= thL
rise = rise + 1;
end
end