Phasor Estiamation with DFT algoridhm

Sir i am traying to implement DFT algoridhm for calculation of phasor magnitude and angle, but it is not working. I have generated arrey of sine wave in code itself so without analog input we can check that it is working or not. Please help

a.ino (1.36 KB)

erkngajjar:
Sir i am traying to implement DFT algoridhm for calculation of phasor magnitude and angle, but it is not working. I have generated arrey of sine wave in code itself so without analog input we can check that it is working or not. Please help

This is not an Apple site...
"Sir i am traying to..."

You are not correctly calculating the entire DFT of the input, just one term.

Since the number of points is a power of two, just use the FFT. ArduinoFFT - Open Music Labs Wiki

Your a[] waveform and your sin/cos frequency are different, so you're not going to see
anything meaningful, especially as you're not using a sample window function like Blackman or Hanning.

You can only resolve at the same frequency as the signal.

And this is not really a DFT, the transform converts time-domain to frequency-domain
keeping all the information, here you are picking out one complex frequency component.

Sir here the frequency of a[] waveform if 50hz and the frequency of sin/cos terms are also changed to 50 but nothing is happening again.

Also i have tried FFT library but it only gives magnitude with frequency, but my requirement is to take only 50hz signal and calculate its magnitude and phase angle. I want to measure phase angle between voltage and current vector later which is of 50hz.

Also i have prepared a code for taking analog samples withe 3.2kHz sampling frequency so every cycle of sine wave can have 64 samples. Both the codes are attached here with.

Thank you Mr. MarkT, Mr. Jremington and Mr. GarryP for replaying. I am also thankful to all who have tried to help.

3.2khz_sampler.ino (655 Bytes)

a.ino (1.72 KB)

Please post your code in code tags so we can see it easily.

Here is first code which is for phasor estimation..

int i=0;
int N=64;
float a[64];
float t[64];
float   p_m_1[64];
float   p_a_1[64];
float Xr_1 = 0;
float Xi_1 = 0;
float Xr_1_1 = 0; 
float Xi_1_1 = 0;
float Phasor_Magnitude_1 = 0;
float Phasor_Angle_1 = 0;
float Phasor_Angle_Degree_1= 0;


void setup() {
  
Serial.begin(9600);

}

void loop() {

  
   for (i=0; i<N; i++){
    
       t[i] = i * 0.0003125;
       a[i] = 325 * sin(314.14 * t[i]);
       Serial.print("Sine wave freq: ");
       Serial.print(a[i]);
       Serial.print("    Sample no: ");
       Serial.println(i);
       
       //delay(1);
       
     }
i=0;
      

    for (i = 0; i<N; i++){
      p_m_1[i] = a[i] * cos ((2*PI * i*50)/ N);
      //delay(2);
      p_a_1[i] =- a[i] * sin ((2*PI * i*50)/ N);

      
      //delay(2);
      Serial.print("Sample no:  ");
      Serial.print(i);
      Serial.print("   Real:   ");
      Serial.print(p_m_1[i]);
      Serial.print("   Imaginary    ");
      Serial.println(p_a_1[i]);
      Xr_1 = Xr_1 + p_m_1[i];
      Xi_1 = Xr_1 + p_a_1[i];
      
     }

     Xr_1_1 = (sqrt(2)/N) * Xr_1;
     Xi_1_1 = -(sqrt(2)/N) * Xi_1;
     Serial.println("");
     Serial.print("Real value is: ");
     Serial.print(Xr_1_1);
     Serial.print("    Imaginary value is");
     Serial.println(Xi_1_1);

     Phasor_Magnitude_1 = sqrt((Xr_1_1 * Xr_1_1) + (Xi_1_1 * Xi_1_1));
     Phasor_Angle_1 = atan2 (Xi_1_1,Xr_1_1);
     Serial.print(Phasor_Magnitude_1);
     Serial.print("     ");
     Serial.println( Phasor_Angle_1);

     Phasor_Angle_Degree_1 =  Phasor_Angle_1 * 57.32;
     Serial.println(Phasor_Angle_Degree_1);

     Xr_1_1=0;
     Xi_1_1=0;
     Phasor_Magnitude_1=0;
     Phasor_Angle_1=0;
     Phasor_Angle_Degree_1=0;
     Xr_1=0;
     Xi_1=0;   

     
    
  
  

   while(1){}
  
}

Here is second code which is for taking analog samples at 3.2khz sampling frequency

#include <TimerOne.h>

int i;
unsigned long ts;
unsigned long t;
int r[64];
boolean sampling_done;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  i = 0;
Timer1.initialize(312);
Timer1.attachInterrupt(aquire);
ts = micros();
}

void loop() {
  
if (i == 63){
     t = micros() - ts;
     noInterrupts();
     Serial.println("AB");
     Serial.print("Time is");
     Serial.println((float)t/(float)1000000);
     sampling_done = true;

    if (sampling_done = true){
      i = 0;
      interrupts();
      ts = micros();
    }
  }
}


void aquire(){
  r[i] = analogRead(A0);
  r[i] = analogRead(A1);
  i += 1;
  
}

Your phasor code is populating a with 325 sin (0.09817 i),
then correlating it with sine and cosine of (100π/64) i
100π/64 is very different from 0.9817
100π/64 is also over the Nyquist limit.