-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfftTest.cpp
138 lines (102 loc) · 2.7 KB
/
fftTest.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
133
134
135
136
137
138
//g++ -std=gnu++11 fftTest.cpp ./kiss_fft130/kiss_fft.c -L /home/l1f3/mylib/lib/ -lAquila -lOoura_fft -o fftTest
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <chrono>
#include <sys/time.h>
#include "aquila/source/WaveFile.h"
#include "kiss_fft130/kiss_fft.h"
#ifndef M_PI
#define M_PI 3.14159265358979324
#endif
#define N 2048
kiss_fft_cpx in[N],out[N];
typedef unsigned long long timestamp_t;
static timestamp_t
get_timestamp ()
{
struct timeval now;
gettimeofday (&now, NULL);
return now.tv_usec + (timestamp_t)now.tv_sec * 1000000;
}
void getFft(const kiss_fft_cpx in[N], kiss_fft_cpx out[N])
{
kiss_fft_cfg cfg;
if ((cfg = kiss_fft_alloc(N, 0/*is_inverse_fft*/, NULL, NULL)) != NULL)
{
size_t i;
kiss_fft(cfg, in, out);
free(cfg);
}
else
{
printf("not enough memory?\n");
exit(-1);
}
}
void print_vec(const std::vector<int>& vec)
{
for (auto x: vec) {
std::cout << ' ' << x;
}
std::cout << '\n';
}
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cout << "Usage: wave_iteration <FILENAME>" << std::endl;
return 1;
}
Aquila::WaveFile wav(argv[1]);
std::vector<int> array(0);
std::vector<int>::iterator it;
it = array.begin();
timestamp_t t0 = get_timestamp();
int i,j,x;
int graph[N/2];
double mag[N/2];
double roof = wav.getSamplesCount();
double framePointer = 0;
while(framePointer < roof ){
for( i = framePointer, j = 0; i < (framePointer + N)
&& framePointer < roof - N ; i++,j++ )
{
//Apply window function on the sample
double multiplier = 0.5 * (1 - cos(2*M_PI*j/(N-1)));
in[j].r = multiplier * wav.sample(i);
in[j].i = 0; //stores N samples
}
if(framePointer < roof-N ){
framePointer = i;
}
else {
timestamp_t t1 = get_timestamp();
double secs = (t1 - t0) / 1000000.0L;
// print_vec(array);
std::cout<<"Total exec time: "<<secs<<std::endl;
break;
}
//std::cout<<"Framepointer = "<<framePointer<<std::endl;
getFft(in,out);
// calculate magnitude of first n/2 FFT
for(i = 0; i < N/2; i++ ){
int val;
mag[i] = sqrt((out[i].r * out[i].r) + (out[i].i * out[i].i));
// N/2 Log magnitude values.
//for (i = 0; i < N/2 ; ++i){
// x = 10 * log10(mag[i]) ;
// printf(" log x= %g ", log(x));
val = graph[i] = log(mag[i]) *10;
// std::cout<<graph[i]<<std::endl;
it = array.end();
it = array.insert(it,val);
}
}
//std::cout<<array[3]<<std::endl;;
//print_vec(array);
std::vector<int>::size_type sz = array.size();
//for(size_t i = 0; i< sz; i++)
// std::cout<<array[i]<<std::endl;
}