From de961853c7d1724515261232684e72600b711408 Mon Sep 17 00:00:00 2001 From: Geoffrey Merck Date: Mon, 3 Feb 2020 20:52:14 +0100 Subject: [PATCH] Add Filtering and defines Add FIR passband Filtering Add defines to swithc features on and off --- ambed/cfirfilter.cpp | 74 +++++++++++++++++++++++++++++++++++++ ambed/cfirfilter.h | 52 ++++++++++++++++++++++++++ ambed/cusb3xxxinterface.cpp | 9 ++++- ambed/cvocodecchannel.cpp | 24 +++++++++++- ambed/cvocodecchannel.h | 17 ++++++++- ambed/main.h | 44 +++++++++++++++++++++- 6 files changed, 214 insertions(+), 6 deletions(-) create mode 100644 ambed/cfirfilter.cpp create mode 100644 ambed/cfirfilter.h diff --git a/ambed/cfirfilter.cpp b/ambed/cfirfilter.cpp new file mode 100644 index 0000000..602e01c --- /dev/null +++ b/ambed/cfirfilter.cpp @@ -0,0 +1,74 @@ +// +// cfirfilter.cpp +// ambed +// +// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017. +// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// +// ---------------------------------------------------------------------------- +// This file is part of ambed. +// +// xlxd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// xlxd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Foobar. If not, see . +// ---------------------------------------------------------------------------- +// FIRFilter by Geoffrey Merck F4FXL / KC3FRA + +#include "cfirfilter.h" +#include + +CFIRFilter::CFIRFilter(const float* taps, int tapsLength) +{ + m_taps = new float[tapsLength]; + m_buffer = new float[tapsLength]; + + ::memcpy(m_taps, taps, tapsLength * sizeof(float)); + ::memset(m_buffer, 0, tapsLength * sizeof(float)); + m_currentBufferPostion = 0; +} + +CFIRFilter::~CFIRFilter() +{ + delete[] m_taps; + delete[] m_buffer; +} + +inline float CFIRFilter::Process(float inputSample) +{ + // Buffer latest sample + m_buffer[m_currentBufferPostion] = inputSample; + + float outputSample = 0.0f; + for(int i = 0; i < m_tapsLength; i++) + { + outputSample += m_taps[i] * m_buffer[(m_currentBufferPostion + i) % m_tapsLength]; + } + + m_currentBufferPostion = (m_currentBufferPostion + 1) % m_tapsLength; + + return outputSample; +} + +void CFIRFilter::Process(uint8* voice, int length) +{ + for(int i = 0; i < length; i++) + { + //Get the sample + float input = (float)(short)MAKEWORD(voice[i+1], voice[i]); + + float output = Process(input); + + //write processed sample back + voice[i] = HIBYTE((short)output); + voice[i+1] = LOBYTE((short)output); + } +} diff --git a/ambed/cfirfilter.h b/ambed/cfirfilter.h new file mode 100644 index 0000000..aa1dafd --- /dev/null +++ b/ambed/cfirfilter.h @@ -0,0 +1,52 @@ +// +// cfirfilter.h +// ambed +// +// Created by Jean-Luc Deltombe (LX3JL) on 26/04/2017. +// Copyright © 2015 Jean-Luc Deltombe (LX3JL). All rights reserved. +// +// ---------------------------------------------------------------------------- +// This file is part of ambed. +// +// xlxd is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// xlxd is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with Foobar. If not, see . +// ---------------------------------------------------------------------------- +// FIRFilter by Geoffrey Merck F4FXL / KC3FRA + +#ifndef cfirfilter_h +#define cfirfilter_h + +#include "main.h" + +class CFIRFilter +{ +public : + //Constructor + CFIRFilter(const float* taps, int tapsLength); + + // Destructor + ~CFIRFilter(); + + // Processing + float Process(float inputSample); + void Process(uint8* voice, int length); + +private: + float* m_taps; + int m_tapsLength; + float* m_buffer; + int m_currentBufferPostion; +}; + +#endif //cfirfilter_h + diff --git a/ambed/cusb3xxxinterface.cpp b/ambed/cusb3xxxinterface.cpp index 210b01f..49c1269 100644 --- a/ambed/cusb3xxxinterface.cpp +++ b/ambed/cusb3xxxinterface.cpp @@ -152,8 +152,15 @@ void CUsb3xxxInterface::Task(void) { Queue = Channel->GetVoiceQueue(); CVoicePacket *clone = new CVoicePacket(VoicePacket); +#if USE_BANDPASSFILTER + //Aply band pass before AGC to avoidd amplifying signals we do not want + Channel->ApplyFilter(*clone); +#endif +#if USE_AGC == 1 Channel->ApplyAGC(*clone); - //clone->ApplyGain(Channel->GetSpeechGain()); +#else + clone->ApplyGain(Channel->GetSpeechGain()); +#endif Queue->push(clone); Channel->ReleaseVoiceQueue(); } diff --git a/ambed/cvocodecchannel.cpp b/ambed/cvocodecchannel.cpp index 382c04d..8ce57f8 100644 --- a/ambed/cvocodecchannel.cpp +++ b/ambed/cvocodecchannel.cpp @@ -31,7 +31,6 @@ // constructor CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVocodecInterface *InterfaceOut, int iChOut, int iSpeechGain) -: m_AGC((float)iSpeechGain) { m_bOpen = false; m_InterfaceIn = InterfaceIn; @@ -39,6 +38,12 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc m_InterfaceOut = InterfaceOut; m_iChannelOut = iChOut; m_iSpeechGain = iSpeechGain; +#if USE_AGC == 1 + m_AGC = new CAGC((float)iSpeechGain); +#endif +#if USE_BANDPASSFILTER == 1 + m_filter = new CFIRFilter(FILTER_TAPS, FILTER_TAPS_LENGTH); +#endif } //////////////////////////////////////////////////////////////////////////////////////// @@ -47,6 +52,12 @@ CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVoc CVocodecChannel::~CVocodecChannel() { PurgeAllQueues(); +#if USE_AGC == 1 + delete m_AGC; +#endif +#if USE_BANDPASSFILTER == 1 + delete m_filter; +#endif } //////////////////////////////////////////////////////////////////////////////////////// @@ -92,11 +103,20 @@ uint8 CVocodecChannel::GetCodecOut(void) const return m_InterfaceOut->GetChannelCodec(m_iChannelOut); } +#if USE_AGC == 1 void CVocodecChannel::ApplyAGC(CVoicePacket& voicePacket) { - m_AGC.Apply(voicePacket.GetVoice(), voicePacket.GetVoiceSize()); + m_AGC->Apply(voicePacket.GetVoice(), voicePacket.GetVoiceSize()); //std::cout << "Gain : " << m_AGC.GetGain() << "\n"; } +#endif + +#if USE_BANDPASSFILTER == 1 +void CVocodecChannel::ApplyFilter(CVoicePacket& voicePacket) +{ + m_filter->Process(voicePacket.GetVoice(), voicePacket.GetVoiceSize()); +} +#endif //////////////////////////////////////////////////////////////////////////////////////// // queues helpers diff --git a/ambed/cvocodecchannel.h b/ambed/cvocodecchannel.h index 58fdfe5..735fa00 100644 --- a/ambed/cvocodecchannel.h +++ b/ambed/cvocodecchannel.h @@ -27,7 +27,12 @@ #define cvocodecchannel_h #include "cpacketqueue.h" +#if USE_AGC == 1 #include "cagc.h" +#endif +#if USE_BANDPASSFILTER == 1 +#include "cfirfilter.h" +#endif #include "cvoicepacket.h" //////////////////////////////////////////////////////////////////////////////////////// @@ -57,7 +62,12 @@ public: int GetSpeechGain(void) const { return m_iSpeechGain; } //Processing +#if USE_AGC == 1 void ApplyAGC(CVoicePacket& voicePacket); +#endif +#if USE_BANDPASSFILTER + void ApplyFilter(CVoicePacket& voicePacket); +#endif // interfaces bool IsInterfaceIn(const CVocodecInterface *interface) { return (interface == m_InterfaceIn); } @@ -98,7 +108,12 @@ protected: int m_iSpeechGain; private: - CAGC m_AGC; +#if USE_AGC == 1 + CAGC* m_AGC; +#endif +#if USE_BANDPASSFILTER == 1 + CFIRFilter* m_filter; +#endif }; //////////////////////////////////////////////////////////////////////////////////////// diff --git a/ambed/main.h b/ambed/main.h index bcfb1f7..ef60a2b 100644 --- a/ambed/main.h +++ b/ambed/main.h @@ -68,8 +68,12 @@ #define CODEC_AMBE2PLUS 2 // Transcoding speech gains -#define CODECGAIN_AMBEPLUS -3//-10 // in dB -#define CODECGAIN_AMBE2PLUS +3//+10 // in dB +#define CODECGAIN_AMBEPLUS -10 // in dB +#define CODECGAIN_AMBE2PLUS +10 // in dB + +// Transcoding Tweaks +#define USE_AGC 0 +#define USE_BANDPASSFILTER 1 // Timeouts ----------------------------------------------------- #define STREAM_ACTIVITY_TIMEOUT 3 // in seconds @@ -96,6 +100,42 @@ typedef unsigned int uint; #define LOWORD(dw) ((uint16)(uint32)(dw & 0x0000FFFF)) #define HIWORD(dw) ((uint16)((((uint32)(dw)) >> 16) & 0xFFFF)) +//////////////////////////////////////////////////////////////////////////////////////// +// FIR Filter coefficients computed to be the closest to the recommended filter in +// Documentation +// +// Following GNU Octave script was used +/* +pkg load signal; +fsamp = 8000; +fcuts = [200 300 3000 3400]; +mags = [0 1 0]; +devs = [0.2 1 0.2]; + +[n,Wn,beta,ftype] = kaiserord(fcuts,mags,devs,fsamp); +n = n + rem(n,2); +hh = fir1(n,Wn,ftype,kaiser(n+1,beta),'noscale'); + +freqz(hh); +[H,f] = freqz(hh,1,1024,fsamp); +plot(f,abs(H)) +disp(hh); +grid +*/ + +#if USE_BANDPASSFILTER == 1 + +const float FILTER_TAPS[] { +-0.0153779f, 0.0114832f, -0.0060703f, -0.0221526f, 0.0085472f, -0.0449400f, +-0.0068112f, -0.0307485f, -0.0548559f, -0.0022596f, -0.0879344f, -0.0166698f, +-0.0533627f, -0.1015552f, 0.0424673f, -0.2116654f, 0.1267453f, 0.7375000f, +0.1267453f, -0.2116654f, 0.0424673f, -0.1015552f, -0.0533627f, -0.0166698f, +-0.0879344f, -0.0022596f, -0.0548559f, -0.0307485f, -0.0068112f, -0.0449400f, +0.0085472f, -0.0221526f, -0.0060703f, 0.0114832f, -0.0153779f }; +#define FILTER_TAPS_LENGTH 35 + +#endif + //////////////////////////////////////////////////////////////////////////////////////// // global objects