# software 1bit DAC

#### kubeek

Joined Sep 20, 2005
5,753
Hi,

im trying to write a 1bit sigma-delta DAC in C, but I don´t really know where to start. I wrote this piece of code to try it, but I really have no idea how to correctly do the integration which i feel should be there somewhere. Basically i would like to get it to compute this http://upload.wikimedia.org/wikipedia/en/2/20/Pulse-density_modulation_1_period.gif, or actually the 16 bits that approximate the sine first quadrant.

Here is my code:
Rich (BB code):
#include <math.h>
#include <stdio.h>
#define PI 3.141592654

int main(int argc, char** argv) {
double sinv[16];
for(int i=0;i<16;i++){
sinv=sin(((double)i+1)*PI/32);
}

double val=0.25;
double delta, sum=0;

for(int i=0;i<16;i++){
//val=sinv;

delta=sum-val;
if(delta<=0){
sum=(sum*1.0+1.0/16)/1;
printf("1 %f %f\n",sum, val);
}
else {
sum=(sum*1.0-1.0/16)/1;
printf("0 %f %f\n", sum, val);
}

}

return 0;
}



#### kubeek

Joined Sep 20, 2005
5,753
Ok so I got it to work like this with the help of this cool page http://www.beis.de/Elektronik/DeltaSigma/DeltaSigma.html

#include <math.h>
#include <stdio.h>

//using namespace std;

#define PI 3.141592654
#define SIZE 64
/*
*
*/
int main(int argc, char** argv) {

double sinv[SIZE];

for(int i=0;i<SIZE;i++){
sinv=sin(((double)i+1)*PI/SIZE/2);
}

double val=0.05;
double delta, sum=0, out=0, oldsum=0;

for(int i=0;i<SIZE;i++){
val=sinv;

delta=val-out;
sum=delta+oldsum;
if( oldsum>=0)
{
printf("1 %f %f %f \n",sum, oldsum,val);
out=1.0;
}
else {
printf("0 %f %f %f\n", sum, oldsum,val);
out=-1.0;
}
oldsum=sum;
}

return 0;
}

Last edited: