sndlock/dsp_lib.h

37 lines
751 B
C
Raw Normal View History

2024-12-30 23:45:14 +08:00
#pragma once
#include <cstdint>
class DDS {
private:
uint32_t phase = 0;
public:
uint32_t ftw = 0;
void set_frequency(double f) {
ftw = f*(double)UINT32_MAX;
}
double get() {
phase += ftw; // wraps on overflow
return sin(phase*2.0*M_PI/(double)UINT32_MAX);
}
};
template<typename T, unsigned int N>
class Lowpass {
private:
double k = 0.0;
T s[N] = {};
public:
void set_bandwidth(double f) {
k = 2.0*M_PI*f;
}
T update(T x) {
T a = x;
for(int i=0;i<N;i++) {
s[i] += (a - s[i])*k;
a = s[i];
}
return a;
}
};