How can plot multiple same amplitude signals along with same y axis ?

I am using this code to plot multiple signals. But unable to plot using same y axis. I am some overlapping plot due same amplitude. How to manage them in same figure? Signals will be in dufferent length but later I want to scale them with same length. I am attaching figure that is representing how will be my output.
clc;
close all;
clear;
workspace;
a = imread('img.png');
bwimg =bwareafilt(~a, 1);
s=regionprops(bwimg,'Orientation','Centroid','MajorAxisLength','MinorAxisLength');
circleCenterX =s.Centroid(1);
circleCenterY =s.Centroid(2);
diameters = mean([s.MajorAxisLength s.MinorAxisLength],2);
t=0:0.001:2*pi;
r1 =1*diameters/8;
x = circleCenterX + r1 * sin(t);
y = circleCenterY + r1 * cos(t);
[r,c]=size(bwimg);
bw=false(r,c);
t=0:0.001:2*pi;
bw =bw|poly2mask(x,y,r,c);
boundaries = bwboundaries(bw);
thisBoundary = boundaries{1};
x1=thisBoundary(:,2);
y1=thisBoundary(:,1);
indexes = y1<circleCenterY; % Only those in the upper half.
xTop = x1(indexes);
yTop = y1(indexes);
figure;
for k=1:length(xTop )
profile(k) = bwimg(yTop(k),xTop(k ));
plot(profile,'r-','LineWidth',2 );
%grid on ;
ylim([0 2]);
axis off;
end
r2 =2*diameters/8;
x2 = circleCenterX + r2 * sin(t);
y2 = circleCenterY + r2 * cos(t);
bw =bw|poly2mask(x2,y2,r,c);
boundaries = bwboundaries(bw);
thisBoundary = boundaries{1};
x3=thisBoundary(:,2);
y3=thisBoundary(:,1);
indexes = y3<circleCenterY; % Only those in the upper half.
xTop1 = x3(indexes);
yTop1= y3(indexes);
for k=1:length(xTop1)
profile1(k) = bwimg(yTop1(k),xTop1(k ));
plot(profile1,'b-','LineWidth',2 );
%grid on ;
ylim([0 2]);
axis off;
end
r3 =7*diameters/8;
x4= circleCenterX + r3 * sin(t);
y4= circleCenterY + r3 * cos(t);
bw =bw|poly2mask(x4,y4,r,c);
boundaries = bwboundaries(bw);
thisBoundary = boundaries{1};
x5=thisBoundary(:,2);
y5=thisBoundary(:,1);
indexes = y5<circleCenterY; % Only those in the upper half.
xTop2 = x5(indexes);
yTop2 = y5(indexes);
for k=1:length(xTop2)
profile2(k) = bwimg(yTop2(k),xTop2(k ));
plot(profile2,'r-','LineWidth',2 );
%grid on ;
ylim([0 2]);
axis off;
end
r4 =8*diameters/8;
x5= circleCenterX + r4 * sin(t);
y5= circleCenterY + r4 * cos(t);
bw =bw|poly2mask(x5,y5,r,c);
boundaries = bwboundaries(bw);
thisBoundary = boundaries{1};
x6=thisBoundary(:,2);
y6=thisBoundary(:,1);
indexes = y6<circleCenterY; % Only those in the upper half.
xTop3 = x6(indexes);
yTop3 = y6(indexes);
for k=1:length(xTop3)
profile3(k) = bwimg(yTop3(k),xTop3(k ));
plot(profile3,'b-','LineWidth',2 );
%grid on ;
ylim([0 2]);
axis off;
end

4 commentaires

