3 This file is part of a program that implements a Software-Defined Radio.
5 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY, Phil Harman, VK6APH
6 Based on Visual Basic code for SDR by Phil Harman
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 The authors can be reached by email at
30 The DTTS Microwave Society
35 #include <speechproc.h>
38 newSpeechProc(REAL K, REAL MaxCompression, COMPLEX *spdat, int size) {
39 SpeechProc sp = (SpeechProc) safealloc(1, sizeof(speech_proc), "new speech processor");
40 sp->CG = newRLB(size, NULL, "CG buffer in Speech Processor");
42 sp->MaxGain = pow(10.0, MaxCompression * 0.05);
44 sp->SpeechProcessorBuffer = newCXB(size, spdat, "speech processor data");
50 delSpeechProc(SpeechProc sp) {
53 delCXB(sp->SpeechProcessorBuffer);
54 safefree((char *) sp);
59 SpeechProcessor(SpeechProc sp) {
63 // K was 0.4 in VB version, this value is better, perhaps due to filters that follow?
64 for (i = 0; i < sp->size; i++)
65 r = max(r, Cmag(CXBdata(sp->SpeechProcessorBuffer, i))); // find the peak magnitude value in the sample buffer
66 RLBdata(sp->CG, 0) = sp->LastCG; // restore from last time
67 for (i = 1; i <= sp->size ; i++) {
68 Mag = Cmag(CXBdata(sp->SpeechProcessorBuffer, i - 1));
70 RLBdata(sp->CG, i) = RLBdata(sp->CG, i - 1) * (1 - sp->K) + (sp->K * r / Mag); // Frerking's formula
71 if (RLBdata(sp->CG, i) > sp->MaxGain)
72 RLBdata(sp->CG, i) = sp->MaxGain;
74 RLBdata(sp->CG, i) = sp->MaxGain;
76 sp->LastCG = RLBdata(sp->CG, sp->size); // save for next time
78 for (i = 0; i < sp->size; i++) // multiply each sample by its gain constant
79 CXBdata(sp->SpeechProcessorBuffer, i) = Cscl(CXBdata(sp->SpeechProcessorBuffer, i),