MicroLS (Part 2)

Part 123

All fun of the project lies in the code! Its construction comes almost trivial and will look like:

Set acquisition parameter
Endlessly:
Acquired data
Adjust data
Execute FFT
Compute frequency spectrum
Extract information from spectrum and switch leds on and off accordingly

Here is a look and feel about the header of the sketch

#include 
#include 
PlainADC PADC; /* Create ADC object */
PlainFFT FFT; /* Create FFT object */
/* Data acquisition parameters */
const uint16_t samples = 64;
const double samplingFrequency = 16.0E+3;
const uint16_t adcChannel = 0; /* From 0 to 5 on ATmega328 powered Arduinos */
const uint16_t refVoltage = ADC_REF_VOL_DEFAULT; /* VCC: 5V */
/* Data acquisition vector */
uint8_t *vBuffer;
/* FFT vectors */
double *vReal;
double *vImag;

Initializing acquisition parameters will simply require

	/* Set data acquisition parameters */
	vBuffer = PADC.SetAcquisitionEngine(adcChannel, refVoltage,  samplingFrequency, samples, ADC_DAT_FMT_DBL);	
	/* Size data vectors */
	vReal = (double*)malloc(samples * sizeof(double));
	vImag = (double*)malloc(samples * sizeof(double));

And some additional code for setting led ports.

Then the loop section of the skecth will contain:

	/* Acquire data */
	PADC.GetScanData();
	/* Cast data in a doubles vector */
	vReal = reinterpret_cast(vBuffer);
	/* Nulls offset and clear imaginary vector */
	double sum = 0.0;
	for (uint16_t i = 0; i < samples; i++) {
		sum += vReal[i];
		vImag[i] = 0.0;
	}	
	double average = (sum / samples);
	for (uint16_t i = 0; i < samples; i++) {
		vReal[i] -= average;
	}	
	/* Window data: optional */
	// FFT.Windowing(vReal, samples, FFT_WIN_TYP_HAMMING, FFT_FORWARD);	
	/* Compute FFT */
	FFT.Compute(vReal, vImag, samples, FFT_FORWARD);	
	/* Compute magnitudes */
	FFT.ComplexToReal(vReal, vImag, samples, FFT_AMPLITUDE);
	/* Normalize data */
	FFT.Normalize(vReal, (samples >> 1), 100.0);

… followed by some additional code which will turn leds on and off according to our taste!

Next post on same subject

One Comment

  1. Matt says:

    Which libraries did you call? I see the #include but no file. without the lib the functions are all coming up undefined. It also doesn’t like the PADC and FFT creation right below. Any help would be greatly appreciated

Leave a Reply

You must be logged in to post a comment.