Browse Source

cmProc2.h/c : Added spectral input/output files to cmSpecDist().

Added phase modulation via _smSpecDistPhaseMod() to cmSpecDist().
Changed track score calculation in FrqTrk.
Added attenuation delay state to FrqTrk attenution stage.
master
Kevin Larke 10 years ago
parent
commit
dfc94439da
2 changed files with 124 additions and 27 deletions
  1. 109
    26
      cmProc2.c
  2. 15
    1
      cmProc2.h

+ 109
- 26
cmProc2.c View File

4779
   p->minTrkN   = a->minTrkSec * a->srate / a->hopSmpCnt;
4779
   p->minTrkN   = a->minTrkSec * a->srate / a->hopSmpCnt;
4780
   p->nextTrkId = 1;
4780
   p->nextTrkId = 1;
4781
   p->aV        = cmMemResizeZ(cmReal_t,p->aV,p->a.binCnt);
4781
   p->aV        = cmMemResizeZ(cmReal_t,p->aV,p->a.binCnt);
4782
+  p->attenDlyPhsMax = cmMax(3,a->attenDlySec * a->srate / a->hopSmpCnt );
4782
   p->attenPhsMax    = cmMax(3,a->attenAtkSec * a->srate / a->hopSmpCnt );
4783
   p->attenPhsMax    = cmMax(3,a->attenAtkSec * a->srate / a->hopSmpCnt );
4783
   
4784
   
4784
   if( a->logFn != NULL )
4785
   if( a->logFn != NULL )
5019
       c->hz_mean = cmVOR_Mean(c->hzV,n);
5020
       c->hz_mean = cmVOR_Mean(c->hzV,n);
5020
       c->hz_std  = sqrt(cmVOR_Variance( c->hzV,n,&c->hz_mean));
5021
       c->hz_std  = sqrt(cmVOR_Variance( c->hzV,n,&c->hz_mean));
5021
 
5022
 
5022
-      c->score = c->db / ((cmMin(0.1,c->db_std) + cmMin(0.1,c->hz_std))/2);
5023
+      //c->score = c->db / ((cmMax(0.1,c->db_std) + cmMax(0.1,c->hz_std))/2);
5024
+
5025
+      c->score = c->db - (c->db_std * 5) - (c->hz_std/50);
5026
+
5027
+      //printf("%f %f %f %f %f\n",c->db,cmMin(0.1,c->db_std),c->hz,cmMin(0.1,c->hz_std),c->score);
5023
     }
5028
     }
5024
 }
5029
 }
5025
 
5030
 
5082
       // 
5087
       // 
5083
       if( c->score >= p->a.attenThresh && c->state == kNoStateFrqTrkId )
5088
       if( c->score >= p->a.attenThresh && c->state == kNoStateFrqTrkId )
5084
       {
5089
       {
5090
+        //printf("%f\n",c->score);
5085
         c->attenPhsIdx = 0;
5091
         c->attenPhsIdx = 0;
5086
-        c->state  = kAtkFrqTrkId;        
5092
+        c->state       = kDlyFrqTrkId;        
5087
       }
5093
       }
5088
 
5094
 
5089
       switch( c->state )
5095
       switch( c->state )
5091
         case kNoStateFrqTrkId:
5097
         case kNoStateFrqTrkId:
5092
           break;
5098
           break;
5093
 
5099
 
5100
+        case kDlyFrqTrkId:
5101
+          c->attenPhsIdx += 1;
5102
+
5103
+          if( c->attenPhsIdx >= p->attenDlyPhsMax && c->dN == 0 )
5104
+            c->state = kAtkFrqTrkId;          
5105
+
5106
+          break;
5107
+
5094
         case kAtkFrqTrkId:
5108
         case kAtkFrqTrkId:
5095
-          if( c->attenPhsIdx < p->attenPhsMax )
5109
+          if( c->attenPhsIdx < p->attenDlyPhsMax + p->attenPhsMax )
5096
           {
5110
           {
5097
 
5111
 
5098
             c->attenGain = cmMin(1.0,p->a.attenGain * c->attenPhsIdx / p->attenPhsMax);
5112
             c->attenGain = cmMin(1.0,p->a.attenGain * c->attenPhsIdx / p->attenPhsMax);
5101
           }
5115
           }
