Phase shift by arduinofft is not corrected

I use this code for calculated phase shift in a sine wave with 90 degree phase shift(1.57 rafian)
When it calculate phase shift the response is not corrcted and it show -0.7239


#include "arduinoFFT.h"
arduinoFFT FFT = arduinoFFT(); /* Create FFT object */
const uint16_t samples = 64; //This value MUST ALWAYS be a power of 2
const double signalFrequency = 1.0156;
const double samplingFrequency = 5;
const uint8_t amplitude = 100;
const double pahse_shift=twoPi/4;
double vReal[samples];
double vImag[samples];


void setup()
{
  Serial.begin(115200);
  Serial.println("Ready");
}

void loop()
{
  /* Build raw data */
  double cycles = (((samples-1) * signalFrequency) / samplingFrequency); //Number of signal cycles that the sampling will read
  for (uint16_t i = 0; i < samples; i++)
  {
    vReal[i] = int8_t((amplitude * (sin((i * (twoPi * cycles)) / samples + pahse_shift))));/* Build data with positive and negative values*/
    //vReal[i] = uint8_t((amplitude * (sin((i * (twoPi * cycles)) / samples) + 1.0)) / 2.0);/* Build data displaced on the Y axis to include only positive values*/
    vImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
  }

FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);	/* Weigh data */
FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
PrintVector(vReal, vImag);

  while(1); /* Run Once */
  // delay(2000); /* Repeat after delay */
}

void PrintVector(double *vReal,double * vImag)
{
  for (uint16_t i = 0; i < (samples/2)-1; i++)
  {
    Serial.print(((i * 1.0 * samplingFrequency) / samples), 6);
    Serial.print("Hz");
    Serial.print(" ");
    Serial.print(vReal[i], 4);
    Serial.print("               ");
    Serial.print("Phase Shift:");
    Serial.print(atan2(vImag[i],vReal[i]),4);
    Serial.println();
}
}

Results

Ready
0.000000Hz 3.2861               Phase Shift:0.0000
0.078125Hz 9.1711               Phase Shift:0.1394
0.156250Hz 7.0690               Phase Shift:0.0671
0.234375Hz 5.7918               Phase Shift:0.3446
0.312500Hz 8.6649               Phase Shift:0.1964
0.390625Hz 7.8663               Phase Shift:0.4595
0.468750Hz 7.5131               Phase Shift:0.2865
0.546875Hz 8.3915               Phase Shift:0.5073
0.625000Hz 8.8484               Phase Shift:0.5192
0.703125Hz 7.6524               Phase Shift:0.5496
0.781250Hz 2.5691               Phase Shift:-0.1633
0.859375Hz 52.9961               Phase Shift:-0.7046
0.937500Hz 1016.2028               Phase Shift:0.7093
1.015625Hz 1641.0535               Phase Shift:-0.7239
1.093750Hz 487.8829               Phase Shift:0.7356
1.171875Hz 10.1056               Phase Shift:0.5581
1.250000Hz 6.7123               Phase Shift:-0.7764
1.328125Hz 7.3569               Phase Shift:-0.7847
1.406250Hz 7.1390               Phase Shift:-0.7853
1.484375Hz 6.0180               Phase Shift:-0.7803
1.562500Hz 5.4468               Phase Shift:-0.7771
1.640625Hz 3.6320               Phase Shift:-0.7783
1.718750Hz 5.7395               Phase Shift:-0.7038
1.796875Hz 2.7663               Phase Shift:-0.4674
1.875000Hz 6.0546               Phase Shift:-0.7836
1.953125Hz 6.0958               Phase Shift:-0.4018
2.031250Hz 1.6903               Phase Shift:0.0599
2.109375Hz 4.5332               Phase Shift:-0.4295
2.187500Hz 2.5690               Phase Shift:-0.3970
2.265625Hz 2.8542               Phase Shift:-0.4336
2.343750Hz 1.9680               Phase Shift:-0.5078

Anyone know where is problems?

shouldn't the "pahse_shift" [sic] be in the numerator. And why truncate to integer?

    w      = twoPi * cycles / samples
    ...
        vReal[i] = ((amplitude * (sin( (i * w) + pahse_shift))))

Do this change like your code but phase shift is -0.7242 and is not correct yet

looks like the phase if off across most of the spectrum.

and it's odd that it's close to pi/4

If this is the case, then define it as

const uint16_t samples = 2 << 5;

Instead of splitting your complex numbers into two parallel arrays I suggest that you define a structure, like:

struct Complex
{
  float real;
  float imag;
};

or that you use the Complex library.

To compute the real part, as in vReal[i] = int8_t ..., maybe define a function that will take smaller steps and use intermediate variables.

And lastly: where is twoPi defined? Are you sure you got it right?

twoPi is equal 6.28 and is correct
This problem is not related to this

The problem is the response of atan2 is not correct

Why do you truncate 2π to 2 decimal places when your output has 4 decimal places? Make good use of what a float can store, and precompute it as

