findpeaks [pks, locs] cannot detect NaN
조회 수: 4 (최근 30일)
이전 댓글 표시
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;
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/491145/image.jpeg)
답변 (1개)
Walter Roberson
2021년 1월 18일
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
Walter Roberson
2021년 1월 19일
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.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!