MATLAB: It shows error (NRZ encoder) : “Vectors must be the same lengths in plot(t,s).”

doit4meMATLABmatlab codernrz-i doit4me nrz-iplotvectorvectors

% Performs NRZI encoding on a bit string. It assumes each signal element
% is 0.5 sec giving 2 bps data rate.
clear;
bits=[1 1 1 1 1 1 1 1 1 1 1]; % input string
numOfBits = length(bits); % store no of bits in string
sampTime = 0.001; % sample time
endTime = numOfBits-sampTime; % required end time for given bit string
t=0:sampTime:10; % x-axis
done=0; % flag to assist in voltage inversion
j = 1; bit = 1;
prevVol = -1; % initial voltage (-ve)
for i = 0:sampTime:endTime
if(floor(i)+1 ~= bit)
% checks whether in the same bit
bit = bit + 1;
done = 0;
end;
if(bits(bit) == 0)
% Maintains voltage for bit 0
s(j) = prevVol;
else
if(~done)
% Inverts voltage for bit 1
s(j) = -1*prevVol;
prevVol = s(j);
done = 1;
j = j + 1;
continue;
end;
s(j) = s(j-1); % Maintains voltage for a signal element
end;
j = j + 1;
end;
% plots the signal
plot(t,s);
axis([0 numOfBits -3 3]);
xlabel('Time (s)');
ylabel('Voltage');

Best Answer

The length of t is 10001 while the length of s is 11000
Instead of t=0:sampTime:10 you should writte
t=0:sampTime:endTime
Related Question