/*============================================================ * FUNCTION definitions *============================================================*/ int main(int argc, char *argv[]) { static LALStatus status; /* LALStatus pointer */ UserVariables_t XLAL_INIT_DECL(uvar); ConfigVariables_t XLAL_INIT_DECL(cfg); UINT4 k, numBins, numIFOs, maxNumSFTs, X, alpha; REAL8 Freq0, dFreq, normPSD; UINT4 finalBinSize, finalBinStep, finalNumBins; REAL8Vector *overSFTs = NULL; /* one frequency bin over SFTs */ REAL8Vector *overIFOs = NULL; /* one frequency bin over IFOs */ REAL8Vector *finalPSD = NULL; /* math. operation PSD over SFTs and IFOs */ REAL8Vector *finalNormSFT = NULL; /* normalised SFT power */ vrbflg = 1; /* verbose error-messages */ /* set LAL error-handler */ lal_errhandler = LAL_ERR_EXIT; /* register and read user variables */ if (initUserVars(argc, argv, &uvar) != XLAL_SUCCESS) return EXIT_FAILURE; MultiSFTVector *inputSFTs = NULL; if ( ( inputSFTs = XLALReadSFTs ( &cfg, &uvar ) ) == NULL ) { XLALPrintError ("Call to XLALReadSFTs() failed with xlalErrno = %d\n", xlalErrno ); return EXIT_FAILURE; } /* clean sfts if required */ if ( XLALUserVarWasSet( &uvar.linefiles ) ) { RandomParams *randPar=NULL; FILE *fpRand=NULL; INT4 seed, ranCount; if ( (fpRand = fopen("/dev/urandom", "r")) == NULL ) { fprintf(stderr,"Error in opening /dev/urandom" ); return EXIT_FAILURE; } if ( (ranCount = fread(&seed, sizeof(seed), 1, fpRand)) != 1 ) { fprintf(stderr,"Error in getting random seed" ); return EXIT_FAILURE; } LAL_CALL ( LALCreateRandomParams (&status, &randPar, seed), &status ); LAL_CALL( LALRemoveKnownLinesInMultiSFTVector ( &status, inputSFTs, uvar.maxBinsClean, uvar.blocksRngMed, uvar.linefiles, randPar), &status); LAL_CALL ( LALDestroyRandomParams (&status, &randPar), &status); fclose(fpRand); } /* end cleaning */ LogPrintf (LOG_DEBUG, "Computing spectrogram and PSD ... "); /* get power running-median rngmed[ |data|^2 ] from SFTs */ MultiPSDVector *multiPSD = NULL; XLAL_CHECK_MAIN( ( multiPSD = XLALNormalizeMultiSFTVect ( inputSFTs, uvar.blocksRngMed, NULL ) ) != NULL, XLAL_EFUNC); /* restrict this PSD to just the "physical" band if requested using {--Freq, --FreqBand} */ if ( ( XLALCropMultiPSDandSFTVectors ( multiPSD, inputSFTs, cfg.firstBin, cfg.lastBin )) != XLAL_SUCCESS ) { XLALPrintError ("%s: XLALCropMultiPSDandSFTVectors (inputPSD, inputSFTs, %d, %d) failed with xlalErrno = %d\n", __func__, cfg.firstBin, cfg.lastBin, xlalErrno ); return EXIT_FAILURE; } /* start frequency and frequency spacing */ Freq0 = multiPSD->data[0]->data[0].f0; dFreq = multiPSD->data[0]->data[0].deltaF; /* number of raw bins in final PSD */ numBins = multiPSD->data[0]->data[0].data->length; if ( (finalPSD = XLALCreateREAL8Vector ( numBins )) == NULL ) { LogPrintf (LOG_CRITICAL, "Out of memory!\n"); return EXIT_FAILURE; } /* number of IFOs */ numIFOs = multiPSD->length; if ( (overIFOs = XLALCreateREAL8Vector ( numIFOs )) == NULL ) { LogPrintf (LOG_CRITICAL, "Out of memory!\n"); return EXIT_FAILURE; } /* maximum number of SFTs */ maxNumSFTs = 0; for (X = 0; X < numIFOs; ++X) { maxNumSFTs = GSL_MAX(maxNumSFTs, multiPSD->data[X]->length); } if ( (overSFTs = XLALCreateREAL8Vector ( maxNumSFTs )) == NULL ) { LogPrintf (LOG_CRITICAL, "Out of memory!\n"); return EXIT_FAILURE; } /* normalize rngmd(power) to get proper *single-sided* PSD: Sn = (2/Tsft) rngmed[|data|^2]] */ normPSD = 2.0 * dFreq; /* loop over frequency bins in final PSD */ for (k = 0; k < numBins; ++k) { /* loop over IFOs */ for (X = 0; X < numIFOs; ++X) { /* number of SFTs for this IFO */ UINT4 numSFTs = multiPSD->data[X]->length; /* copy PSD frequency bins and normalise multiPSD for later use */ for (alpha = 0; alpha < numSFTs; ++alpha) { multiPSD->data[X]->data[alpha].data->data[k] *= normPSD; overSFTs->data[alpha] = multiPSD->data[X]->data[alpha].data->data[k]; } /* compute math. operation over SFTs for this IFO */ overIFOs->data[X] = math_op(overSFTs->data, numSFTs, uvar.PSDmthopSFTs); if ( isnan( overIFOs->data[X] ) ) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in overIFOs->data[X=%d] = NAN ... exiting\n", X ); } /* for IFOs X */ /* compute math. operation over IFOs for this frequency */ finalPSD->data[k] = math_op(overIFOs->data, numIFOs, uvar.PSDmthopIFOs); if ( isnan ( finalPSD->data[k] ) ) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in finalPSD->data[k=%d] = NAN ... exiting\n", k ); } /* for freq bins k */ LogPrintfVerbatim ( LOG_DEBUG, "done.\n"); /* compute normalised SFT power */ if (uvar.outputNormSFT) { LogPrintf (LOG_DEBUG, "Computing normalised SFT power ... "); if ( (finalNormSFT = XLALCreateREAL8Vector ( numBins )) == NULL ) { LogPrintf (LOG_CRITICAL, "Out of memory!\n"); return EXIT_FAILURE; } /* loop over frequency bins in SFTs */ for (k = 0; k < numBins; ++k) { /* loop over IFOs */ for (X = 0; X < numIFOs; ++X) { /* number of SFTs for this IFO */ UINT4 numSFTs = inputSFTs->data[X]->length; /* compute SFT power */ for (alpha = 0; alpha < numSFTs; ++alpha) { COMPLEX8 bin = inputSFTs->data[X]->data[alpha].data->data[k]; overSFTs->data[alpha] = crealf(bin)*crealf(bin) + cimagf(bin)*cimagf(bin); } /* compute math. operation over SFTs for this IFO */ overIFOs->data[X] = math_op(overSFTs->data, numSFTs, uvar.nSFTmthopSFTs); if ( isnan ( overIFOs->data[X] )) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in overIFOs->data[X=%d] = NAN ... exiting\n", X ); } /* over IFOs */ /* compute math. operation over IFOs for this frequency */ finalNormSFT->data[k] = math_op(overIFOs->data, numIFOs, uvar.nSFTmthopIFOs); if ( isnan( finalNormSFT->data[k] ) ) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in bin finalNormSFT->data[k=%d] = NAN ... exiting\n", k ); } /* over freq bins */ LogPrintfVerbatim ( LOG_DEBUG, "done.\n"); } /* output spectrograms */ if ( uvar.outputSpectBname ) { LAL_CALL ( LALfwriteSpectrograms ( &status, uvar.outputSpectBname, multiPSD ), &status ); } /* ---------- if user requested it, output complete MultiPSDVector over IFOs X, timestamps and freq-bins into ASCI file(s) */ if ( uvar.dumpMultiPSDVector ) { if ( XLALDumpMultiPSDVector ( uvar.outputPSD, multiPSD ) != XLAL_SUCCESS ) { XLALPrintError ("%s: XLALDumpMultiPSDVector() failed, xlalErrnor = %d\n", __func__, xlalErrno ); return EXIT_FAILURE; } } /* if uvar.dumpMultiPSDVector */ /* ----- if requested, compute data-quality factor 'Q' -------------------- */ if ( uvar.outputQ ) { REAL8FrequencySeries *Q; if ( (Q = XLALComputeSegmentDataQ ( multiPSD, cfg.dataSegment )) == NULL ) { XLALPrintError ("%s: XLALComputeSegmentDataQ() failed with xlalErrno = %d\n", __func__, xlalErrno ); return EXIT_FAILURE; } if ( XLAL_SUCCESS != XLALWriteREAL8FrequencySeries_to_file ( Q, uvar.outputQ ) ) { return EXIT_FAILURE; } XLALDestroyREAL8FrequencySeries ( Q ); } /* if outputQ */ /* ---------- BINNING if requested ---------- */ /* work out bin size */ if (XLALUserVarWasSet(&uvar.binSize)) { finalBinSize = uvar.binSize; } else if (XLALUserVarWasSet(&uvar.binSizeHz)) { finalBinSize = (UINT4)floor(uvar.binSizeHz / dFreq + 0.5); /* round to nearest bin */ } else { finalBinSize = 1; } /* work out bin step */ if (XLALUserVarWasSet(&uvar.binStep)) { finalBinStep = uvar.binStep; } else if (XLALUserVarWasSet(&uvar.binStepHz)) { finalBinStep = (UINT4)floor(uvar.binStepHz / dFreq + 0.5); /* round to nearest bin */ } else { finalBinStep = finalBinSize; } /* work out total number of bins */ finalNumBins = (UINT4)floor((numBins - finalBinSize) / finalBinStep) + 1; /* write final PSD to file */ if (XLALUserVarWasSet(&uvar.outputPSD)) { FILE *fpOut = NULL; if ((fpOut = fopen(uvar.outputPSD, "wb")) == NULL) { LogPrintf ( LOG_CRITICAL, "Unable to open output file %s for writing...exiting \n", uvar.outputPSD ); return EXIT_FAILURE; } /* write header info in comments */ if ( XLAL_SUCCESS != XLALOutputVersionString ( fpOut, 0 ) ) XLAL_ERROR ( XLAL_EFUNC ); /* write the command-line */ for (int a = 0; a < argc; a++) fprintf(fpOut,"%%%% argv[%d]: '%s'\n", a, argv[a]); /* write column headings */ fprintf(fpOut,"%%%% columns:\n%%%% FreqBinStart"); if (uvar.outFreqBinEnd) fprintf(fpOut," FreqBinEnd"); fprintf(fpOut," PSD"); if (uvar.outputNormSFT) fprintf(fpOut," normSFTpower"); fprintf(fpOut,"\n"); LogPrintf(LOG_DEBUG, "Printing PSD to file ... "); for (k = 0; k < finalNumBins; ++k) { UINT4 b = k * finalBinStep; REAL8 f0 = Freq0 + b * dFreq; REAL8 f1 = f0 + finalBinStep * dFreq; fprintf(fpOut, "%f", f0); if (uvar.outFreqBinEnd) fprintf(fpOut, " %f", f1); REAL8 psd = math_op(&(finalPSD->data[b]), finalBinSize, uvar.PSDmthopBins); if ( isnan ( psd )) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in psd[k=%d] = NAN ... exiting\n", k ); fprintf(fpOut, " %e", psd); if (uvar.outputNormSFT) { REAL8 nsft = math_op(&(finalNormSFT->data[b]), finalBinSize, uvar.nSFTmthopBins); if ( isnan ( nsft )) XLAL_ERROR ( EXIT_FAILURE, "Found Not-A-Number in nsft[k=%d] = NAN ... exiting\n", k ); fprintf(fpOut, " %f", nsft); } fprintf(fpOut, "\n"); } // k < finalNumBins LogPrintfVerbatim ( LOG_DEBUG, "done.\n"); fclose(fpOut); } /* we are now done with the psd */ XLALDestroyMultiPSDVector ( multiPSD); XLALDestroyMultiSFTVector ( inputSFTs); XLALDestroyUserVars(); XLALDestroyREAL8Vector ( overSFTs ); XLALDestroyREAL8Vector ( overIFOs ); XLALDestroyREAL8Vector ( finalPSD ); XLALDestroyREAL8Vector ( finalNormSFT ); LALCheckMemoryLeaks(); return EXIT_SUCCESS; } /* main() */
/* * Addr mode must be used for strings */ void eval(int64_t instr) { enum opcode opcode = GET_OPCODE(instr); enum opcode_format format = get_opcode_format(opcode); OPERAND *operands[2]; int vals[2]; print_dbg("eval(): opcode: %d instr: 0x%016llx (ip: %d)\n", opcode, instr, cpu->registers[RESERVED_REG(REG_IP)]); switch(format) { case NO_OPERANDS: break; case ONE_OPERAND: operands[0] = malloc(sizeof(OPERAND)); operands[0]->mode = GET_ADDR_MODE(instr, 0); operands[0]->val = (instr >> (56 - get_mode_size(operands[0]->mode))) & get_mode_mask(operands[0]->mode); print_dbg("Operand one: mode = %d, value = %d\n", operands[0]->mode, operands[0]->val); break; case TWO_OPERANDS: operands[0] = malloc(sizeof(OPERAND)); operands[0]->mode = GET_ADDR_MODE(instr, 0); operands[0]->val = (instr >> (56 - get_mode_size(operands[0]->mode))) & get_mode_mask(operands[0]->mode); operands[1] = malloc(sizeof(OPERAND)); operands[1]->mode = GET_ADDR_MODE(instr, 1); operands[1]->val = (instr >> (56 - get_mode_size(operands[0]->mode) - get_mode_size(operands[1]->mode))) & get_mode_mask(operands[1]->mode); print_dbg("Operand one: mode = %d, value = %d\n", operands[0]->mode, operands[0]->val); print_dbg("Operand two: mode = %d, value = %d\n", operands[1]->mode, operands[1]->val); break; } switch(opcode) { case HALT: cpu->running = 0; break; case MOV: if(operands[1]->mode != MODE_REGISTER) { vm_error(VM_ERR_FATAL, "mov: destination is not a register\n"); } cpu->registers[operands[1]->val] = get_val(operands[0]); break; case PUSH: push(get_val(operands[0])); break; case POP: if(operands[0]->mode == MODE_REGISTER) { cpu->registers[operands[0]->val] = pop(); } break; case INC: if(operands[0]->mode != MODE_REGISTER) { vm_error(VM_ERR_FATAL, "inc: operand is not a register\n"); } cpu->registers[operands[0]->val]++; break; case DEC: if(operands[0]->mode != MODE_REGISTER) { vm_error(VM_ERR_FATAL, "dec: operand is not a register\n"); } cpu->registers[operands[0]->val]--; break; case PRINT: if(operands[0]->mode == MODE_CONSTANT) { printf("%d", operands[0]->val); print_dbg("PRINT (MODE_CONSTANT): [%d]\n", operands[0]->val); } else if(operands[0]->mode == MODE_REGISTER) { printf("%d", cpu->registers[operands[0]->val]); print_dbg("PRINT (MODE_REGISTER): [%d]\n", operands[0]->val); } else if(operands[0]->mode == MODE_ADDR) { printf("%s", &data_section[operands[0]->val]); print_dbg("PRINT (MODE_ADDR): [%d][%s]\n", operands[0]->val, &data_section[operands[0]->val]); } break; case PRINTC: printf("%c", get_val(operands[0])); break; case GETI: if(operands[0]->mode != MODE_REGISTER) { vm_error(VM_ERR_FATAL, "geti: destination is not a register\n"); } scanf("%d", &cpu->registers[operands[0]->val]); break; case RAND: if(operands[0]->mode != MODE_REGISTER) { vm_error(VM_ERR_FATAL, "rand: destination is not a register\n"); } cpu->registers[operands[0]->val] = rand() % 255; break; case ADD: case SUB: case MUL: case DIV: case MOD: case XOR: case OR: case SHL: case SHR: math_op(opcode, operands); break; case CMP: vals[0] = get_val(operands[0]); vals[1] = get_val(operands[1]); if(vals[0] == vals[1]) { cpu->registers[RESERVED_REG(REG_CMP)] = 0; } else if(vals[0] > vals[1]) { cpu->registers[RESERVED_REG(REG_CMP)] = 1; } else if(vals[0] < vals[1]) { cpu->registers[RESERVED_REG(REG_CMP)] = 2; } break; case JMP: case JE: case JNE: case JL: case JLE: case JG: case JGE: jmp_op(opcode, operands); break; } }