A Recursive Least Square harmonic estimation algorithm is implemented in Arduino Due microcontroller. A Laboratory setup to generate a test signal, i.e. a modified sine wave inverter was used to generate a 24 volt AC signal, which is sensed by the Microcontroller through a signal conditioning circuit and then the microcontroller finds out the harmonic components and communicates them to the computer which are plotted in MATLAB.

To make the AC Voltage Signal Get Accepted by the ADC of the Microcontroller we need the following circuits.

Voltage Supply Circuit (230 Volt AC to 5 Volt DC):

1. 230 Volt to 6 Volt Step-Down Transformer: It steps down the voltage to 6 Volt to be used by our Power Supply Circuit for the Microcontroller as well as the Voltage Offset Circuit.
2. Diode Bridge Rectifier: A bridge Rectifier made of Four Diodes rectify the 6 Volt AC of the Transformer secondary, i.e. Only Positive Parts of the signals i.e. DC.
3. Smoothing Capacitor: A 1000uF 25 Volt Smoothing capacitor smooths out the Voltage coming from the Bridge Rectifier.
4. CD7805 Voltage Regulator: A Voltage regulator eliminates any ripple present in the Voltage supply after the capacitor so that a maximum allowable ripple of .005 Volt is present, i.e. the Output is close to Pure DC suitable for the sensitive Microcontroller.

Measurement Signal Pre-Processing Circuit:

1. The 9 Volt Transformer: Since we will be measuring 230 Volt AC it has to be stepped down to a suitable voltage range to be operated on and also the Transformer Provides Sufficient Isolation necessary to keep ourselves as well as the sophisticated electronics from harm’s way.
2. Voltage Divider (1/10): The output of the Transformer, when unloaded can give up to 14-17 volt Peak. Hence it has to be further reduced, for this purpose we have used a Voltage divider which divides it 10 times which is the ratio of the resistances 10K and 100K.
3. Voltage Offset Adding Circuit: The voltage after the voltage divider has both a positive and negative peak, which is not suitable for our ADC. Therefore, we add an OFFSET using the Rail to Rail swing OPAMP LM358. The OPAMP functions as a voltage follower, and buffers the mid-point voltage that appears at the junction of R3 & R4. This significantly reduces the impedance of the voltage source, resulting in enhanced performance.
4. The Final Voltage Divider: Since the original circuit gives out a signal in the range of 5 Volt, but since we have used a microcontroller, which has an ADC capable of only measuring a signal in the range of 3.3 Volt, a final Voltage divider has been used to keep the voltage in range suitable for the ADC.

The Experimental Setup looks Like the Following:

The Output waveform of the PWM inverter whose Harmonics are to be estimated

Estimated Amplitudes Of The Harmonics

Estimated Phases Of The Harmonics

The MATLAB Code for SImulation:

