changeset 4:f34c63568523

_new FFT example [still noisy]
author Victor Zappi <victor.zappi@qmul.ac.uk>
date Thu, 06 Nov 2014 19:02:48 +0000
parents 6810f166482f
children 09f03ac40fcc db025ec86c41
files .cproject projects/audio_in_FFT/main.cpp projects/audio_in_FFT/render.cpp projects/filter_IIR/main.cpp
diffstat 4 files changed, 210 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/.cproject	Thu Nov 06 17:55:05 2014 +0000
+++ b/.cproject	Thu Nov 06 19:02:48 2014 +0000
@@ -84,7 +84,7 @@
 					<sourceEntries>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/>
 						<entry excluding="audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/>
-						<entry excluding="filter_FIR|oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/>
+						<entry excluding="filter_IIR|filter_FIR|oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/>
 					</sourceEntries>
 				</configuration>
 			</storageModule>
@@ -157,7 +157,7 @@
 					<sourceEntries>
 						<entry flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="include"/>
 						<entry excluding="audio_routines_old.S" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="core"/>
-						<entry excluding="filter_FIR|oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/>
+						<entry excluding="filter_IIR|filter_FIR|oscillator_bank|samples|basic_sensor|d-box|basic_analog_output|basic" flags="VALUE_WORKSPACE_PATH|RESOLVED" kind="sourcePath" name="projects"/>
 					</sourceEntries>
 				</configuration>
 			</storageModule>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/projects/audio_in_FFT/main.cpp	Thu Nov 06 19:02:48 2014 +0000
