From 7b25d27d064b561cd8f3b72e25f61102241991f8 Mon Sep 17 00:00:00 2001 From: Maximilien Cuony Date: Thu, 10 May 2018 23:38:09 +0200 Subject: Base with a working implementation on the simulator for software 1750 detection --- src/common/includes/Audio/audio.h | 2 +- src/common/includes/Audio/audio_in.h | 40 +++++++++++++++ src/common/includes/Audio/tone.h | 52 +++++++++++++++++++ src/common/sourcelist.txt | 2 + src/common/src/Audio/audio_in.c | 46 +++++++++++++++++ src/common/src/Audio/tone.c | 97 ++++++++++++++++++++++++++++++++++++ src/common/src/Core/main.c | 9 +++- 7 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 src/common/includes/Audio/audio_in.h create mode 100644 src/common/includes/Audio/tone.h create mode 100644 src/common/src/Audio/audio_in.c create mode 100644 src/common/src/Audio/tone.c (limited to 'src/common') diff --git a/src/common/includes/Audio/audio.h b/src/common/includes/Audio/audio.h index 7aeb6e9..d48cbe5 100644 --- a/src/common/includes/Audio/audio.h +++ b/src/common/includes/Audio/audio.h @@ -32,7 +32,7 @@ void audio_stop_dma(void); // Initialize and power up audio hardware. Use the above defines for the parameters. // Can probably only be called once. -void audio_initialize(int plln,int pllr,int i2sdiv,int i2sodd, int rate); +void audio_initialize(int plln, int pllr, int i2sdiv, int i2sodd, int rate); // Power up and down the audio hardware. void audio_put_codec_in_reset(void); diff --git a/src/common/includes/Audio/audio_in.h b/src/common/includes/Audio/audio_in.h new file mode 100644 index 0000000..86c93b2 --- /dev/null +++ b/src/common/includes/Audio/audio_in.h @@ -0,0 +1,40 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#ifndef __AUDIO_IN_H__ +#define __AUDIO_IN_H__ + +#include +#include "FreeRTOS.h" + +#define AUDIO_IN_BUF_LEN 2048 +#define AUDIO_IN_RATE 8000 + +int16_t * audio_in_buffer; + +void audio_in_initialize(int rate); +void audio_in_initialize_plateform(int rate); +void audio_in_buffer_ready(void); + +#endif diff --git a/src/common/includes/Audio/tone.h b/src/common/includes/Audio/tone.h new file mode 100644 index 0000000..1398ca5 --- /dev/null +++ b/src/common/includes/Audio/tone.h @@ -0,0 +1,52 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#ifndef __TONE_H_ +#define __TONE_H_ + +#include +#include "Audio/audio_in.h" + +#define TONE_BUFFER_LEN AUDIO_IN_BUF_LEN + +#define TONE_N 100 + +struct tone_detector { + float coef; + float Q1; + float Q2; + float threshold; +}; + +static struct tone_detector detector_1750; + + +void init_tones(void); +void init_tone(struct tone_detector* detector, int freq, int threshold); +void detect_tones(int16_t * buffer); + +int TONE_1750_DETECTED = 0; + + +#endif diff --git a/src/common/sourcelist.txt b/src/common/sourcelist.txt index eada47f..9d6ec78 100644 --- a/src/common/sourcelist.txt +++ b/src/common/sourcelist.txt @@ -8,3 +8,5 @@ src/Core/fsm.c src/Core/main.c src/Audio/cw.c src/Audio/audio.c +src/Audio/audio_in.c +src/Audio/tone.c diff --git a/src/common/src/Audio/audio_in.c b/src/common/src/Audio/audio_in.c new file mode 100644 index 0000000..01f4794 --- /dev/null +++ b/src/common/src/Audio/audio_in.c @@ -0,0 +1,46 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + + +#include "Audio/audio_in.h" + +int16_t audio_in_buffer_0[AUDIO_IN_BUF_LEN]; +int16_t audio_in_buffer_1[AUDIO_IN_BUF_LEN]; + +void audio_in_initialize(int rate) { + audio_in_buffer = &audio_in_buffer_0; + audio_in_initialize_plateform(rate); +} + +void audio_in_buffer_ready() { + + detect_tones(audio_in_buffer); + + if (audio_in_buffer == audio_in_buffer_0) { + audio_in_buffer = audio_in_buffer_1; + } else { + audio_in_buffer = audio_in_buffer_0; + } + +} diff --git a/src/common/src/Audio/tone.c b/src/common/src/Audio/tone.c new file mode 100644 index 0000000..dda9432 --- /dev/null +++ b/src/common/src/Audio/tone.c @@ -0,0 +1,97 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#include "Audio/tone.h" +#include "Core/common.h" + +#ifdef SIMULATOR +#include +#define arm_cos_f32 cosf +#define arm_sin_f32 sinf +#else +#include "arm_math.h" +#endif + +static int current_pos = 0; + + +void init_tones() { + init_tone(&detector_1750, 1750, 1); +} + +void init_tone(struct tone_detector* detector, int freq, int threshold) { + + detector->coef = 2.0 * cos(2.0 * FLOAT_PI * freq / AUDIO_IN_RATE); + detector->Q1 = 0; + detector->Q2 = 0; + detector->threshold = 200000; // TODO + +} + +void detect_tones(int16_t * buffer) { + + // Normalize buffer + int max_v = 0; + for (int i = 0; i < TONE_BUFFER_LEN; i++) { + max_v = fmax(abs(buffer[i]), max_v); + } + + float coef = 32767.0 / max_v; + + for (int i = 0; i < TONE_BUFFER_LEN; i++) { + buffer[i] *= coef; + } + + TONE_1750_DETECTED = detect_tone(buffer, &detector_1750); +} + +int detect_tone(int16_t * buffer, struct tone_detector* detector) { + + float Q0; + int tt_samples = 0; + int tt = 0; + + for (int i = 0; i < TONE_BUFFER_LEN; i++) { + Q0 = detector->coef * detector->Q1 - detector->Q2 + buffer[i]; + detector->Q2 = detector->Q1; + detector->Q1 = Q0; + + current_pos++; + + if (current_pos == TONE_N) { + float m = sqrt(detector->Q1 * detector->Q1 + detector->Q2 * detector->Q2 - detector->coef * detector->Q1 * detector->Q2); + + if (m > detector->threshold) { + tt += 1; + } + + tt_samples++; + detector->Q1 = 0; + detector->Q2 = 0; + current_pos = 0; + } + } + + return (float)tt / tt_samples > 0.5; +} diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c index 3fe441f..3a7d186 100644 --- a/src/common/src/Core/main.c +++ b/src/common/src/Core/main.c @@ -35,6 +35,8 @@ /* Includes */ #include "Audio/audio.h" +#include "Audio/audio_in.h" +#include "Audio/tone.h" #include "Audio/cw.h" #include "GPIO/pio.h" #include "GPIO/i2c.h" @@ -194,8 +196,10 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) trigger_fault(FAULT_SOURCE_MAIN); } - usart_debug_puts("Audio init\r\n"); + usart_debug_puts("Tone init\r\n"); + init_tones(); + usart_debug_puts("Audio init\r\n"); audio_initialize(Audio16000HzSettings); usart_debug_puts("Audio set volume\r\n"); @@ -204,6 +208,9 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) usart_debug_puts("Audio set callback\r\n"); audio_play_with_callback(audio_callback, NULL); + usart_debug_puts("Audio in init\r\n"); + audio_in_initialize(AUDIO_IN_RATE); + usart_debug_puts("Init done.\r\n"); int last_qrp_from_supply = 0; -- cgit v1.2.3 From 76f58c6eb4991d3f8c9648d0dc8a57e496f01d5a Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 17 Jun 2018 18:56:15 +0200 Subject: Add tone-test-sim --- src/common/includes/Audio/tone.h | 15 +-- src/common/src/Audio/tone.c | 96 +++++++++--------- src/tone-test-sim/Makefile | 149 ++++++++++++++++++++++++++++ src/tone-test-sim/Source | 1 + src/tone-test-sim/analyse.py | 24 +++++ src/tone-test-sim/src/Core/FreeRTOSConfig.h | 4 + src/tone-test-sim/src/Core/vc.c | 30 ++++++ src/tone-test-sim/src/test.c | 38 +++++++ 8 files changed, 298 insertions(+), 59 deletions(-) create mode 100644 src/tone-test-sim/Makefile create mode 120000 src/tone-test-sim/Source create mode 100755 src/tone-test-sim/analyse.py create mode 100644 src/tone-test-sim/src/Core/FreeRTOSConfig.h create mode 100644 src/tone-test-sim/src/Core/vc.c create mode 100644 src/tone-test-sim/src/test.c (limited to 'src/common') diff --git a/src/common/includes/Audio/tone.h b/src/common/includes/Audio/tone.h index 1398ca5..0e80228 100644 --- a/src/common/includes/Audio/tone.h +++ b/src/common/includes/Audio/tone.h @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Maximilien Cuony + * Copyright (c) 2018 Matthias P. Braendli, Maximilien Cuony * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -37,16 +37,11 @@ struct tone_detector { float Q1; float Q2; float threshold; -}; - -static struct tone_detector detector_1750; - -void init_tones(void); -void init_tone(struct tone_detector* detector, int freq, int threshold); -void detect_tones(int16_t * buffer); - -int TONE_1750_DETECTED = 0; + int num_samples_analysed; +}; +void tone_init(int threshold); +int tone_detect_1750(int16_t sample); #endif diff --git a/src/common/src/Audio/tone.c b/src/common/src/Audio/tone.c index dda9432..2cd97a9 100644 --- a/src/common/src/Audio/tone.c +++ b/src/common/src/Audio/tone.c @@ -25,6 +25,8 @@ #include "Audio/tone.h" #include "Core/common.h" +#include + #ifdef SIMULATOR #include #define arm_cos_f32 cosf @@ -33,65 +35,61 @@ #include "arm_math.h" #endif -static int current_pos = 0; - +static struct tone_detector detector_1750; -void init_tones() { - init_tone(&detector_1750, 1750, 1); -} +int TONE_1750_DETECTED = 0; -void init_tone(struct tone_detector* detector, int freq, int threshold) { - - detector->coef = 2.0 * cos(2.0 * FLOAT_PI * freq / AUDIO_IN_RATE); +static void init_tone(struct tone_detector* detector, int freq, int threshold) { + detector->coef = 2.0 * arm_cos_f32(2.0 * FLOAT_PI * freq / AUDIO_IN_RATE); detector->Q1 = 0; detector->Q2 = 0; - detector->threshold = 200000; // TODO - + detector->threshold = threshold ; // 200000; + detector->num_samples_analysed = 0; } -void detect_tones(int16_t * buffer) { +void tone_init(int threshold) { + init_tone(&detector_1750, 1750, threshold); +} - // Normalize buffer - int max_v = 0; - for (int i = 0; i < TONE_BUFFER_LEN; i++) { - max_v = fmax(abs(buffer[i]), max_v); +/* Analyse a sample. Returns -1 if more samples needed, 0 if no tone detected, + * 1 if a tone was detected. + */ +static inline int analyse_sample(int16_t sample, struct tone_detector *detector) +{ + float Q0 = detector->coef * detector->Q1 - detector->Q2 + sample; + detector->Q2 = detector->Q1; + detector->Q1 = Q0; + + detector->num_samples_analysed++; + + if (detector->num_samples_analysed == TONE_N) { + const float m = sqrtf( + detector->Q1 * detector->Q1 + + detector->Q2 * detector->Q2 - + detector->coef * detector->Q1 * detector->Q2); + + detector->Q1 = 0; + detector->Q2 = 0; + detector->num_samples_analysed = 0; + + if (m > detector->threshold) { + return 1; + } + else { + return 0; + } } - - float coef = 32767.0 / max_v; - - for (int i = 0; i < TONE_BUFFER_LEN; i++) { - buffer[i] *= coef; + else { + return -1; } - - TONE_1750_DETECTED = detect_tone(buffer, &detector_1750); } -int detect_tone(int16_t * buffer, struct tone_detector* detector) { - - float Q0; - int tt_samples = 0; - int tt = 0; - - for (int i = 0; i < TONE_BUFFER_LEN; i++) { - Q0 = detector->coef * detector->Q1 - detector->Q2 + buffer[i]; - detector->Q2 = detector->Q1; - detector->Q1 = Q0; - - current_pos++; - - if (current_pos == TONE_N) { - float m = sqrt(detector->Q1 * detector->Q1 + detector->Q2 * detector->Q2 - detector->coef * detector->Q1 * detector->Q2); - - if (m > detector->threshold) { - tt += 1; - } - - tt_samples++; - detector->Q1 = 0; - detector->Q2 = 0; - current_pos = 0; - } +int tone_detect_1750(int16_t sample) +{ + int r = analyse_sample(sample, &detector_1750); + if (r == 0 || r == 1) { + TONE_1750_DETECTED = r; } - - return (float)tt / tt_samples > 0.5; + return r; } + diff --git a/src/tone-test-sim/Makefile b/src/tone-test-sim/Makefile new file mode 100644 index 0000000..9e48dbc --- /dev/null +++ b/src/tone-test-sim/Makefile @@ -0,0 +1,149 @@ + +######## Build options ######## + +verbose = 1 + +######## Build setup ######## + +# SRCROOT should always be the current directory +SRCROOT = $(CURDIR) + +# .o directory +ODIR = obj + +# Source VPATHS +VPATH += $(SRCROOT)/Source +VPATH += $(SRCROOT)/Source/portable/MemMang +VPATH += $(SRCROOT)/Source/portable/GCC/POSIX +VPATH += $(SRCROOT)/src/Core +VPATH += $(SRCROOT)/src/GPIO +VPATH += $(SRCROOT)/src/GPS + +# FreeRTOS Objects +C_FILES += croutine.c +C_FILES += event_groups.c +C_FILES += list.c +C_FILES += queue.c +C_FILES += tasks.c +C_FILES += timers.c + +# portable Objects +C_FILES += heap_3.c +C_FILES += port.c + +# common Objects +#COMMON_SOURCE_LIST=$(shell cat ../common/sourcelist.txt) +#C_FILES+=$(COMMON_SOURCE_LIST:%.c=../common/%.c) + +C_FILES+=../common/src/Audio/tone.c + +# Main Object +SRC_SOURCES+=$(shell find -L src/ -name '*.c' -not -name 'vc.c') +C_FILES += $(SRC_SOURCES) + +# Include Paths +INCLUDES += -I$(SRCROOT)/Source/include +INCLUDES += -I$(SRCROOT)/Source/portable/GCC/POSIX/ +INCLUDES += -I$(SRCROOT)/src/Core +INCLUDES += -I$(SRCROOT)/../common/includes/ +INCLUDES += -I$(SRCROOT) + +# Generate OBJS names +OBJS = $(patsubst %.c,%.o,$(C_FILES)) +OBJS += src/Core/vc.o + +######## C Flags ######## + +# Warnings +CWARNS += -W +CWARNS += -Wall +# CWARNS += -Werror +CWARNS += -Wextra +CWARNS += -Wformat +CWARNS += -Wmissing-braces +CWARNS += -Wno-cast-align +CWARNS += -Wparentheses +CWARNS += -Wshadow +CWARNS += -Wno-sign-compare +CWARNS += -Wswitch +CWARNS += -Wuninitialized +CWARNS += -Wunknown-pragmas +CWARNS += -Wunused-function +CWARNS += -Wunused-label +CWARNS += -Wunused-parameter +CWARNS += -Wunused-value +CWARNS += -Wunused-variable +CWARNS += -Wmissing-prototypes + +CFLAGS += -DDEBUG=1 +CFLAGS += -g -DUSE_STDIO=1 -D__GCC_POSIX__=1 -lm -lm +ifneq ($(shell uname), Darwin) +CFLAGS += -pthread +endif + +# MAX_NUMBER_OF_TASKS = max pthreads used in the POSIX port. +# Default value is 64 (_POSIX_THREAD_THREADS_MAX), the minimum number required by POSIX. +CFLAGS += -DMAX_NUMBER_OF_TASKS=300 -DSIMULATOR + +CFLAGS += $(INCLUDES) $(CWARNS) -O2 + +######## Makefile targets ######## + +# Rules +.PHONY : all +all: vc.h setup tone-test-sim + +.PHONY : setup +setup: +# Make obj directory + @mkdir -p $(ODIR) + +# Fix to place .o files in ODIR +_OBJS = $(patsubst %,$(ODIR)/%,$(OBJS)) + +dir_guard=@mkdir -p $(@D) + +$(ODIR)/src/Core/vc.o: src/Core/vc.c vc.h + $(dir_guard) + @echo "[CC] version information vc.c" +ifeq ($(verbose),1) + $(CC) $(CFLAGS) src/Core/vc.c -c -o $(ODIR)/src/Core/vc.o +else + @$(CC) $(CFLAGS) src/Core/vc.c -c -o $(ODIR)/src/Core/vc.o +endif + +$(ODIR)/%.o: %.c + $(dir_guard) +# If verbose, print gcc execution, else hide +ifeq ($(verbose),1) + @echo "[CC] $<" + $(CC) $(CFLAGS) -c -o $@ $< +else + @echo "[CC] $(notdir $<)" + @$(CC) $(CFLAGS) -c -o $@ $< +endif + +.PHONY: vc.h +vc.h: ../../.git/logs/HEAD + @echo "// This file is generated by Makefile." > vc.h + @echo "// Do not edit this file!" >> vc.h + @echo "const char* vc_get_version(void);" >> vc.h + @echo >> vc.h + @git log -1 --format="format:#define GIT_VERSION \"%h\"" >> vc.h + @echo >> vc.h + @echo >> vc.h + @echo [GEN] vc.h + +tone-test-sim: $(_OBJS) + @echo "[LK] $@" +ifeq ($(verbose),1) + $(CC) $(CFLAGS) $^ $(LINKFLAGS) $(LIBS) -o $@ +else + @$(CC) $(CFLAGS) $^ $(LINKFLAGS) $(LIBS) -o $@ +endif + @echo "[:)] Happiness :)" + +.PHONY : clean +clean: + @-rm -rf $(ODIR) tone-test-sim common/ + @echo "[RM] Cleanuped °o°" diff --git a/src/tone-test-sim/Source b/src/tone-test-sim/Source new file mode 120000 index 0000000..5f455fb --- /dev/null +++ b/src/tone-test-sim/Source @@ -0,0 +1 @@ +../FreeRTOS-Sim-master/Source \ No newline at end of file diff --git a/src/tone-test-sim/analyse.py b/src/tone-test-sim/analyse.py new file mode 100755 index 0000000..a67644c --- /dev/null +++ b/src/tone-test-sim/analyse.py @@ -0,0 +1,24 @@ +#!/usr/bin/env python + +import numpy as np +import matplotlib.pyplot as plt + +dat = np.loadtxt("tone.csv", dtype=np.int32, delimiter=",") +# columns :"freq, threshold, num_samples, detector_output" + +fig, ax = plt.subplots() +plt.scatter(dat[...,0], dat[...,1], c=dat[...,3].astype(np.float32)) +plt.show() + +if 0: + thresholds = [800, 2000, 4000, 8000] + + fig, ax = plt.subplots() + + for th in thresholds: + dat_th = dat[dat[...,1] == th] + dat_th[...,1] + ax.plot(dat_th[...,0], dat_th[...,3], label="{}".format(th)) + + legend = ax.legend(loc='upper left', shadow=True) + plt.show() diff --git a/src/tone-test-sim/src/Core/FreeRTOSConfig.h b/src/tone-test-sim/src/Core/FreeRTOSConfig.h new file mode 100644 index 0000000..19086b7 --- /dev/null +++ b/src/tone-test-sim/src/Core/FreeRTOSConfig.h @@ -0,0 +1,4 @@ +#include "../../../common/src/Core/FreeRTOSConfig.h" + + +#define configCHECK_FOR_STACK_OVERFLOW 0 /* Do not use this option on the PC port. */ diff --git a/src/tone-test-sim/src/Core/vc.c b/src/tone-test-sim/src/Core/vc.c new file mode 100644 index 0000000..253ab2d --- /dev/null +++ b/src/tone-test-sim/src/Core/vc.c @@ -0,0 +1,30 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016 Matthias P. Braendli, Maximilien Cuony + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ + +#include "vc.h" + +const char* vc_get_version() +{ + return GIT_VERSION; +} diff --git a/src/tone-test-sim/src/test.c b/src/tone-test-sim/src/test.c new file mode 100644 index 0000000..ca35c1c --- /dev/null +++ b/src/tone-test-sim/src/test.c @@ -0,0 +1,38 @@ +#include +#include +#include +#include "vc.h" +#include "Audio/tone.h" + +#define FLOAT_PI 3.1415926535897932384f + +int main(int argc, char **argv) +{ + printf("Hello, ver %s\n", vc_get_version()); + printf("Saving results to tone.csv\n"); + printf("freq, threshold, num_samples, detector_output\n"); + + FILE *fd = fopen("tone.csv", "w"); + if (!fd) { + printf("Cannot create file\n"); + return 1; + } + + const int freq_start = 1750 - 175; + const int freq_stop = 1750 + 175; + + for (int freq = freq_start; freq < freq_stop; freq += 20) { + for (int threshold = 800; threshold < 8000; threshold += 80) { + tone_init(threshold); + + for (size_t j = 0; j < 200; j++) { + float samplef = cosf(2.0f * FLOAT_PI * freq / AUDIO_IN_RATE); + int16_t sample = samplef * 32767.0f; + int r = tone_detect_1750(sample); + if (r != -1) { + fprintf(fd, "%d,%d,%zu,%d\n",freq, threshold, j, r); + } + } + } + } +} -- cgit v1.2.3 From 9076fd7a7cef6bde83bdf52d41e45a3e0d540a2f Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 17 Jun 2018 19:03:43 +0200 Subject: Fix simulator compilation --- src/common/src/Audio/audio_in.c | 8 +++++--- src/common/src/Core/main.c | 3 ++- src/simulator/src/Audio/audio_in.c | 8 ++------ 3 files changed, 9 insertions(+), 10 deletions(-) (limited to 'src/common') diff --git a/src/common/src/Audio/audio_in.c b/src/common/src/Audio/audio_in.c index 01f4794..b046a62 100644 --- a/src/common/src/Audio/audio_in.c +++ b/src/common/src/Audio/audio_in.c @@ -22,20 +22,22 @@ * SOFTWARE. */ - +#include "Audio/tone.h" #include "Audio/audio_in.h" int16_t audio_in_buffer_0[AUDIO_IN_BUF_LEN]; int16_t audio_in_buffer_1[AUDIO_IN_BUF_LEN]; void audio_in_initialize(int rate) { - audio_in_buffer = &audio_in_buffer_0; + audio_in_buffer = audio_in_buffer_0; audio_in_initialize_plateform(rate); } void audio_in_buffer_ready() { - detect_tones(audio_in_buffer); + for (int i = 0; i < AUDIO_IN_BUF_LEN; i++) { + tone_detect_1750(audio_in_buffer[i]); + } if (audio_in_buffer == audio_in_buffer_0) { audio_in_buffer = audio_in_buffer_1; diff --git a/src/common/src/Core/main.c b/src/common/src/Core/main.c index 3a7d186..aaa629b 100644 --- a/src/common/src/Core/main.c +++ b/src/common/src/Core/main.c @@ -197,7 +197,8 @@ static void launcher_task(void __attribute__ ((unused))*pvParameters) } usart_debug_puts("Tone init\r\n"); - init_tones(); +#warning TODO + tone_init(20000); usart_debug_puts("Audio init\r\n"); audio_initialize(Audio16000HzSettings); diff --git a/src/simulator/src/Audio/audio_in.c b/src/simulator/src/Audio/audio_in.c index df3e690..936fe1c 100644 --- a/src/simulator/src/Audio/audio_in.c +++ b/src/simulator/src/Audio/audio_in.c @@ -23,6 +23,7 @@ */ #include +#include #include #include "Audio/audio_in.h" #include "FreeRTOS.h" @@ -46,10 +47,7 @@ void audio_in_initialize_plateform(int rate) { s_in = pa_simple_new(NULL, "GlutteR", PA_STREAM_RECORD, NULL, "record", &ss, NULL, NULL, &error); - if (!s_in) { - printf("Pulseaudio record init error\n"); - while(1); - } + assert(s_in); TaskHandle_t task_handle; xTaskCreate( @@ -59,13 +57,11 @@ void audio_in_initialize_plateform(int rate) { (void*) NULL, tskIDLE_PRIORITY + 2UL, &task_handle); - } static void audio_buffer_reader(void __attribute__ ((unused)) *args) { while(1) { - pa_simple_read(s_in, audio_in_buffer, AUDIO_IN_BUF_LEN * 2, NULL); audio_in_buffer_ready(); -- cgit v1.2.3 From d8df88e8b299fe697ff695f61cd1e85032530a84 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Tue, 19 Jun 2018 21:09:47 +0200 Subject: Widen frequency range of tone test --- src/common/src/Audio/tone.c | 6 +++--- src/tone-test-sim/analyse.py | 2 +- src/tone-test-sim/src/test.c | 29 ++++++++++++++++++++++++++--- 3 files changed, 30 insertions(+), 7 deletions(-) (limited to 'src/common') diff --git a/src/common/src/Audio/tone.c b/src/common/src/Audio/tone.c index 2cd97a9..60226ce 100644 --- a/src/common/src/Audio/tone.c +++ b/src/common/src/Audio/tone.c @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2018 Maximilien Cuony + * Copyright (c) 2018 Maximilien Cuony, Matthias P. Braendli * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -40,10 +40,10 @@ static struct tone_detector detector_1750; int TONE_1750_DETECTED = 0; static void init_tone(struct tone_detector* detector, int freq, int threshold) { - detector->coef = 2.0 * arm_cos_f32(2.0 * FLOAT_PI * freq / AUDIO_IN_RATE); + detector->coef = 2.0f * arm_cos_f32(2.0f * FLOAT_PI * freq / AUDIO_IN_RATE); detector->Q1 = 0; detector->Q2 = 0; - detector->threshold = threshold ; // 200000; + detector->threshold = threshold; detector->num_samples_analysed = 0; } diff --git a/src/tone-test-sim/analyse.py b/src/tone-test-sim/analyse.py index a67644c..8222213 100755 --- a/src/tone-test-sim/analyse.py +++ b/src/tone-test-sim/analyse.py @@ -11,7 +11,7 @@ plt.scatter(dat[...,0], dat[...,1], c=dat[...,3].astype(np.float32)) plt.show() if 0: - thresholds = [800, 2000, 4000, 8000] + thresholds = [200, 2000, 4000, 8000, 12000] fig, ax = plt.subplots() diff --git a/src/tone-test-sim/src/test.c b/src/tone-test-sim/src/test.c index ca35c1c..3ba00c4 100644 --- a/src/tone-test-sim/src/test.c +++ b/src/tone-test-sim/src/test.c @@ -1,3 +1,26 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2018 Maximilien Cuony, Matthias P. Braendli + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. +*/ #include #include #include @@ -18,11 +41,11 @@ int main(int argc, char **argv) return 1; } - const int freq_start = 1750 - 175; - const int freq_stop = 1750 + 175; + const int freq_start = 1750 - 350; + const int freq_stop = 1750 + 350; for (int freq = freq_start; freq < freq_stop; freq += 20) { - for (int threshold = 800; threshold < 8000; threshold += 80) { + for (int threshold = 200; threshold < 8000; threshold += 240) { tone_init(threshold); for (size_t j = 0; j < 200; j++) { -- cgit v1.2.3