Implementing BPSK using MATLAB - matlab

I am trying to write some simple MATLAB code for plotting a BPSK but the code is not working. Here is the code:
t=0:(1/1000):3;
figure(1);
s0=sin( (2*pi)*t );
s1=sin( (2*pi)*t+(pi) );
sout=[s0 s1];
plot(t,sout);
grid on;

You can plot this code vectorized without having the need to use hold on as what Nematollah suggested. Simply place each data trace in a separate column. The t vector can be left alone. The reason why your code isn't working is because you placed s0 and s1 and concatenated them to be a single 1D array. t has 3001 elements while sout would have 3001 * 2 = 6002 elements. The dimensions are incompatible, which is why you're getting the error.
You can plot multiple signals using plot without a problem without the use of hold on. Just make sure that each trace is in a separate column. With your code setup, you just have to transpose s0 and s1 and this will work. As such:
t=0:(1/1000):3;
figure(1);
s0=sin( (2*pi)*t );
s1=sin( (2*pi)*t+(pi) );
sout=[s0.' s1.']; %// Change - transpose s0 and s1
plot(t,sout);
grid on;
This is what I get:
Notice that MATLAB automatically colour codes the traces for you, seeing as how each data trace is in a separate column.
Edit
You now want to concatenate these two signals together (which is actually what BPSK is supposed to do). We can easily do this by extending the time vector that you have and then tacking on the second signal when you're done like you did in your original post. You can easily do this in the following way:
t=0:(1/1000):3;
figure(1);
s0=sin( (2*pi)*t );
s1=sin( (2*pi)*t+(pi) );
sout = [s0 s1]; %// Note we are concatenating now
tvec = [t t(end) + t]; %// NEW
plot(tvec, sout); %// Plot 2 signals together as 1
Take a look at this statement:
tvec = [t t(end) + t];
I create a new vector which had the original time vector that goes between [0,3]. I then extend this vector so that we take the time vector from [0,3] and offset it by the last time value of the original signal, which is 3. As such, we would then add a vector of [3,6] at the end, as we will have two signals that are 3 seconds each - thus 6 seconds. This is what I get:
Looks like BPSK to me! :)

If you want to plot one symbol time, you should plot both signals on the same plot like this.
t = 0 : (1/1000) : 3;
s0 = sin((2*pi)*t);
s1=sin((2*pi)*t+pi);
plot(t, s0);
hold on
plot(t, s1, 'r');
grid

Related

Is it possible to plot rows of a matrix without a for loop?

I have a matrix that stores multiple functions in its rows each evaluated against the interval [0,20]. I'm running through a for loop to output them at the moment. Is there a better way of doing this or is this the only way of doing it in MATLAB?
h = 0.1;
xLine = 0:h:20;
nGrid = length(xLine);
nu = [ 1, 2, 3 ];
nNu = length(nu);
b = zeros(nNu,nGrid);
for i=1:nNu
b(i:i,1:nGrid) = besselj(nu(i), xLine);
end
hFig = figure(1);
hold on
set(hFig, 'Position', [1000 600 800 500]);
for i=1:nNu
plot(xLine, b(i:i,1:nGrid))
end
You can use plot vectorized. Specifically, you can supply b directly into plot but you need to make sure that the larger of the two dimensions in b matches the total number of elements in the vector xLine. This is what you have, so we're good. Therefore, because each unique signal occupies a row in your matrix, just supply b into your plot call and use it a single time.
hFig = figure(1);
hold on
set(hFig, 'Position', [1000 600 800 500]);
plot(xLine, b);
This will plot each row as a separate colour. If you tried doing this, you'll see that the plots are the same in comparison to the for loop approach.
Check out the documentation for plot for more details: http://www.mathworks.com/help/matlab/ref/plot.html
Replace for loop with:
plot(xLine, b(:,1:nGrid))
Note: I can't perfectly recall but some older versions of Matlab may want everything in columns and you'd want to transpose the matrices:
plot(xLine.', b(:,1:nGrid).')

Sequential connecting points in 2D in Matlab

