• Home
  • Raw
  • Download

Lines Matching full:bat

30 #include "bat-signal.h"
32 static void check_amplitude(struct bat *bat, float *buf) in check_amplitude() argument
38 for (i = 0, sum = 0.0, average = 0.0; i < bat->frames; i++) in check_amplitude()
40 average = sum / bat->frames; in check_amplitude()
43 for (i = 0, sum = 0.0; i < bat->frames; i++) in check_amplitude()
45 amplitude = sum / bat->frames * M_PI / 2.0; in check_amplitude()
48 percent = amplitude * 100 / ((1 << ((bat->sample_size << 3) - 1)) - 1); in check_amplitude()
50 fprintf(bat->log, _("Amplitude: %.1f; Percentage: [%d]\n"), in check_amplitude()
53 fprintf(bat->err, _("ERROR: Amplitude can't be negative!\n")); in check_amplitude()
55 fprintf(bat->err, _("WARNING: Signal too weak!\n")); in check_amplitude()
57 fprintf(bat->err, _("WARNING: Signal overflow!\n")); in check_amplitude()
66 int check_peak(struct bat *bat, struct analyze *a, int end, int peak, float hz, in check_peak() argument
71 float delta_rate = DELTA_RATE * bat->target_freq[channel]; in check_peak()
75 fprintf(bat->log, _("Detected peak at %2.2f Hz of %2.2f dB\n"), hz_peak, in check_peak()
77 fprintf(bat->log, _(" Total %3.1f dB from %2.2f to %2.2f Hz\n"), in check_peak()
81 fprintf(bat->err, _(" WARNING: Found low peak %2.2f Hz,"), in check_peak()
83 fprintf(bat->err, _(" very close to DC\n")); in check_peak()
85 } else if (hz_peak < bat->target_freq[channel] - tolerance) { in check_peak()
86 fprintf(bat->err, _(" FAIL: Peak freq too low %2.2f Hz\n"), in check_peak()
89 } else if (hz_peak > bat->target_freq[channel] + tolerance) { in check_peak()
90 fprintf(bat->err, _(" FAIL: Peak freq too high %2.2f Hz\n"), in check_peak()
94 fprintf(bat->log, _(" PASS: Peak detected")); in check_peak()
95 fprintf(bat->log, _(" at target frequency\n")); in check_peak()
105 static int check(struct bat *bat, struct analyze *a, int channel) in check() argument
107 float hz = 1.0 / ((float) bat->frames / (float) bat->rate); in check()
110 int err = 0, N = bat->frames / 2; in check()
128 if (a->mag[i] > mean + bat->sigma_k * sigma) { in check()
142 err |= check_peak(bat, a, end, peak, hz, mean, in check()
158 fprintf(bat->log, _("Detected at least %d signal(s) in total\n"), in check()
164 static void calc_magnitude(struct bat *bat, struct analyze *a, int N) in calc_magnitude() argument
178 static int find_and_check_harmonics(struct bat *bat, struct analyze *a, in find_and_check_harmonics() argument
182 int err = -ENOMEM, N = bat->frames; in find_and_check_harmonics()
185 a->in = (float *) fftwf_malloc(sizeof(float) * bat->frames); in find_and_check_harmonics()
189 a->out = (float *) fftwf_malloc(sizeof(float) * bat->frames); in find_and_check_harmonics()
193 a->mag = (float *) fftwf_malloc(sizeof(float) * bat->frames); in find_and_check_harmonics()
204 bat->convert_sample_to_float(a->buf, a->in, bat->frames); in find_and_check_harmonics()
207 check_amplitude(bat, a->in); in find_and_check_harmonics()
213 calc_magnitude(bat, a, N); in find_and_check_harmonics()
216 err = check(bat, a, channel); in find_and_check_harmonics()
230 static int calculate_noise_one_period(struct bat *bat, in calculate_noise_one_period() argument
290 static int calculate_noise(struct bat *bat, float *src, int channel) in calculate_noise() argument
294 float freq = bat->target_freq[channel]; in calculate_noise()
298 int nsamples = (int) ceilf(bat->rate / freq); in calculate_noise()
303 int nsection = bat->frames / nsamples - 1; in calculate_noise()
305 fprintf(bat->log, _("samples per period: %d\n"), nsamples); in calculate_noise()
306 fprintf(bat->log, _("total sections to detect: %d\n"), nsection); in calculate_noise()
320 err = generate_sine_wave_raw_mono(bat, na.target, freq, nsamples); in calculate_noise()
337 err = calculate_noise_one_period(bat, &na, src + offset, in calculate_noise()
342 if (na.snr_db > bat->snr_thd_db) { in calculate_noise()
352 fprintf(bat->err, _("Noise detected at %d points.\n"), in calculate_noise()
358 fprintf(bat->log, _("No noise detected.\n")); in calculate_noise()
363 fprintf(bat->log, _("Average SNR is %.2f dB (%.2f %%) at %d points.\n"), in calculate_noise()
374 static int find_and_check_noise(struct bat *bat, void *buf, int channel) in find_and_check_noise() argument
379 source = (float *)malloc(sizeof(float) * bat->frames); in find_and_check_noise()
384 bat->convert_sample_to_float(buf, source, bat->frames); in find_and_check_noise()
387 err = calculate_noise(bat, source, channel); in find_and_check_noise()
396 static int reorder_data(struct bat *bat) in reorder_data() argument
401 if (bat->channels == 1) in reorder_data()
404 p = malloc(bat->frames * bat->frame_size); in reorder_data()
409 for (ch = 0; ch < bat->channels; ch++) { in reorder_data()
410 for (j = 0; j < bat->frames; j++) { in reorder_data()
411 for (i = 0; i < bat->sample_size; i++) { in reorder_data()
412 *p++ = ((char *) (bat->buf))[j * bat->frame_size in reorder_data()
413 + ch * bat->sample_size + i]; in reorder_data()
418 free(bat->buf); in reorder_data()
419 bat->buf = new_bat_buf; in reorder_data()
425 static int truncate_frames(struct bat *bat) in truncate_frames() argument
430 if (bat->frames & (1 << shift)) { in truncate_frames()
431 bat->frames = 1 << shift; in truncate_frames()
438 int analyze_capture(struct bat *bat) in analyze_capture() argument
445 err = truncate_frames(bat); in analyze_capture()
447 fprintf(bat->err, _("Invalid frame number for analysis: %d\n"), in analyze_capture()
448 bat->frames); in analyze_capture()
452 fprintf(bat->log, _("\nBAT analysis: signal has %d frames at %d Hz,"), in analyze_capture()
453 bat->frames, bat->rate); in analyze_capture()
454 fprintf(bat->log, _(" %d channels, %d bytes per sample.\n"), in analyze_capture()
455 bat->channels, bat->sample_size); in analyze_capture()
457 bat->buf = malloc(bat->frames * bat->frame_size); in analyze_capture()
458 if (bat->buf == NULL) in analyze_capture()
461 bat->fp = fopen(bat->capture.file, "rb"); in analyze_capture()
463 if (bat->fp == NULL) { in analyze_capture()
464 fprintf(bat->err, _("Cannot open file: %s %d\n"), in analyze_capture()
465 bat->capture.file, err); in analyze_capture()
470 err = read_wav_header(bat, bat->capture.file, bat->fp, true); in analyze_capture()
474 items = fread(bat->buf, bat->frame_size, bat->frames, bat->fp); in analyze_capture()
475 if (items != bat->frames) { in analyze_capture()
480 err = reorder_data(bat); in analyze_capture()
484 for (c = 0; c < bat->channels; c++) { in analyze_capture()
485 fprintf(bat->log, _("\nChannel %i - "), c + 1); in analyze_capture()
486 fprintf(bat->log, _("Checking for target frequency %2.2f Hz\n"), in analyze_capture()
487 bat->target_freq[c]); in analyze_capture()
488 a.buf = bat->buf + in analyze_capture()
489 c * bat->frames * bat->frame_size in analyze_capture()
490 / bat->channels; in analyze_capture()
491 if (!bat->standalone) { in analyze_capture()
492 err = find_and_check_harmonics(bat, &a, c); in analyze_capture()
497 if (snr_is_valid(bat->snr_thd_db)) { in analyze_capture()
498 fprintf(bat->log, _("\nChecking for SNR: ")); in analyze_capture()
499 fprintf(bat->log, _("Threshold is %.2f dB (%.2f%%)\n"), in analyze_capture()
500 bat->snr_thd_db, 100.0 in analyze_capture()
501 / powf(10.0, bat->snr_thd_db / 20.0)); in analyze_capture()
502 err = find_and_check_noise(bat, a.buf, c); in analyze_capture()
509 fclose(bat->fp); in analyze_capture()
511 free(bat->buf); in analyze_capture()