Skip to content

Instantly share code, notes, and snippets.

@dekrain
Created February 26, 2018 21:00
Show Gist options
  • Save dekrain/34641107969fc6f1ef820b6fbc5e9d2a to your computer and use it in GitHub Desktop.
Save dekrain/34641107969fc6f1ef820b6fbc5e9d2a to your computer and use it in GitHub Desktop.
A try to implement Fourier Transform in C programming language
#define float_t _std__float_t
#include <stddef.h>
#include <math.h>
#include <stdio.h>
#undef float_t
#define PI trig(atan2)(0, -1)
#define trig(f) f // Uses trigonometric function for float precision of float_t
#define fm "%f" // Format for type float_t (printf)
typedef double float_t; // Defines float precision
typedef struct complex {
float_t real, imag;
} complex_t; // Definition of complex number
// buf - buffer with samples
// size - count of samples in the buffer
// rate - samples per time unit
// freq - transform frequency
complex_t fourier_transform(float_t *buf, size_t size, size_t rate, float_t freq) {
complex_t result, tmp;
size_t i;
float_t x;
const float_t dx = 1.0 / rate;
result.real = 0;
result.imag = 0;
for (i = 0; i < size; i++) {
x = (float_t)i / rate;
// tmp = dx*sample*e^(-2*pi*x*f*i)
// = dx*sample*cos(tau*x*f) - i*dx*sample*sin(tau*x*f)
// sin(-x) = -sin(x); cos(-x) = cos(x)
tmp.real = dx*buf[i]*trig(cos)(2 * PI * x * freq);
tmp.imag = -dx*buf[i]*trig(sin)(2 * PI * x * freq);
result.real += tmp.real;
result.imag += tmp.imag;
}
return result;
}
int main(void) {
float_t buf[1000];
size_t i;
complex_t c;
for (i = 0; i < 1000; i++) {
// Fill in buffer with 3 Hz + 5 Hz wave
buf[i] = 100*trig(sin)(3 * (float_t)i / 100)
+ 100*trig(sin)(5 * (float_t)i / 100);
}
for (i = 0; i < 20; i++) {
c = fourier_transform(buf, 1000, 100, (float_t)i/2);
printf("Fourier transform (freq = "fm"): mod="fm" real="fm" imag="fm"\n",
(float_t)i/2, trig(sqrt)(c.real*c.real + c.imag*c.imag), c.real, c.imag);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment