# How do I write MATLAB code for a 1 bit DAC?

83 views (last 30 days)
Sharada Reddy on 20 Jan 2017
Answered: ayyoub on 28 Feb 2020
I am doing my final year project where I am struck with the 1 bit Digital to Analog Converter block. I want to convert my Digital 1 bit pulse signal into an analog sinusoidal signal. Can anybody tell me how to write1 the MATLAB code to do it? Even the basic command syntax is enough. Thank you.

Walter Roberson on 13 Feb 2017
"The power of a signal is the sum of the absolute squares of its time-domain samples divided by the signal length, or, equivalently, the square of its RMS level"
fig2 = open('dsm output and original signal.fig');
ax = findall(fig2,'axes');
ch = get(ax,'Children');
dig_y = get(ch(2),'YData');
mod_y = get(ch(1),'YData');
fdig = fft(dig_y);
fmod = fft(mod_y);
fd = fdig;
fd(fd == 0) = 1;
warp = fmod ./ fd;
Now, for any digital output of length exactly 272, you can
f_signal = fft(TheDigitalSignalOf272);
f_mod_signal = f_signal .* warp;
ModSignalOf272 = ifft(f_mod_signal);
and ModSignalOf272 will now be version of the original signal modulated according to exactly the same pattern as was present in the diagram you posted a while ago.
Sharada Reddy on 21 Feb 2017
My digital data is for instance 101011101. How should I convert it into analog signal? Do you have any MATLAB code (not Simulink) that can convert digital data into analog? I have searched a lot and all gave ADC code only and none DAC. Hope u can give a sample code or logic that will convert digital into analog. The above code uses ,fig i dont have GUI output. I have a normal digital output in terms of 0 and 1. In accordance to your previous comment :"The power of a signal is the sum of the absolute squares of its time-domain samples divided by the signal length, or, equivalently, the square of its RMS level" the power digital signal will be 1 as it contains only 0 and 1. which is of no use to my project. could you please give a valid dac code?
Walter Roberson on 21 Feb 2017
You cannot convert your digital data into analog signal without using hardware to do so. You can at most create simulated analog signals. Which is what we have been discussing.
The digital data you have is nice and clean. There is no point post filtering it and so on. Your simulation of the transport layer would be useless without some distortion of the signal.
But how should be simulate distortion of the signal reasonably accurately? Well, I posted links to File Exchange contributions that allow detailed simulation of optical signal distortion. You said that was too complicated.
So I went back to the GUI output you posted. You posted it in https://www.mathworks.com/matlabcentral/answers/321197-how-do-i-write-matlab-code-for-a-1-bit-dac#comment_423138 . And I used that as a template for how your distortion should look.
This is not a matter of you going through a graphical output and reading off the graph and so on: you already posted representative readings, and I showed you how to use those representative readings to calculate parameters that you can use from now on as your simulated distortion function. Provided, that is, that you transmit in batches of 272 samples.
"the power digital signal will be 1 as it contains only 0 and 1"
First of all, that will not be true for the (simulated) distorted signal: it will have a range of values.
Secondly: your digital signals (1 and 0) are being ideally mapped into +1 to -1. The squares of +1 and -1 are both +1, so Yes, the ideal is constant power. However, you asked for half sine waves, not for square waves, and sine waves take time to transition between +1 and -1.

Walter Roberson on 21 Feb 2017
shows an example of Digital to Analog conversion in MATLAB.

ayyoub on 28 Feb 2020
hey did you manage to make it !

### Community Treasure Hunt

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

Start Hunting!