-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.c
148 lines (125 loc) · 4.37 KB
/
test.c
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* FFT test
*
* Copyright (c) 2021 Project Nayuki. (MIT License)
* https://www.nayuki.io/page/free-small-fft-in-multiple-languages
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
* - The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* - The Software is provided "as is", without warranty of any kind, express or
* implied, including but not limited to the warranties of merchantability,
* fitness for a particular purpose and noninfringement. In no event shall the
* authors or copyright holders be liable for any claim, damages or other
* liability, whether in an action of contract, tort or otherwise, arising from,
* out of or in connection with the Software or the use or other dealings in the
* Software.
*/
#include <complex.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define RFFT_IMPLEMENTATION
#include "rfft.h"
// Private function prototypes
static void test_fft(int n);
static void naive_dft(const double complex invec[restrict], double complex outvec[restrict], int n, bool inverse);
static double log10_rms_err(const double complex xvec[], const double complex yvec[], int n);
static double complex *random_complexes(int n);
static void *memdup(const void *src, size_t n);
static double max_log_error = -INFINITY;
/*---- Main and test functions ----*/
int main(void) {
srand(time(NULL));
// Test power-of-2 size FFTs
for (int i = 0; i <= 12; i++)
test_fft(1 << i);
// Test small size FFTs
for (int i = 0; i < 30; i++)
test_fft(i);
// Test diverse size FFTs
for (int i = 0, prev = 0; i <= 100; i++) {
int n = (int)lround(pow(1500, i / 100.0));
if (n > prev) {
test_fft(n);
prev = n;
}
}
printf("Max log err = %.1f\n", max_log_error);
printf("Test %s\n", max_log_error < -10 ? "passed" : "failed");
return EXIT_SUCCESS;
}
static void test_fft(int n) {
double complex *input = random_complexes(n);
double complex *expect = malloc(n * sizeof(double complex));
naive_dft(input, expect, n, false);
double complex *actual = memdup(input, n * sizeof(double complex));
fft_transform(actual, n, false);
double err0 = log10_rms_err(expect, actual, n);
for (int i = 0; i < n; i++)
actual[i] /= n;
fft_transform(actual, n, true);
double err1 = log10_rms_err(input, actual, n);
printf("fftsize=%4d logerr=%5.1f %5.1f\n", n, err0, err1);
free(input);
free(expect);
free(actual);
}
/*---- Naive reference computation functions ----*/
static void naive_dft(const double complex invec[restrict], double complex outvec[restrict], int n, bool inverse) {
double coef = (inverse ? 2 : -2) * M_PI;
for (int k = 0; k < n; k++) { // For each output element
double complex sum = 0.0;
for (int t = 0; t < n; t++) { // For each input element
double angle = coef * ((uintmax_t)t * k % n) / n;
sum += invec[t] * (cos(angle) + I * sin(angle));
}
outvec[k] = sum;
}
}
/*---- Utility functions ----*/
static double log10_rms_err(const double complex xvec[], const double complex yvec[], int n) {
double err = pow(10, -99 * 2);
double max = 0;
int maxindex = 0;
for (int i = 0; i < n; i++) {
double temp = cabs(xvec[i] - yvec[i]);
err += temp * temp;
if (temp > max) {
max = temp;
maxindex = i;
}
}
//printf("Max: %d %f\n", maxindex, max);
err /= n > 0 ? n : 1;
err = sqrt(err); // Now this is a root mean square (RMS) error
err = log10(err);
if (err > max_log_error)
max_log_error = err;
return err;
}
static double complex *random_complexes(int n) {
double complex *result = malloc(n * sizeof(double complex));
for (int i = 0; i < n; i++) {
double re = (rand() / (RAND_MAX + 1.0)) * 2 - 1;
double im = (rand() / (RAND_MAX + 1.0)) * 2 - 1;
result[i] = re + im * I;
}
return result;
}
static void *memdup(const void *src, size_t n) {
void *dest = malloc(n);
if (n > 0 && dest != NULL)
memcpy(dest, src, n);
return dest;
}