Finding specific peaks and valleys
I have a periodic signal with 6 peaks and 6 valleys. I am able to find all 6 peaks and all 6 valleys using the code below:
% plot periodic signal
plot(angle,signal)
hold on;
% fit to avoid noisy oscillations and find 6 global peaks
f = fit(angle,signal,’sin9′);
yfitted = feval(f,angle);
plot(angle,yfitted,’r’)
% find peaks and corresponding angles
[ypk0,idx0] = findpeaks(yfitted);
peaks = signal(idx0);
max_angles = angle(idx0);
% invert fitted signal to find 6 global valleys and corresponding angles
yfittedinv = max(yfitted) – yfitted;
[ypk,idx] = findpeaks(yfittedinv);
valleys = signal(idx);
min_angles = angle(idx);
Furthermore, I am able to find the global minimum (i.e., the lowest valley) using this line:
% find the global minimum
minangle = min_angles(valleys == min(valleys));
Both the arrays "max_angles" and "min_angles" have 6 elements. The question is how to reduce the number of elements in the array "min_angles" from 6 to 3, where I will have only the global minimum and the 2 non-neighbor valleys. The attached pic should help. I am able to find the green circle, but I also need to find the 2 red circles. Note that the global valley (green circle) is not always in the middle. It can be the first element of the array "min_angles", or it can be the last one. In the picture below, it’s number 4.I have a periodic signal with 6 peaks and 6 valleys. I am able to find all 6 peaks and all 6 valleys using the code below:
% plot periodic signal
plot(angle,signal)
hold on;
% fit to avoid noisy oscillations and find 6 global peaks
f = fit(angle,signal,’sin9′);
yfitted = feval(f,angle);
plot(angle,yfitted,’r’)
% find peaks and corresponding angles
[ypk0,idx0] = findpeaks(yfitted);
peaks = signal(idx0);
max_angles = angle(idx0);
% invert fitted signal to find 6 global valleys and corresponding angles
yfittedinv = max(yfitted) – yfitted;
[ypk,idx] = findpeaks(yfittedinv);
valleys = signal(idx);
min_angles = angle(idx);
Furthermore, I am able to find the global minimum (i.e., the lowest valley) using this line:
% find the global minimum
minangle = min_angles(valleys == min(valleys));
Both the arrays "max_angles" and "min_angles" have 6 elements. The question is how to reduce the number of elements in the array "min_angles" from 6 to 3, where I will have only the global minimum and the 2 non-neighbor valleys. The attached pic should help. I am able to find the green circle, but I also need to find the 2 red circles. Note that the global valley (green circle) is not always in the middle. It can be the first element of the array "min_angles", or it can be the last one. In the picture below, it’s number 4. I have a periodic signal with 6 peaks and 6 valleys. I am able to find all 6 peaks and all 6 valleys using the code below:
% plot periodic signal
plot(angle,signal)
hold on;
% fit to avoid noisy oscillations and find 6 global peaks
f = fit(angle,signal,’sin9′);
yfitted = feval(f,angle);
plot(angle,yfitted,’r’)
% find peaks and corresponding angles
[ypk0,idx0] = findpeaks(yfitted);
peaks = signal(idx0);
max_angles = angle(idx0);
% invert fitted signal to find 6 global valleys and corresponding angles
yfittedinv = max(yfitted) – yfitted;
[ypk,idx] = findpeaks(yfittedinv);
valleys = signal(idx);
min_angles = angle(idx);
Furthermore, I am able to find the global minimum (i.e., the lowest valley) using this line:
% find the global minimum
minangle = min_angles(valleys == min(valleys));
Both the arrays "max_angles" and "min_angles" have 6 elements. The question is how to reduce the number of elements in the array "min_angles" from 6 to 3, where I will have only the global minimum and the 2 non-neighbor valleys. The attached pic should help. I am able to find the green circle, but I also need to find the 2 red circles. Note that the global valley (green circle) is not always in the middle. It can be the first element of the array "min_angles", or it can be the last one. In the picture below, it’s number 4. findpeaks, findvalleys MATLAB Answers — New Questions