How do I write MATLAB code for a 1 bit DAC?
23 次查看(过去 30 天)
显示 更早的评论
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.
34 个评论
Walter Roberson
2017-1-20
What would the sinusoidal pulse look like? Are you thinking of all zeros normally but when the pulse occurs then there is a single sine period? If so would it be fixed width or does the width of the pulse have to be changed? Do you need to represent a 0 differently than a 1?
Sharada Reddy
2017-1-24
I am working with dsm toolkit in matlab and i want to convert the output of dsm which is digital into analog using 1-bit DAC. could anyone help me with the code?
Walter Roberson
2017-1-24
We might be able to help if you could clarify what the analog output should look like. Also please link to the documentation for the block you are using as there are multiple Digital to Analog blocks.
Sharada Reddy
2017-1-25
编辑:Sharada Reddy
2017-1-25
I have attached two figures, one showing the original signal which is in analog and the second one showing the digital signal along with the original. I want to convert this digital signal back to original analog signal, preferably using 1-bit DAC . Could you please help me with the code? The pulse width is changing..it is not a standard sine signal
Walter Roberson
2017-1-25
It appears to me that it cannot be converted using a 1-bit DAC.
If it could be converted with a 1 bit DAC, then whether the analog signal is above 0 or not should depend exactly to whether the digital is above 0 or not, or else should depend upon it in a simple repeating pattern. For example one could imagine that the analog signal being above 0 should be in agreement with the digital for the first half of a cycle of period 2, and should be exactly opposite it for the second half of the cycle of period 2. So if you use
ydigital = digital signal
yanalog = analog signal
ydr = (yanalog > 0) * 2 - 1; %y reconstructed
seq = ydigital == ydr;
ff_seq = fft(seq);
ff_seq(1) = 0;
plot(abs(ff_seq))
then you would expect to see a simple plot with a single frequency peak corresponding to the analog-to-digital concurrence period. But you do not: you see lots of peaks, none of which is clearly dominant, and it is not a "signal and overtones" matter either.
We have to conclude from this that the analog signal is not generated by the principle of being the same polarity as the digital signal, and not generated by the principle of being same or opposite polarity in short pattern. This leads us to the conclusion that the analog signal must have more than 1 bit of state and so cannot be generated by a 1 bit DAC.
Sharada Reddy
2017-1-25
I get what you said, that the analog signal does not have one single frequency and has many peaks. I was going by the concept of Delta sigma modulator(DSM) where the digital output (-1 to 1) has to be passed through a 1-bit DAC inorder to reduce errors and obtain the analog signal which was fed originally. What I have actually done is designed OFDM transmitter and extended it to DSM that is how my analog (non-continous) got converted to digital. It would be grateful if you could tell me what should i do to retrieve my analog waveform.
Ajith Kumar
2017-1-25
I understand what you are saying. But Delta sigma modulator has this 1 bit conversion right.Can you say how do i convert my two level signal (-1 and +1) into analog signal ? Where i want my analog signal to change with respect to width of my two level signal.
Walter Roberson
2017-1-25
Is ofdm being applied to the digital signal that you plot before the analog signal is created? If so then the digital signal you plotted could have been spread out to create a different digital signal that was passed to the daq.
If the digital signal you plotted is the result of the ofdm process, then that analog signal cannot be created from it using a 1 bit daq, not unless perhaps there is a known long period frequency modulation that is supposed to be imposed. (And if there is, I am not convinced that it could be handled with a 1 bit daq)
Sharada Reddy
2017-1-26
编辑:Walter Roberson
2017-1-26
yeah OFDM is applied to the analog signal and then it is converted into digital signal using DSM. In DSM one bit comparator is used after integrator which converts analog signal to digital (having two levels -1 and +1) I have attached a block diagram which shows what are the steps that were followed .
if any(strcmp(fieldnames(arg2),'zeros'))
ntf.zeros = arg2.zeros;
else
error('No zeros field in the NTF.')
end
if any(strcmp(fieldnames(arg2),'poles'))
ntf.poles = arg2.poles;
else
error('No poles field in the NTF.')
end
This part if the code from DSM toolkit i suppose is the code for comparator which converts analog to digital that is acts acts as adc.
Walter Roberson
2017-1-26
The digital signal that is plotted in your second figure: which signal does that correspond to on your block diagram? Does it correspond to the exact signal that is to be input to the 1 bit DAC, and is the analog signal you plotted intended to be the result of that 1 bit DAC? If so then No, I do not see any reasonable way to convert that digital stream to that analog stream.
If, though, the digital signal that you plotted is intended to correspond to the "data" that is flowing in from the top left, then because there are several processing steps after that, such as adding the cyclic prefix, then perhaps it could correspond to the analog signal.
Another possibility is that the analog signal that you plotted is intended to be the received signal, that has has signal spreading happen on it, and which has be converted back to digital and error correction done: if so, if we assume that the analog signal contains some errors, then there might be some leeway.
Sharada Reddy
2017-1-26
NO the digital signal is obtained after doing cyclic prefix and all other processes. That is ,what you said formerly is right,what I intend to do is to feed this digital output to 1- bit DAC to recover the analog signal that i got after OFDM. As per the block diagram the digital signal is obtained after DSM block and the analog signal is the original signal fed initially. However the analog signal need not to correspond exactly to the one in plot after passing through 1-bit DAC because I will be further processing the output of 1-bit DAC as you said it will have errors and hence LPF , anti alising filter will be used. Right now I am not able to get analog signal back from the DAC. Once I get i will further process it to retrieve the exact same one as in the plot. Hope you could suggest a way out.
Walter Roberson
2017-1-26
The analog signal that you show, where was it measured in the block diagram? And is there a part of the diagram that corresponds to the signal transmission (through a media that would have signal spreading, such as through a fibre optic cable)?
Sharada Reddy
2017-1-27
The analog signal that I attached is received after "add cyclic prefix" block. And I dont want the exact analog signal back after 1-bit DAC. I just want to convert digital signal into analog, need not be exactly same as in the plot. The analog signal has errors in it since it was processed. SO all I want is convert the digital signal into analog using 1-bit DAC , its not required to be same.Hope you could give me a code that performs the work of a DAC.
Sharada Reddy
2017-1-27
Could you tell me if its possible to convert the two level digital signal(-1 and +1) into two level analog signal. If so please give the code. Also could you tell how to add bias to the two level analog signal thus obtained.
Walter Roberson
2017-1-27
To convert to analog we need to know what kind of digital to analog hardware you are using and how you are creating connecting to it. Are you using an NI usb-6009? Are you using an arduino with an I2c device? Are you using an Ethernet connected Tektronics signal generator with a VISA driver?
Sharada Reddy
2017-1-27
I am working in visible light communication, the analog signal from DAC will be sent to LED, I am not using any hardware device to convert digital into analog. First I will analyse and only then will proceed with hardware. So I want just the code in matlab itself for dac.
Sharada Reddy
2017-1-30
The harware for vlc I will be using is N5182A MXG VSG 100khz-3Ghz. I want to convert my digital signal into analog signal in this particular frequency range. Could you please give me atleast the outline code of DAC ?
Walter Roberson
2017-1-31
There is an example https://www.mathworks.com/help/instrument/examples/generating-and-downloading-a-baseband-signal-to-an-rf-signal-generator.html showing communications with that device. It requires Instrument Control Toolbox, as it uses VISA drivers.
Sharada Reddy
2017-2-6
编辑:Sharada Reddy
2017-2-6
Thank-you so much for your help. Also could you tell if there is any way to convert electrical(digital signal) to optical signal using matlab and not using hardware part? The above example is for baseband signal to rf signal. But i want to pass through optical channel so need a way to convert.
Walter Roberson
2017-2-6
You would need hardware to convert digital to real optical.
There is probably a way to convert digital to simulated optical. Perhaps not an a convenient form.
https://www.mathworks.com/matlabcentral/fileexchange/40093-interactive-simulation-toolbox-for-optics
https://www.mathworks.com/products/connections/product_detail/product_35663.html (commercial product)
Sharada Reddy
2017-2-7
Thank you for those links Walter and I wanted to know if there is any hardware setup possible using my signal generator other than usage of micro controller-MATLAB interface?
Walter Roberson
2017-2-7
I do not understand the question? That device has uses VISA drivers; you could connect to it using any interface you both have. That might be a USB interface or might be a TCP interface, if the device happens to have an ethernet card. It is not necessary to have that device plugged into any kind of micro-controller for it to be accessed by MATLAB.
Sharada Reddy
2017-2-7
编辑:Sharada Reddy
2017-2-7
But that would not be an optical channel. My main aim is to do VLC(visible light communication). The signal generator has will process electrical signal.. but i want to use optical channel using LED. If this method will not work could you please suggest a way out which can convert electrical signal to optical. Opti system is there but its little difficult to implement. If there is a way out in MATLAB istlef it would be of great help. Toolboxes like VLC ,FDTD but none of them look to work as VLC channel part
Walter Roberson
2017-2-7
Your signal generator outputs signals only, except for the command inputs that tell it what signal to output. Sending the commands to it over optical channel would not appear to be useful ? It would seem more likely to me that you want to connect to your signal generator as usual but have it somehow output an optical signal ?
You need a digital or electrical signal to control voltages to one or more LED in order to have them transmit information as light. You also need one or more optical receivers to receive the light and turn it into an analog signal, and you need some kind of high speed analog to digital device to be able to convert the electrical signal (from optical) to digital form that can be read in by MATLAB. (You might be able to find direct optical to digital devices without having to go through a specific analog to digital step.) Getting digital input device that works fast enough might not be easy.
Your MGX can handle up to 6 output channels. If you do not have more than 6 LED then you could potentially take the outputs of the MGX channels and connect them to LEDs fairly directly, perhaps with some resistors; possibly with an induction coil as well.
It is not clear to me, though, that the MGX is doing anything useful for you, compared to just using a standard digital to analog device. The MGX might perhaps be much faster than most digital to analog devices, but really to take advantage of faster you need to keep the data channel filled, which is going to be difficult. The MGX would be most useful in creating a repeated signal for debugging your receiver portion. Debug your receiver first and then use the receiver to debug the transition.
Sharada Reddy
2017-2-7
I guess you got me somewhere wrong. I want to simulate this purely using MATLAB first for which I want my digital signal to be converted to optical using Matlab itself.Hardware is a later part, I think I was not clear with my question. Please let me know if what I am trying to simulate purely using MATLAB is not possible or is tedious.
Walter Roberson
2017-2-7
You wrote "The signal generator has will process electrical signal.. but i want to use optical channel using LED"
The signal generator "process electrical signal" only as command input, telling it what to generate. Are you asking that an optical connection be used between MATLAB and the signal generator to tell the signal generator what to output?
If what you want is a normal connection between MATLAB and the signal generator, and you want to convert the electrical output of the signal generator into optical signals, then you have choices. If you do not need to drive more than 6 LED, then one choice is to simply wire the output signals of the signal generator to the LEDs, just with enough resistors to prevent the voltage of the signal generator output from exploding the LEDs. Just attach a wire to the signal generator output, and attach the wire to a breadboard, add some resistors, and connect the LEDs.
But then you start talking about "without physical LED and MGX". For that you need simulated optical, which is what I was talking about in http://www.mathworks.com/matlabcentral/answers/321197-how-do-i-write-matlab-code-for-a-1-bit-dac#comment_426548. You need a program that models the characteristics of light in some medium, including diffraction and reflection and the way that different frequencies of light travel at different speeds in ways related to the index of refraction. If you are simulating over any significant distance in air, you also need to model in temperature differences (light refracts near temperature boundaries) and you might also need to model dust (which is going to scatter the light, but the way it scatters the light is going to depend upon the size of the dust and possibly even the composition of the dust.)
You might also need to model the activation of LEDs, whether they turn on strictly according to a threshold voltage, whether they turn off at the same voltage or a lower one, whether brightness varies with current, whether shape of the pulse matters to how the LED produces light.
Sharada Reddy
2017-2-7
Yeah I am sorry for the confusion created, I want to simulate and i need help in writing a program/matlab code that would serve this purpose. I dont want to switch to optisystem software as it will be difficult to implement. Is matlab alone can be used to do simulation part? Just by defining various parameters as you mentioned, in that case how will I give digital signal as input. I went through visible light communication codes in file exchange but there was no sign of defining input data.
Walter Roberson
2017-2-7
What aspects of the optical channel do you want to model?
For example for your purposes would it be enough just to do a convolution? Or to run through a FIR filter designed to smear data from different times so that waveforms are distorted?
Sharada Reddy
2017-2-8
Just convolution is enough as I will be using filter at the receiver side once the light signal is detected and converted back to electrical at receiver end, So only convolution part would do the work. I want to write a code which converts electrical into light , passes through a channel/medium and then is detected by photo detector and converted back to electrical. I will thereafter process the signal.
Walter Roberson
2017-2-8
If you had a digital input from before the simulated optical transfer, and you also had a pre-filtering digital input from after the simulated optical transfer, then you could fft the two and divide the two fft results, and inverse fft to get the filter coefficients that would transform the one to another.
If not, then you will have to make something up, perhaps by sythesizing a signal and what it might look like after the simulated transfer.
Sharada Reddy
2017-2-13
I wanted to know how to calculate the power of a digital signal and how to revert it back. The input to my LED is power signal and the output of my photo detector is also power signal. But the digital signal should pass through the channel. Please see if you can help Walter.
Walter Roberson
2017-2-13
"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
2017-2-21
编辑:Sharada Reddy
2017-2-21
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
2017-2-21
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
2021-6-17
编辑:MathWorks Support Team
2021-6-17
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.
更多回答(2 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Analog Data Acquisition 的更多信息
标签
尚未输入任何标签。
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)