Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

VrSquelch.h

Go to the documentation of this file.
00001 /* -*- Mode: c++ -*- 
00002  *
00003  *  Copyright 1997 Massachusetts Institute of Technology
00004  * 
00005  *  Permission to use, copy, modify, distribute, and sell this software and its
00006  *  documentation for any purpose is hereby granted without fee, provided that
00007  *  the above copyright notice appear in all copies and that both that
00008  *  copyright notice and this permission notice appear in supporting
00009  *  documentation, and that the name of M.I.T. not be used in advertising or
00010  *  publicity pertaining to distribution of the software without specific,
00011  *  written prior permission.  M.I.T. makes no representations about the
00012  *  suitability of this software for any purpose.  It is provided "as is"
00013  *  without express or implied warranty.
00014  * 
00015  */
00016 
00017 
00018 #ifndef _VrSQUELCH_H_
00019 #define _VrSQUELCH_H_
00020 
00021 #include <VrHistoryProc.h>
00022 
00023 template<class T>
00024 class VrSquelch : public VrHistoryProc<T, T>
00025 {
00026 protected:
00027   float thresh;
00028   float duration;
00029 
00030   bool squelching;
00031   int bufferCount;
00032 
00033   T *zeroBuf;
00034   
00035 public:
00036   VrSquelch(float th, float d) : 
00037     thresh(th), duration(d), squelching(false), bufferCount(0) {}
00038 
00039   virtual void work(timestamp ts, unsigned int size, T *i[], T *o[]);
00040   virtual void initialize()
00041     {
00042       int h= ((int)(duration*getInputSamplingFrequencyN(0)));
00043       history=h;
00044       setOutputSize (h);
00045       zeroBuf = new T[h];
00046       for(int i=0;i<h;i++) zeroBuf[i]=0;
00047     }
00048   void setThresh(float th) { thresh = th; }
00049   float getThresh() { return thresh; }
00050   void setDuration(float d) 
00051     { 
00052       duration = d;
00053       delete[] zeroBuf;
00054       int h = (int)(duration*getInputSamplingFrequencyN(0));
00055       history=h;
00056       setOutputSize (h);
00057       zeroBuf = new T[h];
00058     }
00059   float getDuration() { return duration; }
00060 };
00061 
00062 template<class T>
00063 void
00064 VrSquelch<T>::work(timestamp ts, unsigned int size, T *i[], T *o[])
00065 {
00066   while (size > 0)
00067     {
00068       float total = 0;
00069       T *buf=i[0];
00070       for (unsigned int ii=0; ii<history; ii++)
00071         {
00072           total += abs(buf[ii]);
00073         }
00074       if (squelching)
00075         {
00076           if (total/history > thresh)
00077             {
00078               squelching = false;
00079               bufferCount = 0;
00080               memcpy(o[0],buf,history*sizeof(T));
00081             }
00082           else
00083             memcpy(o[0],zeroBuf,history*sizeof(T));
00084         }
00085       else 
00086         {
00087           if (total/history < thresh)
00088             bufferCount++;
00089           if (bufferCount > 2)
00090             {
00091               squelching = true;
00092               memcpy(o[0],zeroBuf,history*sizeof(T));
00093             }
00094           else
00095             memcpy(o[0],buf,history*sizeof(T));
00096         }
00097       size -= history;
00098       i[0]+=history;
00099       o[0]+=history;
00100     }
00101 }
00102 
00103 #endif

Generated on Tue Mar 15 23:46:36 2005 for GNU Radio by  doxygen 1.4.0