fftanalyzer/fftanalyzer.c
int fft(fftw_plan p, int length){

	double *in;
	fftw_complex* out;


	fftw_execute(p); /* repeat as needed */

	double re, im, mag, pha;

	long samples=length/2+1; //

	double samplerate=16000.0;
	double maxfreq=samplerate/2.0;
	double res=samplerate/length; // this is the frequency resolution in hertz for each frequency sample

	double freq;

	printf("// fast fourier transform output:\n"
			"{\n"
			"//sample rate - samples per second\n"
			"\"rate\":16000.0,\n"
			"//array of fft samples, representing frequecy magnitudes\n"
			"\"samples\":[\n");
	int i;
	for(i=0;i<samples;i++){

		re = out[i][0]; //real component
		im = out[i][1]; //imaginary component
		mag = sqrt((re * re) + (im * im)); //magnitude

		//calculate phase
		if(re!=0){
			pha= atan(im/re);
			if(re<0){
				if(im>=0){
					pha+=M_PI;
				}else{
					pha-=M_PI;
				}
			}
		}else{
			if(im>0){
				pha=M_PI_2;
			}else if(im<0){
				pha=-M_PI_2;
			}else{
				pha=0;
			}
		}

		if(i==0){
			//this is the DC component
			printf("\t// the first sample is the dc component\n"
					"\t{\"m\":%0.3f, \"f\":\"0.0\"},\n"
					"", mag, pha);
		}else{
			freq=(i/(double)(samples-1))*maxfreq;
			printf("\t{\"m\":%0.3f, \"p\":%lf, \"f\":%0.1f}", mag, pha, freq-(res/2.0));
			if(i<samples-1){
				printf(",");
			}
			printf("\n");
		}

	}


	printf("\t],\n"
			"\"count\":%d,\n"
			"\"resolution\":%0.3f,\n"
			"\"max_freq\":%0.3f,\n"
			"\"time_len_sec\":%0.2f\n"
			"}\n", samples, res, maxfreq, length/samplerate);



	fftw_destroy_plan(p);
	fftw_free(in); fftw_free(out);
}
/**
 * @param FILE stream, file handle or stdin
 * @param int N number of samples to use for fft transform
 */
@param FILE stream, file handle or stdin
@param int N number of samples to use for fft transform
@param FILE stream file handle or stdin @param int N number of samples to use for fft transform
int fft(fftw_plan p, int length){
	double *in;
	fftw_complex* out;
	fftw_execute(p); 
	double re, im, mag, pha;
	long samples=length/2+1; 
	double samplerate=16000.0;
	double maxfreq=samplerate/2.0;
	double res=samplerate/length;  this is the frequency resolution in hertz for each frequency sample
	double freq;
	printf(" fast fourier transform output:\n"
			"{\n"
			"sample rate - samples per second\n"
			"\"rate\":16000.0,\n"
			"array of fft samples, representing frequecy magnitudes\n"
			"\"samples\":[\n");
	int i;
	for(i=0;i<samples;i++){
		re = out[i][0]; real component
		im = out[i][1]; imaginary component
		mag = sqrt((re * re) + (im * im)); magnitude
		calculate phase
		if(re!=0){
			pha= atan(im/re);
			if(re<0){
				if(im>=0){
					pha+=M_PI;
				}else{
					pha-=M_PI;
				}
			}
		}else{
			if(im>0){
				pha=M_PI_2;
			}else if(im<0){
				pha=-M_PI_2;
			}else{
				pha=0;
			}
		}
		if(i==0){
			this is the DC component
			printf("\t the first sample is the dc component\n"
					"\t{\"m\":%0.3f, \"f\":\"0.0\"},\n"
					"", mag, pha);
		}else{
			freq=(i/(double)(samples-1))*maxfreq;
			printf("\t{\"m\":%0.3f, \"p\":%lf, \"f\":%0.1f}", mag, pha, freq-(res/2.0));
			if(i<samples-1){
				printf(",");
			}
			printf("\n");
		}
	}
	printf("\t],\n"
			"\"count\":%d,\n"
			"\"resolution\":%0.3f,\n"
			"\"max_freq\":%0.3f,\n"
			"\"time_len_sec\":%0.2f\n"
			"}\n", samples, res, maxfreq, length/samplerate);
	fftw_destroy_plan(p);
	fftw_free(in); fftw_free(out);
}
int fft ( fftw_plan p , int length ) { double * in ; fftw_complex * out ; fftw_execute ( p ) ; double re , im , mag , pha ; long samples = length / 2 + 1 ; double samplerate = 16000.0 ; double maxfreq = samplerate / 2.0 ; double res = samplerate / length ; double freq ; printf ( " \n " " \n " " \n " " \" \" \n " " \n " " \" \" \n " ) ; int i ; for ( i = 0 ; i < samples ; i ++ ) { re = out [ i ] [ 0 ] ; im = out [ i ] [ 1 ] ; mag = sqrt ( ( re * re ) + ( im * im ) ) ; if ( re != 0 ) { pha = atan ( im / re ) ; if ( re < 0 ) { if ( im >= 0 ) { pha += M_PI ; } else { pha -= M_PI ; } } } else { if ( im > 0 ) { pha = M_PI_2 ; } else if ( im < 0 ) { pha = - M_PI_2 ; } else { pha = 0 ; } } if ( i == 0 ) { printf ( " \t \n " " \t \" \" \" \" \" \" \n " " " , mag , pha ) ; } else { freq = ( i / ( double ) ( samples - 1 ) ) * maxfreq ; printf ( " \t \" \" \" \" \" \" " , mag , pha , freq - ( res / 2.0 ) ) ; if ( i < samples - 1 ) { printf ( " " ) ; } printf ( " \n " ) ; } } printf ( " \t \n " " \" \" \n " " \" \" \n " " \" \" \n " " \" \" \n " " \n " , samples , res , maxfreq , length / samplerate ) ; fftw_destroy_plan ( p ) ; fftw_free ( in ) ; fftw_free ( out ) ; }
@param FILE stream, file handle or stdin
 @param int N number of samples to use for fft transform
@param FILE stream file handle or stdin @param int N number of samples to use for fft transform
/* repeat as needed */ // // this is the frequency resolution in hertz for each frequency sample //real component //imaginary component //magnitude //calculate phase //this is the DC component