findpeaks [pks, locs] cannot detect NaN

Hello.
I did findpeaks in the matlab simulink to input a fast fourier transform discrete sine signal with a buffer of 1000 and a single side fft so that it becomes [1x500],
but the end result is a flat array value of NaN if under certain conditions. I want to use pks, locs to find out the peak size and location from the peak fft input, but there is an error where he can't read it as below,
how do I solve this problem, sorry if I ask again maybe in the previous question my explanation is not good thank you
function [dist,peak2,locs2] = fcn(u)
locs = zeros(size(u));
pks = zeros(size(u));
if u ~= 0
[pks,locs] = findpeaks(u);
elseif isnan(u)== 1/true
locs(size(u))= 0;
pks(size(u)) = 0;
else
locs(size(u))= 0;
pks(size(u)) = 0;
end
a = locs(2)- locs(1);
b = pks(1);
c = locs(1);
dist = a;
peak2 = b;
locs2 = c;

Answers (1)

if u ~= 0
Your u is a vector. The condition will be considered true only if all the entries are true, which is to say the same as if
if all(u(:) ~= 0)
and conversely it will fail if even a single value happens to be exactly 0.
This seems to be unlikely to be what you want.
If you are still dealing with the buffer initialization problem that I discussed with you before, you probably want
if any(u(:) ~= 0)
instead.
elseif isnan(u)== 1/true
You have the same issue that u is a vector so the condition is only true if all of the values are equal to 1/true
Why the heck are you dividing 1 by true ?? The result is the same as 1 (double precision) . And you do not need to compare to anything as isnan() is already true or false. Your line is the same as
elseif isnan(u)
which in turn as discussed is the same as
elseif all(isnan(u(:)))
Then you have
locs(size(u))= 0;
Remember that size() with a single argument always returns a vector with at least two elements. For example if u is a row vector of length 7, size(u) would return the vector [1 7] . And you are then using that vector of values as linear indices. If u does happen to be a vector, the result will be the same as if you had set locs(1) and locs(numel(u)) to 0... but they are already 0 because of the way you initialized them.
Then you have your else, which does the same thing as your elseif . Why bother to have the elseif if you are not going to do something different?
a = locs(2)- locs(1);
You are no testing for the possibility that u is empty or has only one element.

8 Comments

Forgive me @Walter Roberson,
for the buffer problem that we discussed earlier, luckily it was able to run with the code above, it's just that then I found that the NaN condition was the output from findpeaks not from the input to findpeaks so I was wrong in asking the above, I'm sorry,
so because the output of findpeaks is NaN, so there is only 1 element in the array because NaN is empty so even for locs (2) I can't once again apologize even though you have explained at length
so if the findpeaks output condition does not produce NaN output then the code above can be successful
function [dist,peak2,locs2] = fcn(u)
locs = zeros(size(u));
pks = zeros(size(u));
if u ~= 0
[pks,locs] = findpeaks(u);
else
locs(size(u))= 0;
pks(size(u)) = 0;
end
a = locs(2)- locs(1);
b = pks(1);
c = locs(1);
dist = a;
peak2 = b;
locs2 = c;
I have run this before and it works if the input is an induction motor that has a missalignment, but if it doesn't misalign, the findpeaks output becomes NaN so that's why I ask again @Walter Roberson
function [dist,peak2,locs2] = fcn(u)
assert(numel(u) > 1, 'u is too small')
locs = zeros(size(u));
pks = zeros(size(u));
if any(u ~= 0)
[pks,locs] = findpeaks(u);
else
%do nothing, locs and pks are already zero
end
a = locs(2)- locs(1);
b = pks(1);
c = locs(1);
dist = a;
peak2 = b;
locs2 = c;
I have tried with the code that you gave but still haven't succeeded. I'm sorry,
I trust you by sending my simulink, please help me, the time I have is left a little longer so I beg you very much for your help,
I will really appreciate your help @Walter Roberson
are you unable to run my simulink sir @Walter Roberson?
I did not notice the posting; I saw you post that to someone else and assumed that they would take care of it as they were the primary person on that thread.
I had to uninstall MATLAB due to operating system limitations; I need to figure out how I want to put my system back together again.
Naufal Arfani
Naufal Arfani on 19 Jan 2021
Edited: Naufal Arfani on 19 Jan 2021
I'm not sticking to one person that's why I'm asking this to everyone because I really need help from all of you, you cant help me Mr. @Walter Roberson?

Sign in to comment.

Products

Release

R2019b

Asked:

on 18 Jan 2021

Edited:

on 19 Jan 2021

Community Treasure Hunt

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

Start Hunting!