-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdft_brute_force.cc
More file actions
114 lines (92 loc) · 3.08 KB
/
dft_brute_force.cc
File metadata and controls
114 lines (92 loc) · 3.08 KB
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
/*
* Copyright (c) 2019 Stephen Williams (steve@icarus.com)
*
* This source code is free software; you can redistribute it
* and/or modify it in source code form under the terms of the GNU
* General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
# include "priv.h"
# include <complex>
# include <iostream>
# include <vector>
# include <cmath>
# include <cstdio>
# include <cstring>
# include <cassert>
# define TRANS_SIGN (1)
using namespace std;
/*
* Implement the DFT using brute force. If f[n] is the input vector,
* and F[n] the output vector, and there are N elements in the input
* and output vectors, the formula being calculated is:
*
* F[n] = SUM[k=0 to N-1]( f[k] * (W ** (n*k)) )
*
* where:
*
* W = exp( (2*pi*i)/N )
*
* This function run time is O(N**2), which is not practical; but this
* makes for a workable reference implementation.
*/
std::vector< complex<double> > dft_brute_force (const std::vector< complex<double> >&src)
{
const double N = src.size();
const int Ni = src.size();
vector< complex<double> > dst;
dst.resize(Ni);
// W = exp( 2*PI*i/N )
// This is a constant that is raised to various powers. It
// depends only on the input vector size.
const complex<double> W = exp( complex<double>(0.0, TRANS_SIGN * 2*M_PI)/N );
// Iterate over the output vector components. Note that W**x
// is periodic, with period N, so W**x == W**(x%N).
for (int idx = 0 ; idx < Ni ; idx += 1) {
dst[idx] = src[0];
for (int k = 1 ; k < Ni ; k += 1) {
dst[idx] += src[k] * pow(W, (idx*k) % Ni);
}
}
return dst;
}
int main(int argc, char*argv[])
{
const char*src_path = 0;
const char*dst_path = 0;
for (int idx = 1 ; idx < argc ; idx += 1) {
if (strncmp(argv[idx],"--src=",6) == 0) {
src_path = argv[idx]+6;
} else if (strncmp(argv[idx],"--dst=",6) == 0) {
dst_path = argv[idx]+6;
} else {
}
}
FILE*src_fd = fopen(src_path, "rt");
if (src_fd == 0) {
fprintf(stderr, "Unable to open input file %s\n", src_path);
return -1;
}
vector< complex<double> > src = read_values_d(src_fd);
assert(src.size() != 0);
fclose(src_fd);
src_fd = 0;
vector< complex<double> > dst = dft_brute_force(src);
FILE*dst_fd = fopen(dst_path, "wt");
if (dst_fd == 0) {
fprintf(stderr, "Unable to open output file %s\n", dst_path);
return -1;
}
write_values(dst_fd, dst);
return 0;
}