Commit ee53a5d4 authored by Arnaud Blanchard's avatar Arnaud Blanchard
Browse files

Add the possibility to save in a file

parent abac5a05
......@@ -5,7 +5,7 @@
#include <math.h> //sqrtf
#include <pthread.h>
#define DEFAULT_OUTPUT_NAME "fourier<pid> or spectrum<pid>"
#define DEFAULT_OUTPUT_NAME "spectrum<pid>"
sem_t *sem_output, *sem_input;
int i=0;
......@@ -14,9 +14,6 @@ char const *display, *option_record;
int columns_nb=64, rows_nb=16;
static void init_output_channel(blc_channel *output_channel, char const *output_name, int output_length){
if (strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) asprintf((char**)&output_name, ":spectrum%d", getpid());
//Create a channel or reopen a previous one.
output_channel->create_or_open(output_name, BLC_CHANNEL_WRITE, 'FL32', 'NDEF', 1, output_length);
......@@ -29,8 +26,9 @@ static void init_output_channel(blc_channel *output_channel, char const *output_
int main(int argc, char**argv){
blc_channel input, output;
char const *input_name, *output_name, *spectrum_option, *period_str;
FILE *file;
int output_length, period;
FILE *file=NULL;
int ret;
int output_length, period, pos;
size_t i;
fftwf_complex *intermediate;
fftwf_plan fftw_plan;
......@@ -52,23 +50,34 @@ int main(int argc, char**argv){
output.init('FL32', 'NDEF', 1, output_length);
file=stdout;
}
else
{
init_output_channel(&output, output_name, output_length);
else if (strcmp(output_name, DEFAULT_OUTPUT_NAME)==0) {
asprintf((char**)&output_name, ":spectrum%d", getpid());
init_output_channel(&output, output_name, output_length);
}
else{
ret=sscanf(output_name, "%*[:/.^]%*[^/]%n", &pos);
if (pos==strlen(output_name)) init_output_channel(&output, output_name, output_length); //C'est un blc_channel
else //C'est un format de fichier
{
if (strcmp(blc_get_filename_extension(output_name), "tsv")!=0) EXIT_ON_ERROR("Filename extension must be .tsv but your file is: '%s'.\nThis is to be sure you are not overwriting important file by mistake", output_name);
output.init('FL32', 'NDEF', 1, output_length);
SYSTEM_ERROR_CHECK(file=fopen(output_name,"w"), NULL, "opening '%s'", output_name);
}
}
blc_loop_try_add_waiting_semaphore(input.sem_new_data);
blc_loop_try_add_posting_semaphore(input.sem_ack_data);
intermediate=fftwf_alloc_complex(output.total_length);
intermediate=fftwf_alloc_complex(output_length);
fftw_plan = fftwf_plan_dft_r2c_1d(input.total_length, input.floats, intermediate , FFTW_ESTIMATE);
BLC_COMMAND_LOOP(period*1000){
fftwf_execute(fftw_plan); /* repeat as needed */
FOR(i, output.total_length){
output.floats[i]=sqrtf(intermediate[i][0]*intermediate[i][0]+intermediate[i][1]*intermediate[i][1])/output.total_length;
FOR(i, output_length){
output.floats[i]=sqrtf(intermediate[i][0]*intermediate[i][0]+intermediate[i][1]*intermediate[i][1])/input.total_length;
}
//if output is not a channel we export to standard output
if (output.fd==-1) output.fprint_tsv(file);
if (file) output.fprint_tsv(file);
if(display){
blc_fprint_float_graph(stderr, output.floats, output.total_length, "spectrum", columns_nb, rows_nb, 1, 0, "Frequency", "Intensity");
......@@ -78,7 +87,7 @@ int main(int argc, char**argv){
fftwf_destroy_plan(fftw_plan);
blc_eprint_cursor_down(rows_nb);
if (file) SYSTEM_ERROR_CHECK(fclose(file), -1, NULL);
return EXIT_SUCCESS;
}
Supports Markdown
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