@@ -0,0 +1,114 @@
+/*
+ * main.cpp
+ *
+ *  Created on: Oct 24, 2014
+ *      Author: parallels
+ */
+
+#include <iostream>
+#include <cstdlib>
+#include <libgen.h>
+#include <signal.h>
+#include "../../include/RTAudio.h"
+
+using namespace std;
+
+// Handle Ctrl-C by requesting that the audio rendering stop
+void interrupt_handler(int var)
+{
+	gShouldStop = true;
+}
+
+// Print usage information
+void usage(const char * processName)
+{
+	cerr << "Usage: " << processName << " [-h] [-v] [-p period] [-f frequency]" << endl;
+	cerr << "   -h:           Print this menu\n";
+	cerr << "   -v:           Enable verbose messages\n";
+	cerr << "   -p period:    Set the period (hardware buffer) size in sensor frames\n";
+	cerr << "   -s fftsize:   Set the fSize of the FFT, in samples\n";
+	cerr << "   -m:           Enable the matrix (ADC and DAC) as well as audio\n";
+}
+
+int main(int argc, char *argv[])
+{
+	int periodSize = 8;			// Period size in sensor frames
+	int verbose = 0;			// Verbose printing level
+	int fftSize = 64;		    // Size of the FFT, in samples
+	int useMatrix = 0;			// Whether to use the matrix or just audio
+
+	// Parse command-line arguments
+	while (1) {
+		int c;
+		if ((c = getopt(argc, argv, "hp:vf:m")) < 0)
+				break;
+		switch (c) {
+		case 'h':
+				usage(basename(argv[0]));
+				exit(0);
+		case 'p':
+				periodSize = atoi(optarg);
+				if(periodSize < 1)
+					periodSize = 1;
+				break;
+		case 'v':
+				verbose = 1;
+				break;
+		case 's':
+				fftSize = atof(optarg);
+				break;
+		case 'm':
+				useMatrix = 1;
+				break;
+		case '?':
+		default:
+				usage(basename(argv[0]));
+				exit(1);
+		}
+	}
+
+
+	// Set verbose logging information (optional by using value > 0; default is 0)
+	setVerboseLevel(verbose);
+
+	if(verbose) {
+		cout << "Starting with period size " << periodSize << " and FFT size " << fftSize << endl;
+		if(useMatrix)
+			cout << "Matrix enabled\n";
+		else
+			cout << "Matrix disabled\n";
+	}
+
+	// Initialise the PRU audio device
+	if(initAudio(periodSize, useMatrix, &fftSize) != 0) {
+		cout << "Error: unable to initialise audio" << endl;
+		return -1;
+	}
+
+	// Start the audio device running
+	if(startAudio()) {
+		cout << "Error: unable to start real-time audio" << endl;
+		return -1;
+	}
+
+	// Set up interrupt handler to catch Control-C
+	signal(SIGINT, interrupt_handler);
+
+	// Run until told to stop
+	while(!gShouldStop) {
+		usleep(100000);
+	}
+
+	// Stop the audio device
+	stopAudio();
+
+	if(verbose) {
+		cout << "Cleaning up..." << endl;
+	}
+
+	// Clean up any resources allocated for audio
+	cleanupAudio();
+
+	// All done!
+	return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/projects/audio_in_FFT/render.cpp	Thu Nov 06 19:02:48 2014 +0000
@@ -0,0 +1,93 @@
+/*
+ * render.cpp
+ *
+ *  Created on: Oct 24, 2014
+ *      Author: parallels
+ */
+
+
+#include "../../include/render.h"
+#include <rtdk.h>
+#include <NE10.h>					// neon library
+#include <cmath>
+
+int gFftSize;
+int gNumChannels;
+
+int gReadPointer = 0;
+int gWritePointer = 0;
+
+// FFT vars
+ne10_fft_cpx_float32_t* timeDomain;
+ne10_fft_cpx_float32_t* frequencyDomain;
+ne10_fft_cfg_float32_t cfg;
+
+// initialise_render() is called once before the audio rendering starts.
+// Use it to perform any initialisation and allocation which is dependent
+// on the period size or sample rate.
+//
+// userData holds an opaque pointer to a data structure that was passed
+// in from the call to initAudio().
+//
+// Return true on success; returning false halts the program.
+
+bool initialise_render(int numChannels, int numMatrixFramesPerPeriod,
+					   int numAudioFramesPerPeriod, float matrixSampleRate,
+					   float audioSampleRate, void *userData)
+{
+	// Retrieve a parameter passed in from the initAudio() call
+	gFftSize = *(int *)userData;
+	gNumChannels = numChannels;
+
+	//memset(outSamples, gFftSize, 0.0); // set all to 0
+
+	timeDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
+	frequencyDomain = (ne10_fft_cpx_float32_t*) NE10_MALLOC (gFftSize * sizeof (ne10_fft_cpx_float32_t));
+	cfg = ne10_fft_alloc_c2c_float32 (gFftSize);
+
+	return true;
+}
+
+// render() is called regularly at the highest priority by the audio engine.
+// Input and output are given from the audio hardware and the other
+// ADCs and DACs (if available). If only audio is available, numMatrixFrames
+// will be 0.
+
+void render(int numMatrixFrames, int numAudioFrames, float *audioIn, float *audioOut,
+			uint16_t *matrixIn, uint16_t *matrixOut)
+{
+	for(int n = 0; n < numAudioFrames; n++) {
+		timeDomain[gReadPointer].r = (ne10_float32_t) ((audioIn[n*gNumChannels] + audioIn[n*gNumChannels+1])/2);
+		timeDomain[gReadPointer].i = 0;
+		gReadPointer++;
+	}
+
+	if(gReadPointer>=gFftSize)
+	{
+		//FFT
+		ne10_fft_c2c_1d_float32_neon (frequencyDomain, timeDomain, cfg->twiddles, cfg->factors, gFftSize, 0);
+
+		//Do frequency domain stuff
+
+		//IFFT
+		ne10_fft_c2c_1d_float32_neon (timeDomain, frequencyDomain, cfg->twiddles, cfg->factors, gFftSize, 1);
+
+		gReadPointer = 0;
+		gWritePointer = 0;
+	}
+
+	for(int n = 0; n < numAudioFrames; n++) {
+		for(int channel = 0; channel < gNumChannels; channel++)
+			audioOut[n * gNumChannels + channel] = (float) timeDomain[gWritePointer++].r/gFftSize;
+	}
+}
+
+// cleanup_render() is called once at the end, after the audio has stopped.
+// Release any resources that were allocated in initialise_render().
+
+void cleanup_render()
+{
+	NE10_FREE(timeDomain);
+	NE10_FREE(frequencyDomain);
+	NE10_FREE(cfg);
+}
--- a/projects/filter_IIR/main.cpp	Thu Nov 06 17:55:05 2014 +0000
+++ b/projects/filter_IIR/main.cpp	Thu Nov 06 19:02:48 2014 +0000
@@ -142,7 +142,7 @@
 	setVerboseLevel(verbose);
 
 	if(verbose) {
-		cout << "Starting with period size " << periodSize << endl;
+		cout << "Starting with period size " << periodSize << " and cut-off frequency" << gCutFreq << endl;
 		if(useMatrix)
 			cout << "Matrix enabled\n";
 		else