コード例 #1
1
int main(int argc, char **argv) {
	
	int k, N = STD_N, adcNumber = STD_ADCPIN, bench = BENCH_FLAG;
	float yMax = STD_YMAX, lines = STD_LINES, DELTA = STD_DELTA;
	mraa_aio_context adc;

	struct timeval inicio, fim;
	double tmili;

	if (argc == 2) {
		yMax = atof(argv[1]);
	}
	else if (argc == 3) {
		yMax = atof(argv[1]);
		lines = atof(argv[2]);
	}

	adc = mraa_aio_init(adcNumber);
	if (adc == NULL) return 1;

	hideCursor();
	clear();

	while (1) {
		cplx samples[N];
		double deltaFreq;

		gettimeofday(&inicio, NULL);
		for (k = 0; k < N; k++) {
			uint16_t sample = mraa_aio_read(adc);
			samples[k] = (cplx) 5*sample/(resolution - 1.0);
		}
		gettimeofday(&fim, NULL);

		tmili = (double) (1000 *(fim.tv_sec - inicio.tv_sec) + (fim.tv_usec - inicio.tv_usec) / 1000);
		deltaFreq = 1000.0 / tmili;

		if (bench == 1) {
			printf("\n>>>> Bench with:\n     N=%d\n     delta=%f\n", N, DELTA);
			printf(">>>> Sample time : %.2fms\n", tmili);
			printf("     deltaFreq   : %.2fHz\n", deltaFreq);
			printf("     max freq.   : %.2fHz\n", 500*N/tmili);
		}
		runFFT(samples, N, DELTA, deltaFreq, bench, yMax, lines);
		printCoord (N/2, 40);
	}

	mraa_aio_close(adc);
	return 0;
}
コード例 #2
0
void ADC0_SampleHandler()
{
	MAP_ADCIntClear(ADC0_BASE, 3);

	MAP_ADCSequenceDataGet(ADC0_BASE, 3, adc_value);

	inputData[inputIndex++] = (float) adc_value[0];

	if (inputIndex > TEST_LENGTH_SAMPLES - 1)
	{
		inputIndex = 0;

		// 'Permanently disable interrupt'
		// We will only run the fft once for now
		MAP_ADCIntDisable(ADC0_BASE, 3);
		MAP_TimerDisable(TIMER1_BASE, TIMER_A);

		runFFT();

		// Delay for 1 second
		MAP_SysCtlDelay(g_ui32SysClock / 3);

		//Re-enable timer and ADC interrupts to run another FFT
		MAP_ADCIntEnable(ADC0_BASE, 3);
		MAP_TimerEnable(TIMER1_BASE, TIMER_A);

	}

}
コード例 #3
0
float* CalcFFT(GDALDataset *srcDS1, GDALDataset *srcDS2, GDALDataset *dstDS) 
{
	fftwf_plan plan1, plan2, planI;
	fftwf_complex *img1, *img2;
	unsigned char *out;
	int band;

	const size_t px_count = dstDS->GetRasterXSize() * dstDS->GetRasterYSize();
	const size_t buffer_len = sizeof(fftwf_complex) * px_count;
	img1  = (fftwf_complex*) fftwf_malloc(buffer_len);
	img2  = (fftwf_complex*) fftwf_malloc(buffer_len);
	out   = (unsigned char*) fftwf_malloc(sizeof(unsigned char) * px_count); 
		/* ^ not used in fft, but aligned is good anyway */
	if(img1 == NULL || img2 == NULL || out == NULL)
		error("Could not allocate memory\n");

	if(fftwf_init_threads())
		fftwf_plan_with_nthreads(CORES);

	plan1 = fftwf_plan_dft_2d(dstDS->GetRasterYSize(), dstDS->GetRasterXSize(), 
	                        img1, img1, FFTW_FORWARD, FFTW_ESTIMATE);

	plan2 = fftwf_plan_dft_2d(dstDS->GetRasterYSize(), dstDS->GetRasterXSize(), 
	                        img2, img2, FFTW_FORWARD, FFTW_ESTIMATE);

	planI = fftwf_plan_dft_2d(dstDS->GetRasterYSize(), dstDS->GetRasterXSize(), 
	                        img2, img2, FFTW_BACKWARD, FFTW_ESTIMATE);

	if(plan1 == NULL || plan2 == NULL || planI == NULL)
		error("Could not plan FFT\n");

	for(band = 1; band <= dstDS->GetRasterCount(); band++) {
		printf("FFT 1 band %d\n", band);
		runFFT( plan1, srcDS1, img1, band, dstDS );
		printf("FFT 2 band %d\n", band);
		runFFT( plan2, srcDS2, img2, band, dstDS );

		
		printf("Complex Conj band %d\n", band);
		/* mult img1 and conj of img2 */
		for(int px = 0; px < px_count; px++) {
			img2[px] = img1[px] * conj(img2[px]);
		}
	
		/* IFFT of result */	
		printf("IFFT band %d\n", band);
		fftwf_execute(planI);	

		printf("normalize band %d\n", band);
		complex float norm = csqrt(px_count + 0I);
		float max = cabs(img2[0] / norm);
		float min = cabs(img2[0] / norm);
		for(int i = 0; i < px_count; i++) {
			img2[i] = img2[i] / norm;
			
			if(cabs(img2[i]) < min)
				min = cabs(img2[i]);
			if(cabs(img2[i]) > max)
				max = cabs(img2[i]);
		}
		/* img2 should now be real - normalize 0-255 and -- write output */
		printf("Save band %d; min = %f max = %f\n", band, min, max);
		for(int i = 0; i < px_count; i++) {
			out[i] = floor( ((cabs(img2[i]) - min) / (max-min) ) * 255.0 );
		}

		fft2shift(out, dstDS->GetRasterYSize(), dstDS->GetRasterXSize());

 		dstDS->GetRasterBand(band)->RasterIO( GF_Write, 0, 0, 
					   dstDS->GetRasterXSize(), dstDS->GetRasterYSize(),
					   out, dstDS->GetRasterXSize(), dstDS->GetRasterYSize(),
					   GDT_Byte, 0, 0);
	}



	fftwf_destroy_plan(plan1);
	fftwf_destroy_plan(plan2);
	fftwf_destroy_plan(planI);
	fftwf_free(img1);
	fftwf_free(img2);
	fftwf_free(out);
}