x=[0:1/15:1]';
y=cos(2*pi*1*x);
q=[0:1/15:1];
z=7.5*(y+1);
w=round(z);
u=dec2bin(w,4);
NN=max(size(z)); % NN is the number of data points or samples
D=w; % reasign the w variable to D
for ii=1:NN % This loop picks out the 4 bits per data point and stores them
Temp=D(ii); % every time through the loop, assign the data to the temporary variable Temp
if Temp >7 % this checks to see if the most significant bit is a 1
A(4*(ii-1)+1)=1; % if it is a 1, then store a 1 in the A vector
Temp=Temp-8; % now subtract 8 so that the most significant bit is set to 0 (this sets up the variable for the next check)
else
A(4*(ii-1)+1)=0; % if the most significant bit is 0, then store 0
end
if Temp >3 % this checks to see if the next most significant bit is a 1
A(4*(ii-1)+2)=1; % if it is a 1, then store a 1 in the A vector
Temp=Temp-4; % now subtract 4 so that the bit is set to 0
else
A(4*(ii-1)+2)=0; % if the bit is 0, then store 0
end
if Temp >1 % this checks to see if the third most significant bit is a 1
A(4*(ii-1)+3)=1; % if it is a 1, then store a 1 in the A vector
Temp=Temp-2; % now subtract 2 so that the bit is set to 0
else
A(4*(ii-1)+3)=0; % if the bit is 0, then store 0
end
if Temp >0 % this checks to see if the least significant bit is a 1
A(4*(ii-1)+4)=1; % if it is a 1, then store a 1 in the A vector
else
A(4*(ii-1)+4)=0; % if the bit is 0, then store 0
end
end
% note that the "stairs" command can be used rather than the following loop
for ii=1:max(size(A)) % This loop creates two plot points for every time point
B(2*(ii-1)+1)=A(ii);
B(2*(ii-1)+2)=A(ii);
T(2*(ii-1)+1)=(ii-1);
T(2*(ii-1)+2)=(ii);
end
plot(T,B)