const float twoPi = 8 * atan(1);

Are you sure the problem is with the output of atan2() and it's not upstream?

Try to add debugging serial print after each intermediate step. Try to work it out on paper (maybe on a reduced set) with a pocket calculator to see if your formulas are sound.

In upstreams in this code is fft code
I do fft by excel and then but in that is not correct
I dont know where is problem

I change twoPit in this code 8× atan(1)

By define twoPi Not effect in result

This will not affect the result much if the formulas are off; just improve the precision a little bit once everything is working as intended.

What does this mean?

Yeah, me neither. There's too much going on at once. Why don't you try to break up the code in small chunks as I suggested in reply #7 and debug piece by piece?

I think that including this line is a MAJOR part of your problem. I think it calculates the magnitude AND SAVES IT IN 'vReal'.

If I remove that line then I get a phase value that is low by about 20° for every phase value from -179 to +179.

I found the other problem: That "(samples - 1)" caused the 20° phase error. Change it to "samples".

Here is the code I was using to test:

#include <arduinoFFT.h>
arduinoFFT FFT;
const uint16_t samples = 128; //This value MUST ALWAYS be a power of 2
const double signalFrequency = 1.0917;
const double samplingFrequency = 10;
double phase_shift = 0;
double vReal[samples];
double vImag[samples];

void setup()
{
  Serial.begin(115200);
  delay(200);
  Serial.println("Ready");
}

void loop()
{
  Serial.println("Phase,Real,Imaginary,Phase");
  for (int phaseDegrees = -179; phaseDegrees < 180; phaseDegrees += 5)
  {
    phase_shift = phaseDegrees * PI / 180.0;
    DoTheFFT();
    Serial.print(phaseDegrees);
    Serial.print(",\t");
    if (samples == 64)
      ShowSample(7); // 64 samples
    if (samples == 128)
      ShowSample(14); // 128 samples
  }

  while (1) {}
}

void DoTheFFT()
{
  /* Build raw data */
  double cycles = ((samples * signalFrequency) / samplingFrequency); //Number of signal cycles that the sampling will read
  for (uint16_t i = 0; i < samples; i++)
  {
    /* Build data with positive and negative values*/
    vReal[i] = cos((i * (twoPi * cycles)) / samples + phase_shift);
    vImag[i] = 0.0; //Imaginary part must be zeroed in case of looping to avoid wrong calculations and overflows
  }

  FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);  /* Weigh data */
  FFT.Compute(vReal, vImag, samples, FFT_FORWARD); /* Compute FFT */
  // FFT.ComplexToMagnitude(vReal, vImag, samples); /* Compute magnitudes */
  // PrintVector();

}

void PrintVector()
{
  Serial.println("Amplitude,Real,Imaginary,Phase");
  for (uint16_t i = 0; i < (samples / 2) - 1; i++)
  {
    for (int j = 0; j < 1; j++)
    {
      ShowSample(i);
    }
  }
}

void ShowSample(int i)
{
//  Serial.print(((i * samplingFrequency) / samples));
//  Serial.print(",\t");
  float amplitude = sqrt(vReal[i] * vReal[i] + vImag[i] * vImag[i]);
//  Serial.print(amplitude);
  Serial.print(",\t");
  Serial.print(vReal[i]);
  Serial.print(",\t");
  Serial.print(vImag[i]);
  Serial.print(",\t");
  if (amplitude > 1.0)
    Serial.println(atan2(vImag[i], vReal[i]) * 180 / PI);
  else
    Serial.println('0');
}

If vreal get signal from microphone and sample rate set 9600, do it show true phase shift for sine wave for example 1000 Hz?

Sure. For each set of samples, it will get a phase value that is correct for that set of samples. Since the audio sources is not synchronized to the samples, you will probably get a different phase each time.

That s right,each sample has a diffrent phase.
Do location of microphone or angle of sound source to the microphone effect on each phase?
If i want to get diffrence phase between two microphones, could i calculate phase shift in any microphone and then calculatediffrence phase in that time?

Certainly, changing the distance from the sound source to the microphone will change the phase of the sound. I don't think changing an angle will change the phase.

You should be able to calculate the phase difference between two microphones by recording the time difference in microseconds between the two sets of samples.

But My sound is continusely and it is not pulsy (claps) thus recording microsecond for continusly sound is not possible.
I want to calcualte phase shift between two microphones and then calculate diffrence phase shift

Use a cross-correlation function for that. Example:

The time is measured between the first sample on microphone 1 (sample set 1) and the first sample on microphone 2 (sample set 2). Divide that by the frequency to see the phase difference between the two sample sets. I think you then add the three phases: phase at microphone 1 (sample set 1), difference in phase between the two sets of samples, and phase at microphone 2 (sample set 2). That should give you a phase difference between the two microphones.

I need simple code for two microphone by this method, do you have this code please?

The way this forum works is that you write the code and we help fix problems that arise.

Otherwise, post on the "Jobs and Paid Consultancies" forum section.