-
Notifications
You must be signed in to change notification settings - Fork 4
/
demo.cpp
132 lines (120 loc) · 3.57 KB
/
demo.cpp
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "fft.hpp"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
double lerp(double a, double b, double i)
{
return a*(1-i) + b*i;
}
enum {
TEST_IMPULSE
,TEST_DC
,TEST_NYQUIST
,TEST_HALF
,TEST_SQUARE
,TEST_SQUAREDOUBLE
,TEST_SAW
,TEST_NOISE
,TEST_COMPLEXIMPULSE
,TEST_COMPLEXDC
,TEST_COMPLEXNOISE
,TEST_BLANK
};
#define TEST TEST_IMPULSE
void init(double* in_real, double* in_imag, uint64_t size)
{
for(uint64_t i = 0; i < size; i++)
{
switch(TEST)
{
case TEST_IMPULSE:
in_real[i] = 0;
in_imag[i] = 0;
in_real[0] = 1;
break;
case TEST_DC:
in_real[i] = 1;
in_imag[i] = 0;
break;
case TEST_NYQUIST:
in_real[i] = cos(M_PI*i);
in_imag[i] = 0;
break;
case TEST_HALF:
in_real[i] = cos(M_PI*i/2);
in_imag[i] = 0;
break;
case TEST_SQUARE:
in_real[i] = (i<size/2)?1:-1;
in_imag[i] = 0;
break;
case TEST_SQUAREDOUBLE:
in_real[i] = (i%(size/2)<size/4)?1:-1;
in_imag[i] = 0;
break;
case TEST_SAW:
in_real[i] = lerp(-1, 1, i/double(size-1)); // subtract 1 from size to avoid DC from incorrect starting phase
in_imag[i] = 0;
break;
case TEST_NOISE:
in_real[i] = rand()%1024/512.0-1;
in_imag[i] = 0;
break;
case TEST_COMPLEXIMPULSE:
in_real[i] = 0;
in_imag[i] = 0;
in_real[0] = 1;
in_imag[0] = 1;
break;
case TEST_COMPLEXDC:
in_real[i] = 1;
in_imag[i] = 1;
break;
case TEST_COMPLEXNOISE:
in_real[i] = rand()%1024/512.0-1;
in_imag[i] = rand()%1024/512.0-1;
break;
case TEST_BLANK:
default:
in_real[i] = 0;
in_imag[i] = 0;
}
}
}
int main()
{
const int SIZE = 16;
srand(time(NULL));
double* in_real = (double*)malloc(sizeof(double)*SIZE);
double* in_imag = (double*)malloc(sizeof(double)*SIZE);
double* out_real = (double*)malloc(sizeof(double)*SIZE);
double* out_imag = (double*)malloc(sizeof(double)*SIZE);
init(in_real, in_imag, SIZE);
puts("data");
puts("sample\treal\timag\tmag");
for(int i = 0; i < SIZE; i++)
printf("%d\t%.2f\t%.2f\t%.2f \n", i, in_real[i], in_imag[i], sqrt(in_real[i]*in_real[i]+in_imag[i]*in_imag[i]));
puts("");
fft(in_real, in_imag, SIZE, out_real, out_imag);
// end FFT display for real inputs early
bool signal_was_real = (TEST < TEST_COMPLEXIMPULSE or TEST > TEST_COMPLEXNOISE);
puts("transform");
puts("bin\treal\timag\tmag");
for(int i = 0; i < SIZE; i++)
{
// boost nonunique frequencies for real inputs on display
double factor = (i>0 and i<SIZE/2 and signal_was_real)?2:1;
printf("%d\t%.2f\t%.2f\t%.2f \n", i, out_real[i]*factor, out_imag[i]*factor, sqrt(out_real[i]*out_real[i]+out_imag[i]*out_imag[i])*factor);
// end FFT display for real inputs early
if(i >= SIZE/2 and signal_was_real)
break;
}
puts("");
ifft(out_real, out_imag, SIZE, in_real, in_imag);
puts("inverse");
puts("sample\treal\timag\tmag");
for(int i = 0; i < SIZE; i++)
printf("%d\t%.2f\t%.2f\t%.2f \n", i, in_real[i], in_imag[i], sqrt(in_real[i]*in_real[i]+in_imag[i]*in_imag[i]));
puts("");
}