diff --git a/dsp_lib.h b/dsp_lib.h new file mode 100644 index 0000000..3cfe7b9 --- /dev/null +++ b/dsp_lib.h @@ -0,0 +1,36 @@ +#pragma once + +#include + +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 +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 lpf_y[SND_PCHAN]; + Lowpass, 4> lpf[SND_PCHAN]; int lpf_count[SND_PCHAN] = { 0 }; nfds_t nfds = sio_nfds(hdl); @@ -75,21 +77,18 @@ static void dsp_thread() poll(pfd, nfds, INFTIM); int revents = sio_revents(hdl, pfd); - uint32_t ftw[SND_PCHAN]; double lpf_k[SND_PCHAN]; for(int i=0;i rotated; rotated = sample*std::polar(scale, phase_in[i]*2.0*M_PI/(double)UINT32_MAX); - phase_in[i] -= ftw[i]; // wraps on underflow + phase_in[i] -= dds[i].ftw; // wraps on underflow - lpf_y[i] += (rotated - lpf_y[i])*lpf_k[i]; - double mag = std::abs(lpf_y[i]); + double mag = std::abs(lpf[i].update(rotated)); lpf_count[i]++; if(lpf_count[i] == 200) {