Main Page | Namespace List | Class Hierarchy | Alphabetical List | Compound List | File List | Compound Members | File Members

LofarDemon.cc

Go to the documentation of this file.
00001 /*
00002 
00003     LOFAR/DEMON calculation server
00004     Copyright (C) 2000-2003 Jussi Laako
00005 
00006     This program is free software; you can redistribute it and/or modify
00007     it under the terms of the GNU General Public License as published by
00008     the Free Software Foundation; either version 2 of the License, or
00009     (at your option) any later version.
00010 
00011     This program is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014     GNU General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 
00020 */
00021 
00022 
00023 #include <stdio.h>
00024 #include <stdlib.h>
00025 #include <unistd.h>
00026 #include <limits.h>
00027 #include <string.h>
00028 #include <signal.h>
00029 #include <errno.h>
00030 #include <math.h>
00031 #include <float.h>
00032 #include <time.h>
00033 #include <sys/time.h>
00034 
00035 #include "LofarDemon.hh"
00036 #include "DC-Block.h"
00037 
00038 
00039 static bool bDebug;
00040 static bool bDaemonProc;
00041 clLofarDemon *LofarDemon;
00042 
00043 #define LOFAR_NWINS 12
00044 const static char *cpaWindowFuncs[] = {
00045     "Rectangle",
00046     "Bartlett",
00047     "Blackman",
00048     "Blackman-Harris",
00049     "Cosine tapered",
00050     "Exponential",
00051     "Flat-top",
00052     "Hamming",
00053     "Hanning",
00054     "Kaiser",
00055     "Kaiser-Bessel",
00056     "Tukey" };
00057 
00058 
00059 void SigHandler (int iSigNum)
00060 {
00061     switch (iSigNum)
00062     {
00063         case SIGSEGV:
00064             if (bDebug) 
00065             {
00066                 printf("Oops, received SIGSEGV, crash...\n");
00067                 abort();
00068             }
00069             else exit(-1);
00070             break;
00071     }
00072 }
00073 
00074 
00075 int main (int argc, char *argv[])
00076 {
00077     int iRetVal;
00078 
00079     signal(SIGPIPE, SIG_IGN);
00080     signal(SIGFPE, SIG_IGN);
00081     signal(SIGSEGV, SigHandler);
00082     bDebug = false;
00083     bDaemonProc = false;
00084     if (argc >= 2)
00085     {
00086         if (strcmp(argv[1], "--debug") == 0)
00087         {
00088             bDebug = true;
00089         }
00090         if (strcmp(argv[1], "-D") == 0)
00091         {
00092             bDaemonProc = true;
00093         }
00094     }
00095     if (bDebug) printf("lofardemon: Started\n");
00096     LofarDemon = new clLofarDemon(3, 4);
00097     iRetVal = LofarDemon->Exec();
00098     delete LofarDemon;
00099     if (bDebug) printf("lofardemon: Exit = %i\n", iRetVal);
00100     return iRetVal;
00101 }
00102 
00103 
00104 bool clLofarDemon::GetRequestMsg ()
00105 {
00106     char cpMsgBuf[GLOBAL_HEADER_LEN];
00107 
00108     if (!SOpRequest.ReadSelect(LOFAR_REQ_TIMEOUT))
00109         return false;
00110     if (SOpRequest.ReadN(cpMsgBuf, GLOBAL_HEADER_LEN) != GLOBAL_HEADER_LEN)
00111         return false;
00112     LofarMsg.GetRequest(cpMsgBuf, &sLofarRq);
00113     return true;
00114 }
00115 
00116 
00117 bool clLofarDemon::GetDataHdr ()
00118 {
00119     int iTempHandle;
00120     char cpSocketName[_POSIX_PATH_MAX + 1];
00121     stRawDataReq sDataReq;
00122 
00123     Cfg.SetFileName(SD_CFGFILE);
00124     if (!Cfg.GetStr("LocalSocket", cpSocketName))
00125         return false;
00126     iTempHandle = SockClie.Connect(cpSocketName);
00127     if (iTempHandle < 0)
00128         return false;
00129     SOpData.SetHandle(iTempHandle);
00130     if (!SOpData.ReadSelect(LOFAR_RAW1ST_TIMEOUT))
00131         return false;
00132     if (SOpData.ReadN(&sDataHdr, sizeof(sDataHdr)) != sizeof(sDataHdr))
00133         return false;
00134     sDataReq.iChannel = sLofarRq.iChannel;
00135     if (SOpData.WriteN(&sDataReq, sizeof(sDataReq)) != sizeof(sDataReq))
00136         return false;
00137     return true;
00138 }
00139 
00140 
00141 bool clLofarDemon::Initialize ()
00142 {
00143     Cfg.SetFileName(LOFAR_CFGFILE);
00144     if (!InitFilter()) return false;
00145     if (!InitFFT()) return false;
00146     return true;
00147 }
00148 
00149 
00150 bool clLofarDemon::InitFilter ()
00151 {
00152     int iTempInt;
00153     int iBandNo;
00154     long lTwosPower;
00155     float fNyquist;
00156     float fBandWidth;
00157     GDT fLowCorner;
00158     GDT fHighCorner;
00159     GDT fNormBandCenter;
00160     GDT fDemonLowCorner;
00161     GDT fDemonHighCorner;
00162     GDT *fpNullPtr = NULL;
00163 
00164     // Initialize filter
00165     if (Cfg.GetInt("FilterSize", &iTempInt)) 
00166         lFilterSize = iTempInt;
00167     else
00168         lFilterSize = LOFAR_DEF_FILTER_SIZE;
00169     if (!Cfg.GetInt("FilterType", &iFilterType))
00170         iFilterType = clRecDecimator::FILTER_TYPE_FFT;
00171     if (Cfg.GetInt("DEMONDecimation", &iTempInt))
00172         lDemonDecimation = iTempInt;
00173     else
00174         lDemonDecimation = 2;
00175     if (!Cfg.GetInt("DEMONDCNotch", &iDCBlock))
00176         iDCBlock = LOFAR_DEF_DC_BLOCK;
00177     fNyquist = (float) sDataHdr.dSampleRate / 2.0f;
00178     fBandWidth = sLofarRq.fHighFreq - sLofarRq.fLowFreq;
00179     lTwosPower = (long) (log(fNyquist / fBandWidth) / log(2.0) + 0.5);
00180     lDecimation = (long) pow(2.0, lTwosPower);
00181     fBandWidth = fNyquist / lDecimation;
00182     iBandNo = (int) (sLofarRq.fHighFreq / fBandWidth + 0.5f);
00183     if ((fBandWidth * iBandNo) < (sLofarRq.fLowFreq + fBandWidth / 2.0f))
00184         iBandNo++;
00185     fHighCorner = fBandWidth * iBandNo;
00186     fLowCorner = fHighCorner - fBandWidth;
00187     fDemonLowCorner = 0;
00188     fDemonHighCorner = fBandWidth / lDemonDecimation;
00189     fNormBandCenter = (fLowCorner + fBandWidth / 2) / fNyquist;
00190     bReverseOrder = ((iBandNo - 1) % 2 == 0) ? false : true;
00191     sLofarResHdr.fLowFreq = fLowCorner;
00192     sLofarResHdr.fHighFreq = fHighCorner;
00193     sLofarResHdr.fDemonBand = fDemonHighCorner;
00194     if (bDebug)
00195     {
00196         printf("lofardemon: Samplerate: %g  decimation: %li\n",
00197             sDataHdr.dSampleRate, lDecimation);
00198         printf("lofardemon: Lower corner: %.2f  higher corner: %.2f\n",
00199             fLowCorner, fHighCorner);
00200         switch (iFilterType)
00201         {
00202             case clRecDecimator::FILTER_TYPE_FFT:
00203                 puts("lofardemon: Using FFT decimator");
00204                 break;
00205             case clRecDecimator::FILTER_TYPE_FIR:
00206                 puts("lofardemon: Using FIR decimator");
00207                 break;
00208             case clRecDecimator::FILTER_TYPE_IIR:
00209                 puts("lofardemon: Using IIR decimator");
00210                 break;
00211         }
00212         if (!bReverseOrder)
00213             printf("lofardemon: Normal order\n");
00214         else
00215             printf("lofardemon: Reverse order\n");
00216     }
00217     if (!sLofarRq.bDemon)
00218     {
00219         if (lDecimation >= 2l)
00220         {
00221             Decimator.Initialize(lDecimation, lFilterSize, fpNullPtr,
00222                 fNormBandCenter, iFilterType);
00223             if (bDebug)
00224             {
00225                 printf("lofardemon: Filter %.2f - %.2f\n", 
00226                     fLowCorner, fHighCorner);
00227             }
00228         }
00229     }
00230     else
00231     {
00232         if (lDecimation >= 2l)
00233         {
00234             DemonDecimator.Initialize(lDecimation, lFilterSize, fpNullPtr,
00235                 fNormBandCenter, iFilterType);
00236         }
00237         DCBlockIIR.Initialize(fpDCBlock, lDCBlockCount);
00238         Decimator.Initialize(lDemonDecimation, lFilterSize, fpNullPtr,
00239             0.0f, iFilterType);
00240         if (bDebug)
00241         {
00242             printf("lofardemon: DEMON BP filter %.2f - %.2f\n",
00243                 fLowCorner, fHighCorner);
00244             printf("lofardemon: DEMON LP filter %.2f - %.2f\n",
00245                 fDemonLowCorner, fDemonHighCorner);
00246             if (iDCBlock)
00247                 puts("lofardemon: IIR DC notch");
00248         }
00249     }
00250     FlipBand.Initialize(abs(lFilterSize), (GDT *) NULL);
00251     return true;
00252 }
00253 
00254 
00255 bool clLofarDemon::InitFFT ()
00256 {
00257     float fPercMult;
00258     
00259     fpDataBuf = (GDT *) DataBuf.Size(sLofarRq.lWinLength * sizeof(GDT));
00260     lWinSize = sLofarRq.lWinLength;
00261     lSpectSize = sLofarRq.lWinLength / 2L + 1L;
00262     sLofarResHdr.lSpectLength = lSpectSize;
00263     fpWinFunc = (GDT *) WinFunc.Size(sLofarRq.lWinLength * sizeof(GDT));
00264     if (sLofarRq.iWindow < LOFAR_NWINS && bDebug)
00265         printf("lofardemon: %s window\n", cpaWindowFuncs[sLofarRq.iWindow]);
00266     iNormIdx = 0;
00267     if (!Cfg.GetInt("NormHistorySize", &iNormHistCount))
00268         iNormHistCount = 1;
00269     if (sLofarRq.iClip == MSG_LOFAR_CLIP_SLIDING && bDebug)
00270         printf("lofardemon: Normalization sliding average count %i\n",
00271             iNormHistCount);
00272     switch (sLofarRq.iWindow)
00273     {
00274         case MSG_LOFAR_WIN_BARTLETT:
00275             DSP.WinBartlett(fpWinFunc, sLofarRq.lWinLength);
00276             break;
00277         case MSG_LOFAR_WIN_BLACKMAN:
00278             DSP.WinExactBlackman(fpWinFunc, sLofarRq.lWinLength);
00279             break;
00280         case MSG_LOFAR_WIN_BM_HARRIS:
00281             DSP.WinBlackmanHarris(fpWinFunc, sLofarRq.lWinLength);
00282             break;
00283         case MSG_LOFAR_WIN_COS_TAPER:
00284             DSP.WinCosTapered(fpWinFunc, sLofarRq.lWinLength);
00285             break;
00286         case MSG_LOFAR_WIN_EXP:
00287             DSP.WinExp(fpWinFunc, (GDT) 1.0, sLofarRq.lWinLength);
00288             break;
00289         case MSG_LOFAR_WIN_FLATTOP:
00290             DSP.WinFlatTop(fpWinFunc, sLofarRq.lWinLength);
00291             break;
00292         case MSG_LOFAR_WIN_HAMMING:
00293             DSP.WinHamming(fpWinFunc, sLofarRq.lWinLength);
00294             break;
00295         case MSG_LOFAR_WIN_HANNING:
00296             DSP.WinHanning(fpWinFunc, sLofarRq.lWinLength);
00297             break;
00298         case MSG_LOFAR_WIN_KAISER:
00299             DSP.WinKaiser(fpWinFunc, sLofarRq.fWinParameter, 
00300                 sLofarRq.lWinLength);
00301             break;
00302         case MSG_LOFAR_WIN_KAI_BES:
00303             DSP.WinKaiserBessel(fpWinFunc, sLofarRq.fWinParameter, 
00304                 sLofarRq.lWinLength);
00305             break;
00306         case MSG_LOFAR_WIN_TUKEY:
00307             DSP.WinTukey(fpWinFunc, sLofarRq.lWinLength);
00308             break;
00309         case MSG_LOFAR_WIN_RECT:
00310         default:
00311             DSP.Set(fpWinFunc, (GDT) 1.0, sLofarRq.lWinLength);
00312             break;
00313     }
00314     switch (sLofarRq.iType)
00315     {
00316         case MSG_LOFAR_TYPE_AUTOCORR:
00317         case MSG_LOFAR_TYPE_FFT:
00318         case MSG_LOFAR_TYPE_CEPSTRUM:
00319         default:
00320             DSP.FFTInitialize(sLofarRq.lWinLength, true);
00321     }
00322     if (bDebug) 
00323         printf("lofardemon: %li-point FFT initialized\n", sLofarRq.lWinLength);
00324     fpSpectRes = (GDT *) SpectRes.Size(lSpectSize * sizeof(GDT));
00325     fpLofarRes = (GDT *) LofarRes.Size(lSpectSize * sizeof(GDT));
00326     if (fpLofarRes == NULL) return false;
00327     fPercMult = sLofarRq.iOverlap / 100.0f;
00328     lOldDataSize = (long) (sLofarRq.lWinLength * fPercMult + 0.5f);
00329     lNewDataSize = sLofarRq.lWinLength - lOldDataSize;
00330     if (lNewDataSize <= 0)
00331     {
00332         lNewDataSize = 1;
00333         lOldDataSize = sLofarRq.lWinLength - lNewDataSize;
00334     }
00335     else if (lNewDataSize > sLofarRq.lWinLength)
00336     {
00337         lNewDataSize = sLofarRq.lWinLength;
00338         lOldDataSize = 0;
00339     }
00340     HistoryBuf.Size(lSpectSize * sizeof(GDT) * sLofarRq.lMeanLength);
00341     fpNormHist = (GDT *) 
00342         NormHist.Size(lSpectSize * sizeof(GDT) * iNormHistCount);
00343     DSP.Zero(fpNormHist, lSpectSize * iNormHistCount);
00344     return true;
00345 }
00346 
00347 
00348 int clLofarDemon::MainLoop ()
00349 {
00350     long lBlockSize = (long) (abs(lFilterSize) * (1.0 - DSP_FILT_DEF_OVERLAP));
00351     int iRawSize = lBlockSize * sizeof(GDT);
00352     GDT fTempLevel;
00353     #ifdef __GNUG__
00354     GDT fpFilterData[lBlockSize];
00355     GDT fpNewData[lNewDataSize];
00356     GDT fpOldData[lOldDataSize];
00357     #else
00358     clDSPAlloc FilterData;
00359     clDSPAlloc NewData;
00360     clDSPAlloc OldData;
00361     GDT *fpFilterData = (GDT *) FilterData.Size(iRawSize);
00362     GDT *fpNewData = (GDT *) NewData.Size(lNewDataSize * sizeof(GDT));
00363     GDT *fpOldData = (GDT *) OldData.Size(lOldDataSize * sizeof(GDT));
00364     #endif
00365     
00366     iRawSize = lBlockSize * sizeof(GDT);
00367     DSP.Zero(fpOldData, lOldDataSize);
00368     if (SOpData.GetRecvBufSize() < (iRawSize * 2))
00369     {
00370         if (!SOpData.SetRecvBufSize(iRawSize * 2) && bDebug)
00371         {
00372             printf("lofardemon: Failed to set receive buffer size\n");
00373         }
00374     }
00375     if (bDebug) printf("lofardemon: Entering the main loop\n");
00376     while (bRun)
00377     {
00378         if (SOpData.ReadN(fpFilterData, iRawSize) == iRawSize)
00379         {
00380             //sLofarResHdr.fPeakLevel = DSP.PeakLevel(fpFilterData, lFilterSize);
00381             PutData(fpFilterData, lBlockSize);
00382             while (PullData(fpNewData, lNewDataSize) && bRun)
00383             {
00384                 DSP.Copy(fpDataBuf, fpOldData, lOldDataSize);
00385                 DSP.Copy(&fpDataBuf[lOldDataSize], fpNewData, lNewDataSize);
00386                 DSP.Copy(fpOldData, &fpDataBuf[lNewDataSize], lOldDataSize);
00387                 fTempLevel = DSP.PeakLevel(fpDataBuf, sLofarRq.lWinLength);
00388                 if (fTempLevel > sLofarResHdr.fPeakLevel)
00389                     sLofarResHdr.fPeakLevel = fTempLevel;
00390                 gettimeofday(&sLofarResHdr.sTimeStamp, NULL);
00391                 CalcSpect();
00392                 sLofarResHdr.fLineTime = 1.0f /
00393                     (((float) sDataHdr.dSampleRate / (float) lDecimation) /
00394                     (float) lNewDataSize) * sLofarRq.lAvgCount;
00395                 if (sLofarRq.bDemon)
00396                     sLofarResHdr.fLineTime *= lDemonDecimation;
00397                 if (lAvgCntr >= sLofarRq.lAvgCount)
00398                 {
00399                     bRun = SendResults();
00400                     lAvgCntr = 0;
00401                     sLofarResHdr.fPeakLevel = -FLT_MAX;
00402                     DSP.Zero(fpLofarRes, lSpectSize);
00403                 }
00404             }
00405         }
00406         else
00407         {
00408             if (bDebug) printf("lofardemon: Not enough raw data\n");
00409             bRun = false;
00410         }
00411     }
00412     if (bDebug) printf("lofardemon: Exit from the main loop\n");
00413     return 0;
00414 }
00415 
00416 
00417 void clLofarDemon::CalcSpect ()
00418 {
00419     long lSpectCntr;
00420     GDT fScale;
00421     GDT fSpectMin;
00422     GDT fSpectMax;
00423     GDT fSpectMean;
00424     GDT fSpectMedian;
00425     GDT fAvgScale;
00426     GDT fTemp;
00427     #ifdef __GNUG__
00428     GDT fpTempSpect[lWinSize];
00429     GCDT spSpect[lSpectSize];
00430     #else
00431     clDSPAlloc TempSpect;
00432     clDSPAlloc Spect;
00433     GDT *fpTempSpect = 
00434         (GDT *) TempSpect.Size(lWinSize * sizeof(GDT));
00435     GCDT *spSpect = (GCDT *) Spect.Size(lSpectSize * sizeof(GCDT));
00436     #endif
00437 
00438     DSP.Mul(fpDataBuf, fpWinFunc, sLofarRq.lWinLength);
00439     switch (sLofarRq.iType)
00440     {
00441         case MSG_LOFAR_TYPE_AUTOCORR:
00442             DSP.FFTi(spSpect, fpDataBuf);
00443             DSP.Mul(spSpect, spSpect, lSpectSize);
00444             DSP.IFFTo(fpTempSpect, spSpect);
00445             break;
00446         case MSG_LOFAR_TYPE_CEPSTRUM:
00447             DSP.FFTi(spSpect, fpDataBuf);
00448             for (lSpectCntr = 0; lSpectCntr < lSpectSize; lSpectCntr++)
00449             {
00450                 #ifndef HAVE_GLIBC
00451                 spSpect[lSpectCntr].R = log10(sqrt(
00452                     spSpect[lSpectCntr].R * spSpect[lSpectCntr].R +
00453                     spSpect[lSpectCntr].I * spSpect[lSpectCntr].I));
00454                 #else
00455                 spSpect[lSpectCntr].R = log10(hypot(
00456                     spSpect[lSpectCntr].R, spSpect[lSpectCntr].I));
00457                 #endif
00458                 spSpect[lSpectCntr].I = 0;
00459             }
00460             DSP.IFFTo(fpTempSpect, spSpect);
00461             fpTempSpect[0] = 0;
00462             fpTempSpect[1] = 0;
00463             fScale = (GDT) 2 / (GDT) sLofarRq.lWinLength;
00464             DSP.Mul(fpTempSpect, fScale, lSpectSize);
00465             break;
00466         case MSG_LOFAR_TYPE_FFT:
00467         default:
00468             DSP.FFTi(spSpect, fpDataBuf);
00469             if (sLofarRq.bLinear)
00470             {
00471                 DSP.Magnitude(fpTempSpect, spSpect, lSpectSize);
00472             }
00473             else
00474             {
00475                 DSP.Power(fpTempSpect, spSpect, lSpectSize);
00476             }
00477     }
00478     DSP.Copy(fpSpectRes, fpTempSpect, lSpectSize);
00479     if (sLofarRq.bDemon)
00480     {
00481         fpSpectRes[0] = 0;
00482     }
00483     else if (sLofarResHdr.fLowFreq == 0.0f)
00484     {
00485         fpSpectRes[0] = 0;
00486     }
00487     DSP.Scale01(fpSpectRes, lSpectSize);
00488     DSP.Add(fpLofarRes, fpSpectRes, lSpectSize);
00489     lAvgCntr++;
00490     if (lAvgCntr >= sLofarRq.lAvgCount)
00491     {
00492         fAvgScale = (GDT) 1.0 / sLofarRq.lAvgCount;
00493         DSP.Mul(fpLofarRes, fAvgScale, lSpectSize);
00494         switch (sLofarRq.iRemoveNoise)
00495         {
00496             case MSG_LOFAR_BNER_TPSW:
00497                 RemoveNoise.TPSW(fpLofarRes, sLofarRq.fAlpha, 
00498                     sLofarRq.lMeanLength, sLofarRq.lGapLength, lSpectSize);
00499                 break;
00500             case MSG_LOFAR_BNER_OTA:
00501                 RemoveNoise.OTA(fpLofarRes, sLofarRq.fAlpha, 
00502                     sLofarRq.lMeanLength, lSpectSize);
00503                 break;
00504             case MSG_LOFAR_BNER_DIFF:
00505                 RemoveNoise.Diff(fpLofarRes, sLofarRq.fAlpha, lSpectSize);
00506                 break;
00507             case MSG_LOFAR_BNER_IDIFF:
00508                 RemoveNoise.InvDiff(fpLofarRes, sLofarRq.fAlpha, lSpectSize);
00509                 break;
00510             case MSG_LOFAR_BNER_STDDEV:
00511                 SpectStdDev(fpLofarRes);
00512                 break;
00513             case MSG_LOFAR_BNER_NONE:
00514             default:
00515                 // None
00516                 break;
00517         }
00518         switch (sLofarRq.iClip)
00519         {
00520             case MSG_LOFAR_CLIP_LOW:
00521                 DSP.ClipZero(fpLofarRes, lSpectSize);
00522                 DSP.Scale01(fpLofarRes, lSpectSize);
00523                 break;
00524             case MSG_LOFAR_CLIP_BOTH:
00525                 DSP.Clip(fpLofarRes, (GDT) 0.0, (GDT) 1.0, lSpectSize);
00526                 DSP.Scale01(fpLofarRes, lSpectSize);
00527                 break;
00528             case MSG_LOFAR_CLIP_MEAN:
00529                 fSpectMean = DSP.Mean(fpLofarRes, lSpectSize);
00530                 DSP.Clip(fpLofarRes, fSpectMean, lSpectSize);
00531                 DSP.Scale01(fpLofarRes, lSpectSize);
00532                 break;
00533             case MSG_LOFAR_CLIP_MEDIAN:
00534                 fSpectMedian = DSP.Median(fpLofarRes, lSpectSize);
00535                 DSP.Clip(fpLofarRes, fSpectMedian, lSpectSize);
00536                 DSP.Scale01(fpLofarRes, lSpectSize);
00537                 break;
00538             case MSG_LOFAR_CLIP_10DB:
00539                 fSpectMean = DSP.Mean(fpLofarRes, lSpectSize);
00540                 fSpectMean *= (GDT) pow(10.0, 0.5);
00541                 DSP.Clip(fpLofarRes, (GDT) 0.0, fSpectMean, lSpectSize);
00542                 DSP.Scale01(fpLofarRes, lSpectSize);
00543                 break;
00544             case MSG_LOFAR_CLIP_20DB:
00545                 fSpectMean = DSP.Mean(fpLofarRes, lSpectSize);
00546                 fSpectMean *= (GDT) 10.0;
00547                 DSP.Clip(fpLofarRes, (GDT) 0.0, fSpectMean, lSpectSize);
00548                 DSP.Scale01(fpLofarRes, lSpectSize);
00549                 break;
00550             case MSG_LOFAR_CLIP_50P:
00551                 DSP.MinMax(&fSpectMin, &fSpectMax, fpLofarRes, lSpectSize);
00552                 DSP.Clip(fpLofarRes, (GDT) 0.0, fSpectMax * 0.5f, lSpectSize);
00553                 DSP.Scale01(fpLofarRes, lSpectSize);
00554                 break;
00555             case MSG_LOFAR_CLIP_75P:
00556                 DSP.MinMax(&fSpectMin, &fSpectMax, fpLofarRes, lSpectSize);
00557                 DSP.Clip(fpLofarRes, (GDT) 0.0, fSpectMax * 0.75f, lSpectSize);
00558                 DSP.Scale01(fpLofarRes, lSpectSize);
00559                 break;
00560             case MSG_LOFAR_CLIP_OFFSET2:
00561                 DSP.Clip(fpLofarRes, (GDT) 3.33333, lSpectSize);
00562             case MSG_LOFAR_CLIP_OFFSET:
00563                 DSP.MinMax(&fSpectMin, &fSpectMax, fpLofarRes, lSpectSize);
00564                 fTemp = (fSpectMax == (GDT) 0.0) ? FLT_EPSILON : fSpectMax;
00565                 DSP.MulAdd(fpLofarRes, 
00566                     (GDT) 1.0 / fTemp * (GDT) 0.8, 
00567                     (GDT) 0.2,
00568                     lSpectSize);
00569                 DSP.ClipZero(fpLofarRes, lSpectSize);
00570                 break;
00571             case MSG_LOFAR_CLIP_OFFSET3:
00572                 DSP.Copy(fpTempSpect, fpLofarRes, lSpectSize);
00573                 DSP.Sort(fpTempSpect, lSpectSize);
00574                 fTemp = fpTempSpect[(long) (lSpectSize * 0.99)];
00575                 DSP.Clip(fpLofarRes, fTemp, lSpectSize);
00576                 DSP.MinMax(&fSpectMin, &fSpectMax, fpLofarRes, lSpectSize);
00577                 fTemp = (fSpectMax == (GDT) 0.0) ? FLT_EPSILON : fSpectMax;
00578                 DSP.MulAdd(fpLofarRes, 
00579                     (GDT) 1.0 / fTemp * (GDT) 0.8, 
00580                     (GDT) 0.2,
00581                     lSpectSize);
00582                 DSP.ClipZero(fpLofarRes, lSpectSize);
00583                 break;
00584             case MSG_LOFAR_CLIP_SLIDING:
00585                 DSP.Copy(&fpNormHist[iNormIdx * lSpectSize], fpLofarRes,
00586                     lSpectSize);
00587                 DSP.Zero(fpTempSpect, lSpectSize);
00588                 for (lSpectCntr = 0; 
00589                     lSpectCntr < iNormHistCount;
00590                     lSpectCntr++)
00591                 {
00592                     DSP.Add(fpTempSpect, &fpNormHist[lSpectCntr * lSpectSize],
00593                         lSpectSize);
00594                 }
00595                 DSP.Mul(fpTempSpect, (GDT) 1.0 / iNormHistCount,
00596                     lSpectSize);
00597                 DSP.MinMax(&fSpectMin, &fSpectMax, fpTempSpect, lSpectSize);
00598                 fSpectMax = (fSpectMax == (GDT) 0.0) ? FLT_EPSILON : fSpectMax;
00599                 fScale = (GDT) 1.0 / fSpectMax * (GDT) 0.8;
00600                 DSP.MulAdd(fpLofarRes, fScale, (GDT) 0.2, lSpectSize);
00601                 iNormIdx++;
00602                 if (iNormIdx >= iNormHistCount)
00603                     iNormIdx = 0;
00604                 break;
00605             case MSG_LOFAR_CLIP_NONE:
00606             default:
00607                 DSP.Scale01(fpLofarRes, lSpectSize);
00608                 break;
00609         }
00610     }
00611 }
00612 
00613 
00614 bool clLofarDemon::SendResults ()
00615 {
00616     int iMsgSize = GLOBAL_HEADER_LEN + sizeof(GDT) * lSpectSize;
00617     #ifdef __GNUG__
00618     char cpMsgBuf[iMsgSize];
00619     #else
00620     char *cpMsgBuf;
00621     clDSPAlloc MsgBuf;
00622     cpMsgBuf = (char *) MsgBuf.Size(iMsgSize);
00623     #endif
00624 
00625     LofarMsg.SetResult(cpMsgBuf, &sLofarResHdr, fpLofarRes);
00626     if (SOpResult.WriteSelect(LOFAR_TIMEOUT))
00627     {
00628         if (SOpResult.WriteN(cpMsgBuf, iMsgSize) != iMsgSize)
00629         {
00630             if (bDebug) 
00631                 printf("lofardemon: Write error, client disconnected?\n");
00632             return false;
00633         }
00634     }
00635     else if (SOpResult.GetErrno() != 0)
00636     {
00637         if (bDebug) printf("lofardemon: Client disconnected\n");
00638         return false;
00639     }
00640     return true;
00641 }
00642 
00643 
00644 void clLofarDemon::PutData (GDT *fpProcBuf, long lProcLen)
00645 {
00646     if (!sLofarRq.bDemon)
00647     {
00648         if (lDecimation >= 2l)  // normal case
00649         {
00650             Decimator.Put(fpProcBuf, lProcLen);
00651             if (bReverseOrder)
00652             {
00653                 while (Decimator.Get(fpProcBuf, lProcLen))
00654                     FlipBand.Put(fpProcBuf, lProcLen);
00655             }
00656         }
00657         else  // special case without decimation
00658         {
00659             ReBuffer.Put(fpProcBuf, lProcLen);
00660         }
00661     }
00662     else
00663     {
00664         if (lDecimation >= 2l)  // normal case
00665         {
00666             DSP.Square(fpProcBuf, lProcLen);
00667             //DemonProc(fpProcBuf, lProcLen);
00668             if (iDCBlock) DCBlockIIR.Process(fpProcBuf, lProcLen);
00669             DemonDecimator.Put(fpProcBuf, lProcLen);
00670             if (bReverseOrder)
00671             {
00672                 while (DemonDecimator.Get(fpProcBuf, lProcLen))
00673                     FlipBand.Put(fpProcBuf, lProcLen);
00674                 while (FlipBand.Get(fpProcBuf, lProcLen))
00675                     Decimator.Put(fpProcBuf, lProcLen);
00676             }
00677             else
00678             {
00679                 while (DemonDecimator.Get(fpProcBuf, lProcLen))
00680                     Decimator.Put(fpProcBuf, lProcLen);
00681             }
00682         }
00683         else  // special case without decimation
00684         {
00685             DSP.Square(fpProcBuf, lProcLen);
00686             //DemonProc(fpProcBuf, lProcLen);
00687             if (iDCBlock) DCBlockIIR.Process(fpProcBuf, lProcLen);
00688             Decimator.Put(fpProcBuf, lProcLen);
00689         }
00690     }
00691 }
00692 
00693 
00694 bool clLofarDemon::PullData (GDT *fpDestBuf, long lDestLen)
00695 {
00696     bool bGetRes;
00697 
00698     if (lDecimation >= 2l && (!sLofarRq.bDemon))  // normal case
00699     {
00700         if (bReverseOrder)
00701             bGetRes = FlipBand.Get(fpDestBuf, lDestLen);
00702         else
00703             bGetRes = Decimator.Get(fpDestBuf, lDestLen);
00704     }
00705     else if (sLofarRq.bDemon)
00706     {
00707         bGetRes = Decimator.Get(fpDestBuf, lDestLen);
00708     }
00709     else  // special case without decimation
00710     {
00711         bGetRes = ReBuffer.Get(fpDestBuf, lDestLen);
00712     }
00713 
00714     return bGetRes;
00715 }
00716 
00717 
00718 void clLofarDemon::DemonProc (GDT *fpProcBuf, long lProcLen)
00719 {
00720     long lProcCntr;
00721 
00722     for (lProcCntr = 0L; lProcCntr < lProcLen; lProcCntr++)
00723     {
00724         fpProcBuf[lProcCntr] = fpProcBuf[lProcCntr] * fpProcBuf[lProcCntr];
00725     }
00726 }
00727 
00728 
00729 void clLofarDemon::SpectStdDev (GDT *fpSpect)
00730 {
00731     long lBinCntr;
00732     long lHistLen = sLofarRq.lMeanLength;
00733     GDT fStdDev;
00734     GDT fMean;
00735     GDT *fpHistory = HistoryBuf;
00736     #ifdef __GNUG__
00737     GDT fpBinHist[lHistLen];
00738     #else
00739     clDSPAlloc BinHist;
00740     GDT *fpBinHist = (GDT *) BinHist.Size(lHistLen * sizeof(GDT));
00741     #endif
00742 
00743     if (lHistCntr >= lHistLen) lHistCntr = 0L;
00744     DSP.Copy(&fpHistory[lSpectSize * lHistCntr], fpSpect, lSpectSize);
00745     for (lBinCntr = 0L; lBinCntr < lSpectSize; lBinCntr++)
00746     {
00747         for (lHistCntr = 0L; lHistCntr < lHistLen; lHistCntr++)
00748         {
00749             fpBinHist[lHistCntr] = 
00750                 fpHistory[lSpectSize * lHistCntr + lBinCntr];
00751         }
00752         DSP.StdDev(&fStdDev, &fMean, fpBinHist, lHistLen);
00753         fpSpect[lBinCntr] = fStdDev;
00754     }
00755 }
00756 
00757 
00758 clLofarDemon::clLofarDemon (int iInHandle, int iOutHandle)
00759 {
00760     lAvgCntr = 0;
00761     lHistCntr = 0;
00762     fpWinFunc = NULL;
00763     fpDataBuf = NULL;
00764     fpSpectRes = NULL;
00765     fpLofarRes = NULL;
00766     bRun = true;
00767     SOpRequest.SetHandle(iInHandle);
00768     SOpResult.SetHandle(iOutHandle);
00769     sLofarResHdr.fPeakLevel = -FLT_MAX;
00770 }
00771 
00772 
00773 clLofarDemon::~clLofarDemon ()
00774 {
00775 }
00776 
00777 
00778 int clLofarDemon::Exec ()
00779 {
00780     if (!GetRequestMsg())
00781     {
00782         if (bDebug) printf("lofardemon: GetRequestMsg() timed out\n");
00783         return 1;
00784     }
00785     if (!GetDataHdr())
00786     {
00787         if (bDebug) 
00788             printf("lofardemon: Unable to get header from streamdist\n");
00789         return 1;
00790     }
00791     if (!Initialize())
00792     {
00793         if (bDebug)
00794             printf("lofardemon: Initialization failed\n");
00795         return 1;
00796     }
00797     return MainLoop();
00798 }
00799 

Generated on Sun Oct 26 19:11:21 2003 for HASAS by doxygen 1.3.3