Arduino
#include <Arduino.h>
#define SAMPLES 128
#define SAMPLING_RATE 1408 /* in Hz */
#define SAMPLING_TIME_US round(1000000.0/SAMPLING_RATE) /* in microseconds */
void sampleInQ(int16_t*, int16_t*);
int16_t getMeanValue(int16_t*, int16_t);
void printBuffer(int16_t*, int16_t);
int16_t bufferI[SAMPLES] = {0};
int16_t bufferQ[SAMPLES] = {0};
void setup()
{
Serial.begin(38400);
Serial.println("Init done!");
pinMode(BGT_ON, OUTPUT);
digitalWrite(BGT_ON, LOW); // Turn on the board
}
void loop()
{
sampleInQ(bufferI, bufferQ);
int16_t meanI = getMeanValue(bufferI, SAMPLES);
int16_t meanQ = getMeanValue(bufferQ, SAMPLES);
printBuffer(bufferI, SAMPLES);
Serial.print("Mean I: ");
Serial.println(meanI);
printBuffer(bufferQ, SAMPLES);
Serial.print("Mean Q: ");
Serial.println(meanI);
delay(5000);
}
void sampleInQ(int16_t* buffI, int16_t* buffQ)
{
for (int i = 0; i < SAMPLES; i++)
{
buffI[i] = analogRead(CH_I);
buffQ[i] = analogRead(CH_Q);
delayMicroseconds(SAMPLING_TIME_US);
}
}
int16_t getMeanValue(int16_t* buff, int16_t length)
{
int16_t meanRet = 0;
int sum = 0;
for (int i = 0; i < length; i++)
{
sum += buff[i];
}
meanRet = round((float)sum/(float)length);
return meanRet;
}
void printBuffer(int16_t* buff, int16_t length)
{
for (int i = 0; i < SAMPLES; i++)
{
Serial.print(buff[i]);
Serial.println(";");
}
}