void testAbsSimplePixelVerification() { std::cout << std::endl << "GPU ABS DIFF VERIFICATION TEST" << std::endl; int cuda_device_id = 0; cvt::Tiler read_tiler; cv::Size2i tSize(256,256); read_tiler.setCvTileSize(tSize); read_tiler.open("test1.tif"); cvt::cvTile<unsigned short> inputTile; inputTile = read_tiler.getCvTile<unsigned short>(4); std::vector<unsigned short> data(tSize.area(),0); cvt::cvTile<unsigned short> zeroedTile(data.data(),tSize,1); cvt::gpu::GpuAbsoluteDifference<unsigned short,1,unsigned short,1> absDiff(cuda_device_id, tSize.width,tSize.height); absDiff.initializeDevice(); cvt::cvTile<unsigned short> *outputTile; absDiff(inputTile,zeroedTile,(const cvt::cvTile<unsigned short> **)&outputTile); if (!outputTile) { std::cout << "FAILURE TO GET DATA FROM DEVICE" << std::endl; return; } TS_ASSERT_EQUALS(outputTile->getBandCount(),1); for (int i = 0; i < tSize.area(); ++i) { const size_t row = i / tSize.height; const size_t col = i % tSize.width; TS_ASSERT_EQUALS(inputTile[0].at<unsigned short>(row,col),(*outputTile)[0].at<unsigned short>(row,col)); } read_tiler.close(); }
static const T_choice *IFindChoiceDown(const T_choice *aChoiceList, const T_choice *aSelectedChoice, TUInt32 aSearchRange) { const T_choice *p; TUInt32 ySmallestDiff = UEZPlatform_LCDGetHeight(); TUInt32 xDiff, yDiff; const T_choice *p_nextChoice = aSelectedChoice; for (p = aChoiceList; p->iText; p++) { if(p != aSelectedChoice) { yDiff = yMid(p) - yMid(aSelectedChoice); xDiff = absDiff(xMid(aSelectedChoice), xMid(p)); if((yDiff < ySmallestDiff) && (xDiff<(yDiff/aSearchRange))) { ySmallestDiff = yDiff; p_nextChoice = p; } } } // If we didn't find a new, choice. Lets wrap around our search if(p_nextChoice == aSelectedChoice) { ySmallestDiff = UEZPlatform_LCDGetHeight(); for (p = aChoiceList; p->iText; p++) { if(p != aSelectedChoice) { yDiff = yMid(aSelectedChoice); xDiff = absDiff(xMid(aSelectedChoice), xMid(p)); if((yDiff < ySmallestDiff) && (xDiff<(yDiff/aSearchRange))) { ySmallestDiff = yDiff; p_nextChoice = p; } } } } return p_nextChoice; }
static const T_choice *IFindChoiceLeft(const T_choice *aChoiceList, const T_choice *aSelectedChoice, TUInt32 aSearchRange) { const T_choice *p; TUInt32 xSmallestDiff = UEZPlatform_LCDGetWidth(); TUInt32 xDiff, yDiff; const T_choice *p_nextChoice = aSelectedChoice; for (p = aChoiceList; p->iText; p++) { if(p != aSelectedChoice) { xDiff = xMid(aSelectedChoice) - xMid(p); yDiff = absDiff(yMid(aSelectedChoice), yMid(p)); if((xDiff < xSmallestDiff) && (yDiff<(xDiff/aSearchRange))) { xSmallestDiff = xDiff; p_nextChoice = p; } } } // If we didn't find a new, choice. Lets wrap around our search if(p_nextChoice == aSelectedChoice) { xSmallestDiff = UEZPlatform_LCDGetWidth(); for (p = aChoiceList; p->iText; p++) { if(p != aSelectedChoice) { xDiff = UEZPlatform_LCDGetWidth() - xMid(p); yDiff = absDiff(yMid(p), yMid(aSelectedChoice)); if((xDiff < xSmallestDiff) && (yDiff<(xDiff/aSearchRange))) { xSmallestDiff = xDiff; p_nextChoice = p; } } } } return p_nextChoice; }
void testAbsDiffFullPixelVerification () { std::cout << std::endl << "GPU ABS DIFF VERIFICATION TEST" << std::endl; int cuda_device_id = 0; cvt::Tiler read_tiler; cvt::Tiler read_tiler2; cv::Size2i tSize(256,256); read_tiler.setCvTileSize(tSize); read_tiler2.setCvTileSize(tSize); read_tiler.open("test1.tif"); read_tiler2.open("test1-1.tif"); cvt::cvTile<unsigned short> inputTile; cvt::cvTile<unsigned short> inputTile2; inputTile = read_tiler.getCvTile<unsigned short>(4); inputTile2 = read_tiler2.getCvTile<unsigned short>(4); cvt::gpu::GpuAbsoluteDifference<unsigned short,1,unsigned short,1> absDiff(cuda_device_id, tSize.width,tSize.height); absDiff.initializeDevice(); cvt::cvTile<unsigned short> *outputTile; absDiff(inputTile,inputTile2,(const cvt::cvTile<unsigned short> **)&outputTile); if (!outputTile) { std::cout << "FAILURE TO GET DATA FROM DEVICE" << std::endl; return; } TS_ASSERT_EQUALS(outputTile->getBandCount(),1); /*Calculate Window Histogram Statistics for each pixel*/ cv::Size2i dims = inputTile.getSize(); const int area = dims.width * dims.height; std::vector<int> results; results.resize(area); for (int i = 0; i < area; ++i) { const size_t row = i / tSize.height; const size_t col = i % tSize.width; const short diff = abs(inputTile[0].at<unsigned short>(row,col) - inputTile2[0].at<unsigned short>(row,col)); results[i] = diff > 0 ? diff : -diff; } for (size_t s = 0; s < results.size(); ++s) { const size_t row = s / tSize.height; const size_t col = s % tSize.width; TS_ASSERT_EQUALS((unsigned short)results[s],(*outputTile)[0].at<unsigned short>(row,col)); } delete outputTile; read_tiler.close(); read_tiler2.close(); }
void dfs(TreeNode* root, double target) { if (root == NULL) return; double mydiff = absDiff(root->val, target); mheap.push(make_pair(mydiff, root->val)); dfs(root->left, target); dfs(root->right, target); }
// ###################################################################### void IntegerFlickerChannel::doInputInt(const IntegerInput& inp, const SimTime& t, PyramidCache<int>* cache, const Image<byte>& clipMask) { GVX_TRACE(__PRETTY_FUNCTION__); ASSERT(inp.grayInt().initialized()); // If this is the first time the flicker channel has seen input, // then itsPrevInput will be uninitialized; obviously we can't // compute any flicker with only one frame, so we just store the // current input as the next iteration's previous input if (!itsPrevInput.initialized()) { itsPrevInput = inp.grayInt(); } else { // take simple abs difference between current and previous // frame: const Image<int> fli = absDiff(inp.grayInt(), itsPrevInput); IntegerSimpleChannel::doInputInt(IntegerInput::fromGrayOnly(fli), t, cache, clipMask); // update the previous image: itsPrevInput = inp.grayInt(); } }
void TComb::buildDiffMask(TCombFrame *tf1, TCombFrame *tf2, int lc, IScriptEnvironment *env) { absDiff(tf1->orig, tf2->orig, tf2->msk1, lc, env); for (int i = 0; i<5; ++i) absDiffAndMinMask(tf1->b[i], tf2->b[i], tf2->msk1, lc, env); absDiffAndMinMaskThresh(tf1->b[5], tf2->b[5], tf2->msk1, lc, env); }
Strategy::statusType StartInfoStrategy::doStrategy() { bool cell_nr, v_balance, v_out = false, balance; //ign v_out = false I need to charge 0-voltage batt's uint8_t is_cells, should_be_cells; cell_nr = v_balance = false; // v_out = ! AnalogInputs::isConnected(AnalogInputs::Vout); //ign I need to charge 0-voltage batt's // if(ProgramData::battery.type == ProgramData::UnknownBatteryType || ProgramData::battery.type == ProgramData::LED) { // v_out = false; // } is_cells = AnalogInputs::getConnectedBalancePortCellsCount(); if(Strategy::doBalance) { should_be_cells = ProgramData::battery.cells; cell_nr = (should_be_cells != is_cells); v_balance = (is_cells == 0); if(should_be_cells == 1 && is_cells == 0) { //one cell cell_nr = false; v_balance = false; } } if(AnalogInputs::isBalancePortConnected() && absDiff(AnalogInputs::getRealValue(AnalogInputs::Vout), AnalogInputs::getRealValue(AnalogInputs::Vbalancer)) > ANALOG_VOLT(0.5)) v_out = true; Screen::blink.blinkIndex_ = 7; if(cell_nr) Screen::blink.blinkIndex_ -= 4; if(v_balance) Screen::blink.blinkIndex_ -= 2; if(v_out) Screen::blink.blinkIndex_ -= 1; if(cell_nr || v_balance || v_out) { Buzzer::soundInfo(); } else { Buzzer::soundOff(); } balance = (v_balance || cell_nr) && (is_cells != 0); if(Keyboard::getLast() == BUTTON_NONE) ok_ = 0; if(!balance && !v_out && Keyboard::getLast() == BUTTON_START) { ok_++; } if(ok_ == 2) { return Strategy::COMPLETE; } return Strategy::RUNNING; }
::testing::AssertionResult SimdBaseTest::compareVectorRealUlp(const char * refExpr, const char * tstExpr, const std::vector<real> &ref, const std::vector<real> &tst) { std::vector<real> absDiff(tst.size()); std::vector<gmx_int64_t> ulpDiff(tst.size()); bool allOk; size_t i; union { #ifdef GMX_DOUBLE double r; gmx_int64_t i; #else float r; gmx_int32_t i; #endif } conv0, conv1; // Internal test of the test - make sure reference and test have the same length. if (ref.size() != tst.size()) { return ::testing::AssertionFailure() << "Internal test error - unequal size vectors in compareVectorRealUlp" << std::endl; } for (i = 0, allOk = true; i < tst.size(); i++) { absDiff[i] = fabs(ref[i]-tst[i]); conv0.r = ref[i]; conv1.r = tst[i]; ulpDiff[i] = llabs(conv0.i-conv1.i); /* Use strict smaller-than for absolute tolerance check, so we disable it with absTol_=0 */ allOk = allOk && ( ( absDiff[i] < absTol_ ) || ( ( ref[i]*tst[i] >= 0 ) && (ulpDiff[i] <= ulpTol_) ) ); } if (allOk == true) { return ::testing::AssertionSuccess(); } else { return ::testing::AssertionFailure() << "Failing comparison between " << refExpr << " and " << tstExpr << std::endl << "Requested abs tolerance: " << absTol_ << std::endl << "Requested ulp tolerance: " << ulpTol_ << std::endl << "(And values should not differ in sign unless within abs tolerance.)" << std::endl << "Reference values: " << ::testing::PrintToString(ref) << std::endl << "SIMD values: " << ::testing::PrintToString(tst) << std::endl << "Abs. difference: " << ::testing::PrintToString(absDiff) << std::endl << "Ulp difference: " << ::testing::PrintToString(ulpDiff) << std::endl; } }
// ###################################################################### void TaskRelevanceMapKillStatic::inputFrame(const InputFrame& f) { // NOTE: we are guaranteed that itsMap is initialized and of correct // size, as this is done by the TaskRelevanceMapAdapter before // calling us. // NOTE: this is duplicating some of the computation done in the // IntensityChannel, so there is a tiny performance hit (~0.25%) in // doing this operation twice; however the benefit is that we avoid // having to somehow pick information out of an IntensityChannel and // pass it to a TaskRelevanceMap -- that was making for a mess in // Brain. In the future, we could avoid the ineffiency by caching // the intensity pyramid in the InputFrame object, and then letting // IntensityChannel and TaskRelevanceMap both share access to that // pyramid. const Image<float> img = downSize(f.grayFloat(), itsMap.getWidth(), itsMap.getHeight(), 5); // is this our first time here? if (itsStaticBuff.initialized() == false) { itsStaticBuff = img; return; } // otherwise derive TRM from difference between current frame and // staticBuf: Image<float> diff = absDiff(itsStaticBuff, img); // useful range of diff is 0..255; anything giving an output less // that itsKillStaticThresh will be considered static; here let's // clamp to isolate that: inplaceClamp(diff, 0.0F, itsKillStaticThresh.getVal()); // the more static, the greater the killing: so, first let's change // the range so that the min is at -itsKillStaticThresh and zero is // neutral: diff -= itsKillStaticThresh.getVal(); // itsKillStaticCoeff determines how strong the killing should be: diff *= (1.0F / itsKillStaticThresh.getVal()) * // normalize to min at -1.0 itsKillStaticCoeff.getVal(); // apply coeff // mix our new TRM to the old one; we want to have a fairly high // mixing coeff for the new one so that we don't penalize recent // onset objects too much (i.e., were previously static and killed, // but are not anymore): itsMap = itsMap * 0.25F + (diff + 1.0F) * 0.75F; float mi, ma; getMinMax(itsMap, mi, ma); LINFO("TRM range = [%.2f .. %.2f] -- 1.0 is baseline", mi, ma); // update our cumulative buffer: itsStaticBuff = itsStaticBuff * 0.75F + img * 0.25F; }
void Thevenin::calculateRth(AnalogInputs::ValueType v, AnalogInputs::ValueType i) { if(absDiff(i, ILast_) > ILastDiff_/2 && absDiff(v, VLast_) > 0) { int16_t rth_v; uint16_t rth_i; if(i > ILast_) { rth_i = i; rth_i -= ILast_; rth_v = v; rth_v -= VLast_; } else { rth_v = VLast_; rth_v -= v; rth_i = ILast_; rth_i -= i; } if(sign(rth_v) == sign(Rth.iV)) { ILastDiff_ = rth_i; Rth.iV = rth_v; Rth.uI = rth_i; } } }
/** * @brief This function paints a * @param x1 Starting point x coordinate. * @param y1 Starting point y coordinate. * @param x2 Finish point x coordinate. * @param y2 Finish point y coordinate. * @param color Color of the line. * @return none */ void TFT_Line(unsigned int x1, unsigned int y1, unsigned int x2, unsigned int y2, unsigned int color) { unsigned int deltax, deltay, x,y; unsigned char steep; int lerror, ystep; steep = absDiff(y1,y2) > absDiff(x1,x2); //check slope if (steep) { swap(x1, y1); swap(x2, y2); } if (x1 > x2) { swap(x1, x2); swap(y1, y2); } deltax = x2 - x1; deltay = absDiff(y2,y1); lerror = deltax / 2; y = y1; if(y1 < y2) ystep = 1; else ystep = -1; for(x = x1; x <= x2; x++) { if (steep) TFT_SetPixel(y,x, color); else TFT_SetPixel(x,y, color); lerror -= deltay; if (lerror < 0){ y = y + ystep; lerror += deltax; } } }
Strategy::statusType StartInfoStrategy::doStrategy() { bool cell_nr, v_balance, v_out, balance; cell_nr = v_balance = false; v_out = ! AnalogInputs::isConnected(AnalogInputs::Vout); if(balancePort_) { uint8_t is_cells, should_be_cells; is_cells = AnalogInputs::getConnectedBalancePorts(); should_be_cells = ProgramData::currentProgramData.battery.cells; cell_nr = (should_be_cells != is_cells); v_balance = (is_cells == 0); if(should_be_cells == 1 && is_cells == 0) { //one cell cell_nr = false; v_balance = false; } } if(AnalogInputs::isConnected(AnalogInputs::Vbalancer) && absDiff(AnalogInputs::getRealValue(AnalogInputs::Vout), AnalogInputs::getRealValue(AnalogInputs::Vbalancer)) > ANALOG_VOLT(0.5)) v_out = true; Screen::blink.blinkIndex_ = 7; if(cell_nr) Screen::blink.blinkIndex_ -= 4; if(v_balance) Screen::blink.blinkIndex_ -= 2; if(v_out) Screen::blink.blinkIndex_ -= 1; if(cell_nr || v_balance || v_out) { Buzzer::soundInfo(); } else { Buzzer::soundOff(); } balance = (v_balance || cell_nr) && settings.forceBalancePort_; if(Keyboard::getPressed() == BUTTON_NONE) ok_ = 0; if(!balance && !v_out && Keyboard::getPressed() == BUTTON_START) { ok_++; } if(ok_ == 2) { return Strategy::COMPLETE; } return Strategy::RUNNING; }
/* * This function sets the SSP Bit Rate * * Parameter: * desiredBitRate - Desired bit rate to be set * * Return: * The actual bit rate that is set. */ uint32_t sspCalculateBitRate( uint32_t desiredBitRate, unsigned char *pSerialClockRate, unsigned char *pClockPrescale ) { uint32_t inputClock, difference, calculatedBitRate; uint32_t bestDifference = 0xffffffff; uint32_t serialClockRate, clockPrescale, bestSCR, bestPrescale; uint32_t value; /* Get the SSP input clock, which is the master clock */ inputClock = getMasterClock(); /* Calculate the best possible value. The clock divider / clock prescaler is an even value from 2 to 254. */ for (clockPrescale = 2; clockPrescale < 256; clockPrescale+=2) { /* The serial clock rate is the value between 0 to 255. */ for (serialClockRate = 0; serialClockRate < 256; serialClockRate++) { calculatedBitRate = inputClock / (clockPrescale * (1 + serialClockRate)); difference = absDiff(calculatedBitRate, desiredBitRate); /* If the difference is less than the current, use it. */ if (difference < bestDifference) { /* Store best difference. */ bestDifference = difference; /* Store the frequency clock values based on the best difference. */ bestPrescale = clockPrescale; bestSCR = serialClockRate; } } } if (pSerialClockRate != (unsigned char *) 0) *pSerialClockRate = bestSCR; if (pClockPrescale != (unsigned char *) 0) *pClockPrescale = bestPrescale; return (inputClock / (bestPrescale * (1 + bestSCR))); }
void USObstacleGridProvider::line(const Vector2<int>& start, const Vector2<int>& end, const FrameInfo& theFrameInfo) { Vector2<int> diff = end - start, inc(diff.x > 0 ? 1 : -1, (diff.y > 0 ? 1 : -1) * (&cells[GRID_LENGTH] - &cells[0])), absDiff(abs(diff.x), abs(diff.y)); USObstacleGrid::Cell* p = &cells[start.y * GRID_LENGTH + start.x]; if(absDiff.y < absDiff.x) { int error = -absDiff.x; for(int i = 0; i <= absDiff.x; ++i) { if((p->state < parameters.cellMaxOccupancy) && (p->lastUpdate != theFrameInfo.time)) p->state++; p->lastUpdate = theFrameInfo.time; p += inc.x; error += 2 * absDiff.y; if(error > 0) { p += inc.y; error -= 2 * absDiff.x; } } } else { int error = -absDiff.y; for(int i = 0; i <= absDiff.y; ++i) { if((p->state < parameters.cellMaxOccupancy) && (p->lastUpdate != theFrameInfo.time)) p->state++; p->lastUpdate = theFrameInfo.time; p += inc.y; error += 2 * absDiff.x; if(error > 0) { p += inc.x; error -= 2 * absDiff.y; } } } }
int main() { char line[60]; int dump; int threshold; double frr, frr_prev, far, far_prev; double min = 101.0; double diff; while(fgets(line, 59, stdin)) { sscanf(line, "%d%d%lf%d%d%lf", &threshold, &dump, &frr, &dump, &dump, &far); diff = absDiff(frr, far); if(diff <= min) min = diff; else break; frr_prev = frr; far_prev = far; } printf("FRR at EER: %f\nFAR at EER: %f\nDiff at EER: %f\nThreshold at EER: %d\n", frr_prev, far_prev, absDiff(frr_prev, far_prev), --threshold); FILE * fp = fopen("temp.txt", "w"); fprintf(fp, "%d", threshold); return 0; }
unsigned int calcPllValue2( unsigned int ulRequestClk, /* Required pixel clock in Hz unit */ pll_value_t *pPLL /* Structure to hold the value to be set in PLL */ ) { unsigned int M, N, OD, POD = 0, diff, pllClk, odPower, podPower; unsigned int bestDiff = 0xffffffff; /* biggest 32 bit unsigned number */ unsigned int ret; /* Init PLL structure to know states */ pPLL->M = 0; pPLL->N = 0; pPLL->OD = 0; pPLL->POD = 0; /* Sanity check: None at the moment */ /* Convert everything in Khz range in order to avoid calculation overflow */ pPLL->inputFreq /= 1000; ulRequestClk /= 1000; #ifndef VALIDATION_CHIP /* The maximum of post divider is 8. */ for (POD=0; POD<=3; POD++) #endif { #ifndef VALIDATION_CHIP /* MXCLK_PLL does not have post divider. */ if ((POD > 0) && (pPLL->clockType == MXCLK_PLL)) break; #endif /* Work out 2 to the power of POD */ podPower = twoToPowerOfx(POD); /* OD has only 2 bits [15:14] and its value must between 0 to 3 */ for (OD=0; OD<=3; OD++) { /* Work out 2 to the power of OD */ odPower = twoToPowerOfx(OD); #ifdef VALIDATION_CHIP if (odPower > 4) podPower = 4; else podPower = odPower; #endif /* N has 4 bits [11:8] and its value must between 2 and 15. The N == 1 will behave differently --> Result is not correct. */ for (N=2; N<=15; N++) { /* The formula for PLL is ulRequestClk = inputFreq * M / N / (2^OD) In the following steps, we try to work out a best M value given the others are known. To avoid decimal calculation, we use 1000 as multiplier for up to 3 decimal places of accuracy. */ M = ulRequestClk * N * odPower * 1000 / pPLL->inputFreq; M = roundedDiv(M, 1000); /* M field has only 8 bits, reject value bigger than 8 bits */ if (M < 256) { /* Calculate the actual clock for a given M & N */ pllClk = pPLL->inputFreq * M / N / odPower / podPower; /* How much are we different from the requirement */ diff = absDiff(pllClk, ulRequestClk); if (diff < bestDiff) { bestDiff = diff; /* Store M and N values */ pPLL->M = M; pPLL->N = N; pPLL->OD = OD; #ifdef VALIDATION_CHIP if (OD > 2) POD = 2; else POD = OD; #endif pPLL->POD = POD; } } } } } /* Restore input frequency from Khz to hz unit */ // pPLL->inputFreq *= 1000; ulRequestClk *= 1000; pPLL->inputFreq = DEFAULT_INPUT_CLOCK; /* Default reference clock */ /* Output debug information */ //DDKDEBUGPRINT((DISPLAY_LEVEL, "calcPllValue: Requested Frequency = %d\n", ulRequestClk)); //DDKDEBUGPRINT((DISPLAY_LEVEL, "calcPllValue: Input CLK = %dHz, M=%d, N=%d, OD=%d, POD=%d\n", pPLL->inputFreq, pPLL->M, pPLL->N, pPLL->OD, pPLL->POD)); /* Return actual frequency that the PLL can set */ ret = calcPLL(pPLL); return ret; }
/* monk liu @ 4/6/2011: re-write the calculatePLL function of ddk750. the original version function does not use some mathematics tricks and shortcut when it doing the calculation of the best N,M,D combination I think this version gives a little upgrade in speed 750 pll clock formular: Request Clock = (Input Clock * M )/(N * X) Input Clock = 14318181 hz X = 2 power D D ={0,1,2,3,4,5,6} M = {1,...,255} N = {2,...,15} */ unsigned int calcPllValue(unsigned int request_orig,pll_value_t *pll) { /* used for primary and secondary channel pixel clock pll */ static pllcalparam xparm_PIXEL[] = { /* 2^0 = 1*/ {0,0,0,1}, /* 2^ 1 =2*/ {1,0,1,2}, /* 2^ 2 = 4*/ {2,0,2,4}, {3,0,3,8}, {4,1,3,16}, {5,2,3,32}, /* 2^6 = 64 */ {6,3,3,64}, }; /* used for MXCLK (chip clock) */ static pllcalparam xparm_MXCLK[] = { /* 2^0 = 1*/ {0,0,0,1}, /* 2^ 1 =2*/ {1,0,1,2}, /* 2^ 2 = 4*/ {2,0,2,4}, {3,0,3,8}, }; /* as sm750 register definition, N located in 2,15 and M located in 1,255 */ int N,M,X,d; int xcnt; int miniDiff; unsigned int RN,quo,rem,fl_quo; unsigned int input,request; unsigned int tmpClock,ret; pllcalparam * xparm; #if 1 if (getChipType() == SM750LE) { /* SM750LE don't have prgrammable PLL and M/N values to work on. Just return the requested clock. */ return request_orig; } #endif ret = 0; miniDiff = ~0; request = request_orig / 1000; input = pll->inputFreq / 1000; /* for MXCLK register , no POD provided, so need be treated differently */ if(pll->clockType != MXCLK_PLL){ xparm = &xparm_PIXEL[0]; xcnt = sizeof(xparm_PIXEL)/sizeof(xparm_PIXEL[0]); }else{ xparm = &xparm_MXCLK[0]; xcnt = sizeof(xparm_MXCLK)/sizeof(xparm_MXCLK[0]); } for(N = 15;N>1;N--) { /* RN will not exceed maximum long if @request <= 285 MHZ (for 32bit cpu) */ RN = N * request; quo = RN / input; rem = RN % input;/* rem always small than 14318181 */ fl_quo = (rem * 10000 /input); for(d = xcnt - 1;d >= 0;d--){ X = xparm[d].value; M = quo*X; M += fl_quo * X / 10000; /* round step */ M += (fl_quo*X % 10000)>5000?1:0; if(M < 256 && M > 0) { unsigned int diff; tmpClock = pll->inputFreq *M / N / X; diff = absDiff(tmpClock,request_orig); if(diff < miniDiff) { pll->M = M; pll->N = N; pll->OD = xparm[d].od; pll->POD = xparm[d].pod; miniDiff = diff; ret = tmpClock; } } } } //printk("Finally: pll->n[%lu],m[%lu],od[%lu],pod[%lu]\n",pll->N,pll->M,pll->OD,pll->POD); return ret; }
uchar Check4Drum(void) { static uchar timePerDrumCheck=50; long currTime=ES_Timer_GetTime(); static uchar state =0; //0 is noiseDataCollection, 1 is Drumming static int numPts=0; static uchar lastRevState; static uchar currRevState; static uint drum[2]; static uint hits[2]; static int i; static uchar recording=0; static uchar hitInc=0; if(! (currTime-lastDrumTime > timePerDrumCheck || currTime<lastDrumTime) ) return 0; //exit immediately if not enough time has passed drum[LEFT]=(int) ADS12_ReadADPin(drumLPin); drum[RIGHT]=(int) ADS12_ReadADPin(drumRPin); if(state==0) { if(!recording) { if(numPts>=NUM_IN_A_ROW) { recording=1; numPts=0; bg[0]=0; bg[1]=0; } if(absDiff(drum[LEFT],bg[LEFT]) > 10 || absDiff(drum[RIGHT],bg[RIGHT]) > 10 || bg[LEFT]<300 || bg[RIGHT] < 300) { printf("discard %i %i\r\n",bg[LEFT],bg[RIGHT]); bg[LEFT]=drum[LEFT]; bg[RIGHT]=drum[RIGHT]; } else { numPts++; //uncommented below } } if(recording) //recording { bg[0]+=drum[0]; bg[1]+=drum[1]; bgSaves[numPts]=drum[LEFT]; //11 to 30 ->0 to 19 bgSaves[numPts+NUM_SAMPLES]=drum[RIGHT]; //11 to 30 -> 20 -> 39 printf("pts: %lu, %lu\r\n", bg[0], bg[1]); numPts++; } //numPts++; //------------CalcBGNoise---------------------- if(numPts>=NUM_SAMPLES)//actually 20 points here because we ignore first 10 (see above) (2.5 seconds total because thsi is 20Hz function) { bg[LEFT]/=numPts; //calculate background noise bg[RIGHT]/=numPts; printf("bg is: %lu, %lu\r\n",bg[LEFT],bg[RIGHT]); state=1; timePerDrumCheck=5; hits[LEFT]=0; hits[RIGHT]=0; lastRevState=revDown(); bgDone=1; } } //-----------CalcBGNoiseComplete-------------- //-------ReadAnalogDrumHit------------- else if(state==1) { currRevState=revDown(); for(i=0;i<2;i++) //Check if we have any good analog readings { if(currRevState!=lastRevState) { currHit[i]=0; //change direction, immediately set currHit to 0 so it wont trigger. } //if(absDiff(drum[i],bg[i]) > NOISE_THRESHOLD) //noise thresholding if(absDiff(drum[i],600) > NOISE_THRESHOLD) //noise thresholding HARDCODE { //printf("%i-%i\r\n",i,drum[i]); if(!hitInc) { hitInc=1; classifyStartTime=currTime; //starting classification } hits[i]++; } } //-------ReadAnalogDrumHitComplete----- //----Classify hit--- if( ((currTime>classifyStartTime + CLASSIFY_TIME)||classifyStartTime>currTime) && hitInc) //timer expired { hitInc=0; //end classify period if(hits[0]>=MIN_HITS_PER_CLASSIFY || hits[1] >= MIN_HITS_PER_CLASSIFY) { //begin classifying printf("class ify %i,%i",hits[LEFT],hits[RIGHT]); if(hits[LEFT] > hits[RIGHT]) //if there are more lefts than rights, good we know what this is. { registerHit(LEFT,currTime); if(hits[RIGHT] > hits[LEFT]/2) //probably hit both at once { registerHit(RIGHT,currTime); } } else { registerHit(RIGHT,currTime); if(hits[LEFT] > hits[RIGHT]/2) //probably hit both at once { registerHit(LEFT,currTime); } } } hits[LEFT]=0; hits[RIGHT]=0; } //----Classify Hit END--- } lastRevState=currRevState; lastDrumTime=currTime; return 0; }