-
Notifications
You must be signed in to change notification settings - Fork 0
/
FFTManager_opencv.cpp
71 lines (56 loc) · 1.85 KB
/
FFTManager_opencv.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
#include "FFTManager.h"
#include <stdlib.h>
#include <math.h>
#include <opencv2/core/core.hpp>
using namespace cv;
struct FFTManager {
unsigned int N;
float* multipliers;
};
void setupHammingWindow(float *values, int N) {
for (int i = 0; i < N; ++i) {
values[i] = 0.54 - 0.46 * cos(2*M_PI*i/N);
}
}
FFTManager* createFFTManager(int sampleSize) {
FFTManager* _fft = (struct FFTManager*) malloc(sizeof(struct FFTManager));
_fft->N = sampleSize;
_fft->multipliers = (float*) malloc(sizeof(float) * sampleSize);
setupHammingWindow(_fft->multipliers, sampleSize);
return _fft;
}
void fft(FFTManager *_fft, float * input, int inputSize, float *output) {
if (inputSize != _fft->N) {
// throw?
return;
}
float *multipliedInput = (float*) malloc(sizeof(float) * _fft->N);
for (int i = 0; i < _fft->N; ++i) {
multipliedInput[i] = input[i] * _fft->multipliers[i];
}
Mat dftInput = Mat(inputSize, 1, CV_32F, multipliedInput);
Mat dftOutput;
Mat splitComplex[] = { Mat(inputSize, 1, CV_32F), Mat(inputSize, 1, CV_32F) };
//Mat(inputSize, 1, CV_32F, output)
dft(dftInput, dftOutput, DFT_COMPLEX_OUTPUT);
// splitComplex[0] = Re(dftOutput), splitComplex[1] = Im(dftOutput)
split(dftOutput, splitComplex);
// Compute *squared* magnitudes == "power"
for (int i = 0; i <= _fft->N/2; ++i) {
output[i] = (splitComplex[0].at<float>(i) * splitComplex[0].at<float>(i)) + (splitComplex[1].at<float>(i) * splitComplex[1].at<float>(i));
}
free(multipliedInput);
}
void deleteFFTManager(FFTManager *_fft) {
free(_fft->multipliers);
free(_fft);
}
float dominantPower(float *output, int inputSize) {
float max = 0.0;
for (int i = 1; i <= inputSize/2; ++i) {
if (output[i] > max) {
max = output[i];
}
}
return max;
}