What is the reason for adding 1 to magnitude of sine when we want to send samples to DAC (12 bit resolution)?

1 view (last 30 days)
I'm not understanding reason for using y=y+1; We know that sin(0) is 0 . But getting 2048 as first sample.
What is the reason for adding 1 to Y?
Y = Y + 1;
This is the code written to generate sine wave for DAC input .
clear all;
close all;
clc; % Clear The Previous Points
Ns = 128; % Set The Number of Sample Points
RES = 12; % Set The DAC Resolution
OFFSET = 0; % Set An Offset Value For The DAC Output
%------------[ Calculate The Sample Points ]-------------
T = 0:((2*pi/(Ns-1))):(2*pi);
Y = sin(T);
Y = Y + 1;
Y = Y*((2^RES-1)-2*OFFSET)/(2+OFFSET);
Y = round(Y);
plot(T, Y);
%--------------[ Print The Sample Points ]---------------
fprintf('%d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, %d, \n', Y);

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jun 2021
Adding one transforms it from the range [-1 1] to [0 2] . Multiply by 2^12 to get a value in the range [0 4096] . Divide by 2 to get [0 2048]. Which is suitable for transmission over a digital interface using unsigned bits.
(The way they use OFFSET looks a bit dubious to me though. But it doesn't affect anything when OFFSET is 0.)
If 1 were not added to y then you would start having to transmit values in the range [-2048 2048], and then you would start having to argue about which of the three major representations you use for negative values.

Sign in to comment.

More Answers (0)


Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!