Clear Filters
Clear Filters

finding the max/peak of signal for a given vicinity of points without using matlab native functions

8 views (last 30 days)
So, basically I'm trying to create a function that allows me to detec the peaks/maximums of a signal without using matlab native functions (basically max, min, findpeaks, etc --- because I can still use length, floor, ceil, etc).
And I've written a code that I thougth should do the trick, but it shows up as an error and I can't understand why.
This is what I have so far:
function [Picos] = detect_peaks(FINAL,k)
L = length(FINAL);
Picos = [];
if k/2==floor(k/2)
par=1; % k é par
else
par=0; % k é ímpar
end
if par==0
a = (k-1)/2;
else
a=k/2;
end
for i = 1:L
if i<(a)
for q = 1:k
if FINAL(i) > FINAL(q)
max = FINAL(i);
else
max = FINAL(q);
end
end
elseif i>(L-a)
for q = (L - k):L
if FINAL(i) > FINAL(q)
max = FINAL(i);
else
max = FINAL(q);
end
end
else
for q = (i-(ceil(a))):(i + (floor(a)))
if FINAL(i) > FINAL(q)
max = FINAL(i);
else
max = FINAL(q);
end
end
Picos(i) = max;
end
end
--------
All of this considering that k is the vecinity (in which the the value associated whith the indice in question is included ) and FINAL is a signal that looks something like the image attached.
I would really apreciate it if someone could help me.
  9 Comments
Inês Silva
Inês Silva on 19 Jan 2023
I've given another try at the code and came up with this:
function [Picos] = detect_peaks(FINAL,k,limiar)
L = length(FINAL);
Picos = [];
a = (k-1)/2;
for i = 1:k:L
if i<(ceil(a))
maximo = 0;
for q = 1:k
if FINAL(q) > maximo
maximo = FINAL(q);
Picos(i) = [maximo,q];
end
end
elseif i>(L-(floor(a)))
maximo = 0;
for q = (L - k):L
if FINAL(q) > maximo
maximo = FINAL(q);
Picos(i) = [maximo,q];
end
end
else
maximo = 0;
for q = (i-(ceil(a))):(i + (floor(a)))
if FINAL(q) > maximo
maximo = FINAL(q);
Picos(i) = [maximo,q];
end
end
end
end
The problem is that this error shows up:
Unable to perform assignment because the indices on the left side are not compatible with the size
of the right side.
Error in untitled (line 26)
Picos(i) = [maximo,q];
But if I don't put the 'i' in Picos(i) then only the last maximum / peak is registred in the matrix.
Inês Silva
Inês Silva on 19 Jan 2023
And this is the last update on the code:
But there is still an error of some sort because the values don't match what I can see in the graphic
function [Picos] = detect_peaks(FINAL,k,limiar)
L = length(FINAL);
Picos = [];
a = (k-1)/2;
t = 1;
for i = 1:k:L
if i<(ceil(a))
for q = 1:k
maximo = 0;
if FINAL(q) > maximo
maximo = FINAL(q);
M = q;
end
end
Picos(t,1) = maximo;
Picos(t,2) = M;
t = t + 1;
elseif i>(L-(floor(a)))
for q = (L - k):L
maximo = 0;
if FINAL(q) > maximo
maximo = FINAL(q);
M = q;
end
end
Picos(t,1) = maximo;
Picos(t,2) = M;
t = t + 1;
else
for q = (i-(ceil(a))):(i + (floor(a)))
maximo = 0;
if FINAL(q) > maximo
maximo = FINAL(q);
M = q;
end
end
Picos(t,1) = maximo;
Picos(t,2) = M;
t = t + 1;
end
end
m = 1;
Peaks = [];
for p = 1:t
if Picos(p,1) > limiar
Peaks(m,1) = Picos(p,1);
Peaks(m,2) = Picos(m,2);
m = m + 1;
end
end

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!