mirror of
https://github.com/ShaYmez/xlxd.git
synced 2025-08-07 07:42:25 -04:00
Add AGC
This commit is contained in:
parent
a78c30abfb
commit
64f5fddf17
82
ambed/cagc.cpp
Normal file
82
ambed/cagc.cpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
//
|
||||||
|
// cagc.cpp
|
||||||
|
// ambed
|
||||||
|
//
|
||||||
|
// Created by Jean-Luc Deltombe (LX3JL) on 28/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 <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Geoffrey Merck F4FXL / KC3FRA AGC code borrowed from Liquid DSP
|
||||||
|
// Only took the parts we need qnd recoeded it to be close the XLX coding style
|
||||||
|
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
#include <math.h>
|
||||||
|
#include "cagc.h"
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// constructor
|
||||||
|
|
||||||
|
CAGC::CAGC(float initialLeveldB)
|
||||||
|
{
|
||||||
|
// set internal gain appropriately
|
||||||
|
m_g = powf(10.0f, -initialLeveldB/20.0f);
|
||||||
|
|
||||||
|
// ensure resulting gain is not arbitrarily low
|
||||||
|
if (m_g < 1e-16f)
|
||||||
|
m_g = 1e-16f;
|
||||||
|
|
||||||
|
m_scale = 1.0f;
|
||||||
|
m_bandwidth = 1e-2f;
|
||||||
|
m_y2_prime = 1.0f;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CAGC::Apply(uint8 * voice, int size)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < size; i+=2)
|
||||||
|
{
|
||||||
|
//Get the sample
|
||||||
|
float _x = (float)(short)MAKEWORD(voice[i+1], voice[i]);
|
||||||
|
|
||||||
|
//apply AGC
|
||||||
|
// apply gain to input sample
|
||||||
|
float _y = _x * m_g;
|
||||||
|
|
||||||
|
// compute output signal energy
|
||||||
|
float y2 = _y * _y;
|
||||||
|
|
||||||
|
// smooth energy estimate using single-pole low-pass filter
|
||||||
|
m_y2_prime = (1.0f - m_alpha) * m_y2_prime + m_alpha*y2;
|
||||||
|
|
||||||
|
// update gain according to output energy
|
||||||
|
if (m_y2_prime > 1e-6f)
|
||||||
|
m_g *= exp( -0.5f * m_alpha * log(m_y2_prime) );
|
||||||
|
|
||||||
|
// clamp to 120 dB gain
|
||||||
|
if (m_g > 1e6f)
|
||||||
|
m_g = 1e6f;
|
||||||
|
|
||||||
|
// apply output scale
|
||||||
|
_y *= m_scale;
|
||||||
|
|
||||||
|
//write processed sample back to it
|
||||||
|
voice[i] = HIBYTE((short)_y);
|
||||||
|
voice[i+1] = LOBYTE((short)_y);
|
||||||
|
}
|
||||||
|
}
|
56
ambed/cagc.h
Normal file
56
ambed/cagc.h
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
//
|
||||||
|
// cagc.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 <http://www.gnu.org/licenses/>.
|
||||||
|
// ----------------------------------------------------------------------------
|
||||||
|
// Geoffrey Merck F4FXL / KC3FRA AGC code borrowed from Liquid DSP
|
||||||
|
// Only took the parts we need qnd recoeded it to be close the XLX coding style
|
||||||
|
// https://github.com/jgaeddert/liquid-dsp/blob/master/src/agc/src/agc.c
|
||||||
|
|
||||||
|
#ifndef cagc_h
|
||||||
|
#define cagc_h
|
||||||
|
|
||||||
|
#include "main.h"
|
||||||
|
|
||||||
|
class CAGC
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
//Constructor
|
||||||
|
CAGC(float initialLeveldB);
|
||||||
|
|
||||||
|
//methods
|
||||||
|
void Apply(uint8 * voice, int size);
|
||||||
|
float GetGain(){ return m_scale; }//gets current gain (linear)
|
||||||
|
|
||||||
|
private:
|
||||||
|
// gain variables
|
||||||
|
float m_g; // current gain value
|
||||||
|
float m_scale; // output scale value
|
||||||
|
|
||||||
|
// gain control loop filter parameters
|
||||||
|
float m_bandwidth; // bandwidth-time constant
|
||||||
|
float m_alpha; // feed-back gain
|
||||||
|
|
||||||
|
// signal level estimate
|
||||||
|
float m_y2_prime; // filtered output signal energy estimate
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* cgc_h */
|
@ -152,7 +152,10 @@ void CUsb3xxxInterface::Task(void)
|
|||||||
{
|
{
|
||||||
Queue = Channel->GetVoiceQueue();
|
Queue = Channel->GetVoiceQueue();
|
||||||
CVoicePacket *clone = new CVoicePacket(VoicePacket);
|
CVoicePacket *clone = new CVoicePacket(VoicePacket);
|
||||||
clone->ApplyGain(Channel->GetSpeechGain());
|
CAGC agc = Channel->GetAGC();
|
||||||
|
agc.Apply(clone->GetVoice(), clone->GetVoiceSize());
|
||||||
|
std::cout << "Gain : " << agc.GetGain();
|
||||||
|
//clone->ApplyGain(Channel->GetSpeechGain());
|
||||||
Queue->push(clone);
|
Queue->push(clone);
|
||||||
Channel->ReleaseVoiceQueue();
|
Channel->ReleaseVoiceQueue();
|
||||||
}
|
}
|
||||||
|
@ -27,11 +27,11 @@
|
|||||||
#include "cvocodecchannel.h"
|
#include "cvocodecchannel.h"
|
||||||
#include "cvocodecinterface.h"
|
#include "cvocodecinterface.h"
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// constructor
|
// constructor
|
||||||
|
|
||||||
CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVocodecInterface *InterfaceOut, int iChOut, int iSpeechGain)
|
CVocodecChannel::CVocodecChannel(CVocodecInterface *InterfaceIn, int iChIn, CVocodecInterface *InterfaceOut, int iChOut, int iSpeechGain)
|
||||||
|
: m_AGC((float)iSpeechGain)
|
||||||
{
|
{
|
||||||
m_bOpen = false;
|
m_bOpen = false;
|
||||||
m_InterfaceIn = InterfaceIn;
|
m_InterfaceIn = InterfaceIn;
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#define cvocodecchannel_h
|
#define cvocodecchannel_h
|
||||||
|
|
||||||
#include "cpacketqueue.h"
|
#include "cpacketqueue.h"
|
||||||
|
#include "cagc.h"
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// class
|
// class
|
||||||
@ -53,6 +54,7 @@ public:
|
|||||||
int GetChannelIn(void) const { return m_iChannelIn; }
|
int GetChannelIn(void) const { return m_iChannelIn; }
|
||||||
int GetChannelOut(void) const { return m_iChannelOut; }
|
int GetChannelOut(void) const { return m_iChannelOut; }
|
||||||
int GetSpeechGain(void) const { return m_iSpeechGain; }
|
int GetSpeechGain(void) const { return m_iSpeechGain; }
|
||||||
|
CAGC& GetAGC() { return m_AGC; };
|
||||||
|
|
||||||
// interfaces
|
// interfaces
|
||||||
bool IsInterfaceIn(const CVocodecInterface *interface) { return (interface == m_InterfaceIn); }
|
bool IsInterfaceIn(const CVocodecInterface *interface) { return (interface == m_InterfaceIn); }
|
||||||
@ -92,6 +94,8 @@ protected:
|
|||||||
// settings
|
// settings
|
||||||
int m_iSpeechGain;
|
int m_iSpeechGain;
|
||||||
|
|
||||||
|
private:
|
||||||
|
CAGC m_AGC;
|
||||||
};
|
};
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////////////
|
||||||
|
Loading…
x
Reference in New Issue
Block a user