Newer
Older
/*
************************************
* Fast Fourier transform FFT
************************************
*/
#include <iostream>
#include "bmp.h"
#include "fft.h"
int main(int argc, char** args) {
//Check args
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
printf("USAGE: fft lena_gray.bmp\n");
return 0;
}
//Read input file
const char* filename = args[1];
BMP lena_bmp(filename);
int image_size = lena_bmp.bmp_info_header.width * lena_bmp.bmp_info_header.height;
int image_channels = lena_bmp.bmp_info_header.bit_count / 8;
//Allocate memory for data
double* d_input = new double[(size_t)2 * image_size * image_channels]; //complex value
double* d_output = new double[(size_t)2 * image_size * image_channels]; //complex value
double* d_w = new double[image_size];
//Fill signal array with data
for (int c = 0; c < image_channels; c++) {
double* in = d_input + c * 2 * image_size;
for (int i = 0; i < image_size; i++) {
//Set only RE part, IM is 0
in[i * 2 + 0] = (double)lena_bmp.data[image_channels * i + c]; //real part
in[i * 2 + 1] = 0; //im part
}
}
//Init FFT
cffti(image_size, d_w);
//Apply FFT
for (int c = 0; c < image_channels; c++) {
double* in = d_input + c * 2 * image_size;
double* out = d_output + c * 2 * image_size;
//Forward
cfft2(image_size, in, out, d_w, +1.0);
//Backward
cfft2(image_size, out, in, d_w, -1.0);
}
//Fill signal array with data
for (int c = 0; c < image_channels; c++) {
double* in = d_input + c * 2 * image_size;
for (int i = 0; i < image_size; i++) {
lena_bmp.data[image_channels * i + c] = in[i * 2 + 0] / (double)image_size; //real part
}
}
//Write to output file
std::string out_filename = std::string(filename) + ".out.bmp";
lena_bmp.write(out_filename.c_str());
//Free memory
delete[] d_input;
delete[] d_output;
delete[] d_w;
return 0;
}