void updateFFT() { //kiss_fft_scalar pt; int timespend = 0; int timestamp = millis(); for(int i=0; i < FFT_SIZE; i++) { fft_in[i] = (windowfunction(i)*(((float)analogRead(MICROPHONE))-2048)/16.f); // decrease the signal input number so that u can process it with fft /*float anIN = ((float)analogRead(MICROPHONE))/32.f; fft_in[i] = windowfunction(i)*anIN; Serial.print(anIN); Serial.print(" \t"); Serial.print(windowfunction(i)); Serial.print(" \t"); Serial.println(fft_in[i]); //testing with artificial sinus signal */ //fft_in[i] = sin(2*3.14*5/FFT_SIZE*i)+sin(2*3.14*0.45*i); delayMicroseconds(SAMPLEDELAY); // Define the sample rate: 280us are about 3500Hz samplerate, // frequency analysis works up to the half of this frequency } timespend = millis() - timestamp; frequency = FFT_SIZE*1000/timespend; Serial.print(FFT_SIZE); Serial.print("analog reads at "); Serial.print(frequency); Serial.print(" Hz needed"); Serial.print(timespend); Serial.println(" ms"); kiss_fftr(fft_cfg, fft_in, fft_out); }
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; }