PeakList* find_peaks(int signal_size, sample* signal, MQParameters* params) { int i; int num_peaks = 0; sample prev_amp, current_amp, next_amp; PeakList* peak_list = (PeakList*)malloc(sizeof(PeakList)); peak_list->next = NULL; peak_list->prev = NULL; peak_list->peak = NULL; // take fft of the signal memcpy(params->fft_in, signal, sizeof(sample)*params->frame_size); for(i = 0; i < params->frame_size; i++) { params->fft_in[i] *= params->window[i]; } fftw_execute(params->fft_plan); // get initial magnitudes prev_amp = get_magnitude(params->fft_out[0][0], params->fft_out[0][1]); current_amp = get_magnitude(params->fft_out[1][0], params->fft_out[1][1]); // find all peaks in the amplitude spectrum for(i = 1; i < params->num_bins-1; i++) { next_amp = get_magnitude(params->fft_out[i+1][0], params->fft_out[i+1][1]); if((current_amp > prev_amp) && (current_amp > next_amp) && (current_amp > params->peak_threshold)) { Peak* p = (Peak*)malloc(sizeof(Peak)); p->amplitude = current_amp; p->frequency = i * params->fundamental; p->phase = get_phase(params->fft_out[i][0], params->fft_out[i][1]); p->bin = i; p->next = NULL; p->prev = NULL; add_peak(p, peak_list); num_peaks++; } prev_amp = current_amp; current_amp = next_amp; } // limit peaks to a maximum of max_peaks if(num_peaks > params->max_peaks) { PeakList* current = peak_list; for(i = 0; i < params->max_peaks-1; i++) { current = current->next; } delete_peak_list(current->next); current->next = NULL; num_peaks = params->max_peaks; } return sort_peaks_by_frequency(peak_list, num_peaks); }
void Vector3<T>::normalize() { float m = get_magnitude(); x /= m; y /= m; z /= m; }
void zerophase(int window_size, void* args, Spectra source, Spectra result) { gsl_rng* r = gsl_rng_alloc(gsl_rng_default); int i; for (i = 0; i < window_size / 2 + 1; i++) { set_by_polar(result, i, get_magnitude(source, i), gsl_rng_uniform (r) * 2 * M_PI); fprintf(stderr, "%f\n", get_phase(result, i)); if (i > 0 && i < window_size / 2) { set_value(result, window_size - i, get_real(result, i), get_imag(result, i)); } } }
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); }
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); }
vec2d vec2d::normalized() const { float magnitude = get_magnitude(); return vec2d(x/magnitude, y/magnitude); }