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

118 views (last 30 days)
Sharada Reddy on 20 Jan 2017
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 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.

MathWorks Support Team on 17 Jun 2021
Edited: MathWorks Support Team on 17 Jun 2021
When you go to convert your 1-bit digital signal to an analog signal using an ideal DAC on [-1, 1], you will get the same square wave signal out of the DAC that you put in.  This is because the analog signal only changes when the digital one does, and their amplitudes and biases are the same.  Any samples not corresponding to a digital value change would inherit their value from the most recent sample that did.
How Do We Make an Ideal DAC in MATLAB?
Since we can’t work with truly continuous signals, the simplest method to approximate them is to use a time step size that is small relative to your digital time step size.  You can use
xq = interp1(t, x, tq, previous)
as an ideal DAC, given digital sampling points t and “analog” sampling points tq.  Once in the new higher sample rate you can low-pass filter, add noise, etc. until the response sufficiently represents your device.  This is, however, inefficient, due to the sample points spread all over a square wave instead of being concentrated at the transitions.
How Do We Do This More Efficiently, Sample Timing-Wise?
To get efficiently placed sample points, use Simulink®.  The solver will take care of placing sample points for you; an ideal DAC therefore would be the gain and bias required to change from the digital scale to the analog scale.  A ZOH (Zero-Order Hold) block will keep the sample rate on the digital side constant if placed between it and the analog side, and Gain and/or Bias blocks can handle the amplitude conversion between the digital and analog realms.
Non-ideal DAC Models with Impairments
Everything I said above assumes you, like the original asker, want an ideal DAC as fast as possible.  If you want to model the effects a practical DAC would have on your system a ZOH and/or Gain block will not suffice. The Mixed-Signal Blockset™ contains a Binary Weighted DAC block, which models offset error, gain error and settling time.  If you want to model stochastic noise impairments (thermal, environmental, etc.), Simulink offers a selection of configurable random sources in the standard library.

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 !