Commit 5c9c0760590b7a92519ec6648e56eccd3d16fa42

Authored by Arnaud Blanchard
1 parent cd0c932a

Funtion to discretize spikes from vector fomat (time,period) to buffer (list of interpolatd periods)

  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
  7 +# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
  8 +# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
  9 +# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
  10 +# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
  11 +
  12 +cmake_minimum_required(VERSION 2.6)
  13 +
  14 +project(i_discretize_file)
  15 +
  16 +find_package(blc_channel REQUIRED)
  17 +find_package(blc_program REQUIRED)
  18 +
  19 +add_definitions(${BL_DEFINTIIONS})
  20 +include_directories(${BL_INCLUDE_DIRS})
  21 +add_executable(i_discretize_file i_discretize_file.cpp)
  22 +target_link_libraries(i_discretize_file ${BL_LIBRARIES})
  23 +
  24 +
  25 +
  26 +
  1 +
  2 +
  3 +Read a binary file in form where only columns 2 and 3 are used.
  4 + 0. 1 2. 3
  5 + <uint32_t><uint32_t><uint32_t><uint32_t>
  6 +
  7 +
  8 +2 is the absolute timing in µs of a spike, 3 is the period between two spikes.
  9 +
  10 +It generates a buffer of floats 'FL32' in sound format 'LPCM' interpolating the period with a samplerate given in parameter i.e. -S8000 is a samplerate and -s1024 is the size of the buffer.
  11 +-O112 is the offset of the signal to center the signal around 0. -g2.0 is the gain to amplify or decrease the signal.
  12 +
  13 +Exemple of use to listen a sound
  14 +
  15 + i_discretize_file left_ear.bin -o :sound -s1024 -S8000 -O112 -g2.0 | o_sound.sh -S8000
  16 +
  17 +In parrallel sound can be dislayed in a graph `o_gnuplot /sound`
  18 +
  19 +We can also diplay the fft transform of the signal:
  20 +
  21 + i_discretize_file left_ear.bin -o :sound -s2048 -S8000 -O112 | f_fftw_spectrum | o_gnuplot -M1
  22 +
  23 +
  24 +
  1 +# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
  2 +# Author: Arnaud Blanchard (November 2016)
  3 +# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
  4 +# You can use, modify and/ or redistribute the software under the terms of the CeCILL v2.1 license as circulated by CEA, CNRS and INRIA at the following URL "http://www.cecill.info".
  5 +# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
  6 +# users are provided only with a limited warranty and the software's author, the holder of the economic rights, and the successive licensors have only limited liability.
  7 +# In this respect, the user's attention is drawn to the risks associated with loading, using, modifying and/or developing or reproducing the software by the user in light of its specific status of free software,
  8 +# that may mean that it is complicated to manipulate, and that also therefore means that it is reserved for developers and experienced professionals having in-depth computer knowledge.
  9 +# Users are therefore encouraged to load and test the software's suitability as regards their requirements in conditions enabling the security of their systems and/or data to be ensured  and, more generally, to use and operate it in the same conditions as regards security.
  10 +# The fact that you are presently reading this means that you have had knowledge of the CeCILL v2.1 license and that you accept its terms.
  11 +
  12 +
  13 +cd `dirname $0`
  14 +echo
  15 +echo "Checking `basename $PWD`"
  16 +echo "===================="
  17 +echo
  18 +echo "No check for now"
  19 +echo
  1 +//Read only array of dim 1
  2 +#include "blc_core.h"
  3 +#include "blc_channel.h"
  4 +#include "blc_program.h"
  5 +#include <unistd.h>
  6 +#include <termios.h>
  7 +#include <libgen.h> //basename
  8 +
  9 +#define READ_VAR(file, var) read_file(file, (void*)&var, sizeof(var))
  10 +#define WRITE_VAR(file, var) write_file(file, (void*)&var, sizeof(var))
  11 +
  12 +bool read_file(FILE *file, void *data, size_t size){
  13 + ssize_t ret;
  14 + ret=fread(data, 1, size, file);
  15 + if (ret != (ssize_t)size) {
  16 + if ((ret==0) && feof(file)) return false;
  17 + else EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
  18 + }
  19 + return true;
  20 +}
  21 +
  22 +void write_file(FILE *file, void *data, size_t size){
  23 + ssize_t ret;
  24 + ret=fwrite(data, 1, size, file);
  25 + if (ret != (ssize_t)size) EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
  26 +}
  27 +
  28 +int main(int argc, char** argv){
  29 + char const *channel_name, *input_filename, *output_filename, *length_str, *samplerate_str, *period_str, *offset_str, *gain_str;
  30 + char *default_output;
  31 + float offset, gain;
  32 + int number=-1;
  33 + uint32_t start_load, time_load, start_delay, time_delay;
  34 + blc_channel channel;
  35 + FILE *file;
  36 + int period, length;
  37 + int samplerate;
  38 +
  39 + asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once
  40 + blc_program_add_option(&output_filename, 'f', "file", "string", "filename of the file to export", NULL);
  41 + blc_program_add_option(&gain_str, 'g', "gain", "real", "gain to amplify the signal", "1.0");
  42 + blc_program_add_option(&channel_name, 'o', "blc_channel-out", "string", "blc_channel to export", default_output);
  43 + blc_program_add_option(&period_str, 'p', "period", "integer", "items nb in buffer", "0");
  44 + blc_program_add_option(&length_str, 's', "size", "integer", "items nb in buffer", "1024");
  45 + blc_program_add_option(&offset_str, 'O', "offset", "real", "offset to remove from the signal", "0");
  46 + blc_program_add_option(&samplerate_str, 'S', "samplerate", "integer", "samplerate", "8000");
  47 + blc_program_add_parameter(&input_filename, "filename", 1, "File to load", NULL);
  48 + blc_program_init(&argc, &argv, blc_quit);
  49 + blc_command_forward_blc_channels();
  50 +
  51 + SSCANF(1, period_str, "%d", &period);
  52 + SSCANF(1, length_str, "%d", &length);
  53 + SSCANF(1, samplerate_str, "%d", &samplerate);
  54 + SSCANF(1, offset_str, "%f", &offset);
  55 + SSCANF(1, gain_str, "%f", &gain);
  56 +
  57 + channel.create_or_open(channel_name, BLC_CHANNEL_WRITE, 'FL32', 'LPCM', 1, length);
  58 + blc_loop_try_add_posting_semaphore(channel.sem_new_data);
  59 + blc_loop_try_add_waiting_semaphore(channel.sem_ack_data);
  60 +
  61 + SYSTEM_ERROR_CHECK(file=fopen(input_filename, "r"), NULL, "Opening '%s'", input_filename);
  62 + channel.publish();
  63 +
  64 + uint32_t iteration=0;
  65 + float a, b, u, v,result, moy;
  66 +
  67 + a=0;
  68 + u=0;
  69 + int index;
  70 + b=0;
  71 + start_delay=0;
  72 + iteration=0;
  73 + moy=0;
  74 + v=0;
  75 + BLC_COMMAND_LOOP(period*1000){
  76 + if (feof(file)){
  77 + if (number==-1) blc_command_ask_quit();
  78 + else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
  79 + }
  80 + else{
  81 + for(index=0; index < channel.total_length; index++){
  82 +
  83 + if (b<=iteration){
  84 + READ_VAR(file, start_load);
  85 + READ_VAR(file, time_load);
  86 + READ_VAR(file, start_delay);
  87 + READ_VAR(file, time_delay);
  88 + a=b;
  89 + u=v;
  90 + b=(float)start_delay*samplerate/1000000.f;
  91 + v=gain*((float)(time_delay)-offset);
  92 + }
  93 + //Formule of interpolation
  94 + result=u+(iteration-a)*(v-u)/(b-a);
  95 + channel.floats[index]=result;
  96 + iteration++;
  97 + }
  98 + }
  99 + }
  100 + SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", input_filename);
  101 +
  102 + fprintf(stderr, "Stop at %u µs iteration %d", start_delay, iteration);
  103 + return EXIT_SUCCESS;
  104 +}