Finding peaks and troughs of sinusoidal from a Matrix and elements bounded within

Hello i have a matrix that consists of zeros and ones. It has 5 sinusodial shapes linked together as seen in the image attached. The yellow represents where the value of the matrix is one and the blue for zero. How do i find the peaks and troughs of each of this sinusodial shapes and the indices/elements(rows and columuns) bounded by each of these sinusodial shapes. Basically i should have something that gives the peak and trough for sinusodial one and then the elements within it

 Réponse acceptée

jonas
jonas le 3 Juil 2018
Modifié(e) : jonas le 3 Juil 2018
Since you did not attach the data, I had to start from your attached image. Therefore my x- and y-values are not scaled correctly. Basically you get the coordinates with find() and then findpeaks() to locate peaks and troughs. See attachment for results.
%%Fix binary image
RGB=imread('pic.png');
GRAY = rgb2gray(RGB);
threshold = graythresh(GRAY);
BW = im2bw(GRAY, threshold);
%Crop edges
BW=BW(5:end-10,60:end-30);
imshow(BW)
%%Find coordinates of ones
[y,x]=find(BW==1)
%%Find peaks and troughs
[~,locs1]=findpeaks(y.*-1,'minpeakprominence',20,...
'minpeakdistance',50)
[~,locs2]=findpeaks(y,'minpeakprominence',20,...
'minpeakdistance',50)
%%Plot data
figure;
plot(x,y,'-',...
x(locs1),y(locs1),'-x',...
x(locs2),y(locs2),'-x')

7 commentaires

thank you for your reply it looks just about right with just one little adjustment. The peaks and troughs should look something just like in the picture i just attached in this comment, represented by the thin black lines not the way your answer provided them to be. Thank you again. hope its much clearer.
I'm not sure what those lines represent. What do they extend to? One line is also curved, why is that?
its just meant to be straight lines. Hopefully the diagram ive attached gives more understanding
Try this. You can adjust the lines anyway you want.
%%Load data and crop image
data=load('trap.mat');
BW=data.Trap;
BW=BW(1:216,:)
imshow(BW)
%%Find data coordinates
[y,x]=find(BW==1)
%%Find Peaks and troughs
[~,locs1]=findpeaks(y.*-1,'minpeakprominence',5,...
'minpeakdistance',5)
[~,locs2]=findpeaks(y,'minpeakprominence',5,...
'minpeakdistance',5)
%%Fix lines length
xpeak=[x(locs1)-10,x(locs1)+10];
ypeak=[y(locs1) y(locs1)];
xtrough=[x(locs2)-20,x(locs2)];
ytrough=[y(locs2) y(locs2)];
%%plot
figure;;hold on
plot(x,y,'-')
plot(xpeak',ypeak','k--')
plot(xtrough',ytrough','r--')
set(gca,'Ydir','reverse')
thank you very much ! how do i find the elements within each of these 5 sinusoids?
Simple enough. The peak indices are stored in locs2 (troughs) and locs1 (peaks). So, to find the indices between the first two troughs, you can type:
wave1=x(locs2(1):locs2(2))
then for the next
wave2=x(locs2(2):locs2(3))
and so on. If you want the corresponding y-values, just change x to y.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by