// compute DFT magnitude from input samples #include #include #define N 256 #define N2 ((N/2)+1) int main( int argc, char *argv[]) { double C[N2] = { 0 }, S[N2] = { 0 }, pi = 4*atan(1), x, wk; int s, j, k = 0, twoflag = 0; // check for command-line argument "2" // indicating 2-column input // if( argc > 1 && argv[1][0] == '2') twoflag = 1; while(1) // until error or EOF { // with 2-column input, the first column is ignored // if( twoflag) s = scanf( "%lf", &x); s = scanf( "%lf", &x); if( s < 0) break; if( s != 1) { fprintf( stderr, "dft: bad input data\n"); break; } for( j = 0; j < N2; ++j) { wk = (2*pi*j/N)*k; C[j] += x*cos(wk); S[j] += x*sin(wk); } ++k; } for( j = 0; j < N2; ++j) printf( "%g %g\n", j*8000.0/N, sqrt(C[j]*C[j]+S[j]*S[j]) ); return 0; }