From d2b1bf32b46f1b08ac6e767ccf46cadde2545257 Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Fri, 8 Feb 2019 20:21:42 +0100 Subject: [PATCH] added AM demodulator with AGC --- README.md | 4 ++++ amd.hh | 43 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 amd.hh diff --git a/README.md b/README.md index 17cb821..cff171b 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,10 @@ The following [infinite impulse response](https://en.wikipedia.org/wiki/Infinite [Frequency modulation](https://en.wikipedia.org/wiki/Frequency_modulation) [demodulation](https://en.wikipedia.org/wiki/Demodulation) with and without [atan2](https://en.wikipedia.org/wiki/Atan2). +### [amd.hh](amd.hh) + +[Amplitude modulation](https://en.wikipedia.org/wiki/Amplitude_modulation) [demodulation](https://en.wikipedia.org/wiki/Demodulation) with [automatic gain control](https://en.wikipedia.org/wiki/Automatic_gain_control). + ### [atan2.hh](atan2.hh) [atan](https://en.wikipedia.org/wiki/Inverse_trigonometric_functions) and [atan2](https://en.wikipedia.org/wiki/Atan2). diff --git a/amd.hh b/amd.hh new file mode 100644 index 0000000..1048686 --- /dev/null +++ b/amd.hh @@ -0,0 +1,43 @@ +/* +AM Demodulation + +Copyright 2019 Ahmet Inan +*/ + +#pragma once + +namespace DSP { + +template +class AMD1 +{ + typedef TYPE complex_type; + typedef typename complex_type::value_type value_type; + value_type avg, att, dec, lo, hi, idx; +public: + AMD1() : avg(0), att(0), dec(0), lo(0.001), hi(1), idx(1) + { + } + void index(value_type modidx) + { + idx = modidx; + } + void agc(value_type attack, value_type decay, value_type low = value_type(0.001), value_type high = value_type(1)) + { + att = attack; + dec = decay; + lo = low; + hi = high; + } + value_type operator()(complex_type input) + { + value_type amp = abs(input); + avg = lerp(amp < avg ? dec : att, avg, amp); + avg = min(max(avg, lo), hi); + amp /= avg; + return (amp * (value_type(1)+idx) - value_type(1)) / idx; + } +}; + +} +