void PrnAutocorrelator_Host::make_correlation(std::vector<AutocorrelationRecord>& autocorrelation_records) { static const AutocorrelationRecord tmp_acorr_record; wsgc_complex acorr(0.0, 0.0); wsgc_complex acorr_avgsum = _acorrs[0]; for (unsigned int psi = 0; psi < _fft_N; psi++) { acorr += _prn_samples[psi] * _prn_samples[psi + _fft_N]; } _acorrs[_global_prn_index%_prn_per_symbol] = acorr; for (unsigned int ai=1; ai<_prn_per_symbol; ai++) { acorr_avgsum += _acorrs[ai]; } autocorrelation_records.push_back(tmp_acorr_record); autocorrelation_records.back().global_prn_index = _global_prn_index; autocorrelation_records.back().prn_per_symbol_index = _global_prn_index % _prn_per_symbol; WsgcUtils::magnitude_estimation(&acorr, &autocorrelation_records.back().magnitude); WsgcUtils::magnitude_estimation(&acorr_avgsum, &autocorrelation_records.back().magnitude_avg); autocorrelation_records.back().acorr = acorr; }
int main() { //double data[] = {1.0, 1.0, 1.0, 1.0}; double data[] = {1.0, 2.0, 1.0, 2.0}; double *result = acorr(data, 4); for(int i = 0; i < 7; i++) printf("%.1f ", result[i]); printf("\n"); char x[10]; scanf("%s", x); return 0; }
int main(int argc, char **argv) { FILE *fp = stdin; char *s, *infile = NULL, c; double *x, *r; int l = LENG, np = ORDER; if ((cmnd = strrchr(argv[0], '/')) == NULL) cmnd = argv[0]; else cmnd++; while (--argc) { if (*(s = *++argv) == '-') { c = *++s; if (*++s == '\0') { s = *++argv; --argc; } switch (c) { case 'm': np = atoi(s); break; case 'l': l = atoi(s); break; case 'h': usage(0); default: fprintf(stderr, "%s : Invalid option '%c'!\n", cmnd, c); usage(1); break; } } else infile = s; } if (infile) fp = getfp(infile, "rb"); x = dgetmem(l + np + 1); r = x + l; while (freadf(x, sizeof(*x), l, fp) == l) { acorr(x, l, r, np); fwritef(r, sizeof(*r), np + 1, stdout); } return (0); }
int lpc(double *x, const int flng, double *a, const int m, const double f) { int flag; static double *r = NULL; static int size; if (r == NULL) { r = dgetmem(m + 1); size = m; } if (m > size) { free(r); r = dgetmem(m + 1); size = m; } acorr(x, flng, r, m); flag = levdur(r, a, m, f); return (flag); }
void MakePair(struct DanssEventStruct3 *DanssEvent, struct DanssEventStruct3 *SavedEvent, struct DanssEventStruct3 *VetoEvent, struct DanssPairStruct3 *DanssPair) { memset(DanssPair, 0, sizeof(struct DanssPairStruct3)); DanssPair->number[0] = SavedEvent->number; DanssPair->number[1] = DanssEvent->number; DanssPair->unixTime = DanssEvent->unixTime; DanssPair->SiPmCleanEnergy[0] = SavedEvent->SiPmCleanEnergy; DanssPair->PmtCleanEnergy[0] = SavedEvent->PmtCleanEnergy; DanssPair->SiPmCleanEnergy[1] = DanssEvent->SiPmCleanEnergy; DanssPair->PmtCleanEnergy[1] = DanssEvent->PmtCleanEnergy; DanssPair->PositronHits = SavedEvent->PositronHits; DanssPair->PositronEnergy = SavedEvent->PositronEnergy; memcpy(DanssPair->PositronX, SavedEvent->PositronX, sizeof(SavedEvent->PositronX)); DanssPair->MaxHitEnergy = SavedEvent->MaxHitEnergy; DanssPair->AnnihilationGammas = SavedEvent->AnnihilationGammas; DanssPair->AnnihilationEnergy = SavedEvent->AnnihilationEnergy; DanssPair->NeutronHits = DanssEvent->SiPmCleanHits; DanssPair->NeutronEnergy = (DanssEvent->SiPmCleanEnergy + DanssEvent->PmtCleanEnergy) / 2; memcpy(DanssPair->NeutronX, DanssEvent->NeutronX, sizeof(DanssEvent->NeutronX)); DanssPair->NeutronRadius = DanssEvent->NeutronRadius; DanssPair->gtDiff = (DanssEvent->globalTime - SavedEvent->globalTime) / GFREQ2US; DanssPair->Distance = sqrt( (DanssEvent->NeutronX[0] - SavedEvent->PositronX[0]) * (DanssEvent->NeutronX[0] - SavedEvent->PositronX[0]) + (DanssEvent->NeutronX[1] - SavedEvent->PositronX[1]) * (DanssEvent->NeutronX[1] - SavedEvent->PositronX[1]) + (DanssEvent->NeutronX[2] - SavedEvent->PositronX[2]) * (DanssEvent->NeutronX[2] - SavedEvent->PositronX[2]) ); DanssPair->DistanceZ = DanssEvent->NeutronX[2] - SavedEvent->PositronX[2]; DanssPair->gtFromVeto = (SavedEvent->globalTime - VetoEvent->globalTime) / GFREQ2US; DanssPair->VetoHits = VetoEvent->VetoCleanHits; DanssPair->VetoEnergy = VetoEvent->VetoCleanEnergy; DanssPair->DanssEnergy = (VetoEvent->SiPmCleanEnergy + VetoEvent->PmtCleanEnergy) / 2; acorr(DanssPair); // correct positron energy based on neutron position if only one coordinate of positron cluster is available DanssPair->PositronEnergy = (DanssPair->PositronEnergy - CORR_P0) / CORR_P1; }
void *tracking_thread(tracker_t *trac) { // Lock the mutex. errno = pthread_mutex_lock(&(trac->tl->tracker_busy)); if (errno) err(EXIT_FAILURE, "pthread_mutex_lock() failed"); while (1) { // Wait until there is work to do. while (trac->tl->reason == NONE) { errno = pthread_cond_wait(&(trac->tl->wake_up), &(trac->tl->tracker_busy)); if (errno) err(EXIT_FAILURE, "pthread_cond_wait() failed"); } // Received quit from UI. if (trac->tl->reason & QUIT) { printf("Tracker thread shutting down!\n"); errno = pthread_mutex_unlock(&(trac->tl->tracker_busy)); if (errno) err(EXIT_FAILURE, "pthread_mutex_unlock() failed"); pthread_exit(NULL); } // Received tap from the UI. if (trac->tl->reason & TAP) { jack_nframes_t tap_delta = trac->ud->tap_time - trac->last_tap; if (trac->last_tap != 0 && tap_delta < MAX_TAP_DELTA && tap_delta > MIN_TAP_DELTA) { trac->pd->beat_period = tap_delta; trac->pd->cur_beat = trac->ud->tap_time; trac->pd->next_beat = trac->ud->tap_time + tap_delta; trac->pd->calc_called = false; printf("next_beat: %u, tap_delta: %u, bpm: %f\n", trac->pd->next_beat, tap_delta, 60.*48000./tap_delta); } trac->last_tap = trac->ud->tap_time; } // Received beat from process. if (trac->tl->reason & BEAT) { printf("Beat (bpm = %f)\n", (60.*48000.)/trac->pd->beat_period); } // Received calc from process. if (trac->tl->reason & CALC) { /* * Tempo (i.e. beat period) tracking. */ // Beat period in odf samples. jack_nframes_t beat_period = trac->pd->beat_period/BUFSIZE; // Beat period in odf samples. jack_nframes_t beat_pos = beat_period/2; // Calculate autocorrelation. acorr(trac->acf, trac->pd->odf, 8*beat_period); // Comb filter and weight the autocorrelation. comb_acf(trac->cacf, trac->acf, beat_period, trac->sigma_t); #if 0 // Print odf, acf & cacf. uint_t ps = 8*beat_period; for (jack_nframes_t n = 0; n < ps; n++) { printf("o[%u] = %f\ta[%u] = %f\t", n, trac->pd->odf->data[(trac->pd->odf->ridx + ODFSIZE - ps+1 + n) % ODFSIZE], n, trac->acf[n]); if (n < 2*beat_period) printf("c[%u] = %f\n", n, trac->cacf[n]); else printf("\n"); } #endif // Beat period is index of maximum element of comb filtered and weighted autocorrelation function. beat_period = argmax(trac->cacf, beat_period); #if 0 // Print new beat period in odf samples. printf("beat_period = %u\n", beat_period); #endif /* * Beat tracking. */ // Comb filter onset detection function and apply weighting. comb_odf(trac->codf, trac->pd->odf, beat_pos, beat_period, trac->sigma_b); // Beat position is index of maximum element of comb filtered and weighted onset detection function. beat_pos = argmax(trac->codf, beat_pos); #if 0 // Print beat position in odf samples from ridx. printf("beat_pos = %u\n", beat_pos); #endif // Update beat pos. // TODO: Thread safety! trac->pd->beat_period = beat_period * BUFSIZE; trac->pd->cur_beat = trac->pd->next_beat; trac->pd->next_beat = trac->pd->cur_beat + trac->pd->beat_period; trac->pd->beat_called = false; trac->pd->calc_called = false; //printf("cur_beat = %d\n", trac->pd->cur_beat); //printf("next_beat = %d\n", trac->pd->next_beat); } // Reset reason. trac->tl->reason = NONE; } }