//vcDP5 SDK #include "stdafx.h" #include ".\tuneslowthreshold.h" #include "DppSlowThreshold.h" #include "UserMsgs.h" CTuneSlowThreshold::CTuneSlowThreshold(void) { } CTuneSlowThreshold::~CTuneSlowThreshold(void) { } void CTuneSlowThreshold::GetSpectrumData(double * dblMCAData, long * lngMCAData, int mcaCH) { for (int chan=0; chan < mcaCH; chan++) dblMCAData[chan] = lngMCAData[chan]; } double CTuneSlowThreshold::SetSlowThreshToChPos(int ChPosIn, int mcaCH) { CDppSlowThreshold SlowThreshNew; CString cstrChPos; int ChPos; double FSReal; UCHAR SlowChThreshold; ChPos = ChPosIn; if (ChPos == 0) ChPos = 1; SlowThreshNew.SetGain(mcaCH); SlowThreshNew.GetSlowThresholdSettingFromChannels((double)ChPos); SlowChThreshold = SlowThreshNew.GetSlowChThreshold(); FSReal = (double)SlowChThreshold / 1024.0; return FSReal; } int CTuneSlowThreshold::GetSlowThreshCalStart(double * SpectrumBuffer, double UpperChMax, int mcaCH) { int MinValidChan = 40; int MaxValidChan = 245; int LastSlwCh = (int)(mcaCH * 0.24); int StartCh = LastSlwCh; CString cstrStatus; //DisplaySpecBuf25(SpectrumBuffer); for (int i = LastSlwCh; i >= 0; i--) { if (SpectrumBuffer[i] <= UpperChMax) { if (i < StartCh) StartCh = i; cstrStatus.Format(" Finding Slow Threshold Test Channel %d ...", i); } else { break; } } if ((StartCh > 1) && (StartCh < LastSlwCh - 1)) StartCh++; // remove the last (bad) ch // try to tweak 3x if really bad if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } if ((SpectrumBuffer[StartCh] > UpperChMax) && (StartCh < LastSlwCh - 1)) { StartCh++; } MinValidChan = GetSlowThreshChStrFromVal(6, mcaCH); if (StartCh < MinValidChan) { // set to >= 0.5% FS if no slow thresh needed StartCh = MinValidChan; } MaxValidChan = GetSlowThreshChStrFromVal(245, mcaCH); if (StartCh > MaxValidChan) { // set to >= 24.02% FS if no slow thresh needed StartCh = MaxValidChan; AfxMessageBox("Slow threshold value too high. \n Please check your settings.", MB_TOPMOST | MB_OK | MB_ICONSTOP); } return StartCh; } double CTuneSlowThreshold::GetUpperChCounts(double * SpectrumBuffer, int mcaCH) { int LastSlwCh = (int)(mcaCH * 0.24); double ChCounts = 0; double CurChCount = 0; for (int i = LastSlwCh; i < mcaCH; i++) { CurChCount = SpectrumBuffer[i]; if (CurChCount > 0) { ChCounts += CurChCount; } } return ChCounts; } double CTuneSlowThreshold::GetUpperChMax(double * SpectrumBuffer, int mcaCH) { int LastSlwCh = (int)(mcaCH * 0.24); double ChCount = 0; double CurChCount = 0; for (int i = LastSlwCh; i < mcaCH; i++) { CurChCount = SpectrumBuffer[i]; if (CurChCount > ChCount) ChCount = CurChCount; } return ChCount; } void CTuneSlowThreshold::DelayTask(DWORD d) { MSG msg; d += GetCurrentTime(); while (GetCurrentTime() < d) { if (PeekMessage(&msg, NULL, NULL, NULL, PM_REMOVE)) { TranslateMessage(&msg); DispatchMessage(&msg); } } } int CTuneSlowThreshold::UpdatePlotCursorPosFromIntChPos(int CurCh, double dblPixPerChan) { double dblPixPos; double dblChanPerPix; if (dblPixPerChan > 0.0) { dblChanPerPix = 1.0 / dblPixPerChan; dblPixPos = (double)CurCh * dblChanPerPix; int dpos = (int)dblPixPos; return dpos; } else { return CurCh; } } void CTuneSlowThreshold::DisplaySpecBuf25(double * SpectrumBuffer) { CString cstrBuffer(""); CString cstrChan(""); for (int i=0;i<25;i++) { cstrChan.Format("%f\n",SpectrumBuffer[i]); cstrBuffer += cstrChan; } AfxMessageBox(cstrBuffer); } ////@mfunc Get Slow Threshold display text string from Slow Threshold value. ////@parm Slow Threshold value ////@rdesc Slow Threshold display text string int CTuneSlowThreshold::GetSlowThreshChStrFromVal(UCHAR byteSlowThres, int mcaCH) { long intSlowThresh; double GainChannels; GainChannels = (double)mcaCH; intSlowThresh = (int)(((double)byteSlowThres * GainChannels / 1024.0) + 0.5); return intSlowThresh; }