Example #1
0
File: parse.c Project: NetSys/sts
void
parse_dir (const char *outdir, const char *tfdir, const char *name)
{
  printf ("Parsing: ");
  fflush (stdout);

  struct parse_ntf *ntf;
  struct parse_tf *ttf;
  int stages;

  char buf[PATH_MAX + 1];
  snprintf (buf, sizeof buf, "%s/%s", tfdir, name);
  char *base = buf + strlen (buf);

  strcpy (base, "/stages");
  FILE *f = fopen (buf, "r");
  if (!f) err (1, "Can't open %s", buf);
  if (!fscanf (f, "%d", &stages)) errx (1, "Can't read NTF stages from %s", buf);
  fclose (f);

  *base = 0;
  struct dirent **tfs;
  int n = scandir (buf, &tfs, filter_tfs, alphasort);
  if (n <= 0) err (1, "Couldn't find .tf files in %s", buf);

  ntf = xmalloc (sizeof *ntf + n * sizeof *ntf->tfs);
  ntf->ntfs = n;
  ntf->stages = stages;
  *base = '/';
  for (int i = 0; i < n; i++) {
    strcpy (base + 1, tfs[i]->d_name);
    free (tfs[i]);
    struct parse_tf *tf = parse_tf (buf);
    assert (tf);
    ntf->tfs[i] = tf;
  }
  free (tfs);

  strcpy (base, "/topology.tf");
  ttf = parse_tf (buf);
  assert (ttf);
  printf ("done\n");

  snprintf (buf, sizeof buf, "%s/%s.dat", outdir, name);
  data_gen (buf, ntf, ttf);

  free_ntf (ntf);
  free_tf (ttf);
}
Example #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);
}
Example #3
0
File: parse.c Project: NetSys/sts
static void
free_ntf (struct parse_ntf *ntf)
{
  for (int i = 0; i < ntf->ntfs; i++) free_tf (ntf->tfs[i]);
  free (ntf);
}
Example #4
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);
}
Example #5
-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);
}