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();
}
}
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.
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?
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
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.