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 ...]
Related
I was trying to find out, how to plot a cumulative distribution function (cdf) with specific x values but was not successful.
For example, if the dataset is:
x = [2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45];
y = [0.20 0.09 0.15 0.13 0.17 0.04 0.7 0.15]; % (total 1)
the graph shape definitely looks wrong, when I use y = cdfplot(x).
I also plotted the graph with cumsum(y) and x to check the shape and it looks fine, but I would like to know, if there is any code which plots cumulative distribution plots.
There's the stairs function for creating "stairstep graphs", which should be exactly what you want, incorporating your cumsum(y) idea.
Please see the following code snippet. I added two additional points for the start and end of some interval, here [0 ... 25]. Also, your values in y sum up to something larger than 1, so I modified these values, too.
x = [0 2.50 5.21 7.67 8.43 9.15 11.47 14.59 21.45 25];
y = [0 0.10 0.09 0.05 0.10 0.14 0.04 0.4 0.08 0];
stairs(x, cumsum(y));
xlim([-1 26]);
ylim([-0.2 1.2]);
That'd be the output (Octave 5.1.0, but also tested with MATLAB Online):
Hope that helps!
I have a support (supp_epsilon) and a probability mass function (pr_mass_epsilon) in Matlab, constructed as follows.
supp_epsilon=[0.005 0.01;
0.01 0.015;
0.015 0.02;
0.02 0.025];
suppsize_epsilon=size(supp_epsilon,1);
pr_mass_epsilon=zeros(suppsize_epsilon,1);
mu_epsilon=[0; 0];
sigma_epsilon=[1 0.5; 0.5 1];
pr_mass_epsilon=zeros(suppsize_epsilon,1);
for j=1:suppsize_epsilon
pr_mass_epsilon(j)=mvnpdf(supp_epsilon(j,:),mu_epsilon.',sigma_epsilon)/sum(mvnpdf(supp_epsilon,mu_epsilon.',sigma_epsilon));
end
Note: supp_epsilon is 4x2. Each 1x2 row of supp_epsilon is an element of the support of interest.
Question: I want to draw n=10 vectors of size 1x2 from pr_mass_epsilon. Each of these vectors has to be a row of supp_epsilon. For n very large the empirical frequency of the drawn vectors has to be close to pr_mass_epsilon.
How can I do this?
Note: This question addresses the scalar case where the answer suggests to use randsample.
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, '.')
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.
I am currently creating different signals using Matlab, mixing them by multiplying them by a mixing matrix A, and then trying to get back the original signals using FastICA.
So far, the recovered signals are really bad when compared to the original ones, which was not what I expected.
I'm trying to see whether I'm doing anything wrong. The signals I'm generating are the following: (Amplitudes are in the range [0,1].)
s1 = (-x.^2 + 100*x + 500) / 3000; % quadratic
s2 = exp(-x / 10); % -ve exponential
s3 = (sin(x)+ 1) * 0.5; % sine
s4 = 0.5 + 0.1 * randn(size(x, 2), 1); % gaussian
s5 = (sawtooth(x, 0.75)+ 1) * 0.5; % sawtooth
One condition for ICA to be successful is that at most one signal is Gaussian, and I've observed this in my signal generation.
However, another condition is that all signals are statistically independent.
All I know is that this means that, given two signals A & B, knowing one signal does not give any information with regards to the other, i.e.: P(A|B) = P(A) where P is the probability.
Now my question is this: Are my signals statistically independent? Is there any way I can determine this? Perhaps some property that must be observed?
Another thing I've noticed is that when I calculate the eigenvalues of the covariance matrix (calculated for the matrix containing the mixed signals), the eigenspectrum seems to show that there is only one (main) principal component. What does this really mean? Shouldn't there be 5, since I have 5 (supposedly) independent signals?
For example, when using the following mixing matrix:
A =
0.2000 0.4267 0.2133 0.1067 0.0533
0.2909 0.2000 0.2909 0.1455 0.0727
0.1333 0.2667 0.2000 0.2667 0.1333
0.0727 0.1455 0.2909 0.2000 0.2909
0.0533 0.1067 0.2133 0.4267 0.2000
The eigenvalues are: 0.0000 0.0005 0.0022 0.0042 0.0345 (only 4!)
When using the identity matrix as the mixing matrix (i.e. the mixed signals are the same as the original ones), the eigenspectrum is: 0.0103 0.0199 0.0330 0.0811 0.1762. There still is one value much larger than the rest..
Thank you for your help.
I apologise if the answers to my questions are painfully obvious, but I'm really new to statistics, ICA and Matlab. Thanks again.
EDIT - I have 500 samples of each signal, in the range [0.2, 100], in steps of 0.2, i.e. x = 0:0.1:100.
EDIT - Given the ICA Model: X = As + n (I'm not adding any noise at the moment), but I am referring to the eigenspectrum of the transpose of X, i.e. eig(cov(X')).
Your signals are correlated (not independent). Right off the bat, the sawtooth and the sine are the same period. Tell me the value of one I'll tell you the value of the other, perfect correlation.
If you change up the period of one of them that'll make them more independent.
Also S1 and S2 are kinda correlated.
As for the eigenvalues, first of all your signals are not independent (see above).
Second of all, your filter matrix A is also not well conditioned, spreading out your eigenvalues further.
Even if you were to pipe in five fully independent (iid, yada yada) signals the covariance would be:
E[ A y y' A' ] = E[ A I A' ] = A A'
The eigenvalues of that are:
eig(A*A')
ans =
0.000167972216475
0.025688510850262
0.035666735304024
0.148813869149738
1.042451912479502
So you're really filtering/squishing all the signals down onto one basis function / degree of freedom and of course they'll be hard to recover, whatever method you use.
To find if the signals are mutually independent you could look at the techniques described here In general two random variables are independent if they are orthogonal. This means that: E{s1*s2} = 0 Meaning that the expectation of the random variable s1 multiplied by the random variable s2 is zero. This orthogonality condition is extremely important in statistics and probability and shows up everywhere. Unfortunately it applies to 2 variables at a time. There are multivariable techniques, but none that I would feel comfortable recommending. Another link I dug up was this one, not sure what your application is, but that paper is very well done.
When I calculate the covariance matrix I get:
cov(A) =
0.0619 -0.0284 -0.0002 -0.0028 -0.0010
-0.0284 0.0393 0.0049 0.0007 -0.0026
-0.0002 0.0049 0.1259 0.0001 -0.0682
-0.0028 0.0007 0.0001 0.0099 -0.0012
-0.0010 -0.0026 -0.0682 -0.0012 0.0831
With eigenvectors,V and values D:
[V,D] = eig(cov(A))
V =
-0.0871 0.5534 0.0268 -0.8279 0.0063
-0.0592 0.8264 -0.0007 0.5584 -0.0415
-0.0166 -0.0352 0.5914 -0.0087 -0.8054
-0.9937 -0.0973 -0.0400 0.0382 -0.0050
-0.0343 0.0033 0.8050 0.0364 0.5912
D =
0.0097 0 0 0 0
0 0.0200 0 0 0
0 0 0.0330 0 0
0 0 0 0.0812 0
0 0 0 0 0.1762
Here's my code:
x = transpose(0.2:0.2:100);
s1 = (-x.^2 + 100*x + 500) / 3000; % quadratic
s2 = exp(-x / 10); % -ve exponential
s3 = (sin(x)+ 1) * 0.5; % sine
s4 = 0.5 + 0.1 * randn(length(x), 1); % gaussian
s5 = (sawtooth(x, 0.75)+ 1) * 0.5; % sawtooth
A = [s1 s2 s3 s4 s5];
cov(A)
[V,D] = eig(cov(A))
Let me know if I can help any more, or if I misunderstood.
EDIT Properly referred to eigenvalues and vectors, used 0.2 sampling interval added code.