]> git.rkrishnan.org Git - dttsp.git/blobdiff - jDttSP/speechproc.c
changes to speechproc, correct IQ -- restore squelch in sdr.c -- other minor stuff
[dttsp.git] / jDttSP / speechproc.c
index 505304d10e8c2986fbbdbf491632d1ad01a9ec1b..2d3112601334b10a431e9f2c214a120378b8a3ad 100644 (file)
@@ -1,8 +1,9 @@
-/* speechproc.c
+/* speechproc.h
    
   This file is part of a program that implements a Software-Defined Radio.
 
-Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
+Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY, Phil Harman, VK6APH
+Based on Visual Basic code for SDR by Phil Harman
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -48,6 +49,7 @@ newSpeechProc(REAL K, REAL MaxCompression, COMPLEX *spdat, int size) {
 void
 delSpeechProc(SpeechProc sp) {
   if (sp) {
+    delRLB(sp->CG);
     delCXB(sp->SpeechProcessorBuffer);
     safefree((char *) sp);
   }
@@ -57,27 +59,24 @@ void
 SpeechProcessor(SpeechProc sp) {
   int i;
   REAL r = 0.0, Mag;
+
+  // K was 0.4 in VB version, this value is better, perhaps due to filters that follow?
   for (i = 0; i < sp->size; i++)
-    r = max(r, Cmag(CXBdata(sp->SpeechProcessorBuffer, i)));
-  Mag = Cmag(CXBdata(sp->SpeechProcessorBuffer, 0));
-  if (Mag > 0.0) {
-    RLBdata(sp->CG, 0) = sp->LastCG * (1.0 - sp->K) + (sp->K * r / Mag);
-    if (RLBdata(sp->CG, 0) > sp->MaxGain)
-      RLBdata(sp->CG, 0) = sp->MaxGain;
-  } else
-    RLBdata(sp->CG, 0) = 1.0;
-  for (i = 1; i < sp->size; i++) {
-    Mag = Cmag(CXBdata(sp->SpeechProcessorBuffer, i));
-    if (Mag > 0.0) {
-      RLBdata(sp->CG, i) =
-       RLBdata(sp->CG, i - 1) * (1 - sp->K) + (sp->K * r / Mag);
+    r = max(r, Cmag(CXBdata(sp->SpeechProcessorBuffer, i))); // find the peak magnitude value in the sample buffer 
+  RLBdata(sp->CG, 0) = sp->LastCG; // restore from last time
+  for (i = 1; i <= sp->size ; i++) {
+    Mag = Cmag(CXBdata(sp->SpeechProcessorBuffer, i - 1));
+    if (Mag != 0.0) {
+      RLBdata(sp->CG, i) = RLBdata(sp->CG, i - 1) * (1 - sp->K) + (sp->K * r / Mag); // Frerking's formula
       if (RLBdata(sp->CG, i) > sp->MaxGain)
        RLBdata(sp->CG, i) = sp->MaxGain;
     } else
-      RLBdata(sp->CG, i) = 1.0;
+      RLBdata(sp->CG, i) = sp->MaxGain;
   }
-  sp->LastCG = RLBdata(sp->CG, sp->size - 1);
-  for (i = 0; i < sp->size; i++)
-    CXBdata(sp->SpeechProcessorBuffer, i) =
-      Cscl(CXBdata(sp->SpeechProcessorBuffer, i), RLBdata(sp->CG, i));
+  sp->LastCG = RLBdata(sp->CG, sp->size); // save for next time 
+  
+  for (i = 0; i < sp->size; i++) // multiply each sample by its gain constant 
+    CXBdata(sp->SpeechProcessorBuffer, i) = Cscl(CXBdata(sp->SpeechProcessorBuffer, i),
+                                                RLBdata(sp->CG, i));
 }
+