Here are some insights about the phase calculation:
Teensy Audio System Design
A sine wave is send to
audioOutI2S1 to get the sine wave signal source.
audioInI2S1 provides the I2S data from the CS4272 codec. Channel 1 is the measured voltage and channel 2 is the measured current.
Additional, there are two AudioSynthWaveform objects that create a unity magnitude square wave with 0° and 90° phase. Both are multiplied by the voltage and current signals respectively.
The phase is calculated by
phase = atan(mean2.read() / mean1.read()) - atan(mean4.read() / mean3.read());
// cap phase to +-90°
if (-phase > PI / 2) {
phase = phase + PI;
}
if (phase > PI / 2) {
phase = phase - PI;
}
The impedance is calulated by
impedance = analyzeRmsV.read() / analyzeRmsI.read();
The code lines are simplified. The actual code is a bit more complex due to analog level calibration and moving average calculation.
Basically the same math explained in the TIDA-060029.