I cannot guess, what this means: "But unable to plot using same y axis." Maybe all you need is:
axes('NextPlot', 'add');
plot(rand(1, 10));
plot(rand(1, 10));
plot(rand(1, 10));
See also: doc hold
this is not working. again getting overlapping plots. cant separate all the signals
I have attached a picture to explain what type of output I am expecting
I do not get any idea, how the attached images concern the described problem. You have posted a pile of code, but it is not clear, which lines are interesting to understand, what you want to achieve. Does "cant separate all the signals" mean, that you want to separate some signals? Please elaborate this again. I know, that it is hard to do this in a foreign language, but don't give up. Explain the problem, until somebody understands it and support you to find a solution.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 22 Jan 2019
Modifié(e) : Star Strider le 22 Jan 2019
Try something like this using the bsxfun (link) function:
x = linspace(0, 2*pi); % Independent Variable
y = sin((0:5)'*x); % Original Signals
y = bsxfun(@plus, y, (0:size(y,1)-1)'*2); % Separated Signals
figure
plot(x, y)
grid
EDIT —
Added plot:
How can plot multiple same amplitude signals along with same y axis - 2019 01 22.png

21 commentaires

Zara Khan’s ‘Answer’ moved here:
using my above code am onlty able to separate signals when using subplots. but thats not my aim. i want to plot all these four signals along with same x axis and Y axis gradually like the figure multipleplot.
If you put your pulse train data into a matrix where each pulse train is a row of the matrix (in my example code,‘y’), you can separate the rows to plot them by simply adding a constant to each row, as I did using the bsxfun function, producing the plot that I posted.
can you please modify my attach code with this
Unfortunately I cannot because I have no idea what your code does or what variables you want to plot.
If you tell me what the variables are that you want to plot, and their row and column sizes, and the size of your independent variable, I can suggest an approach.
Zara Khan‘s ‘Answer’ moved here:
look i am considering 4 diffetent radii circles over an binary image. later am considering the circle upper half i.e the semi circle. semi circle circumference pixels are stored in different profiles. Now ploting this profiles. but this is creating diffetent figures. that i dont want. i want to place all signals in one figure with equal length also. here diffetent radii creating different length profile hence diffetent length signals. i want to scale them in same length too.
What variables do you want to plot?
What are the sizes of the matrices of those variables that you want to plot?
Those are all I need to know to help you change your code.
profile, profile1, profile2 snd profile3 am ploting
O.K. Assuming that the data you want to plot are similar to my original ‘y’ matrix:
profile_xpnd = bsxfun(@plus, profile, (0:size(profile,1)-1)'*2);
profile1_xpnd = bsxfun(@plus, profile1, (0:size(profile1,1)-1)'*2);
profile2_xpnd = bsxfun(@plus, profile2, (0:size(profile2,1)-1)'*2);
To plot them:
plot((1:size(profile,2)), profile, '-r')
And similarly for the rest.
Since I still do not know relevant details about these matrices, this is simply a guess.
Brrrr. Indices hidden in the names of variables.
@Jan —
What?
Nothing is hidden that I can see. The plot call makes certain that the expanded data are plotted correctly according to the way I believe OP wants them plotted. This is still only a guess on my part.
@Starstrider: My comment was posted at the same time as yours, but concerned the former comment of Zara Khan, especially the detail: "profile1, profile2 snd profile3". The original code contains several parts, which are almost equal. Then a loop would be better.
Sorry for the confusion.
plot.png
I am getting this .
@Star Strider:
I want to plot like your first figure in this case but unable to create this using my code .
I did everything I can.
I do not have the vectors you want to plot.
Note that the vectors you want to plot must be rows of ‘profile’ and the others, in order to create the necessary offsets in the ‘profile_xpnd’ matrices.
Experiment with my code to get the result you want.
'profile' is a linear array thats the problem. any other solution please ???
Is ‘linear array’ the same as ‘vector’?
If so, and if all 4 ‘profile’ variables are the same size, try this:
profile_xpnd = bsxfun(@plus,[profile; profile1; profile2; profile3], 2*(0:3)');
To illustrate with an example:
t = linspace(0, 2*pi);
profile = sin(t);
profile1 = sin(2*t);
profile2 = sin(3*t);
profile3 = sin(4*t);
profile_xpnd = bsxfun(@plus,[profile; profile1; profile2; profile3], 2*(0:3)');
figure
plot(t, profile_xpnd)
Zara Khan
Zara Khan le 23 Jan 2019
Modifié(e) : Zara Khan le 23 Jan 2019
But why this sign waves? I want to keep all the linear profiles as it is . I dont know where I am going wrong . Still you are trying to help me out. Thanks.
The sine waves are for a demonstration only. You are expected to fill in your data by your own.
@Jan —
Thank you!
'Profiles' are not in same length.
Then just do something like this:
profile_a = profile;
profile1_a = profile1 + 2;
prodile2_a = profile2 + 4;
profile3_a = profile3 + 6;
Then plot ‘profile_a’ and the others, instead.

Connectez-vous pour commenter.

Plus de réponses (1)

Zara Khan
Zara Khan le 2 Fév 2019
Star Strider : Thank you. finally I am done with this. its working well.

Community Treasure Hunt

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

Start Hunting!

Translated by