Mercurial > hg > beaglert
changeset 89:d41631e0fe0e
Added noise floor measurement project; also added option to run script to run without screen
author | andrewm |
---|---|
date | Sun, 19 Jul 2015 16:15:28 +0100 |
parents | 3a5823f7a11f |
children | c74006ef86ca |
files | projects/measure_noisefloor/main.cpp projects/measure_noisefloor/render.cpp scripts/run_project.sh |
diffstat | 3 files changed, 272 insertions(+), 4 deletions(-) [+] |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/measure_noisefloor/main.cpp Sun Jul 19 16:15:28 2015 +0100 @@ -0,0 +1,104 @@ +/* + * main.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + +#include <iostream> +#include <cstdlib> +#include <libgen.h> +#include <signal.h> +#include <getopt.h> +#include <BeagleRT.h> + +extern int gBufferSize; + +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 << " [options]" << endl; + + BeagleRT_usage(); + + cerr << " --buffer-size [-b] size Set the analysis buffer size\n"; + cerr << " --help [-h]: Print this menu\n"; +} + +int main(int argc, char *argv[]) +{ + BeagleRTInitSettings settings; // Standard audio settings + + struct option customOptions[] = + { + {"help", 0, NULL, 'h'}, + {"buffer-size", 1, NULL, 'b'}, + {NULL, 0, NULL, 0} + }; + + // Set default settings + BeagleRT_defaultSettings(&settings); + + // By default use a longer period size because latency is not an issue + settings.periodSize = 32; + + // Parse command-line arguments + while (1) { + int c; + if ((c = BeagleRT_getopt_long(argc, argv, "hb:", customOptions, &settings)) < 0) + break; + switch (c) { + case 'b': + gBufferSize = atoi(optarg); + break; + case 'h': + usage(basename(argv[0])); + exit(0); + case '?': + default: + usage(basename(argv[0])); + exit(1); + } + } + + if(gBufferSize < settings.periodSize) + gBufferSize = settings.periodSize; + + // Initialise the PRU audio device + if(BeagleRT_initAudio(&settings, 0) != 0) { + cout << "Error: unable to initialise audio" << endl; + return -1; + } + + // Start the audio device running + if(BeagleRT_startAudio()) { + cout << "Error: unable to start real-time audio" << endl; + return -1; + } + + // Set up interrupt handler to catch Control-C and SIGTERM + signal(SIGINT, interrupt_handler); + signal(SIGTERM, interrupt_handler); + + // Run until told to stop + while(!gShouldStop) { + usleep(100000); + } + + // Stop the audio device + BeagleRT_stopAudio(); + + // Clean up any resources allocated for audio + BeagleRT_cleanupAudio(); + + // All done! + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/projects/measure_noisefloor/render.cpp Sun Jul 19 16:15:28 2015 +0100 @@ -0,0 +1,155 @@ +/* + * render.cpp + * + * Created on: Oct 24, 2014 + * Author: parallels + */ + + +#include <BeagleRT.h> +#include <Utilities.h> +#include <cmath> + +int gBufferSize = 8192; + +// Buffers to hold samples for noise analysis +float* gBuffers[10]; +int gBufferPointers[10]; + +// Outputs to display +float gDCLevels[10]; +float gNoiseLevels[10]; +float gNumSamplesAnalysed[10]; + +// Task to print results which would otherwise be too slow for render() +AuxiliaryTask gPrintTask; + +void printResults(); + +// setup() 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 setup(BeagleRTContext *context, void *userData) +{ + // Clear the filter data structures + for(int i = 0; i < 10; i++) { + gBufferPointers[i] = 0; + gBuffers[i] = new float[gBufferSize]; + if(gBuffers[i] == 0) { + rt_printf("Error allocating buffer %d\n", i); + return false; + } + } + + gPrintTask = BeagleRT_createAuxiliaryTask(printResults, 50, "beaglert-print-results"); + + 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(BeagleRTContext *context, void *userData) +{ + bool bufferIsFull = false; // Whether at least one buffer has filled + + for(unsigned int n = 0; n < context->audioFrames; n++) { + // Store audio inputs in buffer + for(unsigned int ch = 0; ch < context->audioChannels; ch++) { + if(gBufferPointers[ch] < gBufferSize) { + gBuffers[ch][gBufferPointers[ch]] = + context->audioIn[n * context->audioChannels + ch]; + gBufferPointers[ch]++; + if(gBufferPointers[ch] >= gBufferSize) + bufferIsFull = true; + } + } + } + + if(context->analogChannels != 0) { + for(unsigned int n = 0; n < context->analogFrames; n++) { + // Store analog inputs in buffer, starting at channel 2 + for(unsigned int ch = 0; ch < context->analogChannels; ch++) { + if(gBufferPointers[ch + 2] < gBufferSize) { + gBuffers[ch + 2][gBufferPointers[ch + 2]] = + context->analogIn[n * context->analogChannels + ch]; + gBufferPointers[ch + 2]++; + if(gBufferPointers[ch + 2] >= gBufferSize) + bufferIsFull = true; + } + } + } + } + + if(bufferIsFull) { + // Analyse all active channels at once + for(int ch = 0; ch < 10; ch++) { + // gBufferPointers[ch] tells us how many samples were stored in the buffer + gNumSamplesAnalysed[ch] = gBufferPointers[ch]; + + if(gBufferPointers[ch] != 0) { + float mean = 0; + for(int n = 0; n < gBufferPointers[ch]; n++) { + mean += gBuffers[ch][n]; + } + mean /= (float)gBufferPointers[ch]; + + float rms = 0; + for(int n = 0; n < gBufferPointers[ch]; n++) { + rms += (gBuffers[ch][n] - mean) * (gBuffers[ch][n] - mean); + } + rms = sqrtf(rms / (float)gBufferPointers[ch]); + + gDCLevels[ch] = mean; + gNoiseLevels[ch] = rms; + } + + // Reset pointer to 0 for next time + gBufferPointers[ch] = 0; + } + + BeagleRT_scheduleAuxiliaryTask(gPrintTask); + } +} + +void printResults() +{ + rt_printf("\e[1;1H\e[2J"); // Command to clear the screen + + // Print the analysis results. channels 0-1 are audio, channels 2-9 are analog + for(int ch = 0; ch < 10; ch++) { + int samples = gNumSamplesAnalysed[ch]; + if(samples == 0) + continue; + + if(ch == 0) + rt_printf("Audio In L: "); + else if(ch == 1) + rt_printf("Audio In R: "); + else + rt_printf("Analog In %d: ", ch - 2); + + rt_printf("Noise %6.1fdB DC offset %6.4f (%6.1fdB) window size: %d\n", + 20.0f * log10f(gNoiseLevels[ch]), + gDCLevels[ch], + 20.0f * log10f(fabsf(gDCLevels[ch])), + samples); + } +} + +// cleanup() is called once at the end, after the audio has stopped. +// Release any resources that were allocated in setup(). + +void cleanup(BeagleRTContext *context, void *userData) +{ + for(int i = 0; i < 10; i++) + delete gBuffers[i]; +}
--- a/scripts/run_project.sh Sun Jul 19 14:15:57 2015 +0100 +++ b/scripts/run_project.sh Sun Jul 19 16:15:28 2015 +0100 @@ -7,24 +7,28 @@ BBB_PATH="~/BeagleRT" COMMAND_ARGS= RUN_IN_FOREGROUND=0 +RUN_WITHOUT_SCREEN=0 function usage { THIS_SCRIPT=`basename "$0"` - echo "Usage: $THIS_SCRIPT [-b path-on-beaglebone] [-c command-line-args] [-f]" + echo "Usage: $THIS_SCRIPT [-b path-on-beaglebone] [-c command-line-args] [-fF]" echo " This script runs a previously compiled BeagleRT project on the BeagleBone Black. The -b option changes the default path, which is otherwise $BBB_PATH. The -c option passes command-line arguments to the BeagleRT program; enclose the argument string in quotes. + The -f argument runs the project in the foreground of the current terminal, - within a screen session that can be detached later." + within a screen session that can be detached later. The -F argument runs + the project in the foreground of the current terminal, without screen, so + the output can be piped to another destination." } OPTIND=1 -while getopts "b:c:fh" opt; do +while getopts "b:c:fFh" opt; do case $opt in b) BBB_PATH=$OPTARG ;; @@ -32,6 +36,8 @@ ;; f) RUN_IN_FOREGROUND=1 ;; + F) RUN_WITHOUT_SCREEN=1 + ;; h|\?) usage exit 1 esac @@ -40,7 +46,10 @@ shift $((OPTIND-1)) echo "Running BeagleRT..." -if [ $RUN_IN_FOREGROUND -eq 0 ] +if [ $RUN_WITHOUT_SCREEN -ne 0 ] +then + ssh -t $BBB_ADDRESS "screen -X -S BeagleRT quit &>/dev/null; pkill BeagleRT ; sleep 0.5 ; $BBB_PATH/BeagleRT $COMMAND_ARGS" +elif [ $RUN_IN_FOREGROUND -eq 0 ] then ssh $BBB_ADDRESS "screen -X -S BeagleRT quit &>/dev/null; pkill BeagleRT ; sleep 0.5 ; screen -S BeagleRT -d -m $BBB_PATH/BeagleRT $COMMAND_ARGS" else