示例#1
0
double single_file_correlate(std::string directory, std::string fname,rarray<double,1> predictPS){
  // Read in detection signal, do the Fourier transform and compute the power spectrum
  data_cols detection = readcols(directory,fname);
  rarray<std::complex<double>,1> detection_FT = FT(detection.signal);
  rarray<double,1> detectPS = powerspectrum(detection_FT);                             
  return correlation_coeffecient(predictPS,detectPS);
}
示例#2
0
文件: powerspec.c 项目: jakobmoss/tsa
int main(int argc, char *argv[])
{
    /* Important definitions */
    // Lengths
    size_t N = 0;  // Length of time series
    size_t M = 0;  // Length of sampling vector (number of frequencies)

    // Filenames
    char inname[100];
    char outname[100];

    // Sampling
    double low, high, rate;

    // Frequency of window function
    double winfreq = 0;

    // Options
    int quiet = 0;
    int unit = 1;
    int prep = 1;
    int autosamp = 0;
    int fast = 0;
    int useweight = 0;
    int windowmode = 0;
    int Nclean = 0;
    int filter = 0;

    
    /* Process command line arguments and return line count of the input file */
    N = cmdarg(argc, argv, inname, outname, &quiet, &unit, &prep, &low, &high,\
               &rate, &autosamp, &fast, &useweight, &windowmode, &winfreq,\
               &Nclean, &filter, NULL, NULL);
    
    // Pretty print
    if ( quiet == 0 || fast == 1 ){
        if ( windowmode == 0 && useweight != 0 )
            printf("\nCalculating the weighted power spectrum of \"%s\" ...\n",\
                   inname);
        else if ( windowmode == 0 )
            printf("\nCalculating the power spectrum of \"%s\" ...\n", inname);
        else
            printf("\nCalculating the window function of \"%s\" ...\n", inname);
    }
    

    /* Read data (and weights) from the input file */
    if ( quiet == 0 ) printf(" - Reading input\n");
    double* time = malloc(N * sizeof(double));
    double* flux = malloc(N * sizeof(double));
    double* weight = malloc(N * sizeof(double));
    readcols(inname, time, flux, weight, N, useweight, unit, quiet);
    
    // Do if fast-mode and window-mode is not activated
    if ( fast == 0 && windowmode == 0 ) {
        // Calculate Nyquist frequency
        double* dt = malloc(N-1 * sizeof(double));
        double nyquist;
        arr_diff(time, dt, N);
        nyquist = 1.0 / (2.0 * arr_median(dt, N-1)) * 1e6; // microHz !
        free(dt);

        // Calculate suggested sampling (4 times oversampling)
        double minsamp;
        minsamp = 1.0e6 / (4 * (time[N-1] - time[0])); // microHz !
    
        // Display info?
        if ( quiet == 0 ){
            printf(" -- INFO: Length of time series = %li\n", N);
            printf(" -- INFO: Nyquist frequency = %.2lf microHz\n", nyquist);
            printf(" -- INFO: Suggested minimum sampling = %.3lf microHz\n",\
                   minsamp);
        }

        // Apply automatic sampling?
        if ( autosamp != 0 ) {
            low = 5.0;
            high = nyquist;
            rate = minsamp;
        }
    }

    
    /* Prepare for power spectrum */
    // Calculate proper frequency range for window-function-mode
    double limit = 0;
    if ( windowmode != 0 ) {
        limit = low;
        low = winfreq - limit;
        high = winfreq + limit;
    }
    
    // Get length of sampling vector
    M = arr_util_getstep(low, high, rate);

    // Fill sampling vector with cyclic frequencies
    double* freq = malloc(M * sizeof(double));
    arr_init_linspace(freq, low, rate, M);

    // Initialise arrays for data storage
    double* power = malloc(M * sizeof(double));
    double* alpha = malloc(M * sizeof(double));
    double* beta = malloc(M * sizeof(double));


    /* Calculate power spectrum OR window function */
    if ( windowmode == 0 ) {
        // Subtract the mean to avoid "zero-frequency" problems
        if ( prep != 0 ) {
            if ( quiet == 0 ){
                printf(" - Subtracting the mean from time series\n");
            }
            arr_sca_add(flux, -arr_mean(flux, N), N);
        }
        else {
            if ( quiet == 0 )
                printf(" - Time series used *without* mean subtraction!\n");
        }
    
        // Display info
        if ( quiet == 0 ){
            printf(" - Calculating fourier transform\n");
            if ( autosamp != 0 ) {
                printf(" -- NB: Using automatic sampling!\n");
                printf(" -- INFO: Auto-sampling (in microHz): %.2lf to %.2lf"\
                       " in steps of %.4lf\n", low, high, rate);
            }
            else {
                printf(" -- INFO: Sampling (in microHz): %.2lf to %.2lf in"\
                       " steps of %.4lf\n", low, high, rate);
            }
            printf(" -- INFO: Number of sampling frequencies = %li\n", M);
        }

        // Calculate power spectrum with or without weights
        fourier(time, flux, weight, freq, N, M, power, alpha, beta, useweight);
    }
    else {
        if ( quiet == 0 ){
            printf(" - Calculating window function\n");
            printf(" -- INFO: Window frequency = %.2lf microHz\n", winfreq);
            printf(" -- INFO: Sampling in the range +/- %.2lf microHz in" \
                   " steps of %.4lf microHz\n", limit, rate);
            printf(" -- INFO: Number of sampling frequencies = %li\n", M);
        }

        // Calculate spectral window with or without weights
        windowfunction(time, freq, weight, N, M, winfreq, power, useweight);

        if ( quiet == 0 )
            printf(" - Sum of spectral window = %.4lf\n", arr_sum(power, M));

        // Move frequencies to the origin
        arr_sca_add(freq, -winfreq, M);
    }

        
    /* Write data to file */
    if ( quiet == 0 ) printf(" - Saving to file \"%s\"\n", outname);
    writecols(outname, freq, power, M);

    
    /* Free data */
    free(time);
    free(flux);
    free(weight);
    free(freq);
    free(power);
    free(alpha);
    free(beta);


    /* Done! */
    if ( quiet == 0 || fast ==1 ) printf("Done!\n\n");
    return 0; 
}
示例#3
0
文件: fclean.c 项目: jakobmoss/tsa
int main(int argc, char *argv[])
{
    /* Important definitions */
    // Lengths
    size_t N = 0;  // Length of time series
    size_t M = 0;  // Length of sampling vector (number of frequencies)

    // Filenames
    char inname[100];
    char outname[100];
    char logname[100];

    // Sampling
    double low, high, rate;

    // Options
    int quiet = 0;
    int unit = 1;
    int prep = 1;
    int autosamp = 0;
    int fast = 0;
    int useweight = 0;
    int Nclean = 1;
    int filter = 0;

    
    /* Process command line arguments and return line count of the input file */
    N = cmdarg(argc, argv, inname, outname, &quiet, &unit, &prep, &low, &high,\
               &rate, &autosamp, &fast, &useweight, NULL, NULL, &Nclean,\
               &filter, NULL, NULL);

    // Pretty print
    if ( quiet == 0 || fast == 1){
        if ( useweight != 0 )
            printf("\nCLEANing the time series \"%s\" using weights...\n",\
                   inname);
        else
            printf("\nCLEANing the time series \"%s\" without weights...\n",\
                   inname);
    }

    
    /* Read data (and weights) from the input file */
    if ( quiet == 0 ) printf(" - Reading input\n");
    double* time = malloc(N * sizeof(double));
    double* flux = malloc(N * sizeof(double));
    double* weight = malloc(N * sizeof(double));
    readcols(inname, time, flux, weight, N, useweight, unit, quiet);

    // Do if fast-mode is not activated
    if ( fast == 0 ) {
        // Calculate Nyquist frequency
        double* dt = malloc(N-1 * sizeof(double));
        double nyquist;
        arr_diff(time, dt, N);
        nyquist = 1.0 / (2.0 * arr_median(dt, N-1)) * 1e6; // microHz !
        free(dt);

        // Automatic or manual sampling?
        int oversamp;
        if ( autosamp != 0 ) {
            low = 5.0;
            high = nyquist;
            oversamp = 4;
        }
        else {
            oversamp = (int) rate;
        }
            
       
        // Calculate N times oversampling (in microHz!)
        double minsamp = 1.0e6 / (oversamp * (time[N-1] - time[0]));
        rate = minsamp;
    
        // Display info?
        if ( quiet == 0 ){
            printf(" -- INFO: Length of time series = %li\n", N);
            printf(" -- INFO: Nyquist frequency = %.2lf microHz\n", nyquist);
            printf(" -- INFO: Using %i times oversampling = %.3lf microHz\n",\
                   oversamp, minsamp);
        }
    }
    else {
        // Only set the N times oversampling
        int oversamp = (int) rate;
        double minsamp = 1.0e6 / (oversamp * (time[N-1] - time[0]));
        rate = minsamp;
    }

    
    /* Prepare for power spectrum */
    // Get length of sampling vector
    M = arr_util_getstep(low, high, rate);

    // Fill sampling vector with cyclic frequencies
    double* freq = malloc(M * sizeof(double));
    arr_init_linspace(freq, low, rate, M);
    if ( quiet == 0 )
        printf(" -- INFO: Number of sampling frequencies = %li\n", M);

    // Subtract the mean to avoid "zero-frequency" problems
    double fmean = 0;
    if ( prep != 0 ) {
        if ( quiet == 0 ) printf(" - Subtracting the mean from time series\n");
        fmean = arr_mean(flux, N);
        arr_sca_add(flux, -fmean, N);
    }
    else {
        if ( quiet == 0 )
            printf(" - Time series used *without* mean subtraction!\n");
    }


    /* Prepare file for writing the CLEAN-output */
    // Create log-file
    strcpy(logname, outname);
    strcat(logname, ".cleanlog");
    FILE* logfile = fopen(logname, "w");

    // Write header
    fprintf(logfile, "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\
            "~~~~~~~~~~\n");
    fprintf(logfile, "# Log of CLEAN on \"%s\"\n", inname);
    fprintf(logfile, "# Interval: [%.2lf, %.2lf] microHz\n", low, high);
    fprintf(logfile, "# Finding %i frequencies\n", Nclean);
    fprintf(logfile, "# \n");
    fprintf(logfile, "# %8s %11s %11s %12s %12s\n", "Number", "Frequency",\
            "Power", "Alpha", "Beta");
    fprintf(logfile, "# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"\
            "~~~~~~~~~~\n");

    
    /* Find and clean peaks */
    // Init variables
    double fmax, alpmax, betmax, powmax;
    
    // Display info
    if ( quiet == 0 ) {
        printf(" - CLEANing %i frequencies in the range %.1lf to %.1lf"\
               " microHz\n", Nclean, low, high);
        printf("\n %9s %11s %11s\n", "Number", "Frequency", "Power");
    }

    // Enter CLEAN-loop
    for (int i = 0; i < Nclean; ++i) {
        // Display progress
        if ( quiet == 0 ) printf(" %6i", i+1);

        // Reset variables
        fmax = 0;
        alpmax = 0;
        betmax = 0;
        powmax = 0;

        // Call with or without weights
        fouriermax(time, flux, weight, freq, N, M, &fmax, &alpmax, &betmax,\
                   useweight);

        // Calculate the power and write to log
        powmax = alpmax*alpmax + betmax*betmax;
        fprintf(logfile, " %6i %15.6lf %12.6lg %12.6lf %12.6lf\n", i+1, fmax,\
                powmax, alpmax, betmax);
        if ( quiet == 0) printf(" %15.6lf %12.6lg \n", fmax, powmax);

        // Remove frequency from time series
        for (int j = 0; j < N; ++j) {
            flux[j] = flux[j] - alpmax * sin( PI2micro*fmax * time[j] ) - \
                                betmax * cos( PI2micro*fmax * time[j] );
        }
    }

    // Final touch
    fclose(logfile);
    if ( quiet == 0 ) printf("\n");

    
    /* Write CLEANed time series to file */
    if ( quiet == 0 ) printf(" - Saving to file \"%s\"\n", outname);

    // Add the mean to the time series data again
    if ( prep != 0 ) arr_sca_add(flux, fmean, N);

    // Save to file
    writecols3(outname, time, flux, weight, N, useweight, unit);

    
    /* Free data */
    free(time);
    free(flux);
    free(weight);
    free(freq);


    /* Done! */
    if ( quiet == 0 || fast ==1 ) printf("Done!\n\n");
    return 0; 
}