Nothing Special   »   [go: up one dir, main page]

Lab 7 - Digital Filter On Arduino

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Signals Filtering

Electronics Engineering, Engineering Department, UAEM


Semester 2022A
Date: June 1th, 2022
Name(s):

Lab session 7: Digital IIR filter on Arduino UNO

Objectives:
 To design and implement a digital filter using Matlab and Arduino
 To observe the limitations of microcontrollers for filter´s implementation.
Pre-lab:
1. To study about the IIR filters design analytically and using software as Matlab or others.
2. Install Matlab and Arduino IDE
3. Install the TimerOne.h library your Arduino IDE

Safety precautions:
Ensure that your circuit connections are correct before applying energy to the it. Remember
that Arduino onlys supports positive voltages and only between 0 and 5 volts.

List of equipment and materials


 Function generator (SFG-2110,GW INSTEK)
 Arduino UNO
 Jumpers and USB connectors
 Computer with Matlab and Arduino IDE

Procedure:
1. Write a code in Matlab to design a Bandreject filter with the following characteristics
 Rejected frequency: 60 Hz
 Bandwith: 5 Hz
 Passband ripple: 1dB
 Order filter: 3
 Sampling frequency: 300Hz
Put here the code, put a comment on eachs lien of code to explain it.
clear;clc;
fm=300; % Frecuencia de muestreo
fp1=57.5; %Frecuencia de corte
fp2=62.5; %Frecuencia de paro
Ap=1; %Rizados en banda pasante
n=3;
[num,den]=cheby1(n,Ap,[fp1/(fm/2) fp2/(fm/2)],'stop') %Calcula
coeficientes
freqz (num,den,1024,fm)

Figure 1•Simulated response graph in MATLAB indicates

Figure 2••Running the above code the results are as follows


Put here the results of the coefficients calculated in MATLAB. This page could be useful.
https://www.mathworks.com/help/signal/ref/cheby1.html

2. From the coefficients calculated, write here (using the equation´s editor or similar
program), the following:

a. The transfer function of the filter


Y ( Z ) 0.8788−1.6316 z−1 +3.6462 z−2−3.4715 z−3 +3.6462 z−4−1.6316 z −5 +0.8788 z−6
H ( z )= =
X (Z ) 1−1.7783 z 1 +3.8011 z 2−3.4655 z 3 +3.4815 z 4−1.4909 z 5+ 0.7673 z 6
−1 −2 −3 −4 −5
Y ( z )=0.8788 X ( z )−1.6316 X ( z ) z +3.6462 X ( z ) z −3.4715 X ( z ) z +3.6462 X ( z ) z −1.6316 X ( z ) z +0.8788

b. The difference´s equation


Y ( k )=0.8788 X ( k )−1.6316 X ( k −1 ) +3.6462 X ( k−2 ) −3.4715 X ( k−3 )+3.6462 X ( k −4 )−1.6316 X ( k −5 ) +

3. Modify the following code to implement the difference´s equation designed.


#include "TimerOne.h"
float y1=0;  //Variables para filtro
float y2=0;
float y3=0;
float x1=0;
float x2=0;
float x3=0;
 
void setup()
{
  Serial.begin(115200);   // Abre el puerto serial
  Timer1.initialize(5000);   // initialize timer1,Value 5000 us=5ms
  Timer1.attachInterrupt(interrupcion);  // Nombra la rutina de interrupción por timer
}
void interrupcion()  //Rutina de interrupción
{
float ADCval = analogRead(0);  //Lee canal analógico y lo convierte a voltaje
float filtrado;
//Calcula la salida filtrada
filtrado=0.686*ADCval-2.0579*x1+2.0579*x2-0.686*x3+2.2805*y1-1.7661*y2+0.4412*y3;
Serial.print(ADCval); //Manda dato a monitor serial
Serial.print(' ');
Serial.println(filtrado); //Manda dato a monitor serial
x3=x2;
x2=x1;
x1=ADCval;   //Se actualizan variables
y3=y2;
y2=y1;
y1=filtrado;
}
void loop() //Lazo principal
{

then we modify the previous code to fit our needs and load it on the Arduino
we need 7 variables for X and 6 variables for Y
// No hagas nada en el lazo principal
}
CODIGO MODIFICADO
#include "TimerOne.h"
float x1=0; //Variables para filtro
float x2=0;
float x3=0;
float x4=0;
float x5=0;
float x6=0;
float x7=0;
float y1=0;
float y2=0;
float y3=0;
float y4=0;
float y5=0;
float y6=0;

