Esempio n. 1
0
int main(int argc, char **argv)
{
	SNDFILE *fin, *fin2, *fout;
	SF_INFO input_info, input_info2;
	float *win, *in, *in2, *out, *spec, *spec2;
	int outsize, specsize, fftsize=1024, hopsize=256;
	int dataratio = fftsize/hopsize;
	int frames, extraframes;
	int read=0, written = 0, i, j, dur;
	float *buff;

	if(argc < 3) {
		printf("%s: unsufficient number of arguments(got %d, needed 2)", argv[0], argc-1);
		usage();
		exit(-1);
	}
	if(!(fin = sf_open(argv[1], SFM_READ, &input_info))) {
		printf("could not open %s", argv[1]);
		exit(-1);
	}
	if(!(fin2 = sf_open(argv[2], SFM_READ, &input_info2))) {
		printf("could not open %s", argv[2]);
		exit(-1);
	}
	if(!formats_equal(input_info, input_info2)) {
		sf_close(fin2);
		sf_close(fin);
		printf("%s and %s formats are not equal or files not mono\n", argv[1], argv[2]);
		exit(-1);
	}

	dur = input_info.frames > input_info2.frames ? input_info2.frames : input_info.frames;
	buff = (float*)malloc(sizeof(float)*100);
	win = (float*)malloc(sizeof(float)*fftsize);
	in = (float*)malloc(sizeof(float)*dur);
	in2 = (float*)malloc(sizeof(float)*dur);
	// number of full dataframes
	frames = ((dur*dataratio)/fftsize);
	// extra frames [not completely filled]
	extraframes = 1 + (frames*fftsize - dur*dataratio)/fftsize;
	// size of spectral data
	specsize = (frames + extraframes)*fftsize;
	spec = (float*)malloc(sizeof(float)*specsize);
	spec2 = (float*)malloc(sizeof(float)*specsize);
	outsize = specsize/dataratio;
	out = (float*)malloc(sizeof(float)*outsize);

	if(!(fout = sf_open(argv[3], SFM_WRITE, &input_info))) {
		printf("could not open %s \n", argv[3]);
		exit(-1);
	}

	// generera fönster
	for(i=0; i < fftsize; i++) win[i] = 0.5f - (float)(0.5*cos(i*twopi/fftsize));

	// läs in filer
	for(j=0; j < dur; j+=read) {
		read = sf_read_float(fin, buff, 100);
		for(i=0;i<read;i++) in[i+j] = buff[i];
	}
	for(j=0; j < dur; j+=read) {
		read = sf_read_float(fin2, buff, 100);
		for(i=0;i<read;i++) in2[i+j] = buff[i];
	}

	outsize = stft(in, win, spec, dur, fftsize, hopsize);
	stft(in2, win, spec2, dur, fftsize, hopsize);

	for(i=0; i < outsize; i+= fftsize)
		crosspec(&spec[i], &spec2[i], &spec[i], fftsize);

	dur = istft(spec, win, out, outsize, fftsize, hopsize);

	for(j=0; j < dur; j+= written) {
		for(i=0; i < 100 && j < dur; i++) {
			if(i+j < dur) buff[i] = out[i+j];
			else buff[i] = 0.f;
		}

		written = sf_write_float(fout, buff, i);
	}

	free(out);
	free(in);
	free(in2);
	free(spec);
	free(spec2);
	free(win);
	free(buff);

	sf_close(fout);
	sf_close(fin);
	sf_close(fin2);
	return 0;
}
Esempio n. 2
0
int main(int argc, char** argv) {
  int i, j;
  int showhelp = 0;

  unsigned int channels = 1;
  int window_size = 1024;
  int shift = 256;
  char* somfile = NULL;
  snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

  int opt;
  while ((opt = getopt(argc, argv, "hw:s:f:o:")) != -1) {
    switch (opt) {
      case 'h':
        showhelp = 1;
        break;
      case 'w':
        window_size = atoi(optarg);
        break;
      case 's':
        shift = atoi(optarg);
        break;
      case 'f':
        format = snd_pcm_format_value(optarg);
        break;
      case 'o':
        somfile = strdup(optarg);
        break;
      default: /* '?' */
        showhelp = 1;
        break;
    }
  }

  if (showhelp || argc - optind < 1) {
    fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
        "[-f format] [-o somfile] <inputFile>\n", argv[0]);
    exit(EXIT_SUCCESS);
  }

  if (format == SND_PCM_FORMAT_UNKNOWN) {
    fprintf(stderr, "Unknown format\n");
    exit(EXIT_FAILURE);
  }

  if (somfile == NULL) {
    somfile = strdup(DEFAULT_SOMFILE);
  }

  // Load SOM model
  fprintf(stderr, "Read SOM from %s\n", somfile);
  FILE* file = fopen(somfile, "r");
  if (file == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", somfile,
        strerror(errno));
    exit(EXIT_FAILURE);
  }
  SOM* net = som_load(file);
  fclose(file);

  fprintf(stderr, "SOM Loaded (size: %d * %d, dimension: %d)\n",
      net->rows, net->cols, net->dims);
  assert(window_size / 2 + 1 == net->dims);

  // Load Data
  fprintf(stderr, "Loading Audio File %s (%d channel(s), %s)\n",
      argv[optind], channels, snd_pcm_format_name(format));
  FILE* input = fopen(argv[optind], "r");
  if (input == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind],
        strerror(errno));
    exit(EXIT_FAILURE);
  }

  double** data;
  unsigned long int count = pcm_size(input, channels, format);
  count = read_file(input, count, channels, format, &data);
  fprintf(stderr, "%lu samples read\n", count);
  fclose(input);

  // Transform
  fprintf(stderr, "Calculating STFT (windows size %d, shift %d)\n",
      window_size, shift);
  int nos = number_of_spectrum(count, window_size, shift);
  TimeFreq* tf = alloc_tf(window_size, nos);

  stft(data[0], count, window_size, shift, tf);
  free_data(data, channels);

  fprintf(stderr, "%d STFT Calculated\n", nos);

  // Mapping
  double* mspec = malloc(net->dims * sizeof(double));
  for (i = 0; i < nos; i++) {
    Spectra s = get_spectra(tf, i);
    for (j = 0; j < net->dims; j++) {
      mspec[j] = get_magnitude(s, j);
    }
    double dis;
    int id = som_map_dis(net, mspec, &dis);
    printf("%d ", id);
  }
  printf("\n");

  fprintf(stderr, "Mapping finished\n");

  // Free memory
  free_tf(tf);
  free(mspec);
  som_free(net);

  exit(EXIT_SUCCESS);
}
Esempio n. 3
0
int main(int argc, char** argv) {
  int showhelp = 0;

  unsigned int channels = 1;
  int window_size = 1024;
  int shift = 256;
  int rate = 8000;
  snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

  int opt;
  while ((opt = getopt(argc, argv, "hw:s:r:f:")) != -1) {
    switch (opt) {
      case 'h':
        showhelp = 1;
        break;
      case 'w':
        window_size = atoi(optarg);
        break;
      case 's':
        shift = atoi(optarg);
        break;
      case 'r':
        rate = atoi(optarg);
        break;
      case 'f':
        format = snd_pcm_format_value(optarg);
        break;
      default: /* '?' */
        showhelp = 1;
        break;
    }
  }

  if (showhelp || argc - optind < 1) {
    fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
        "[-r sampling rate] [-f format] <inputFile>\n", argv[0]);
    exit(EXIT_SUCCESS);
  }

  if (format == SND_PCM_FORMAT_UNKNOWN) {
    fprintf(stderr, "Unknown format\n");
    exit(EXIT_FAILURE);
  }
 
  FILE* input = fopen(argv[optind], "r");
  if (input == NULL) {
    fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind], strerror(errno));
    exit(EXIT_FAILURE);
  }

  // Load Audio File
  double** data;
  unsigned long int count = pcm_size(input, channels, format);
  count = read_file(input, count, channels, format, &data);
  fprintf(stderr, "%lu samples read\n", count);
  fclose(input);

  // Transform
  int nos = number_of_spectrum(count, window_size, shift);
  TimeFreq* tf = alloc_tf(window_size, nos);

  stft(data[0], count, window_size, shift, tf);

  printf("set style line 11 lc rgb '#808080' lt 1\n"
      "set border 3 front ls 11\n"
      "set tics nomirror out scale 0.75\n"
      "unset key\n"
      "unset colorbox\n"
      "set palette defined (0 '#000090', 1 '#000fff', 2 '#0090ff', 3 '#0fffee', 4 '#90ff70', 5 '#ffee00', 6 '#ff7000', 7 '#ee0000', 8 '#7f0000')\n"
      "set xlabel 'Time (s)'\n"
      "set ylabel 'Frequency (Hz)'\n");
  printf("set yrange [0:%g]\n", (double) rate / 2.0);
  printf("set xrange [0:%g]\n", (double) count / rate);
  printf("plot '-' matrix using (($2 + 0.5) * %g) : (($1 + 0.5) * %g) : (log($3))"
    " with image\n", (double) shift / rate, 
    (double) rate / window_size);
  int i, j;
  for (i = 0; i < nos; i++) {
    Spectra s = get_spectra(tf, i);
    for (j = 0; j < window_size / 2 + 1; j++) {
      printf("%g ", get_magnitude(s, j));
    }
    printf("\n");
  }
  printf("e\n");

  // Free memory
  free_data(data, channels);
  free_tf(tf);

  exit(EXIT_SUCCESS);
}
Esempio n. 4
0
int main(int argc, char **argv){
	myTimer timer;

	/****************************************************************************************************/
	// Command line parser:
	// All command line parameteres (including default values) are set in "opt."
	/****************************************************************************************************/
	command_line_options opt(argc, argv);

	// Alias to command line parameters
	int& frame_length = opt.frame_length;
	int& frame_shift = opt.frame_shift;

	/****************************************************************************************************/
	// input file
	/****************************************************************************************************/
	const int maximal_buffer_length = frame_length;
	wavistream *input = new wavistream( opt.input_file_name.c_str(), maximal_buffer_length);

	// Alias to input file parameters
	const unsigned int sampling_rate = input->header.sampling_rate;
	const short unsigned int I = input->header.n_channel;
	if( I != 1 && I != 2){
	 cerr << "input file is not monaural audio file! " << endl;
	 exit(1);
	}
	const int total_samples = 8 * (input->header.filesize-36) 	/ (input->header.n_channel * input->header.bits_per_sample);
	const int n_frame = total_samples / frame_shift + 3;
	const int n_freq = frame_length/2 + 1;


	/****************************************************************************************************/
	// Display the information.
	/****************************************************************************************************/
	if(opt.display_flag){
		cerr << "====================================" << endl;
		cerr << "SMOOTHNESS EVALUATION" << endl;
		cerr << "====================================" << endl;
		cerr << "INFORMATION on the input file" << endl;
		cerr << "Sampling Rate: "       << sampling_rate << " Hz" << endl;
		cerr << "Number of Channels: "  << I << endl;
		cerr << "Length of the Input File: " << (double)total_samples / sampling_rate << " [s] " << endl;
		cerr << "Total Samples: " << total_samples << endl;

		cerr << "===================================" << endl;
		cerr << "INFORMATION on STFT" << endl;
		cerr << "===================================" << endl;
		cerr << "n_frame: " << n_frame << endl;
		cerr << "n_freq: " << n_freq << endl;
		cerr << "frame_length: " << frame_length << endl;
		cerr << "frame_shift: " << frame_shift << endl;

		cerr << "===================================" << endl;
		cerr << "INFORMATION on parameters" << endl;
		cerr << "===================================" << endl;
		cerr << "gamma =  " << opt.gamma << endl;
		cerr << "M (= N' = K') = " << opt.M << endl;
	}
	/****************************************************************************************************/
	// STFT
	/****************************************************************************************************/
	//allocation
	twoDimArray<double> abs_spec(n_frame, n_freq);
	twoDimArray<std::complex<double> > phase_spec(n_frame, n_freq);

	// exec stft
	STFT stft(frame_length, frame_shift, n_frame, opt.win_func);
	stft.exec(input, &abs_spec, &phase_spec);
	
	// gamma
	exp_spectrum(&abs_spec, opt.gamma);

	cerr << "temporal  Smoothness function (4): " << smoothness_time(abs_spec, n_frame, n_freq, opt.M) << endl;
	cerr << "frequency Smoothness function (5): " << smoothness_freq(abs_spec, n_frame, n_freq, opt.M) << endl;
	cerr << "sum of energy: " << simple_sum(abs_spec, n_frame, n_freq) << endl;
	cerr << "Notes:" << endl;
	cerr << "\tThe values are small, the spectrogram is smooth." << endl;
	cerr << "\tThe scale of the values depends on not only the smoothness, but also the volume and the length of the input signal" << endl;
	cerr << "\tThe program can be used just for comparing temporal and frequency smoothness of a signal." << endl;
	cout << smoothness_time(abs_spec, n_frame, n_freq, opt.M) / simple_sum(abs_spec, n_frame, n_freq) << endl;
	cout << smoothness_freq(abs_spec, n_frame, n_freq, opt.M) / simple_sum(abs_spec, n_frame, n_freq) << endl;
	delete input;
	return 0;
}
Esempio n. 5
-1
double *test_stft_correctness(double *samples)
{
    double a, d, error;
    int i, count=0;

    for (i = 0; i < NUM_SAMPLES; i++) {
	c_re(in1[i]) = samples[i];
	c_im(in1[i]) = 0.0;

    }

    stft(NUM_SAMPLES, in1, out1);

          /* compute the relative error */
/*          error = 0.0;
          for (i = 0; i < NUM_SAMPLES; ++i) {
               a = sqrt((c_re(out1[i]) - c_re(out2[i])) *
                        (c_re(out1[i]) - c_re(out2[i])) +
                        (c_im(out1[i]) - c_im(out2[i])) *
                        (c_im(out1[i]) - c_im(out2[i])));
               d =  sqrt(c_re(out2[i]) * c_re(out2[i]) +
                         c_im(out2[i]) * c_im(out2[i]));
               if (d < -1.0e-10 || d > 1.0e-10)
                    a /= d;
               if (a > error)
                    error = a;
          }
          if (error > 1e-3) {
               printf("error=%e\n", error);
          }
*/
    for (i=0; i<NUM_SAMPLES; i++){
        power1[i] = sqrt(c_re(out1[i]) * c_re(out1[i]) + c_im(out1[i]) * c_im(out1[i]));
    }

return power1;

}
Esempio n. 6
-1
int main(int argc, char** argv) {
    int showhelp = 0;

    unsigned int channels = 1;
    int window_size = 1024;
    int shift = 256;
    snd_pcm_format_t format = SND_PCM_FORMAT_UNKNOWN;

    int opt;
    while ((opt = getopt(argc, argv, "hw:s:f:")) != -1) {
        switch (opt) {
        case 'h':
            showhelp = 1;
            break;
        case 'w':
            window_size = atoi(optarg);
            break;
        case 's':
            shift = atoi(optarg);
            break;
        case 'f':
            format = snd_pcm_format_value(optarg);
            break;
        default: /* '?' */
            showhelp = 1;
            break;
        }
    }

    if (showhelp || argc - optind < 2) {
        fprintf(stderr, "Usage: %s [-w window_size] [-s shift] "
                "[-f format] <inputFile> <outputFile>\n" , argv[0]);
        exit(EXIT_SUCCESS);
    }

    if (format == SND_PCM_FORMAT_UNKNOWN) {
        fprintf(stderr, "Unknown format\n");
        exit(EXIT_FAILURE);
    }

    // Load Audio File
    FILE* input = fopen(argv[optind], "r");
    if (input == NULL) {
        fprintf(stderr, "Cannot Open File %s : %s\n", argv[optind],
                strerror(errno));
        exit(EXIT_FAILURE);
    }

    double** data;
    unsigned long int count = pcm_size(input, channels, format);
    count = read_file(input, count, channels, format, &data);
    fprintf(stderr, "%lu samples read\n", count);
    fclose(input);

    // Transform
    int nos = number_of_spectrum(count, window_size, shift);
    TimeFreq* tf = alloc_tf(window_size, nos);
    stft(data[0], count, window_size, shift, tf);
    free(data[0]);

    // Spectral subtraction
    TimeFreq* ntf = alloc_tf(window_size, nos);
    do_filter(zerophase, NULL, tf, ntf);

    // Inverse transform
    count = istft_size(tf);
    data[0] = malloc(count * sizeof(double));
    istft(ntf, data[0]);

    FILE* output = fopen(argv[optind + 1], "w");
    if (output == NULL) {
        fprintf(stderr, "Cannot Open File %s : %s\n",
                argv[optind + 1], strerror(errno));
        exit(EXIT_FAILURE);
    }

    count = write_file(output, count, channels, format, data);
    fclose(output);

    free_data(data, channels);
    free_tf(tf);
    free_tf(ntf);

    exit(EXIT_SUCCESS);
}