Discrete Fourier Transform
Doing it the hard way. O(N
2) operations.
uc-dft.c file
Compiles clean (or should) with:
gcc -o uc-dft uc-dft.c -Wall
#include <stdio.h>
#include <complex.h>
#include <math.h>
#include <stdint.h>
// Number of samples.
typedef enum TE_CONSTS
{
N = 256,
} E_CONSTS;
//
int n;
//
int k;
// sample array.
static int ReXn[N];
static int ImXn[N];
// frequency bins.
static double ReXf[N];
static double ImXf[N];
static double ReTmp;
static double ImTmp;
int main(int argc, char **argv)
{
// step function at t=0 to t=7.
ReXn[0]=1;
ReXn[1]=1;
ReXn[2]=1;
ReXn[3]=1;
ReXn[4]=1;
ReXn[5]=1;
ReXn[6]=1;
ReXn[7]=1;
for (k = 0; k < N; ++k)
{
ReTmp = 0.0;
ImTmp = 0.0;
for (n = 0; n < N; ++n)
{
ReTmp += ReXn[n] * cos((2*M_PI*n*k)/N) * 255;
ImTmp += ReXn[n] * sin((2*M_PI*n*k)/N) * 255;
}
ReXf[k] = ReTmp;
ImXf[k] = ImTmp;
printf("X[%d]: %f + %fj\n", k, ReXf[k], ImXf[k]);
}
for (k = 0; k < N; ++k)
{
printf("%f\n", sqrt(ReXf[k]*ReXf[k] + ImXf[k]*ImXf[k]));
}
return 0;
}
(c)2008
Max Vilimpoc,
http://vilimpoc.org/research/