-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathWindowFunction.h
More file actions
191 lines (162 loc) · 4.59 KB
/
WindowFunction.h
File metadata and controls
191 lines (162 loc) · 4.59 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef _H_WINDOW_FUNCTION_
#define _H_WINDOW_FUNCTION_
#include <cmath>
#include <cstdio>
/* Implemented
- hamming
- hann(inherits hamming)
- sine
*/
class WindowFunction {
protected:
const double pi = 3.141592653589793;
int frame_size, shift_size, window_length;
bool norm;
bool sym;
// if sym==false, window is analysis window
double* window;
// if sym==true, not used
double* synthesis_window;
void normalize();
public:
inline WindowFunction(int frame_size, int shift_size, int window_length=-1, bool norm = true, bool sym = true);
inline ~WindowFunction();
// 2D
inline void Process(double** buf, int channels);
// 1D - multi channel
inline void Process(double* buf, int channels);
// 1D - single channel
inline void Process(double* buf);
// 2D
inline void WindowWithScaling(double** buf, int channels);
// 1D - multi channel
inline void WindowWithScaling(double* buf, int channels);
// 1D - single channel
inline void WindowWithScaling(double* buf);
};
class HammingWindow : public WindowFunction {
public:
HammingWindow(int frame_size_, int shift_size_ = -1, int window_length_=-1, double alpha_ = 0.54, double beta_ = 0.46, bool norm_ = true) :WindowFunction(frame_size_,shift_size_, window_length_, norm_,true) {
int i;
double tmp = 0;
// default 25% overlap
if (shift_size == -1) {
shift_size = frame_size / 4;
}
for (i = 0; i < window_length; i++)
window[i] = alpha_ - beta_ * cos(2.0 * pi * (i / (double)window_length));
if (norm)normalize();
}
};
class HannWindow : public HammingWindow {
public:
HannWindow(int frame_size_, int shift_size_ = -1, int window_length_=-1,bool norm_ = true) :HammingWindow(frame_size_, shift_size_,window_length_, 0.5, 0.5, norm_) {
}
};
class SineWindow : public WindowFunction {
private:
public:
SineWindow(int frame_size_, int shift_size_ = -1, int window_length_=-1, bool norm_ = true) : WindowFunction(frame_size_, shift_size_, window_length_, norm_) {
int i;
// default 50% overlap
if (shift_size_ == -1) {
shift_size = frame_size_ / 2;
}
for (i = 0; i < window_length; i++)
window[i] = sin(pi * ((double)i + 0.5) / (double)window_length);
if(norm)normalize();
}
~SineWindow() {
}
};
inline WindowFunction::WindowFunction(int frame_size_, int shift_size_,int window_length_, bool norm_, bool sym_) {
int i;
double tmp = 0;
frame_size = frame_size_;
shift_size = shift_size_;
window_length = window_length_;
norm = norm_;
sym = sym_;
if(window_length == -1)
window_length = frame_size;
if (frame_size < window_length) {
printf("ERROR::frame_size < widnow_length case is not implemented.\n");
exit(-1);
}
window = new double[frame_size];
memset(window, 0, sizeof(double) * frame_size);
if (!sym) {
synthesis_window = new double[frame_size];
memset(synthesis_window, 0, sizeof(double) * frame_size);
}
}
inline WindowFunction::~WindowFunction() {
delete[] window;
if (!sym)
delete[] synthesis_window;
}
inline void WindowFunction::normalize() {
double tmp = 0.0;
for (int i = 0; i < window_length; i++)
tmp += window[i] * window[i];
tmp /= shift_size;
tmp = std::sqrt(tmp);
for (int i = 0; i < window_length; i++)
window[i] /= tmp;
}
inline void WindowFunction::Process(double** buffer,
int n_channels) {
int i, j;
for (i = 0; i < n_channels; i++) {
#pragma omp parallel for
for (j = 0; j <frame_size; j++) {
buffer[i][j] *= window[j];
}
}
}
inline void WindowFunction::Process(double* buffer,
int cwindowels) {
int i, j;
for (i = 0; i < cwindowels; i++) {
#pragma omp parallel for
for (j = 0; j < frame_size; j++) {
buffer[i * (frame_size + 2) + j] *= window[j];
}
}
}
inline void WindowFunction::Process(double* buffer) {
int j;
for (j = 0; j < frame_size; j++) {
buffer[j] *= window[j];
}
}
inline void WindowFunction::WindowWithScaling(double** buffer,
int n_channels) {
int i, j;
for (i = 0; i < n_channels; i++) {
#pragma omp parallel for
for (j = 0; j < frame_size; j++) {
buffer[i][j] *= window[j];
buffer[i][j] /= 32767.0;
}
}
}
inline void WindowFunction::WindowWithScaling(double* buffer) {
int j;
for (j = 0; j < frame_size; j++) {
buffer[j] *= window[j];
buffer[j] /= 32767.0;
}
}
inline void WindowFunction::WindowWithScaling(double* buffer,
int cwindowels) {
int i, j;
for (i = 0; i < cwindowels; i++) {
#pragma omp parallel for
for (j = 0; j < frame_size; j++) {
buffer[i * (frame_size + 2) + j] *= window[j];
buffer[i * (frame_size + 2) + j] /= 32767.0;
}
}
}
#endif