summaryrefslogtreecommitdiff
path: root/fft.h
blob: 61d4ba9630dccac881ba294237f9f1dc1f92821d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <math.h>

#ifndef FALSE
#define FALSE (0)
#endif

#ifndef TRUE
#define TRUE (1)
#endif

typedef struct
{
    double re;
    double im;
} complex_t;

static inline complex_t
complex_mul (complex_t a, complex_t b)
{
    complex_t r;

    r.re = a.re * b.re - a.im * b.im;
    r.im = a.re * b.im + a.im * b.re;

    return r;
}

static inline complex_t
complex_add (complex_t a, complex_t b)
{
    complex_t r;

    r.re = a.re + b.re;
    r.im = a.im + b.im;

    return r;
}

static inline complex_t
complex_sub (complex_t a, complex_t b)
{
    complex_t r;

    r.re = a.re - b.re;
    r.im = a.im - b.im;

    return r;
}

static inline double
complex_mag (complex_t a)
{
    return sqrt (a.re * a.re + a.im * a.im);
}
static inline double
complex_arg (complex_t a)
{
    return fmod (atan2 (a.im, a.re) + 2 * M_PI, 2 * M_PI);
}

static inline complex_t
complex_from_mag_arg (double mag, double arg)
{
    complex_t r;

    r.re = mag * cos (arg);
    r.im = mag * sin (arg);

    return r;
}

static inline double
rad_to_degree (double theta)
{
    return theta * (360.0 / (2 * M_PI));
}

void
fft (complex_t *buffer, int n);

void
ifft (complex_t *buffer, int n);

/* Fourier transform an n x n array */
void
fft_2d (complex_t *buffer, int n);

void
ifft_2d (complex_t *buffer, int n);

/* Shifts the zero component to the center of the array */
void
shift (complex_t *buffer, int n);

void
shift_2d (complex_t *buffer, int n);

void
fft_shift_2d (complex_t *buffer, int n);

void
ifft_shift_2d (complex_t *buffer, int n);

void
show_image (const char *name, complex_t *image, int n);