Plot an eye diagram of a signal in MatLab - matlab

The matrix X contained in a .mat file represents the acquired signal. The element of place (i, j) of the matrix is the i-th sample of the j-th screen. The sampling frequency is equal to 4 GS/s.
How do I plot the eye diagram relative to the signal contained in X using MatLab?
I tried but I could not draw the eye diagram from the matrix X (see http://ge.tt/8Xq5SYh/v/1?c). Here is the link to the matrix X that I used:
http://ge.tt/8Xq5SYh/v/0
and my MatLab code:
%sampling frequency fs=4 GS/s
rows=4000; %4000 rows (samples) |__ in matrix X
columns=10; %1000 columns (screens) |
%for plot all the graphics in the same window (overlapping)
hold on;
%index of the single row (column for the single column)
row=1:1:100;
t=1:1:100;
for column=1:columns,
%plot
plot(t,X(row, column),'-bo','LineWidth',1, 'MarkerEdgeColor','b', 'MarkerFaceColor','b', 'MarkerSize',2);
end
%axis properties
set(gca,'YTick', [-0.5 -0.45 -0.4 -0.35 -0.3 -0.25 -0.2 -0.15 -0.1 -0.05 0 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5]); %soli valori di ascisse da visualizzare
grid on;
set(gca,'GridLineStyle','-');
axis([0 10 -0.5 0.5]);
Someone could try to show me how to do?
maybe the matrix is not correct?
Thanks in advance to anyone who answers

You can simply plot(x,'b'). The plot command will draw a line for every column of x, which corresponds to all the samples of each "screen". The 'b' in the command is just to make every line the same color like a typical eye diagram.

Related

matlab imshow not fitting the defined axes

I have to generate a video made of three main objects coming from some data previously generated:
1)patch graph of a thickness
2)a grey-scale image
3)a plot
I generated three axes: ax1,ax2,ax3 with their properties. When I put the image inside ax2, I always get an image that is smaller, centred, and scaled.
This is what i get:
ResultNotWanted
the upper patch graph and the image should be of the same length but I'm not able to figure out how.
Part of the code:
File=dir(File2Load);
%Figure handles and figure object before the loop to reduce the
%computational power required by the loop itself.
fig=figure('visible','on'); %set off when doing the loop of the whole video
fig.Position=([0 0 900 500]);
pipeLength=371;
limY_TBS=400;
img=[];
x=(1:pipeLength);
xp=[x flip(x)];
z=[];
x1=[-1 x x(end)+1 x(end)+1 flip(x) -1];
y=ones(1,numel(x1));
y(1:end/2)=y(1:end/2)*(limY_TBS+10);
y(end/2+1:end)=-1*y(end/2+1:end);
ax1=axes(fig,'Position',[0.1 0.60 0.80 0.3]);
p1=patch(ax1,x1,y,ones(length(x1),1),'FaceColor','interp');
hc=colorbar(ax1,'Location','manual','Position',[0.91 0.6 0.02 0.3]);
title(hc,'°C');
xticks(ax1,0:50:350)
xticklabels(ax1,["0","35","70","105","140","175","210","245 [mm]"])
set(ax1,'TickDir','out')
xlim(ax1,[0 pipeLength])
yticks(ax1,0:25:limY_TBS)
yticklabels(ax1,["0","","","","100","","","","200","","","","300","","","","400"])
clim(ax1,[20 40]);
%limit of the techinique, the value will build a patch over te region that are out of interest because too thick
patchHeigth=limY_TBS; %patch heigth to be plot when thickness>limT_TBS. This will be the heigth of the patch plotted
colormap(ax1,'turbo')
p2=patch(ax1,x1,y,'w');
ylim([0 limY_TBS]);
ylabel("Film Thickness [" + char(181)+ "m]",'FontSize',15),
%set axes 2 for the image
ax2=axes(fig,'Position',[0.1 0.20 0.8 0.3]);
mfile=matfile(Vid(1).path);
mOnes=size(cell2mat(mfile.newIred(1,1)));
size(cell2mat(mfile.newIred(1,1)),2)],'YData',[-10 size(cell2mat(mfile.newIred(1,1)),1)]);
imgGraph=imshow(mOnes,'Parent', ax2,'XData',[0 size(cell2mat(mfile.newIred(1,1)),2)],...
'YData',[-10 size(cell2mat(mfile.newIred(1,1)),1)],...
initialMagnification','fit');
imgGraph.CDataMapping="direct";
t1=text(ax2,1,200,' ','FontSize',15,'FontSmoothing', 'on'); %time text
t2=text(ax2,600,200,' ','FontSize',15,'FontSmoothing', 'on'); %acceleration text
t3=text(ax2,300,200,' ','FontSize',15,'FontSmoothing', 'on'); %power text
ax3=axes(fig,'Position',[0.1 0.05 0.8 0.1]); %axis for the acceleration graph
I would like that the image on ax2 to completely fit the defined axes to have the same horizontal span of the top graph.
Thank you for any support that you can give.
Riccardo

How to plot point and vectors in Matlab

A = [239920.240412166 1.31193313030682;
243577.444235102 1.38185205485119;
241899.250050298 1.51264147493485;
244659.326936560 1.50845243215867;
239862.361809342 1.50810833389632;
238395.616682194 1.37125000688350;
244558.389789124 1.27212093329482;
244290.890880318 1.35116080948488;
240303.711239396 1.36064181572699;
237464.430450140 1.48857869573721;
244415.381196104 1.51252425335623;
239855.328594799 1.29178640586301;
239304.806448742 1.31075813783171;
244827.243024016 1.32080934043223;
241465.885648910 1.53667019314427;
241139.254464482 1.40424079027764;
242300.037630214 1.27160249886092;
243330.396959248 1.61411410292679;
237530.389940994 1.21846939260826];
B = [0.6 0.18; 0.15 0.46]; % green circles
for i=1:2
plot(A(:,1),A(:,2),'r*');
hold on
plot(B(i,1),B(i,2), '-ko',...
'LineWidth',1,...
'MarkerFaceColor',[.49 1 .63],...
'MarkerSize',9);
end
When I ploted A and B, I got this:
B(1,1) = 0.6, but it appears in 0 (X-axis). Same with B(2,1) = 0.15
How to correct this?
A logarithmic scale on the xaxis will help with the view
set(gca, 'XScale', 'log')
However, it will lead to the fact, that now the values of A appear to populate one vertical line.
If you cannot live with this, you may want to try a broken x-axis. MATLAB doesn't support this with build-in functions, but there is a solution in the MATLAB file exchange
https://de.mathworks.com/matlabcentral/fileexchange/3683-breakxaxis
Btw: There is no need for the loop in your code. In fact you plot A twice on top of each other. Just
% Plot A and B without loop
plot(A(:,1), A(:,2),'r*')
hold on
plot(B(:,1), B(:,2), '-ko', 'LineWidth', 1, ...
'MarkerFaceColor', [.49 1 .63], 'MarkerSize',9)
% Set x axis to logarithmic scale
set(gca, 'XScale', 'log')
is sufficient to display your plot
Your x-axis goes from 0 to 250000. On that range, 0.6 and 0.15 are practically 0.
If you want you can use logarithmic scale in x-axis using semilogx

3d surface plot with vectors in matlab

Could you please help me with the following issue: I would like to 3d plot 3 vectors in MATLAB. I know that it translates into points in a 3d space, but is there a way to obtain actually the surface? I tried similar approaches found in different answers, but they don't seem to work for my data, i.e. I obtain an empty graph. Thanks a lot! Below the codes I tried:
%Method 1:
[X2,Y2]=meshgrid(a1,a2);
Z2=griddata(a1,a2,z,X2,Y2);
surf(a1,a2,Z2); % I obtain an empty graph in this case
%Method 2:
trisurf(delaunay(a1,a2),a1,a2,z) %In this case I obtain a graph but it seems unrealistic
%Method 3: using scatter3, I obtain a line, but I would like to have a surface
scatter3(a1,a2,z)
a1 and a2 are 1x100 vectors with values in (0,1) and z is a complicated function of these two vectors, also having dimension 1x100.
Thanks for your help!
Edit:
a1 = [0.05 0.06 0.07 0.08 0.09 0.1 ...]
a2 = [0.9 0.89 0.88 0.87 0.86 0.85 ...]
z = [-0.0009 -0.0011 -0.0012 -0.0014 -0.0016 -0.0017 ...]

MATLAB/Octave plot markers above the line rather than on the line

I want to visualize the peaks of a function, and I want to have markers for it appear above the line they are associated with.
I fabricated a minimum example where I already have the peaks, the question is just how to visualize the markers correctly:
y = [0.1 0.3 10.0 1.0 0.5 0.1 24.0 0.6 0.1 0.2]
x = (1:length(y))
plot(x,y);
hold on;
peaks = [3 7];
plot(x(peaks), y(peaks), 'v', 'MarkerSize', 24);
print('-dpng', 'example.png', '-S640,480');
So, as a result, the markers appear centered on the line like this:
The result that I want could be achieved by carefully tuning a parameter OFFSET like this:
plot(x(peaks), y(peaks)+OFFSET, 'v', 'MarkerSize', 24);
As shown in the following figure, for this exact example OFFSET=2.56 works for the exported png, but with the interactive plot and exporting vector graphics, it's wrong again.
Can anyone recommend a way to get this result without having to manually doing trial/error?
Currently I am using Octave with gnuplot to export to latex+tikz, and it would be good if the solution would work there.
In my actual (more complicated) use case I am plotting multiple lines after each other into the same figure, and the y limits change, so the offsets can not just be calculated easily, as the markersize doesn't change with the y limits.
Edit: Additionally I am using a semilogx plot, so drawing lines inside the diagram in the x/y-Axis scales would look distorted.
One way to do this is with annotations, but there are some drawbacks (see below).
Annotations enable you to place various graphic objects into your figure. One very annoying thing about them is that they work in so-called normalized coordinates,
which span the whole figure window (not just the plot area) and go from [0,0] to [1,1], forcing you to convert to these coordinates first. I wrote a simple function to do this, provided your plot scale is linear (if you want logarithmic, you will have to modify this function):
## Convert from data coordinates to normalized figure coordinates.
function [xf yf] = figcoords(xa, ya)
axp = get(gca, "position");
lf = axp(1);
bf = axp(2);
rf = lf + axp(3);
tf = bf + axp(4);
xl = xlim();
yl = ylim();
la = xl(1);
ra = xl(2);
ba = yl(1);
ta = yl(2);
xf = lf + (xa-la).*(rf-lf)./(ra-la);
yf = bf + (ya-ba).*(tf-bf)./(ta-ba);
endfunction
With this out of your way, you can proceed to annotating the plot using the annotation function:
y = [0.1 0.3 10.0 1.0 0.5 0.1 24.0 0.6 0.1 0.2];
x = (1:length(y));
peaks = [3 7];
## Plot the data as you would normally
plot(x,y);
## Plot peak markers (no `hold on` needed)
[xp yp] = figcoords(peaks, y(peaks)); # Transform to figure coordinates
for coords = [xp; yp]
xpi = coords(1);
ypi = coords(2);
annotation("arrow", [xpi xpi], [ypi+eps ypi]);
endfor
Plot with annotated peaks
Here, we actually draw little arrows pointing from top onto the peaks.
As their height is very small, we only see the arrowheads.
The arguments to the annotation function are the x and y coordinates
of the endpoints of the arrow. Note that we added a small number (eps)
to the y-value of the starting point to make the arrow point downward.
If you want, you can tweak the appearance of the markers to make them more visually appealing:
y = [0.1 0.3 10.0 1.0 0.5 0.1 24.0 0.6 0.1 0.2];
x = (1:length(y));
peaks = [3 7];
coloridx = get(gca, "ColorOrderIndex")
peakcolor = get(gca, "ColorOrder")(coloridx,:); # Save current plot colour
plot(x,y);
## Plot peak markers
[xp yp] = figcoords(peaks, y(peaks));
for coords = [xp; yp]
xpi = coords(1);
ypi = coords(2);
annotation("arrow", [xpi xpi], [ypi+eps ypi], "headstyle", "plain",...
"color", peakcolor);
endfor
Plot with annotated peaks in the same color
Drawbacks
While this approach works fine regardless of the size of the markers or your plot, there are some drawbacks:
First, the annotations are fixed relative to the figure window, not the plot.
This is fine when you display the plot for the first time, but once you zoom
or pan, the alignment is lost: The markes stay in place while the plot moves.
If you don't need an interactive plot (eg, you just want to export it to image),
just be sure to set the plot limits before adding the annotations and you should
be fine.
Second, this method is very slow compared to plotting the points using the
plot function. On my computer, for example, when drawing a simple example with
seven annotated peaks, it takes about a second before the markers appear.
Plotting a signal with thousands of peaks is near impossible.
Concerning the Matlab part, you could draw the peak markers yourself. Somewhere along these lines (extending your example):
y = [0.1 0.3 10.0 1.0 0.5 0.1 24.0 0.6 0.1 0.2]
x = (1:length(y))
figure, plot(x,y);
leglengthx=0.2;
leglengthy=0.5;
hold on;
peaks = [3 7];
peaks_max=[10 24];
for ii=1:2
line([peaks(ii) peaks(ii)+leglengthx],[peaks_max(ii) peaks_max(ii)+leglengthy]);
line([peaks(ii) peaks(ii)-leglengthx],[peaks_max(ii) peaks_max(ii)+leglengthy]);
line([peaks(ii)-leglengthx peaks(ii)+leglengthx],[peaks_max(ii)+leglengthy peaks_max(ii)+leglengthy]);
end
plot(x(peaks), y(peaks), 'v', 'MarkerSize', 24);
I have added the maxima of the peaks, which should not be an issue to automatically extract and two variables that control the triangle size of the marker. And then its just drawing three lines for every peak.
I don't know how this will translate to Octave.
What about drawing the little triangles?
y = [0.1 0.3 10.0 1.0 0.5 0.1 24.0 0.6 0.1 0.2];
x = (1:length(y));
peaks = [3 7];
plot(x,y);
hold on; line([peaks(1) peaks(1)+0.2], [y(x==peaks(1)) y(x==peaks(1))+1], 'color','b')
hold on; line([peaks(1) peaks(1)-0.2], [y(x==peaks(1)) y(x==peaks(1))+1], 'color','b')
hold on; line([peaks(1)+0.2 peaks(1)-0.2], [y(x==peaks(1))+1 y(x==peaks(1))+1], 'color','b')
hold on; line([peaks(2) peaks(2)+0.2], [y(x==peaks(2)) y(x==peaks(2))+1], 'color','b')
hold on; line([peaks(2) peaks(2)-0.2], [y(x==peaks(2)) y(x==peaks(2))+1], 'color','b')
hold on; line([peaks(2)+0.2 peaks(2)-0.2], [y(x==peaks(2))+1 y(x==peaks(2))+1], 'color','b')
There can be a problem if the y-values of the peaks exists in other locations on the vector. If so, you can specify first or other matching specs for the find function.

Plotting 2D points without line in MATLAB

I have some 2D points and I want to plot them in MATLAB such that every point has a different color and specifier. I have used plot function but it creates line whatever you give. I want to draw these points as discrete points only. How can I do that? Here is what I am trying to achieve in the simplest form (I used TikZ below):
UPDATE:
Thank you for your comments and answers, I have the following code right now:
x = [ 0.56, 0.4526, -0.4324, 0.2749, -0.2993, 0.3404, 0.1959, 0.3363, -0.1706];
y = [0.1999, 0.3939, 0.1999, 0.4414, 0.2000, 0.3931, 0.1999, 0.3966, 0.4056];
figure
plot(x(1),y(1),'rx')
hold on
plot(x(2),y(2),'*','Color','[0 0.9 0]')
hold on
plot(x(3),y(3),'*','Color','[0 0.5 0]')
hold on
plot(x(4),y(4),'o','Color','[0.47 0.52 0.8]','MarkerFaceColor','[0.47 0.52 0.8]')
hold on
plot(x(5),y(5),'o','Color','[0.05 0.28 0.63]','MarkerFaceColor','[0.05 0.28 0.63]')
hold on
plot(x(6),y(6),'s','Color','[1 0.71 0.30]','MarkerFaceColor','[1 0.71 0.30]')
hold on
plot(x(7),y(7),'s','Color','[0.9 0.32 0]','MarkerFaceColor','[0.9 0.32 0]')
%plot(x(7),y(7),'s','Color','[1 0 0.5]','MarkerFaceColor','[1 0 0.5]')
hold on
plot(x(8),y(8),'d','Color','[0.67 0.28 0.73]','MarkerFaceColor','[0.73 0.40 0.78]')
%plot(x(8),y(8),'d','Color','[0.67 0.28 0.73]','MarkerFaceColor','[0.67 0.28 0.73]')
hold on
plot(x(9),y(9),'d','Color','[0.29 0.08 0.55]','MarkerFaceColor','[0.29 0.08 0.55]')
xlabel('X')
ylabel('Y')
h = legend('(^1X,^1Y)','(^2X_1,^2Y_1)','(^2X_2,^2Y_2)','(^3X_1,^3Y_1)','(^3X_2,^3Y_2)','(^4X_1,^4Y_1)','(^4X_2,^4Y_2)','(^5X_1,^5Y_1)','(^5X_2,^5Y_2)');
set(h,'Location','best')
grid
I can now draw the points as dots with different colors and specifiers although this way may not be the best way.
You can simply specify the LineSpec option
http://fr.mathworks.com/help/matlab/ref/plot.html#inputarg_LineSpec
To obtain your example:
plot(xdata, ydata, '.')