How do I find a max value of a signal defined within a range?
22 views (last 30 days)
Show older comments
Hello, so I have a signal with several peaks, but I am only looking for a max peak within a specific range of that signal. So instead of getting the max peak, I want the max value within a specific range of that signal.
This is the code I am using right now to find the max peak of the signal:
if true
s = signal;
max = find(max(s) == s);
end
The key is that I still am looking for a max value of the signal 's', but only within a range of 12 to 15 seconds of that signal.
Any help would be greatly appreciated. Thank you for taking the time to look at my problem.
0 Comments
Answers (1)
Image Analyst
on 17 Mar 2017
To find the max of the signal and what element it occurs at, you can do this:
[maxSignal, indexOfMax] = max(signal)
I don't know how many elements correspond to 12 to 15 seconds, but let's just assume here that it is 100 elements. So you can do
index1 = indexOfMax - 100;
if index1 < 1
index1 = 1; % Don't allow negative or zero indexes.
end
index2 = indexOfMax + 100;
if index2 > length(signal)
index2 = length(signal); % Don't allow negative or zero indexes.
end
Now you want the max in the range from index1 to index2 but obviously not the first max you found. So you can set the array before index1, and after index2, and at the first max to minus infinity. Then call max again. This will find " max value of the signal 's', but only within a range of 12 to 15 seconds" of the first max as you asked for
signal(1:index1) = -inf; % Make a copy of signal if you want to save the original.
signal(index2:end) = -inf;
signal(indexOfMax) = -inf;
[secondMax, indexOfSecondMax] = max(signal);
An alternate method is to sort the signal but then you have to check the sorted values to see if their indexes are within 12-15 seconds of the first index.
[sortedValues, sortIndices] = sort(signal, 'descend');
maxSignal = sortedValues(1);
indexOfMax = sortIndices(1);
% Now find second, but has to be within, say, 100 elements
k = 2;
while k < length(sortIndices)
indexesApart = sortIndices(k) - sortIndices(1);
if abs(indexesApart) < 100
break;
end
% If we get here, it's not within 12-15 seconds of the first max.
% So increment the index and try again.
k = k +1;
end
% Get the actual index
indexOfSecondMax = sortIndices(k)
% Get the signal value at that location.
secondMax = signal(indexOfSecondMax)
3 Comments
Image Analyst
on 18 Mar 2017
I'm not sure I understand. First of all, it looks like signal is a time domain signal. You have not defined your t axis or the frequency. But the frequency looks like a single number, not a range. I can't even run your code because you have not defined t and f.
See Also
Categories
Find more on Get Started with Signal Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!