Files
libpasada/pasada-lib/include/pd_signal.h
2026-05-20 00:10:37 +02:00

79 lines
3.1 KiB
C++

//
// Created by david on 04.03.2026.
//
#ifndef PASADASUPERPROJECT_SIGNAL_H
#define PASADASUPERPROJECT_SIGNAL_H
#include <vector>
#include <deque>
#include <complex>
namespace pd_signal {
using cplx = std::complex<double>;
/** `num` evenly spaced numbers over interval [start,stop] */
void linspace(std::vector<double>& data, double start, double stop, int num);
/** `num` evenly spaced numbers over interval [start,stop] with endpoint=true or [start,stop) with endpoint=false */
void linspace(std::vector<double>& data, double start, double stop, int num, bool endpoint);
/**
* Evaluate at points x the function given by the samples fp[xp[n]].
* Returned in y.
*/
void interp(std::vector<double>& y, std::vector<double>& x, std::vector<double>& xp, std::vector<double>& fp);
/** resample to BEAT_LEN */
void resample(std::vector<double> &out, std::vector<double> &x, int beat_len);
/**
* normalized cross-correlation of the two signals of same length.
* normalization factor is <c>1 / sqrt(\sum_i x_i^2 * \sum_i y_i^2)</c>
*/
double crossCorr(std::vector<double> &x, std::vector<double> &y);
/** clip 'val' to between 'a_min' and 'a_max'. */
double clip(double val, double a_min, double a_max);
/** two-dimensional mean of a collection of signals */
void mean(std::vector<double> &out, std::vector<std::vector<double> >& m);
/** two-dimensional mean of a collection of signals */
void mean(std::vector<double> &out, std::deque<std::vector<double> >& m);
/** simple mean of 1-d signal */
double mean(const std::vector<double>& in);
void diff(std::vector<double>& out, const std::vector<double>& in);
std::vector<double> gauss(size_t N, double mu, double sigma);
/**
* Convolution of two polynomials given in ASCENDING power order.
* If <c>p = p_0 + p_1 x + ... + p_{P-1} x^{P-1}</c> and likewise for q,
* then <c>out = p * q</c> in ascending power order, of length P+Q-1.
*/
void polymul(std::vector<cplx>& out,
const std::vector<cplx>& p, const std::vector<cplx>& q);
/**
* Build a monic polynomial from its roots:
* <c>(x - r_0) (x - r_1) ... (x - r_{N-1})</c>.
* Returned in DESCENDING power order, i.e. <c>out[0]=1, ..., out[N]</c>
* is the constant term. Length is <c>roots.size() + 1</c>.
*/
void poly(std::vector<cplx>& out, const std::vector<cplx>& roots);
/**
* Design an N-th order Butterworth IIR high-pass digital filter via the
* bilinear transform. The passband is normalized to unit gain at Nyquist.
*
* @param b numerator coefficients in DESCENDING powers of z (length N+1)
* @param a denominator coefficients in DESCENDING powers of z (length N+1)
* @param N filter order (>= 1)
* @param fc cutoff frequency of the digital filter in Hz (0 < fc < fs/2)
* @param fs sampling frequency in Hz
*/
void iirHighpass(std::vector<double>& b, std::vector<double>& a,
int N, double fc, double fs);
}
#endif //PASADASUPERPROJECT_SIGNAL_H