Commit 5c9c0760 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Funtion to discretize spikes from vector fomat (time,period) to buffer (list...

Funtion to discretize spikes from vector fomat (time,period) to buffer (list of interpolatd periods)
parent cd0c932a
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# 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".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# 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.
# 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,
# 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.
# 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.
# 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.
cmake_minimum_required(VERSION 2.6)
project(i_discretize_file)
find_package(blc_channel REQUIRED)
find_package(blc_program REQUIRED)
add_definitions(${BL_DEFINTIIONS})
include_directories(${BL_INCLUDE_DIRS})
add_executable(i_discretize_file i_discretize_file.cpp)
target_link_libraries(i_discretize_file ${BL_LIBRARIES})
Read a binary file in form where only columns 2 and 3 are used.
0. 1 2. 3
<uint32_t><uint32_t><uint32_t><uint32_t>
2 is the absolute timing in µs of a spike, 3 is the period between two spikes.
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.
-O112 is the offset of the signal to center the signal around 0. -g2.0 is the gain to amplify or decrease the signal.
Exemple of use to listen a sound
i_discretize_file left_ear.bin -o :sound -s1024 -S8000 -O112 -g2.0 | o_sound.sh -S8000
In parrallel sound can be dislayed in a graph `o_gnuplot /sound`
We can also diplay the fft transform of the signal:
i_discretize_file left_ear.bin -o :sound -s2048 -S8000 -O112 | f_fftw_spectrum | o_gnuplot -M1
# Copyright ETIS — ENSEA, Université de Cergy-Pontoise, CNRS (2011 - 2016)
# Author: Arnaud Blanchard (November 2016)
# This software is governed by the CeCILL v2.1 license under French law and abiding by the rules of distribution of free software.
# 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".
# As a counterpart to the access to the source code and rights to copy, modify and redistribute granted by the license,
# 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.
# 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,
# 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.
# 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.
# 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.
cd `dirname $0`
echo
echo "Checking `basename $PWD`"
echo "===================="
echo
echo "No check for now"
echo
//Read only array of dim 1
#include "blc_core.h"
#include "blc_channel.h"
#include "blc_program.h"
#include <unistd.h>
#include <termios.h>
#include <libgen.h> //basename
#define READ_VAR(file, var) read_file(file, (void*)&var, sizeof(var))
#define WRITE_VAR(file, var) write_file(file, (void*)&var, sizeof(var))
bool read_file(FILE *file, void *data, size_t size){
ssize_t ret;
ret=fread(data, 1, size, file);
if (ret != (ssize_t)size) {
if ((ret==0) && feof(file)) return false;
else EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
}
return true;
}
void write_file(FILE *file, void *data, size_t size){
ssize_t ret;
ret=fwrite(data, 1, size, file);
if (ret != (ssize_t)size) EXIT_ON_SYSTEM_ERROR("Reading file ret: '%d' instead od '%d'", ret, size);
}
int main(int argc, char** argv){
char const *channel_name, *input_filename, *output_filename, *length_str, *samplerate_str, *period_str, *offset_str, *gain_str;
char *default_output;
float offset, gain;
int number=-1;
uint32_t start_load, time_load, start_delay, time_delay;
blc_channel channel;
FILE *file;
int period, length;
int samplerate;
asprintf(&default_output, "/%s%d", basename(argv[0]), getpid()); //This will not be free but it is only allocate once
blc_program_add_option(&output_filename, 'f', "file", "string", "filename of the file to export", NULL);
blc_program_add_option(&gain_str, 'g', "gain", "real", "gain to amplify the signal", "1.0");
blc_program_add_option(&channel_name, 'o', "blc_channel-out", "string", "blc_channel to export", default_output);
blc_program_add_option(&period_str, 'p', "period", "integer", "items nb in buffer", "0");
blc_program_add_option(&length_str, 's', "size", "integer", "items nb in buffer", "1024");
blc_program_add_option(&offset_str, 'O', "offset", "real", "offset to remove from the signal", "0");
blc_program_add_option(&samplerate_str, 'S', "samplerate", "integer", "samplerate", "8000");
blc_program_add_parameter(&input_filename, "filename", 1, "File to load", NULL);
blc_program_init(&argc, &argv, blc_quit);
blc_command_forward_blc_channels();
SSCANF(1, period_str, "%d", &period);
SSCANF(1, length_str, "%d", &length);
SSCANF(1, samplerate_str, "%d", &samplerate);
SSCANF(1, offset_str, "%f", &offset);
SSCANF(1, gain_str, "%f", &gain);
channel.create_or_open(channel_name, BLC_CHANNEL_WRITE, 'FL32', 'LPCM', 1, length);
blc_loop_try_add_posting_semaphore(channel.sem_new_data);
blc_loop_try_add_waiting_semaphore(channel.sem_ack_data);
SYSTEM_ERROR_CHECK(file=fopen(input_filename, "r"), NULL, "Opening '%s'", input_filename);
channel.publish();
uint32_t iteration=0;
float a, b, u, v,result, moy;
a=0;
u=0;
int index;
b=0;
start_delay=0;
iteration=0;
moy=0;
v=0;
BLC_COMMAND_LOOP(period*1000){
if (feof(file)){
if (number==-1) blc_command_ask_quit();
else EXIT_ON_ERROR("End of file and you request '%d' iterations. Only '%d' has been done.", number, blc_loop_iteration);
}
else{
for(index=0; index < channel.total_length; index++){
if (b<=iteration){
READ_VAR(file, start_load);
READ_VAR(file, time_load);
READ_VAR(file, start_delay);
READ_VAR(file, time_delay);
a=b;
u=v;
b=(float)start_delay*samplerate/1000000.f;
v=gain*((float)(time_delay)-offset);
}
//Formule of interpolation
result=u+(iteration-a)*(v-u)/(b-a);
channel.floats[index]=result;
iteration++;
}
}
}
SYSTEM_ERROR_CHECK(fclose(file), -1, "Closing '%s'", input_filename);
fprintf(stderr, "Stop at %u µs iteration %d", start_delay, iteration);
return EXIT_SUCCESS;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment