172 lines
7.9 KiB
C
172 lines
7.9 KiB
C
/*
|
|
* Общий расчёт FFT: оценка частоты дискретизации по времени, ресемплинг, фильтр, окно и
|
|
* спектр. Выход — односторонняя пиковая амплитуда; DC и частота Найквиста не удваиваются.
|
|
* Временные метки задаются в секундах.
|
|
*/
|
|
|
|
#include "set_spectrum.h"
|
|
#include <math.h>
|
|
#include <stdlib.h>
|
|
|
|
#define PI 3.14159265358979323846
|
|
|
|
static int compare_double(const void *left, const void *right)
|
|
{
|
|
const double a = *(const double *)left;
|
|
const double b = *(const double *)right;
|
|
return (a > b) - (a < b);
|
|
}
|
|
|
|
static double window_value(int window, size_t index, size_t n)
|
|
{
|
|
double phase = 2.0 * PI * (double)index / (double)n;
|
|
switch (window) {
|
|
case SET_WINDOW_HANN: return 0.5 - 0.5 * cos(phase);
|
|
case SET_WINDOW_HAMMING: return 0.54 - 0.46 * cos(phase);
|
|
case SET_WINDOW_BLACKMAN: return 0.42 - 0.5 * cos(phase) + 0.08 * cos(2.0 * phase);
|
|
case SET_WINDOW_FLATTOP: return 0.21557895 - 0.41663158 * cos(phase) +
|
|
0.277263158 * cos(2.0 * phase) - 0.083578947 * cos(3.0 * phase) + 0.006947368 * cos(4.0 * phase);
|
|
default: return 1.0;
|
|
}
|
|
}
|
|
|
|
/* RBJ biquads, https://www.w3.org/TR/audio-eq-cookbook/ (LP/HP: Q=1/sqrt(2)). */
|
|
static void filter_block(double *data, size_t n, double fs, double cutoff, int kind)
|
|
{
|
|
double w = 2.0 * PI * cutoff / fs, c = cos(w);
|
|
double alpha = sin(w) / (2.0 * (kind == SET_FILTER_NOTCH ? 30.0 : sqrt(0.5)));
|
|
double a0 = 1.0 + alpha, a1 = -2.0 * c / a0, a2 = (1.0 - alpha) / a0;
|
|
double b0, b1, b2;
|
|
if (kind == SET_FILTER_LOW_PASS) {
|
|
b0 = (1.0 - c) / (2.0 * a0); b1 = 2.0 * b0; b2 = b0;
|
|
} else if (kind == SET_FILTER_HIGH_PASS) {
|
|
b0 = (1.0 + c) / (2.0 * a0); b1 = -2.0 * b0; b2 = b0;
|
|
} else { b0 = 1.0 / a0; b1 = -2.0 * c / a0; b2 = b0; }
|
|
double x1 = data[0], x2 = x1;
|
|
double y1 = kind == SET_FILTER_HIGH_PASS ? 0.0 : x1, y2 = y1;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
double x = data[i], y = b0 * x + b1 * x1 + b2 * x2 - a1 * y1 - a2 * y2;
|
|
data[i] = y; x2 = x1; x1 = x; y2 = y1; y1 = y;
|
|
}
|
|
}
|
|
|
|
static void fft(double *re, double *im, size_t n)
|
|
{
|
|
for (size_t i = 1, j = 0; i < n; ++i) {
|
|
size_t bit = n >> 1;
|
|
for (; j & bit; bit >>= 1) j ^= bit;
|
|
j ^= bit;
|
|
if (i < j) { double tmp = re[i]; re[i] = re[j]; re[j] = tmp; }
|
|
}
|
|
for (size_t length = 2; length <= n; length <<= 1) {
|
|
double angle = -2.0 * PI / (double)length, wr = cos(angle), wi = sin(angle);
|
|
for (size_t start = 0; start < n; start += length) {
|
|
double tr = 1.0, ti = 0.0;
|
|
for (size_t k = 0; k < length / 2; ++k) {
|
|
size_t a = start + k, b = a + length / 2;
|
|
double br = re[b] * tr - im[b] * ti, bi = re[b] * ti + im[b] * tr;
|
|
re[b] = re[a] - br; im[b] = im[a] - bi; re[a] += br; im[a] += bi;
|
|
double next = tr * wr - ti * wi;
|
|
ti = tr * wi + ti * wr; tr = next;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int set_spectrum_analyze(const double *times, const double *values, size_t count,
|
|
size_t max_size, int window, int filter, double low_hz, double high_hz, int remove_mean,
|
|
double *amplitudes, size_t capacity, double *meta)
|
|
{
|
|
if (meta == NULL) return SET_SPECTRUM_INVALID;
|
|
meta[0] = meta[1] = meta[2] = 0.0;
|
|
if (times == NULL || values == NULL || amplitudes == NULL || max_size < 16 ||
|
|
max_size > SET_SPECTRUM_MAX || (max_size & (max_size - 1)) != 0 ||
|
|
window < SET_WINDOW_RECT || window > SET_WINDOW_FLATTOP ||
|
|
filter < SET_FILTER_NONE || filter > SET_FILTER_NOTCH) return SET_SPECTRUM_INVALID;
|
|
if (count < 16) return SET_SPECTRUM_SHORT;
|
|
size_t n = 16;
|
|
while (n * 2 <= count && n * 2 <= max_size) n *= 2;
|
|
if (capacity < n / 2 + 1) return SET_SPECTRUM_INVALID;
|
|
times += count - n; values += count - n;
|
|
double dt = (times[n - 1] - times[0]) / (double)(n - 1);
|
|
if (!isfinite(dt) || dt <= 0.0) return SET_SPECTRUM_TIMING;
|
|
double fs = 1.0 / dt, jitter = 0.0;
|
|
if (!isfinite(fs)) return SET_SPECTRUM_TIMING;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
if (!isfinite(values[i]) || !isfinite(times[i])) return SET_SPECTRUM_INVALID;
|
|
if (i > 0) {
|
|
double step = times[i] - times[i - 1];
|
|
if (step <= 0.0) return SET_SPECTRUM_TIMING;
|
|
double relative = fabs(step / dt - 1.0);
|
|
if (relative > jitter) jitter = relative;
|
|
}
|
|
}
|
|
meta[0] = (double)n; meta[1] = fs; meta[2] = jitter;
|
|
if (jitter > 0.5) return SET_SPECTRUM_TIMING;
|
|
int need_low = filter == SET_FILTER_HIGH_PASS || filter == SET_FILTER_BAND_PASS || filter == SET_FILTER_NOTCH;
|
|
int need_high = filter == SET_FILTER_LOW_PASS || filter == SET_FILTER_BAND_PASS;
|
|
if ((need_low && (!isfinite(low_hz) || low_hz <= 0.0 || low_hz >= fs / 2.0)) ||
|
|
(need_high && (!isfinite(high_hz) || high_hz <= 0.0 || high_hz >= fs / 2.0)) ||
|
|
(filter == SET_FILTER_BAND_PASS && low_hz >= high_hz)) return SET_SPECTRUM_CUTOFF;
|
|
double *scratch = (double *)calloc(n * 2, sizeof(double));
|
|
if (scratch == NULL) return SET_SPECTRUM_MEMORY;
|
|
double *re = scratch, *im = scratch + n, mean = 0.0;
|
|
size_t right = 1;
|
|
for (size_t i = 0; i < n; ++i) {
|
|
double time = times[0] + (double)i * dt;
|
|
while (right < n - 1 && times[right] < time) ++right;
|
|
double fraction = (time - times[right - 1]) / (times[right] - times[right - 1]);
|
|
fraction = fmax(0.0, fmin(1.0, fraction));
|
|
re[i] = values[right - 1] * (1.0 - fraction) + values[right] * fraction;
|
|
mean += re[i] / (double)n;
|
|
}
|
|
if (remove_mean) for (size_t i = 0; i < n; ++i) re[i] -= mean;
|
|
if (filter == SET_FILTER_HIGH_PASS || filter == SET_FILTER_BAND_PASS)
|
|
filter_block(re, n, fs, low_hz, SET_FILTER_HIGH_PASS);
|
|
if (filter == SET_FILTER_LOW_PASS || filter == SET_FILTER_BAND_PASS)
|
|
filter_block(re, n, fs, high_hz, SET_FILTER_LOW_PASS);
|
|
if (filter == SET_FILTER_NOTCH) filter_block(re, n, fs, low_hz, SET_FILTER_NOTCH);
|
|
double gain = 0.0;
|
|
for (size_t i = 0; i < n; ++i) { double w = window_value(window, i, n); gain += w; re[i] *= w; }
|
|
fft(re, im, n);
|
|
int result = SET_SPECTRUM_OK;
|
|
for (size_t i = 0; i <= n / 2; ++i) {
|
|
amplitudes[i] = hypot(re[i], im[i]) / gain * ((i == 0 || i == n / 2) ? 1.0 : 2.0);
|
|
if (!isfinite(amplitudes[i])) result = SET_SPECTRUM_INVALID;
|
|
}
|
|
free(scratch);
|
|
return result;
|
|
}
|
|
|
|
int set_spectrum_dominant_peak(const double *amplitudes, size_t count,
|
|
double bin_hz, double relative_threshold, double absolute_floor,
|
|
double *scratch, size_t scratch_capacity, double *peak, size_t peak_capacity)
|
|
{
|
|
size_t i, usable = 0U, peak_bin = 0U;
|
|
double peak_amplitude = -1.0, median, threshold;
|
|
if (amplitudes == NULL || scratch == NULL || peak == NULL || count < 3U ||
|
|
scratch_capacity < count - 1U || peak_capacity < 2U || !isfinite(bin_hz) ||
|
|
bin_hz <= 0.0 || !isfinite(relative_threshold) || relative_threshold <= 0.0 ||
|
|
!isfinite(absolute_floor) || absolute_floor < 0.0) return -1;
|
|
for (i = 1U; i < count; ++i) {
|
|
if (isfinite(amplitudes[i]) && amplitudes[i] >= 0.0)
|
|
scratch[usable++] = amplitudes[i];
|
|
}
|
|
if (usable < 2U) return 0;
|
|
qsort(scratch, usable, sizeof(double), compare_double);
|
|
median = scratch[usable / 2U];
|
|
for (i = 1U; i + 1U < count; ++i) {
|
|
const double value = amplitudes[i];
|
|
if (isfinite(value) && isfinite(amplitudes[i - 1U]) && isfinite(amplitudes[i + 1U]) &&
|
|
value >= amplitudes[i - 1U] && value > amplitudes[i + 1U] && value > peak_amplitude) {
|
|
peak_bin = i;
|
|
peak_amplitude = value;
|
|
}
|
|
}
|
|
threshold = fmax(absolute_floor, median * relative_threshold);
|
|
if (peak_bin == 0U || peak_amplitude < threshold) return 0;
|
|
peak[0] = (double)peak_bin * bin_hz;
|
|
peak[1] = peak_amplitude;
|
|
return 1;
|
|
}
|