forked from Tsukihime/OpenExpertSDR
-
Notifications
You must be signed in to change notification settings - Fork 4
/
window.c
193 lines (173 loc) · 6.25 KB
/
window.c
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
192
193
/* window.c
This file is part of a program that implements a Software-Defined Radio.
Copyright (C) 2004, 2005, 2006 by Frank Brickle, AB2KT and Bob McGwier, N4HY
Implemented from code by Bill Schottstaedt of Snd Editor at CCRMA
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
The authors can be reached by email at
or
or by paper mail at
The DTTS Microwave Society
6 Kathleen Place
Bridgewater, NJ 08807
*/
#include <window.h>
/* shamelessly stolen from Bill Schottstaedt's clm.c */
/* made worse in the process, but enough for our purposes here */
//static REAL
//sqr(REAL x) { return (x * x); }
/* mostly taken from
* Fredric J. Harris, "On the Use of Windows for Harmonic Analysis with the
* Discrete Fourier Transform," Proceedings of the IEEE, Vol. 66, No. 1,
* January 1978.
* Albert H. Nuttall, "Some Windows with Very Good Sidelobe Behaviour",
* IEEE Transactions of Acoustics, Speech, and Signal Processing, Vol. ASSP-29,
* No. 1, February 1981, pp 84-91
*
* JOS had slightly different numbers for the Blackman-Harris windows.
*/
REAL*
makewindow(Windowtype type, int size, REAL *window)
{
int i, j, midn, midp1, midm1;
REAL freq, rate, sr1, angle, expn, expsum, cx, two_pi;
midn = size >> 1;
midp1 = (size + 1) / 2;
midm1 = (size - 1) / 2;
two_pi = (REAL)(8.0 * atan(1.0));
freq = two_pi / (REAL)size;
rate = (REAL)(1.0 / (REAL)midn);
angle = 0.0;
expn = (REAL)(log(2.0) / (REAL)midn + 1.0);
expsum = 1.0;
switch(type)
{
case RECTANGULAR_WINDOW:
for (i = 0; i < size; i++) window[i] = 1.0;
break;
case HANNING_WINDOW: /* Hann would be more accurate */
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += freq) window[j] = (window[i] = (REAL)(0.5 - 0.5 * cos(angle)));
break;
case WELCH_WINDOW:
for (i = 0, j = size - 1; i <= midn; i++, j--) window[j] =
(window[i] =
(REAL)(1.0 - sqr((REAL)(i - midm1) / (REAL)midp1)));
break;
case PARZEN_WINDOW:
for (i = 0, j = size - 1; i <= midn; i++, j--) window[j] =
(window[i] =
(REAL)(1.0 - fabs((REAL)(i - midm1) / (REAL)midp1)));
break;
case BARTLETT_WINDOW:
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += rate) window[j] = (window[i] = angle);
break;
case HAMMING_WINDOW:
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += freq) window[j] = (window[i] = (REAL)(0.54 - 0.46 * cos(angle)));
break;
case BLACKMAN2_WINDOW: /* using Chebyshev polynomial equivalents here */
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += freq)
{
cx = (REAL)cos(angle);
window[j] = (window[i] =
(REAL)(.34401 + (cx * (-.49755 + (cx * .15844)))));
}
break;
case BLACKMAN3_WINDOW:
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += freq)
{
cx = (REAL)cos(angle);
window[j] =
(window[i] =
(REAL)(.21747 +
(cx * (-.45325 + (cx * (.28256 - (cx * .04672)))))));
}
break;
case BLACKMAN4_WINDOW:
for (i = 0, j = size - 1, angle = 0.0; i <= midn;
i++, j--, angle += freq)
{
cx = (REAL)cos(angle);
window[j] = (window[i] = (REAL)
(.084037 +
(cx *
(-.29145 +
(cx *
(.375696 + (cx * (-.20762 + (cx * .041194)))))))));
}
break;
case EXPONENTIAL_WINDOW:
for (i = 0, j = size - 1; i <= midn; i++, j--)
{
window[j] = (window[i] = (REAL)(expsum - 1.0));
expsum *= expn;
}
break;
case RIEMANN_WINDOW:
sr1 = two_pi / (REAL)size;
for (i = 0, j = size - 1; i <= midn; i++, j--)
{
if(i == midn) window[j] = (window[i] = 1.0);
else
{
/* split out because NeXT C compiler can't handle the full expression */
cx = sr1 * (midn - i);
window[i] = (REAL)(sin(cx) / cx);
window[j] = window[i];
}
}
break;
case BLACKMANHARRIS_WINDOW:
{
REAL a0 = 0.35875f, a1 = 0.48829f, a2 = 0.14128f, a3 = 0.01168f;
for (i = 0; i < size; i++)
{
window[i] =
(REAL)(a0 -
a1 * cos(two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)) +
a2 * cos(2.0 * two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)) -
a3 * cos(3.0 * two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)));
}
}
break;
case NUTTALL_WINDOW:
{
REAL a0 = 0.3635819f, a1 = 0.4891775f, a2 = 0.1365995f, a3 =
0.0106411f;
for (i = 0; i < size; i++)
{
window[i] =
(REAL)(a0 -
a1 * cos(two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)) +
a2 * cos(2.0 * two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)) -
a3 * cos(3.0 * two_pi * (REAL)(i + 0.5) /
(REAL)(size - 1)));
}
}
break;
default:
return (0);
break;
}
return (window);
}