If you want to experiment, here is a fully functional test version of the linked DFT code, intended for a standard C/C++ console program. I also added the ability to do the inverse DFT (merely a change of sign). It does get the correct answer, but uses slow sine/cosine calculations. That could be sped up significantly by using a lookup sine table, for example.
I use the free Code::Blocks IDE on a PC for testing, and more serious calculations.
If you want to modify the code to calculate only a subset of the output bins, just change the limits on k in the following line. In any case, k need run only until n/2 but running until n-1 allows you to see the full symmetry of the transform.
for (k = 0; k < n; k++) { // For each output element
Functional test program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
* Computes the discrete Fourier transform (DFT) of the given complex vector, separated
* into real and imaginary parts.
*
* If (n<0) compute the inverse transformation.
*
* All the array arguments must have the same length.
* Only the first n/2 points in the output arrays are unique.
*/
void computeDft(int n, float *inreal, float *inimag, float *outreal, float *outimag)
{
float sign=1.;
if (n<0) {n=-n; sign=-1.;}
float factor = sign*2*M_PI/n; //take out of inner loop
int k,t;
for (k = 0; k < n; k++) { // For each output element
double sumreal = 0;
double sumimag = 0;
for (t = 0; t < n; t++) { // For each input element
double angle = factor * t * k;
sumreal += inreal[t] * cos(angle) + inimag[t] * sin(angle);
sumimag += -inreal[t] * sin(angle) + inimag[t] * cos(angle);
}
outreal[k] = sumreal;
outimag[k] = sumimag;
}
}
int main()
{
// length of FT
#define N 16
float inreal[]={1, 1 , 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1}; //real square wave of period N samples
float inimag[N]={0};
float outreal[N]={0};
float outimag[N]={0};
computeDft(N,inreal,inimag,outreal,outimag);
int k;
printf(" real imag \n");
for (k=0; k<N; k++) printf("%8.3f %8.3f\n",outreal[k],outimag[k]);
//compute the inverse transform. Output should be N x (original input).
computeDft(-N,outreal,outimag,inreal,inimag);
printf(" real imag \n");
for (k=0; k<N; k++) printf("%8.3f %8.3f\n",inreal[k],inimag[k]);
return 0;
}