`Matlab M Code:clear all;clc;%ticdt=.001;f=50;w=2*pi*f;ph1=4*pi/9;ph3=pi/3;ph5=pi/4;ph7=pi/5;ph11=pi/6;adc=0.5;alpha=5;a1=1.5;a3=0.5;a5=0.2;a7=0.15;a11=0.1;    for i=1:100    t(i)=dt*i;    y(i)=a1*sin(w*dt*i+ph1)+a3*sin(3*w*dt*i+ph3)+a5*sin(5*w*dt*i+ph5)+a7*sin(7*w*dt*i+ph7)+a11*sin(11*w*dt*i+ph11)+adc*exp(-5*dt*i)+.01*randn(1,1);     h11(i)=[sin(w*dt*i)];    h12(i)=[cos(w*dt*i)];    h21(i)=[sin(3*w*dt*i)];    h22(i)=[cos(3*w*dt*i)];    h31(i)=[sin(5*w*dt*i)];    h32(i)=[cos(5*w*dt*i)];    h41(i)=[sin(7*w*dt*i)];    h42(i)=[cos(7*w*dt*i)];    h51(i)=[sin(11*w*dt*i)];    h52(i)=[cos(11*w*dt*i)];    h61(i)=i;    h62(i)=-dt*i;        endR=[h11 ;h12 ; h21 ;h22; h31; h32; h41; h42; h51; h52; h61; h62]';P=diag(100:111); for k=1:length(y)    sitrans(k,:)=R(k,:);% dimenson of 100*6    si=sitrans';% dimenson of 6*100endth=[0;0;0;0;0;0;0;0;0;0;0;0]; for m=1:length(y)     P=P-[(P*si(:,m)*sitrans(m,:)*P)/(1+sitrans(m,:)*P*si(:,m))];     %P=P-[(P*R'*R*P)/(1+R'*R*P)];     k=P*si(:,m);     %e(m)=y(m)-sitrans(m,:)*th;     e(m)=y(m)-R(m,:)*th;     th=th+k*e(m);      yo1(m)=[h11(m) h12(m)]*[th(1,1); th(2,1)];     yo3(m)=[h21(m) h22(m)]*[th(3,1); th(4,1)];     yo5(m)=[h31(m) h32(m)]*[th(5,1); th(6,1)];     yo7(m)=[h41(m) h42(m)]*[th(7,1); th(8,1)];     yo11(m)=[h51(m) h52(m)]*[th(9,1); th(10,1)];     yo(m)=sitrans(m,:)*th;      %%rmse(m)=sqrt(0.5*(e(m)).^2);     %%mse(m)=(0.01*(e(m).^2));      amp1(m)=sqrt((th(1,1))^2+(th(2,1))^2);     amp3(m)=sqrt((th(3,1))^2+(th(4,1))^2);     amp5(m)=sqrt((th(5,1))^2+(th(6,1))^2);     amp7(m)=sqrt((th(7,1))^2+(th(8,1))^2);     amp11(m)=sqrt((th(9,1))^2+(th(10,1))^2);      phased1(m)=atand(th(2,1)/(th(1,1)));     phased3(m)=atand(th(4,1)/(th(3,1)));     phased5(m)=atand(th(6,1)/(th(5,1)));     phased7(m)=atand(th(8,1)/(th(7,1)));     phased11(m)=atand(th(10,1)/(th(9,1))); end    % toc `

The Arduino Microcontroller Code for Realtime Estimation

