-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibrespect.cpp
More file actions
90 lines (79 loc) · 1.89 KB
/
librespect.cpp
File metadata and controls
90 lines (79 loc) · 1.89 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
#include "librespect.h"
#include "ffft/FFTReal.h"
#include <cmath>
#include <iostream>
LibReSpect::LibReSpect(uint16 bufferSize, LRS::Window windowType)
{
bufferFFT = bufferSize;
windowFFT = windowType;
}
void LibReSpect::countFFT(double *array)
{
ffft::FFTReal<double> fft (bufferFFT);
double* tempArray = new double[bufferFFT];
fft.do_fft(tempArray,array);
uint16 halfBuffer = bufferFFT/2;
double* x = tempArray;
double* y = x + halfBuffer;
for (uint16 i=0; i<bufferFFT; i++)
{
if (i < halfBuffer)
{
array[i] = sqrt( (*x)*(*x) + (*y)*(*y) );
x++;
y++;
}
else
array[i] = 0;
}
delete [] tempArray;
}
void LibReSpect::makeWindow(double *array)
{
switch (windowFFT)
{
case LRS::Rectangular: // eatch element of array multiply by 1 so to nothing
return;
case LRS::Hann: hannWindow(array);
break;
case LRS::Hamming: hammingWindow(array);
break;
case LRS::Sine: sineWindow(array);
break;
default: std::cerr << "makeWindow: Invalid window type";
break;
}
}
void LibReSpect::hannWindow(double *array)
{
double multiplier;
double _2pi = 2 * M_PI;
uint16 x = bufferFFT - 1;
for (uint16 i=0; i<bufferFFT; i++)
{
multiplier = 1 - cos ( (_2pi*i) / x );
multiplier *= 0.5;
array[i] *= multiplier;
}
}
void LibReSpect::hammingWindow(double *array)
{
double _2pi = 2 * M_PI;
uint16 x = bufferFFT - 1;
for (uint16 i=0; i<bufferFFT; i++)
array[i] *= 0.54 + 0.46 * cos ( (_2pi*i) / x );
}
void LibReSpect::sineWindow(double *array)
{
uint16 x = bufferFFT - 1;
for (uint16 i=0; i<bufferFFT; i++)
array[i] *= sin(M_PI*i/x);
}
void LibReSpect::setBufferSize(uint16 v)
{
bufferFFT = v;
}
void LibReSpect::setWindowType(LRS::Window v)
{
windowFFT = v;
}