float a0=0.8788; //Variables para los coeficientes


float a1=-1.6316;
float a2=3.6462;
float a3=-3.4715;

float b2=1.7783;
float b3=-3.8011;
float b4=3.4655;
float b5=-3.4815;
float b6=1.4909;
float b7=-0.7673;
void setup()
{
Serial.begin(115200); // Abre el puerto serial
Timer1.initialize(3333.33); // initialize timer1,Value in para 500 Hz
Timer1.attachInterrupt(interrupcion); // Nombra la rutina de interrupción por timer
}
void interrupcion() //Rutina de interrupción
{
//float ADCval = analogRead(0); //Lee canal analógico y lo convierte a voltaje
float x0 = analogRead(0); //Lee canal analógico y lo convierte a voltaje
float filtrado;

//Calcula la salida filtrada


filtrado=a0*x0+a1*x1+a2*x2+a3*x3+a2*x4+a1*x5+a0*x6+b2*y1+b3*y2+b4*y3+b5*y4+b6*y5+b7*y6;
Serial.print(x0); //Manda dato a monitor serial
Serial.print(' ');
Serial.println(filtrado); //Manda dato a monitor serial
y6=y5;
y5=y4;
y4=y3;
y3=y2;
y2=y1;
y1=filtrado;
x7=x6;
x6=x5;
x5=x4;
x4=x3;
x3=x2;
x2=x1;
x1=x0;
}
void loop() //Lazo principal
{
}l{
// No hagas
4. Connect the signal generator to the A0 channel of the Arduino and ground. Make sure
that the signal in the generator does not Exceed the limits of the Arduino (No negative
voltages, only between 0 and 5 V). Put here a photo of the circuit including generator,
Arduino and computer. Explaing the image.
then we connect the arduino to the function generator with an amplitude of 2.3V with
an offset of 2.5

Figure 3 photo of the circuit including generator, Arduino and computer

5. Load the program modified and test the filter. Put three different frequencies and put
and image of each one. Explain the images.

At 10 Hz we can see that it lets the signal pass correctly. the input and output go together

Figure 4 10 HZ frequency

a. A frequency in the lower passband


at 58 Hz we can see that the output begins to dim, the output in red and the
input in blue. the attenuation is expected by the type of filter quite good the
response obtained

Figure 5 input frequency at 58 Hz

b. The 60 Hz

in the following image we can clearly see how it attenuates in an expected way
the desired frequency in this case 60 Hz

Figure 6 60 Hz input

c. A frequency in the upper passband.


in the following image we have an input frequency of 62 Hz and we can
appreciate how it begins to let the higher frequencies pass, as expected

Figure 7 62 Hz input

Discussions and conclusions


Write here your observations and conclusions. What do you think about the digital filters?
What are the advantages and disadvantages of these filters? Do you think this knowledge is
useful for you as an electronic engineer?

Advantage:
1. FDs have high noise immunity
2. Accuracy depends on round-off error, which can be handled
3. It is easier and cheaper to change the characteristics of a FD because only the characteristics
are modified.
program variables
4. Variations in temperature and supply voltage do not affect the
stored program structure nor filter performance
5. The cost is progressively lower in computer systems and they increase
permanent processing speeds
Disadvantages:
1. The precision depends on the length of the data word, that is, the one that contains the
filter coefficients.
2. The size of the registers that contain the results of the multiplications can
cause a state of saturation (overflow), so it is necessary to apply rounding
to avoid error propagation.
3. The steeper the filter characteristic, the greater the number of coefficients, which
implies greater processing time, threatening the processes that are desired
perform in real time.
4. If fixed-point microprocessors are used, there will be a need to scale the
coefficients in order to adjust them to the allowed range.

You might also like