Comparing values in two arrays: find those within a certain difference threshold

6 views (last 30 days)
Hello all,
I am processing a raw accelerometer signal, and have run into a filtering problem of sorts. My goal is to detect the valleys of interest in said signal: this includes only the detected valleys that occur soon after a detected peak. I have two arrays that I must compare: peakLoc (peak locations) and valleyLoc (valley locations), and want to save the values in valleyLoc that have a difference of 50 or less from a value in peakLoc. My current attempt is to compare the first value in peakLoc to every value in valleyLoc, and repeat with the next index of peakLoc until every value in peakLoc has been compared to the other array. If a value in valleyLoc meets the threshold, I want to save it into another array: (valleyLocrev). The values in both peakLoc and valleyLoc are in ascending order so technically not every value in valleyLoc need be compared to the value in peakLoc. Also, please note that peak Loc and valleyLoc may not be the same length.
So far, I have been attempting to use nested loops, but to no avail. Here is my code:
if length(valleyLoc)-length(peakLoc) <0 || length(valleyLoc)-length(peakLoc)==0
valleyLocrev=zeros(1,length(valleyLoc));
for countone= 1:length(valleyLoc)-1
counttwo= 1;
while counttwo<=length(peakLoc)
if abs(peakLoc(countone) - valleyLoc(counttwo)) < 20
valleyLocrev(countone)= valleyLoc(counttwo);
counttwo=counttwo+1;
else
valleyLocrev(countone)= NaN;
counttwo=counttwo+1;
end
end
end
else if length(valleyLoc)-length(peakLoc) >0
valleyLocrev=zeros(1,length(valleyLoc));
for countthree= 1:length(peakLoc)-1
countfour = 1;
while countfour<= length(valleyLoc)
if abs(peakLoc(countthree)-valleyLoc(countfour)) < 20
valleyLocrev(countthree)= valleyLoc(countfour);
countfour=countfour+1;
else
valleyLocrev(countthree) = NaN;
countfour=countfour+1;
end
end
end
end
end
Thanks for your help!

Answers (2)

Star Strider
Star Strider on 19 Jul 2012
You didn't mention if you'd tried the ‘find’ function, so if you haven't, I suggest it to you. It could make your task much easier. It looks as though you're implementing something similar to it.
If ‘find’ won't solve your problem, please be a bit more specific about why the code you posted isn't working as you want it to.
Also, you mention in your question that you're looking for valleyLoc values to be less than 50 from the peakLoc values, but in your ‘if’ statements, you're testing for them to be <20.

Jessica
Jessica on 20 Jul 2012
Hi,
Thank you very much for your input. Something that presented a problem in this situation was that the arrays which I am comparing are of different lengths and the values of interest may not be in parallel locations within the two arrays. I believe this presents a problem when trying to use find, but I could be wrong as I am pretty new to matlab. I found that my problem was that I needed to include a 'break' within the if/else statement since the values of interest were soon overwritten when I had not included this command. I also realized that my use of an absolute value in the difference threshold was not specific enough to find valleys that occur only AFTER peaks. Lastly, my requirements changed, and it was necessary to find values with a difference of 28 or fewer. Just to help anyone else with a similar problem, my revised code is included:
if length(valleyLoc)-length(peakLoc) >0
valleyLocrev=zeros(1,length(valleyLoc));
for countthree= 1:length(peakLoc)-1
countfour = 1;
while countfour<= length(valleyLoc)
if (peakLoc(countthree)-valleyLoc(countfour))> -28 & (peakLoc(countthree)-valleyLoc(countfour))< 0
valleyLocrev(countthree)= valleyLoc(countfour);
countfour=countfour+1;
break;
else
valleyLocrev(countthree) = 0;
countfour=countfour+1;
end
end
end
end

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!