diff --git a/README.md b/README.md index 16a366d..38f63be 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,10 @@ TYPE out[NUM]; fwd(out, history(another_value)); ``` +### [delay.hh](delay.hh) + +A [Digital delay line](https://en.wikipedia.org/wiki/Digital_delay_line) can be used to align signals with different delays - after filtering, for example. + ### [calculus.hh](calculus.hh) Some [calculus](https://en.wikipedia.org/wiki/Calculus) functions: diff --git a/delay.hh b/delay.hh new file mode 100644 index 0000000..8206b64 --- /dev/null +++ b/delay.hh @@ -0,0 +1,33 @@ +/* +Digital delay line + +Copyright 2020 Ahmet Inan +*/ + +#pragma once + +namespace DSP { + +template +class Delay +{ + TYPE buf[NUM]; + int pos; +public: + Delay() : pos(0) + { + for (int i = 0; i < NUM; ++i) + buf[i] = 0; + } + TYPE operator () (TYPE input) + { + TYPE tmp = buf[pos]; + buf[pos] = input; + if (++pos >= NUM) + pos = 0; + return tmp; + } +}; + +} +