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