I was wondering if you could advise me how I can connect several points together exactly one after each other.
Assume:
data =
x y
------------------
591.2990 532.5188
597.8405 558.6672
600.0210 542.3244
606.5624 566.2938
612.0136 546.6825
616.3746 570.6519
617.4648 580.4575
619.6453 600.0688
629.4575 557.5777
630.5477 584.8156
630.5477 618.5906
639.2696 604.4269
643.6306 638.2019
646.9013 620.7697
652.3525 601.1584
"data" is coordinate of points.
Now, I would like to connect(plot) first point(1st array) to second point, second point to third point and so on.
Please mind that plot(data(:,1),data(:,2)) will give me the same result. However, I am looking for a loop which connect (plot) each pair of point per each loop.
For example:
data1=data;
figure
scatter(X,Y,'.')
hold on
for i=1:size(data,1)
[Liaa,Locbb] = ismember(data(i,:),data1,'rows');
data1(Locbb,:)=[];
[n,d] = knnsearch(data1,data(i,:),'k',1);
x=[data(i,1) data1(n,1)];
y=[data(i,2) data1(n,2)];
plot(x,y);
end
hold off
Although, the proposed loop looks fine, I want a kind of plot which each point connect to maximum 2 other points (as I said like plot(x,y))
Any help would be greatly appreciated!
Thanks for all of your helps, finally a solution is found:
n=1;
pt1=[data(n,1), data(n,2)];
figure
scatter(data(:,1),data(:,2))
hold on
for i=1:size(data,1)
if isempty(pt1)~=1
[Liaa,Locbb] = ismember(pt1(:)',data,'rows');
if Locbb~=0
data(Locbb,:)=[];
[n,d] = knnsearch(data,pt1(:)','k',1);
x=[pt1(1,1) data(n,1)];
y=[pt1(1,2) data(n,2)];
pt1=[data(n,1), data(n,2)];
plot(x,y);
end
end
end
hold off
BTW it is possible to delete the last longest line as it is not related to the question, if someone need it please let me know.
You don't need to use a loop at all. You can use interp1. Specify your x and y data points as control points. After, you can specify a finer set of points from the first x value to the last x value. You can specify a linear spline as this is what you want to accomplish if the behaviour you want is the same as plot. Assuming that data is a 2D matrix as you have shown above, without further ado:
%// Get the minimum and maximum x-values
xMin = min(data(:,1));
xMax = max(data(:,1));
N = 3000; % // Specify total number of points
%// Create an array of N points that linearly span from xMin to xMax
%// Make N larger for finer resolution
xPoints = linspace(xMin, xMax, N);
%//Use the data matrix as control points, then xPoints are the values
%//along the x-axis that will help us draw our lines. yPoints will be
%//the output on the y-axis
yPoints = interp1(data(:,1), data(:,2), xPoints, 'linear');
%// Plot the control points as well as the interpolated points
plot(data(:,1), data(:,2), 'rx', 'MarkerSize', 12);
hold on;
plot(xPoints, yPoints, 'b.');
Warning: You have two x values that map to 630.5477 but produce different y values. If you use interp1, this will give you an error, which is why I had to slightly perturb one of the values by a small amount to get this to work. This should hopefully not be the case when you start using your own data. This is the plot I get:
You'll see that there is a huge gap between those two points I talked about. This is the only limitation to interp1 as it assumes that the x values are strictly monotonically increasing. As such, you can't have the same two points in your set of x values.

Matlab: Making errorbar() draw uninterrupted lines for NaN values in matrix

I have a matrix M of data and a matrix E of errors that I want to plot with errorbar against an independent variable x. I would also like lines between points in the same row. One column in M or E corresponds to one x-value, so this can be accomplished with
errorbar(x, M, E, 'o-')
I am missing some data points and these have the value NaN. As it should errorbar() ignores these, but the line between the points closest two points in the same row (the one resulting from the '-' option) is broken.
I have seen some different solutions that work with vectors as opposed to matrices, involving ~any(isnan(y),1) and L=~(isnan(x)|isnan(y)), but these lead to problems like
Error using errorbar
X, Y and error bars must all be the same length
I can't find a solution for matrices, any ideas?
Because the missing data points are probably not the same for every line I advice to plot in a loop like this (assuming plot data are columns of the matrices):
for i = 1 : size(M, 2)
data = M(:, i);
good = not(isnan(data));
xi = x(good);
data = data(good);
error = E(good, i);
errorbar(xi, data, error, 'o-');
hold on;
end

Attempting to create iterations with MATLAB (beginner)

I'm very very new to Matlab and I tried to attempt at making a simple iteration script.
Basically all I wanted to do was plot:
1*sin(x)
2*sin(x)
3*sin(x)
...
4*sin(x)
And this is the program that I wrote:
function test1
x=1:0.1:10;
for k=1:1:5;
y=k*sin(x);
plot(x,y);
end % /for-loop
end % /test1
However, it only plots y=5*sin(x) or whatever the last number is...
Any ideas?
Thanks!
Amit
You need to use the command hold on to make sure that the plot doesn't get erased each time you plot something new.
function test1
figure %# create a figure
hold on %# make sure the plot isn't overwritten
x=1:0.1:10;
%# if you want to use multiple colors
nPlots = 5; %# define n here so that you need to change it only once
color = hsv(nPlots); %# define a colormap
for k=1:nPlots; %# default step size is 1
y=k*sin(x);
plot(x,y,'Color',color(k,:));
end % /for-loop
end % /test1 - not necessary, btw.
EDIT
You can also do this without a loop, and plot a 2D array, as suggested by #Ofri:
function test1
figure
x = 1:0.1:10;
k = 1:5;
%# create the array to plot using a bit of linear algebra
plotData = sin(x)' * k; %'# every column is one k
plot(x,plotData)
Another option would be to use the fact the plot can accept matrices, and treats them as several lines to plot together.
function test1
figure %# create a figure
x=1:0.1:10;
for k=1:1:5;
y(k,:)=k*sin(x); %# save the results in y, growing it by a row each iteration.
end %# for-loop
plot(x,y); %# plot all the lines together.
end %# test1

maybe matrix plot!

for an implicit equation(name it "y") of lambda and beta-bar which is plotted with "ezplot" command, i know it is possible that by a root finding algorithm like "bisection method", i can find solutions of beta-bar for each increment of lambda. but how to build such an algorithm to obtain the lines correctly.
(i think solutions of beta-bar should lie in an n*m matrix)
would you in general show the methods of plotting such problem? thanks.
one of my reasons is discontinuity of "ezplot" command for my equation.
ok here is my pic:
alt text http://www.mojoimage.com/free-image-hosting-view-05.php?id=5039TE-beta-bar-L-n2-.png
or
http://www.mojoimage.com/free-image-hosting-05/5039TE-beta-bar-L-n2-.pngFree Image Hosting
and my code (in short):
h=ezplot('f1',[0.8,1.8,0.7,1.0]);
and in another m.file
function y=f1(lambda,betab)
n1=1.5; n2=1; z0=120*pi;
d1=1; d2=1; a=1;
k0=2*pi/lambda;
u= sqrt(n1^2-betab^2);
wb= sqrt(n2^2-betab^2);
uu=k0*u*d1;
wwb=k0*wb*d2 ;
z1=z0/u; z1_b=z1/z0;
a0_b=tan(wwb)/u+tan(uu)/wb;
b0_b=(1/u^2-1/wb^2)*tan(uu)*tan(wwb);
c0_b=1/(u*wb)*(tan(uu)/u+tan(wwb)/wb);
uu0= k0*u*a; m=0;
y=(a0_b*z1_b^2+c0_b)+(a0_b*z1_b^2-c0_b)*...
cos(2*uu0+m*pi)+b0_b*z1_b*sin(2*uu0+m*pi);
end
fzero cant find roots; it says "Function value must be real and finite".
anyway, is it possible to eliminate discontinuity and only plot real zeros of y?
heretofore,for another function (namely fTE), which is :
function y=fTE(lambda,betab,s)
m=s;
n1=1.5; n2=1;
d1=1; d2=1; a=1;
z0=120*pi;
k0=2*pi/lambda;
u = sqrt(n1^2-betab^2);
w = sqrt(betab^2-n2^2);
U = k0*u*d1;
W = k0*w*d2 ;
z1 = z0/u; z1_b = z1/z0;
a0_b = tanh(W)/u-tan(U)/w;
b0_b = (1/u^2+1/w^2)*tan(U)*tanh(W);
c0_b = -(tan(U)/u+tanh(W)/w)/(u*w);
U0 = k0*u*a;
y = (a0_b*z1_b^2+c0_b)+(a0_b*z1_b^2-c0_b)*cos(2*U0+m*pi)...
+ b0_b*z1_b*sin(2*U0+m*pi);
end
i'd plotted real zeros of "y" by these codes:
s=0; % s=0 for even modes and s=1 for odd modes.
lmin=0.8; lmax=1.8;
bmin=1; bmax=1.5;
lam=linspace(lmin,lmax,1000);
for n=1:length(lam)
increment=0.001; tolerence=1e-14; xstart=bmax-increment;
x=xstart;
dx=increment;
m=0;
while x > bmin
while dx/x >= tolerence
if fTE(lam(n),x,s)*fTE(lam(n),x-dx,s)<0
dx=dx/2;
else
x=x-dx;
end
end
if abs(real(fTE(lam(n),x,s))) < 1e-6 %because of discontinuity some answers are not correct.%
m=m+1;
r(n,m)=x;
end
dx=increment;
x=0.99*x;
end
end
figure
hold on,plot(lam,r(:,1),'k'),plot(lam,r(:,2),'c'),plot(lam,r(:,3),'m'),
xlim([lmin,lmax]);ylim([1,1.5]),
xlabel('\lambda(\mum)'),ylabel('\beta-bar')
you see i use matrix to save data for this plot.
![alt text][2]
because here lines start from left(axis) to rigth. but if the first line(upper) starts someplace from up to rigth(for the first figure and f1 function), then i dont know how to use matrix. lets improve this method.
[2]: http://www.mojoimage.com/free-image-hosting-05/2812untitled.pngFree Image Hosting
Sometimes EZPLOT will display discontinuities because there really are discontinuities or some form of complicated behavior of the function occurring there. You can see this by generating your plot in an alternative way using the CONTOUR function.
You should first modify your f1 function by replacing the arithmetic operators (*, /, and ^) with their element-wise equivalents (.*, ./, and .^) so that f1 can accept matrix inputs for lambda and betab. Then, run the code below:
lambda = linspace(0.8,1.8,500); %# Create a vector of 500 lambda values
betab = linspace(0.7,1,500); %# Create a vector of 500 betab values
[L,B] = meshgrid(lambda,betab); %# Create 2-D grids of values
y = f1(L,B); %# Evaluate f1 at every point in the grid
[c,h] = contour(L,B,y,[0 0]); %# Plot contour lines for the value 0
set(h,'Color','b'); %# Change the lines to blue
xlabel('\lambda'); %# Add an x label
ylabel('$\overline{\beta}$','Interpreter','latex'); %# Add a y label
title('y = 0'); %# Add a title
And you should see the following plot:
Notice that there are now additional lines in the plot that did not appear when using EZPLOT, and these lines are very jagged. You can zoom in on the crossing at the top left and make a plot using SURF to get an idea of what's going on:
lambda = linspace(0.85,0.95,100); %# Some new lambda values
betab = linspace(0.95,1,100); %# Some new betab values
[L,B] = meshgrid(lambda,betab); %# Create 2-D grids of values
y = f1(L,B); %# Evaluate f1 at every point in the grid
surf(L,B,y); %# Make a 3-D surface plot of y
axis([0.85 0.95 0.95 1 -5000 5000]); %# Change the axes limits
xlabel('\lambda'); %# Add an x label
ylabel('$\overline{\beta}$','Interpreter','latex'); %# Add a y label
zlabel('y'); %# Add a z label
Notice that there is a lot of high-frequency periodic activity going on along those additional lines, which is why they look so jagged in the contour plot. This is also why a very general utility like EZPLOT was displaying a break in the lines there, since it really isn't designed to handle specific cases of complicated and poorly behaved functions.
EDIT: (response to comments)
These additional lines may not be true zero crossings, although it is difficult to tell from the SURF plot. There may be a discontinuity at those lines, where the function shoots off to -Inf on one side of the line and Inf on the other side of the line. When rendering the surface or computing the contour, these points on either side of the line may be mistakenly connected, giving the false appearance of a zero crossing along the line.
If you want to find a zero crossing given a value of lambda, you can try using the function FZERO along with an anonymous function to turn your function of two variables f1 into a function of one variable fcn:
lambda_zero = 1.5; %# The value of lambda at the zero crossing
fcn = #(x) f1(lambda_zero,x); %# A function of one variable (lambda is fixed)
betab_zero = fzero(fcn,0.94); %# Find the value of betab at the zero crossing,
%# using 0.94 as an initial guess