`Arduino Code:#include "Eigen312.h"     // Calls main Eigen3.1.2 matrix class library#include "LU"             // Calls inverse, determinant, LU decomp., etc.using namespace Eigen;    // Eigen related statement; simplifies syntax for declaration of matrices void print_mtxf(const Eigen::MatrixXf& K); float dt = .001;int f = 50;float pi = 3.143;float w = 2 * pi * f;float ph1 = 4 * pi / 9, ph3 = pi / 3, ph5 = pi / 4, ph7 = pi / 5, ph11 = pi / 6;float adc = 0.5;int alpha = 5;float a1 = 1.5, a3 = 0.5, a5 = 0.2, a7 = 0.15, a11 = 0.1;int i;int j;int m;MatrixXf y(1, 100);  // Note: without "using namespace Eigen", declaration would be: Eigen::MatrixXf H(6,6);//MatrixXf volt(1,100);MatrixXf actual_data(1, 100);void setup() {   Serial.begin(115200);   // DECLARE MATRICES  //--------------------  MatrixXf t(1, 100); // Produces 6x6 float matrix class  //MatrixXf y(1,100);   // Note: without "using namespace Eigen", declaration would be: Eigen::MatrixXf H(6,6);   MatrixXf h11(1, 100);  MatrixXf h12(1, 100);  MatrixXf h21(1, 100);  MatrixXf h22(1, 100);  MatrixXf h31(1, 100);  MatrixXf h32(1, 100);  MatrixXf h41(1, 100);  MatrixXf h42(1, 100);  MatrixXf h51(1, 100);  MatrixXf h52(1, 100);   MatrixXf h61(1, 100);  MatrixXf h62(1, 100);   MatrixXf  Rt(12, 100);  MatrixXf R(100, 12);   MatrixXf P(12, 12);   MatrixXf sitrans(100, 12);  MatrixXf si(12, 100);   MatrixXf th(12, 1);  MatrixXf k(12, 1);  MatrixXf e(1, 100);   MatrixXf yo1(1, 100);  MatrixXf yo3(1, 100);  MatrixXf yo5(1, 100);  MatrixXf yo7(1, 100);  MatrixXf yo11(1, 100);   MatrixXf yo(1, 100);   MatrixXf amp1(1, 100);  MatrixXf amp3(1, 100);  MatrixXf amp5(1, 100);  MatrixXf amp7(1, 100);  MatrixXf amp11(1, 100);   MatrixXf phased1(1, 100);  MatrixXf phased3(1, 100);  MatrixXf phased5(1, 100);  MatrixXf phased7(1, 100);  MatrixXf phased11(1, 100);   // Print Result  //----------------------------  //print_mtxf(t);      // Print Matrix Result (passed by reference)  get_voltage_samples();   for (int x = 1; i < 101; i++)  {    t(0, i - 1) = dt * i;     //y(0, i - 1) = a1 * sin(w * dt * i + ph1) + a3 * sin(3 * w * dt * i + ph3) + a5 * sin(5 * w * dt * i + ph5) + a7 * sin(7 * w * dt * i + ph7) + a11 * sin(11 * w * dt * i + ph11) + adc * exp(-5 * dt * i) + .01 * random(-1, 1) * i;       h11(0, i - 1) = sin(w * dt * i);    h12(0, i - 1) = cos(w * dt * i);    h21(0, i - 1) = sin(3 * w * dt * i);    h22(0, i - 1) = cos(3 * w * dt * i);    h31(0, i - 1) = sin(5 * w * dt * i);    h32(0, i - 1) = cos(5 * w * dt * i);    h41(0, i - 1) = sin(7 * w * dt * i);    h42(0, i - 1) = cos(7 * w * dt * i);    h51(0, i - 1) = sin(11 * w * dt * i);    h52(0, i - 1) = cos(11 * w * dt * i);     h61(0, i - 1) = i;    h62(0, i - 1) = -dt * i;   }   Serial.println("t Matrix is");  print_mtxf(t);   Serial.println("Actual Data Read from ADC is");  print_mtxf(actual_data);   //Serial.println("Mapped Voltage Matrix is");  //print_mtxf(volt);   Serial.println("y Matrix is");  print_mtxf(y);   Rt.row(0) = h11;  Rt.row(1) = h12;  Rt.row(2) = h21;  Rt.row(3) = h22;  Rt.row(4) = h31;  Rt.row(5) = h32;  Rt.row(6) = h41;  Rt.row(7) = h42;  Rt.row(8) = h51;  Rt.row(9) = h52;  Rt.row(10) = h61;  Rt.row(11) = h62;    Serial.println("Rt Matrix is");  print_mtxf(Rt);  R = Rt.transpose();  Serial.println("R Matrix is");  print_mtxf(R);   P.setZero();  for (i = 0; i < 12; i++) {    for (j = 0; j < 12; j++) {      if (i == j) {        P(i, j) = 100 + i;      }    }  }  Serial.println("P matrix");  print_mtxf(P);   sitrans = R;  si = sitrans.transpose();   Serial.println("si matrix is");  print_mtxf(si);   th.setZero();   for (m = 0; m < 100; m++) {    //P=P-[(P*si(:,m)*sitrans(m,:)*P)/(1+sitrans(m,:)*P*si(:,m))];    //      temp1                        temp2    P = P - ((P * si.col(m) * sitrans.row(m) * P) / (1 + sitrans.row(m) * P * si.col(m)));    k = P * si.col(m);    //e(m)=y(m)-R(m,:)*th;    e(0, m) = y(0, m) - (R.row(m) * th)(0, 0);    //    temp1=R.row(m)*th;    //    e(0,m)= y(0,m) - temp1(0,0);    th = th + k * e(0, m);     //yo1(m)=[h11(m) h12(m)]*[th(1,1); th(2,1)];    MatrixXf temp1(1, 2);    MatrixXf temp2(2, 1);     temp1(0, 0) = h11(0, m);    temp1(0, 1) = h12(0, m);     temp2(0, 0) = th(0, 0);    temp2(1, 0) = th(1, 0);     yo1(0, m) = (temp1 * temp2)(0, 0);     //yo3(m)=[h21(m) h22(m)]*[th(3,1); th(4,1)];    temp1(0, 0) = h21(0, m);    temp1(0, 1) = h22(0, m);     temp2(0, 0) = th(2, 0);    temp2(1, 0) = th(3, 0);     yo3(0, m) = (temp1 * temp2)(0, 0);     //yo5(m)=[h31(m) h32(m)]*[th(5,1); th(6,1)];    temp1(0, 0) = h31(0, m);    temp1(0, 1) = h32(0, m);     temp2(0, 0) = th(4, 0);    temp2(1, 0) = th(5, 0);     yo5(0, m) = (temp1 * temp2)(0, 0);     //yo7(m)=[h41(m) h42(m)]*[th(7,1); th(8,1)];    temp1(0, 0) = h41(0, m);    temp1(0, 1) = h42(0, m);     temp2(0, 0) = th(6, 0);    temp2(1, 0) = th(7, 0);     yo7(0, m) = (temp1 * temp2)(0, 0);     //yo11(m)=[h51(m) h52(m)]*[th(9,1); th(10,1)];    temp1(0, 0) = h51(0, m);    temp1(0, 1) = h52(0, m);     temp2(0, 0) = th(8, 0);    temp2(1, 0) = th(9, 0);     yo11(0, m) = (temp1 * temp2)(0, 0);      yo(0, m) = (sitrans.row(m) * th)(0, 0);     //amp1(m)=sqrt((th(1,1))^2+(th(2,1))^2);    //amp1(0,m)=(th(0,0).square()+th(1,0).square()).sqrt();    amp1(0, m) = sqrt(th(0, 0) * th(0, 0) + th(1, 0) * th(1, 0));    amp3(0, m) = sqrt(th(2, 0) * th(2, 0) + th(3, 0) * th(3, 0));    amp5(0, m) = sqrt(th(4, 0) * th(4, 0) + th(5, 0) * th(5, 0));    amp7(0, m) = sqrt(th(6, 0) * th(6, 0) + th(7, 0) * th(7, 0));    amp11(0, m) = sqrt(th(8, 0) * th(8, 0) + th(9, 0) * th(9, 0));     //phased1(m)=atand(th(2,1)/(th(1,1)));    phased1(0, m) = atan(th(1, 0) / (th(0, 0)));    phased3(0, m) = atan(th(3, 0) / (th(2, 0)));    phased5(0, m) = atan(th(5, 0) / (th(4, 0)));    phased7(0, m) = atan(th(7, 0) / (th(6, 0)));    phased11(0, m) = atan(th(9, 0) / (th(8, 0)));   }  //Convert raian phase matrices into degree  phased1 = phased1 * (180 / pi);  phased3 = phased3 * (180 / pi);  phased5 = phased5 * (180 / pi);  phased7 = phased7 * (180 / pi);  phased11 = phased11 * (180 / pi);   Serial.println("yo1 Matrix is");  print_mtxf(yo1);  Serial.println("yo3 Matrix is");  print_mtxf(yo3);  Serial.println("yo5 Matrix is");  print_mtxf(yo5);  Serial.println("yo7 Matrix is");  print_mtxf(yo7);  Serial.println("yo11 Matrix is");  print_mtxf(yo11);   Serial.println("amp1 Matrix is");  print_mtxf(amp1);  Serial.println("amp3 Matrix is");  print_mtxf(amp3);  Serial.println("amp5 Matrix is");  print_mtxf(amp5);  Serial.println("amp7 Matrix is");  print_mtxf(amp7);  Serial.println("amp11 Matrix is");  print_mtxf(amp11);    Serial.println("phased1 Matrix is");  print_mtxf(phased1);  Serial.println("phased3 Matrix is");  print_mtxf(phased3);  Serial.println("phased5 Matrix is");  print_mtxf(phased5);  Serial.println("phased7 Matrix is");  print_mtxf(phased7);  Serial.println("phased11 Matrix is");  print_mtxf(phased11);}  void loop() {  // put your main code here, to run repeatedly: } void get_voltage_samples(){  for (int i = 1; i < 101; i++)  {    float sample = analogRead(A0);    actual_data(0, i - 1) = sample;    float sample_mapped_230 = map(sample, 108, 426, -320.00, 320.00);    //volt(0, i - 1)=sample_mapped_230;    //float sample_mapped_ex=map_double(sample,108,426,-2.6000,2.6000);    y(0, i - 1) = sample_mapped_230;    delay(1);     //delayMicroseconds(100);  }} float map_double(double x, double in_min, double in_max, double out_min, double out_max){  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;}  // PRINT MATRIX (float type)//-----------------------------void print_mtxf(const Eigen::MatrixXf& X){  int i, j, nrow, ncol;   nrow = X.rows();  ncol = X.cols();   Serial.print("nrow: "); Serial.println(nrow);  Serial.print("ncol: "); Serial.println(ncol);  Serial.println();   for (i = 0; i < nrow; i++)  {    for (j = 0; j < ncol; j++)    {      Serial.print(X(i, j), 6);  // print 6 decimal places      Serial.print(", ");    }    Serial.println();  }  Serial.println();}`