/*created 2020. by Fathia Hamed. FabLab Libya Team **** This example code is used to filter the input signal as Low Pass Filter at fc=10Hz & sample rate=500. read analoge signal from (a1) and write it at (DAC1). you can see the input signal at DAC0. this signal can be generated & tested by (MyDAQ) using NI Elvis software. Note: this code running only on ARDUINO DUE. Note: we must add DC offset to appear full signal. neither that the ARDUINO will cut the negative edge. **** Reference: http://musicdsp.org/ */ int led = 13; float wc,wc2,wc3,wc4,k,k2,k3,k4,sq_tmp1,sq_tmp2,sq_tmp4,a_tmp; float b1, b2, b3, b4, a0,a1,a2,a3,a4; float input,output,pi=3.14285714285714,sqrt2; int fc=10, srate=500; float xm1,xm2,xm3,xm4,ym1,ym2,ym3,ym4; void setup() { Serial.begin(9600); //Blink led 13 to check code. pinMode(led, OUTPUT); digitalWrite(led, HIGH); // For test delay(3000); digitalWrite(led, LOW); delay(1000); ///////////// wc=2*pi*fc; wc2=wc*wc; wc3=wc2*wc; wc4=wc2*wc2; k=wc/tan(pi*fc/srate); k2=k*k; k3=k2*k; k4=k2*k2; sqrt2=sqrt(2); sq_tmp1=sqrt2*wc3*k; sq_tmp2=sqrt2*wc*k3; a_tmp=4*wc2*k2+2*sq_tmp1+k4+2*sq_tmp2+wc4; b1=(4*(wc4+sq_tmp1-k4-sq_tmp2))/a_tmp; b2=(6*wc4-8*wc2*k2+6*k4)/a_tmp; b3=(4*(wc4-sq_tmp1+sq_tmp2-k4))/a_tmp; b4=(k4-2*sq_tmp1+wc4-2*sq_tmp2+4*wc2*k2)/a_tmp; /////////////// a0=wc4/a_tmp; a1=4*wc4/a_tmp; a2=6*wc4/a_tmp; a3=a1; a4=a0; } void loop() { // analogWriteResolution(12); //analogReadResolution(12); /////////////// input = analogRead(1); // analogWrite(DAC0,input); output=a0*input+a1*xm1+a2*xm2+a3*xm3+a4*xm4-b1*ym1-b2*ym2-b3*ym3-b4*ym4; xm4=xm3; xm3=xm2; xm2=xm1; xm1=input; ym4=ym3; ym3=ym2; ym2=ym1; ym1=output; //analogWrite(DAC1,output); Serial.print(input); Serial.print(" "); Serial.println(output); delayMicroseconds(2000); }