-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfft_view_data.cpp
63 lines (56 loc) · 1.92 KB
/
fft_view_data.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
#include <iostream>
#include <complex>
#include <cmath>
#include <fstream>
#include <string>
#include <vector>
std::vector<std::complex<double>> loadSample(const std::string& filepath, const size_t& startIndex, const size_t numOfSamples) {
char* memblock;
std::ifstream ifs;
ifs.open(filepath, std::ios::in | std::ios::binary | std::ios::ate);
const std::streampos sampleSize = ifs.tellg();
if (startIndex * sizeof (double) * 2 >= sampleSize) {
std::cerr << "Index too large" << std::endl;
std::exit(-1);
} else {
ifs.seekg(sizeof(double) * 2 * startIndex, std::ios::beg);
}
size_t readNum;
if (numOfSamples == 0) {
readNum = sampleSize / sizeof (double) / 2 - startIndex;
} else if ((startIndex + numOfSamples) * sizeof (double) * 2 > sampleSize) {
readNum = sampleSize / sizeof (double) / 2 - startIndex;
} else {
readNum = numOfSamples;
}
memblock = new char[readNum * sizeof(double) * 2];
ifs.read(memblock, readNum * sizeof(double) * 2);
ifs.close();
const auto originalData = reinterpret_cast<double*>(memblock);
auto samples = std::vector<std::complex<double>>(readNum);
for (size_t i = 0; i < readNum; ++i) {
samples[i] = std::complex<double>(originalData[i * 2], originalData[i * 2 + 1]);
}
delete[] memblock;
return samples;
}
int main(int argc, char** argv) {
if (argc < 2) {
std::cerr << "Not enough arguments" << std::endl;
return -1;
}
size_t startIndex = 0, numOfSamples = 0;
if (argc == 4) {
std::stringstream nfs{argv[3]};
nfs >> numOfSamples;
}
if (argc >= 3) {
std::stringstream sfs{argv[2]};
sfs >> startIndex;
}
auto samples = loadSample(std::string(argv[1]), startIndex, numOfSamples);
for (size_t i = 0; i < samples.size(); ++i) {
std::cout << i << " : " << samples[i] << std::endl;
}
return 0;
}