]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/speechproc.c
changes to speechproc, correct IQ -- restore squelch in sdr.c -- other minor stuff
[dttsp.git] / jDttSP / speechproc.c
1 /* speechproc.h
2    
3   This file is part of a program that implements a Software-Defined Radio.
4
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
7
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.
12
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.
17
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
21
22 The authors can be reached by email at
23
24 ab2kt@arrl.net
25 or
26 rwmcgwier@comcast.net
27
28 or by paper mail at
29
30 The DTTS Microwave Society
31 6 Kathleen Place
32 Bridgewater, NJ 08807
33 */
34
35 #include <speechproc.h>
36
37 SpeechProc
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");
41   sp->K = K;
42   sp->MaxGain = pow(10.0, MaxCompression * 0.05);
43   sp->LastCG = 1.0;
44   sp->SpeechProcessorBuffer = newCXB(size, spdat, "speech processor data");
45   sp->size = size;
46   return sp;
47 }
48
49 void
50 delSpeechProc(SpeechProc sp) {
51   if (sp) {
52     delRLB(sp->CG);
53     delCXB(sp->SpeechProcessorBuffer);
54     safefree((char *) sp);
55   }
56 }
57
58 void
59 SpeechProcessor(SpeechProc sp) {
60   int i;
61   REAL r = 0.0, Mag;
62
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));
69     if (Mag != 0.0) {
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;
73     } else
74       RLBdata(sp->CG, i) = sp->MaxGain;
75   }
76   sp->LastCG = RLBdata(sp->CG, sp->size); // save for next time 
77   
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),
80                                                  RLBdata(sp->CG, i));
81 }
82