//-------------------------------------------------------------------------------------
// *** TestXcorr.ino ***
//
// Sketch to demonstrate Audio board I2S channel synchronization problem. Computes the
// cross-correlation function between the I2S inputs at lags -2,-1, 0, 1, 2 samples,
// for complete blocks, and prints the lag with maximum correlation, and the values of
// the xcorr estimates for each lag.
// --------------------------
// The cross-correlation function is a statistically optimal measure of
// the delay between two similar signals f(t) and g(t) that differ only in a delay
// (tau) between them. It is a function of the delay, and always has a peak
// value at tau. For a discrete time sampled system, the delay is measured
// in samples. It has a very simple definition:
// psi(M) = sum {f(n) * g(n+M)},
// where the summation is over a finite data block, and where M is the delay.
// psi(M) will be a maximum at the true delay M between the sample sets.
// The value of M
// --------------------------
// Input: Connect both line-inputs (I2S) together, and drive with a common
// wide-band input.
// 1) The sketch generates a "white noise" on I2s line-out channel 0 (left).
// You can connect the two line-in channels to this output, or
// 2) You can use an external wide-band source such as a function generator.
//
// Results: If the input channels are in sync the software will report a lag of 0,
// however I find that
// 1) on program reload it randomly reports either 0, or -1 with 50% probability
// 2) on power-up it reports 0 with about 70% probability, -1 with 30%.
// 3) I have never seen a report of a lag of 1.
//
// Author: Derek Rowell
// Updated: July 1, 2017
//
//-------------------------------------------------------------------------------------
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SerialFlash.h>
#include "AudioXcorr.h"
const int myInput = AUDIO_INPUT_LINEIN;
// ---
AudioInputI2S I2S_in;
AudioOutputI2S I2S_out;
AudioXcorr xcorr_raw_data;
AudioSynthNoiseWhite noise;
AudioControlSGTL5000 audioShield;
//----
AudioConnection c1(I2S_in,0, xcorr_raw_data,0);
AudioConnection c2(I2S_in,1, xcorr_raw_data,1);
AudioConnection c3(noise,0, I2S_out,0);
AudioConnection c4(noise,0, I2S_out,1);
//----------------------------------------------------------------------------
void setup() {
Serial.begin(57600);
delay(2000);
audioShield.enable();
AudioMemory(12);
audioShield.inputSelect(myInput);
noise.amplitude(.3);
}
//----------------------------------------------------------------------------------
void loop() {}