% Analog to Digital Converter - Pulse Code Modulation R = 4; A = 8; t = 0:0.05:1; m = A*sin(2*pi*t); Delta = 2*A/2^(R); k = floor((A + m)/Delta); mq = -A + (k +1/2)*Delta; plot(t, m, 'k') hold on stairs(t,mq,'r') hold off encod = dec2bin(k, R); encodn = arrayfun(@(x)str2double(x), encod); K = 10; v = kron(encodn, ones(1, K)); v = v'; v = v(:); L = length(v); t = (0:L-1)/L; plot(t, v, 'k', 'LineWidth',2); ylim([-0.5 1.5]) xlabel('t [second]') ylabel('Amplitude')