How to use a vector to index another vector?

조회 수: 22 (최근 30일)
Jared King
Jared King 2020년 1월 24일
댓글: Jared King 2020년 1월 24일
Hello, I am admittedly still fairly new to MATLAB, but I am working on something to expedite my research.
Essentially, I want to find the zero-crossing points of a signal. I'm currently using a filter (made by someone who actually knows what they're doing) to find all maxima and minima which are a certain value away from zero:
function out = PeakFinder(in,w)
out = in*0;
for i = w+1:length(in) - w
chunk = in(i-w:i+w);
[mx ind] = min(chunk);
if ind == (w+1);
out(i) = in(i);
end
end
This code gives me a nx1 column vector with either a zero (If the point is not a maximum) or the value of my input vector (If the point is a maximum) as a result. I'm then using find() to give me the row that point is located on in the vector. This gives me a ~15x1 vector
My issue is, I do not know how to use this 15x1 vector to find the values of the original nx1 vector. I tried using simple things like OriginalVector(NewVector) but that didnt work.
  댓글 수: 1
Sindar
Sindar 2020년 1월 24일
In what way did it not work? Error? Wrong values? There are more efficient ways, but your algorithm sounds like it should work

댓글을 달려면 로그인하십시오.

답변 (2개)

Stephen23
Stephen23 2020년 1월 24일
편집: Stephen23 2020년 1월 24일
That function output is rather fragile, as there is no way to distinguish zeros (data) from zeros (place holders). This could easily be rectified preallocating the output array with NaN or using a logical array instead, which would also simplify your task (as you could just use more efficient logical indexing). In any case, I don't see any reason why find should not work:
>> V = randi(9,13,1)
V =
6
7
3
8
3
9
1
3
8
5
2
9
1
>> W = PeakFinder(V,2)
W =
0
0
3
0
0
0
1
0
0
0
0
0
0
>> X = find(W);
>> V(X)
ans =
3
1
I do not see how this function helps you to "to find all maxima and minima which are a certain value away from zero".
  댓글 수: 1
Jared King
Jared King 2020년 1월 24일
Because the peaks on my signal go from, say, 50 to -50 within the span of a few points, setting the width to ~20 allows me to find all points with a local maximum where that maximum is higher than the width. I think. I dont really know the function that well to sy for sure. But I tested it on an actual set of data and it worked well enough to find the maximum. Im planning on using the function twice to find the max and min values which should correlate to the same peak on the signal and then average the results to give me the center of the signal; as in if the max is at row 10 and the min is at row 30 then the "zero point" should be the value in row 20.

댓글을 달려면 로그인하십시오.


Star Strider
Star Strider 2020년 1월 24일
Testing the code (without the function definition or function call):
t = (0:0.1:15);
in = sin(t*2*pi);
w = 5;
out = in*0;
for i = w+1:length(in) - w
chunk = in(i-w:i+w);
[mx ind] = min(chunk);
if ind == (w+1);
out(i) = in(i);
end
end
figure
plot(t, in)
hold on
plot(t, out, 'p')
hold off
grid
reveals that when ‘out’ is not zero, it returns the troughs (negative peaks) of the ‘in’ vector.
To get the indices of the non-zero values in ‘out’:
indices = find(out);
and:
OriginalVector(indices);
should return the correct values.

카테고리

Help CenterFile Exchange에서 Get Started with Signal Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by