5102
 
5116
 
5103
           c->attenPhsIdx += 1;
5117
           c->attenPhsIdx += 1;
5104
-          if( c->attenPhsIdx >= p->attenPhsMax )
5118
+          if( c->attenPhsIdx >= p->attenDlyPhsMax + p->attenPhsMax )
5105
             c->state = kSusFrqTrkId;
5119
             c->state = kSusFrqTrkId;
5106
           break;
5120
           break;
5107
 
5121
 
5275
 
5289
 
5276
 void _cmFrqTrkApplyFrqBias( cmFrqTrk* p, cmReal_t* xV )
5290
 void _cmFrqTrkApplyFrqBias( cmFrqTrk* p, cmReal_t* xV )
5277
 {
5291
 {
5278
-  // 1+2*([0:.01:1].^4)
5292
+  // convert to decibel scale (0.0 - 100.0) and then scale to (0.0 to 1.0)
5279
   unsigned i;
5293
   unsigned i;
5280
   for(i=0; i<p->bN; ++i)
5294
   for(i=0; i<p->bN; ++i)
5281
     xV[i] =  cmMax(0.0, (20*log10( cmMax(xV[i]/1.5,0.00001)) + 100.0)/100.0);
5295
     xV[i] =  cmMax(0.0, (20*log10( cmMax(xV[i]/1.5,0.00001)) + 100.0)/100.0);
5318
   // copy p->dbV to dbM[hi,:] 
5332
   // copy p->dbV to dbM[hi,:] 
5319
   cmVOR_CopyN(p->dbM + p->hi, p->bN, p->hN, p->dbV, 1 );
5333
   cmVOR_CopyN(p->dbM + p->hi, p->bN, p->hN, p->dbV, 1 );
5320
 
5334
 
5321
-  // increment hi
5335
+  // increment hi to next column to fill in dbM[]
5322
   p->hi = (p->hi + 1) % p->hN;
5336
   p->hi = (p->hi + 1) % p->hN;
5323
 
5337
 
5324
-  // Form the spectral magnitude profile by taking the mean over time
5325
-  // of the last hN magnitude vectors
5338
+  // Set dbV[] to spectral magnitude profile by taking the mean over time
5339
+  // of the last hN magnitude vectors 
5326
   cmVOR_MeanM2(p->dbV, p->dbM, p->hN, p->bN, 0, cmMin(p->fN+1,p->hN));
5340
   cmVOR_MeanM2(p->dbV, p->dbM, p->hN, p->bN, 0, cmMin(p->fN+1,p->hN));
5327
   //cmVOR_MeanM(p->dbV, p->dbM, p->hN, p->bN, 0);
5341
   //cmVOR_MeanM(p->dbV, p->dbM, p->hN, p->bN, 0);
5328
 
5342
 
5329
   if( p->fN >= p->hN )
5343
   if( p->fN >= p->hN )
5330
   {
5344
   {
5331
-    // set the indexes of the peaks above pkThreshDb in i0[]
5345
+    // set pkiV[] to the indexes of the peaks above pkThreshDb in i0[]
5332
     unsigned pkN = cmVOR_PeakIndexes(p->pkiV, p->bN, p->dbV, p->bN, p->a.pkThreshDb );
5346
     unsigned pkN = cmVOR_PeakIndexes(p->pkiV, p->bN, p->dbV, p->bN, p->a.pkThreshDb );
5333
 
5347
 
5334
-    // generate the peak frequencies from the magnitude
5348
+    // set hzV[] to the peak frequencies assoc'd with peaks at dbV[ pkiV[] ].
5335
     _cmFrqTrkMagnToHz(p, p->dbV, p->pkiV, pkN, hzV );
5349
     _cmFrqTrkMagnToHz(p, p->dbV, p->pkiV, pkN, hzV );
5336
 
5350
 
5337
     // extend the existing trackers
5351
     // extend the existing trackers
5485
 {
5499
 {
5486
   cmSpecDist_t* p = cmObjAlloc( cmSpecDist_t, ctx, ap );
5500
   cmSpecDist_t* p = cmObjAlloc( cmSpecDist_t, ctx, ap );
5487
 
5501
 
5502
+  p->iSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5488
   p->oSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5503
   p->oSpecVa   = cmVectArrayAlloc(ctx,kRealVaFl);
5489
 
5504
 
5490
   if( procSmpCnt != 0 )
5505
   if( procSmpCnt != 0 )
5505
   cmSpecDist_t* p = *pp;
5520
   cmSpecDist_t* p = *pp;
5506
   
5521
   
5507
   cmSpecDistFinal(p);
5522
   cmSpecDistFinal(p);
5523
+  cmVectArrayFree(&p->iSpecVa);
5508
   cmVectArrayFree(&p->oSpecVa);
5524
   cmVectArrayFree(&p->oSpecVa);
5509
   cmMemPtrFree(&p->hzV);
5525
   cmMemPtrFree(&p->hzV);
5526
+  cmMemPtrFree(&p->iSpecM);
5527
+  cmMemPtrFree(&p->oSpecM);
5528
+  cmMemPtrFree(&p->iSpecV);
5529
+  cmMemPtrFree(&p->oSpecV);
5510
   cmObjFree(pp);
5530
   cmObjFree(pp);
5511
   return cmOkRC;
5531
   return cmOkRC;
5512
 
5532
 
5523
   unsigned flags = 0;
5543
   unsigned flags = 0;
5524
 
5544
 
5525
 
5545
 
5546
+  p->srate        = srate;
5526
   p->wndSmpCnt    = wndSmpCnt;
5547
   p->wndSmpCnt    = wndSmpCnt;
5527
   p->hopSmpCnt    = (unsigned)floor(wndSmpCnt/hopFcmt);
5548
   p->hopSmpCnt    = (unsigned)floor(wndSmpCnt/hopFcmt);
5528
   p->procSmpCnt   = procSmpCnt;
5549
   p->procSmpCnt   = procSmpCnt;
5550
   fta.pkMaxHz       = 20000;
5571
   fta.pkMaxHz       = 20000;
5551
   fta.whFiltCoeff   = 0.33;
5572
   fta.whFiltCoeff   = 0.33;
5552
 
5573
 
5553
-  fta.attenThresh = 900.0;
5554
-  fta.attenGain   = 1.0; 
5555
-  fta.attenAtkSec = 0.25;  
5574
+  fta.attenThresh = 0.4;
5575
+  fta.attenGain   = 0.5; 
5576
+  fta.attenDlySec = 1.0;
5577
+  fta.attenAtkSec = 1.0;  
5556
 
5578
 
5557
   fta.logFn         = "/home/kevin/temp/frqtrk/trk_log.va";
5579
   fta.logFn         = "/home/kevin/temp/frqtrk/trk_log.va";
5558
   fta.levelFn       = "/home/kevin/temp/frqtrk/level.va";
5580
   fta.levelFn       = "/home/kevin/temp/frqtrk/level.va";
5587
   p->aeMin  = 1000;
5609
   p->aeMin  = 1000;
5588
   p->aeMax  = -1000;
5610
   p->aeMax  = -1000;
5589
 
5611
 
5612
+  
5613
+  double histSecs = 0.05;
5614
+  p->hN      = cmMax(1,histSecs * p->srate / p->hopSmpCnt );
5615
+  p->iSpecM  = cmMemResizeZ(cmReal_t,p->iSpecM,p->hN*p->pva->binCnt);
5616
+  p->oSpecM  = cmMemResizeZ(cmReal_t,p->oSpecM,p->hN*p->pva->binCnt);
5617
+  p->iSpecV  = cmMemResizeZ(cmReal_t,p->iSpecV,      p->pva->binCnt);
5618
+  p->oSpecV  = cmMemResizeZ(cmReal_t,p->oSpecV,      p->pva->binCnt);
5619
+  p->hi      = 0;
5620
+
5590
 
5621
 
5591
   //p->bypOut = cmMemResizeZ(cmSample_t, p->bypOut, procSmpCnt );
5622
   //p->bypOut = cmMemResizeZ(cmSample_t, p->bypOut, procSmpCnt );
5592
 
5623
 
5597
 {
5628
 {
5598
   cmRC_t rc = cmOkRC;
5629
   cmRC_t rc = cmOkRC;
5599
 
5630
 
5631
+  cmVectArrayWrite(p->iSpecVa, "/home/kevin/temp/frqtrk/iSpec.va");
5600
   cmVectArrayWrite(p->oSpecVa, "/home/kevin/temp/frqtrk/oSpec.va");
5632
   cmVectArrayWrite(p->oSpecVa, "/home/kevin/temp/frqtrk/oSpec.va");
5601
 
5633
 
5602
   cmPvAnlFree(&p->pva);
5634
   cmPvAnlFree(&p->pva);
5777
 
5809
 
5778
 }
5810
 }
5779
 
5811
 
5812
+void _cmSpecDistPhaseMod( cmSpecDist_t* p, cmReal_t* phsV, unsigned binCnt )
5813
+{
5814
+  unsigned i;
5815
+  cmReal_t offs =  sin( 0.1 * 2.0 * M_PI * (p->phaseModIndex++) / (p->srate/p->hopSmpCnt) );
5816
+
5817
+  //printf("offs %f %i %i %f\n",offs,p->phaseModIndex,p->hopSmpCnt,p->srate);
5818
+
5819
+  cmReal_t new_phs = phsV[0] + offs;
5820
+  for(i=0; i<binCnt-1; ++i)
5821
+  {
5822
+    while( new_phs > M_PI )
5823
+      new_phs -= 2.0*M_PI;
5824
+
5825
+    while( new_phs < -M_PI )
5826
+      new_phs += 2.0*M_PI;
5827
+
5828
+    cmReal_t d = phsV[i+1] - phsV[i];
5829
+
5830
+    phsV[i] = new_phs;
5831
+
5832
+    new_phs += d;    
5833
+  }
5834
+  
5835
+}
5836
+
5780
 cmRC_t  cmSpecDistExec( cmSpecDist_t* p, const cmSample_t* sp, unsigned sn )
5837
 cmRC_t  cmSpecDistExec( cmSpecDist_t* p, const cmSample_t* sp, unsigned sn )
5781
 {
5838
 {
5782
 
5839
 
5783
   assert( sn == p->procSmpCnt );
5840
   assert( sn == p->procSmpCnt );
5784
 
5841
 
5842
+  bool recordFl = false;
5843
+
5785
   // cmPvAnlExec() returns true when it calc's a new spectral output frame
5844
   // cmPvAnlExec() returns true when it calc's a new spectral output frame
5786
   if( cmPvAnlExec( p->pva, sp, sn ) )
5845
   if( cmPvAnlExec( p->pva, sp, sn ) )
5787
   {
5846
   {
5788
     cmReal_t X1m[p->pva->binCnt]; 
5847
     cmReal_t X1m[p->pva->binCnt]; 
5789
 
5848
 
5849
+    // take the mean of the the input magntitude spectrum
5790
     cmReal_t u0 = cmVOR_Mean(p->pva->magV,p->pva->binCnt);
5850
     cmReal_t u0 = cmVOR_Mean(p->pva->magV,p->pva->binCnt);
5791
 
5851
 
5792
-    //cmFrqTrkExec(p->ft, p->pva->magV, p->pva->phsV, NULL );
5793
-
5794
-    // apply the freq track suppression filter
5795
-    //cmVOR_MultVVV(X1m, p->pva->binCnt,p->pva->magV, p->ft->aV );
5852
+    if(recordFl)
5853
+    {
5854
+      // store a time windowed average of the input spectrum to p->iSpecV
5855
+      cmVOR_CopyN(p->iSpecM + p->hi, p->pva->binCnt, p->hN, X1m, 1 );
5856
+      cmVOR_MeanM2(p->iSpecV, p->iSpecM, p->hN, p->pva->binCnt, 0, cmMin(p->fi+1,p->hN));
5857
+    }
5796
 
5858
 
5797
     cmVOR_AmplToDbVV(X1m, p->pva->binCnt, p->pva->magV, -1000.0 );
5859
     cmVOR_AmplToDbVV(X1m, p->pva->binCnt, p->pva->magV, -1000.0 );
5798
     //cmVOR_AmplToDbVV(X1m, p->pva->binCnt, X1m, -1000.0 );
5860
     //cmVOR_AmplToDbVV(X1m, p->pva->binCnt, X1m, -1000.0 );
5799
 
5861
 
5800
 
5862
 
5863
+
5801
     switch( p->mode )
5864
     switch( p->mode )
5802
     {
5865
     {
5803
       case kBypassModeSdId:
5866
       case kBypassModeSdId:
5838
         break;
5901
         break;
5839
     }
5902
     }
5840
 
5903
 
5841
-    //cmVectArrayAppendR(p->oSpecVa,X1m,p->pva->binCnt);
5842
-    
5843
     cmVOR_DbToAmplVV(X1m, p->pva->binCnt, X1m );
5904
     cmVOR_DbToAmplVV(X1m, p->pva->binCnt, X1m );
5844
 
5905
 
5845
 
5906
 
5846
     // run and apply the tracker/supressor
5907
     // run and apply the tracker/supressor
5847
-    cmFrqTrkExec(p->ft, X1m, p->pva->phsV, NULL );
5848
-    cmVOR_MultVV(X1m, p->pva->binCnt,p->ft->aV );
5908
+    cmFrqTrkExec(p->ft, X1m, p->pva->phsV, NULL ); 
5909
+    //cmVOR_MultVV(X1m, p->pva->binCnt,p->ft->aV );
5849
 
5910
 
5850
 
5911
 
5912
+    // convert the mean input magnitude to db
5851
     cmReal_t idb = 20*log10(u0);
5913
     cmReal_t idb = 20*log10(u0);
5914
+    
5915
+    // get the mean output magnitude spectra
5852
     cmReal_t u1 = cmVOR_Mean(X1m,p->pva->binCnt);
5916
     cmReal_t u1 = cmVOR_Mean(X1m,p->pva->binCnt);
5853
 
5917
 
5854
     if( idb > -150.0 )
5918
     if( idb > -150.0 )
5855
     {
5919
     {
5856
-      p->ogain = u0/u1;
5920
+      // set the output gain such that the mean output magnitude
5921
+      // will match the mean input magnitude
5922
+      p->ogain = u0/u1;  
5857
     }
5923
     }
5858
     else
5924
     else
5859
     {
5925
     {
5861
       p->ogain *= a0;
5927
       p->ogain *= a0;
5862
     }
5928
     }
5863
 
5929
 
5864
-    //cmReal_t v[] = { u0, u1, idb, 20*log10(u1), p->ogain };
5865
-    //unsigned vn = sizeof(v)/sizeof(v[0]);
5866
-    //cmVectArrayAppendR(p->oSpecVa,v,vn);
5867
-
5868
     cmVOR_MultVS(X1m,p->pva->binCnt,cmMin(4.0,p->ogain));
5930
     cmVOR_MultVS(X1m,p->pva->binCnt,cmMin(4.0,p->ogain));
5869
 
5931
 
5870
 
5932
 
5871
     //cmFbCtlExec(p->fbc,X1m);
5933
     //cmFbCtlExec(p->fbc,X1m);
5872
 
5934
 
5935
+    //cmReal_t v[ p->pva->binCnt ];
5936
+    //cmVOR_Copy(v,p->pva->binCnt,p->pva->phsV);
5937
+    //_cmSpecDistPhaseMod(p, v, p->pva->binCnt );
5938
+
5939
+    
5940
+    if(recordFl)
5941
+    {
5942
+
5943
+      // store a time windowed average of the output spectrum to p->iSpecV
5944
+      cmVOR_CopyN(p->oSpecM + p->hi, p->pva->binCnt, p->hN, X1m, 1 );
5945
+      cmVOR_MeanM2(p->oSpecV, p->oSpecM, p->hN, p->pva->binCnt, 0, cmMin(p->fi+1,p->hN));
5946
+      
5947
+      // store iSpecV and oSpecV to iSpecVa and oSpecVa to create debugging files
5948
+      cmVectArrayAppendR(p->iSpecVa,p->iSpecV,p->pva->binCnt);
5949
+      cmVectArrayAppendR(p->oSpecVa,p->oSpecV,p->pva->binCnt);
5950
+
5951
+      p->hi = (p->hi + 1) % p->hN;
5952
+    }
5953
+    
5954
+
5873
     cmPvSynExec(p->pvs, X1m, p->pva->phsV );
5955
     cmPvSynExec(p->pvs, X1m, p->pva->phsV );
5874
   
5956
   
5957
+    p->fi += 1;
5875
   }
5958
   }
5876
  
5959
  
5877
   return cmOkRC;
5960
   return cmOkRC;

+ 15
- 1
cmProc2.h View File

902
   typedef enum
902
   typedef enum
903
   {
903
   {
904
     kNoStateFrqTrkId,
904
     kNoStateFrqTrkId,
905
+    kDlyFrqTrkId,
905
     kAtkFrqTrkId,
906
     kAtkFrqTrkId,
906
     kSusFrqTrkId,
907
     kSusFrqTrkId,
907
     kDcyFrqTrkId
908
     kDcyFrqTrkId
924
 
925
 
925
     cmReal_t    attenThresh;
926
     cmReal_t    attenThresh;
926
     cmReal_t    attenGain; 
927
     cmReal_t    attenGain; 
928
+    cmReal_t    attenDlySec;
927
     cmReal_t    attenAtkSec;   
929
     cmReal_t    attenAtkSec;   
928
 
930
 
929
     const char* logFn;          // log file name or NULL if no file is to be written
931
     const char* logFn;          // log file name or NULL if no file is to be written
985
     unsigned      deadTrkCnt;
987
     unsigned      deadTrkCnt;
986
 
988
 
987
     cmReal_t*     aV;
989
     cmReal_t*     aV;
990
+    int           attenDlyPhsMax;
988
     int           attenPhsMax;
991
     int           attenPhsMax;
989
 
992
 
990
     cmWhFilt*      wf;
993
     cmWhFilt*      wf;
1106
     cmReal_t  aeMax;
1109
     cmReal_t  aeMax;
1107
     cmReal_t  aeUnit;
1110
     cmReal_t  aeUnit;
1108
 
1111
 
1109
-    cmVectArray_t* oSpecVa;
1110
     cmReal_t ogain;
1112
     cmReal_t ogain;
1111
 
1113
 
1114
+    unsigned phaseModIndex;
1115
+
1116
+    unsigned       fi;          // total count of frames processed by cmSpecDistExec()
1117
+    unsigned       hN;
1118
+    unsigned       hi;
1119
+    cmReal_t*      iSpecM;      // iSpecMtx[hN binN]
1120
+    cmReal_t*      iSpecV;      // mean of rows of iSpecM 
1121
+    cmVectArray_t* iSpecVa;     
1122
+    cmReal_t*      oSpecM;      // oSpecMtx[hN binN]
1123
+    cmReal_t*      oSpecV;      // mean of rows of oSpecM
1124
+    cmVectArray_t* oSpecVa;
1125
+
1112
   } cmSpecDist_t;
1126
   } cmSpecDist_t;
1113
 
1127
 
1114
   cmSpecDist_t*     cmSpecDistAlloc( cmCtx* ctx,cmSpecDist_t* ap, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId  ); 
1128
   cmSpecDist_t*     cmSpecDistAlloc( cmCtx* ctx,cmSpecDist_t* ap, unsigned procSmpCnt, double srate, unsigned wndSmpCnt, unsigned hopFcmt, unsigned olaWndTypeId  ); 

Loading…
Cancel
Save