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 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
|
/*
***************************************************************************
*
* Author: Teunis van Beelen
*
* Copyright (C) 2018 - 2019 Teunis van Beelen
*
* Email: teuniz@gmail.com
*
***************************************************************************
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
***************************************************************************
*/
#include "fft_wrap.h"
static void window_func(const double *, double *, double *, int, int, int);
static void set_gain_unity(double *, int);
struct fft_wrap_settings_struct * fft_wrap_create(double *buf, int buf_size, int dft_size, int window_type)
{
struct fft_wrap_settings_struct *st;
if(buf == NULL) return NULL;
if(buf_size < 4) return NULL;
if(dft_size < 4) return NULL;
if(dft_size & 1) dft_size--;
if((window_type < 0) || (window_type > 7)) return NULL;
st = (struct fft_wrap_settings_struct *)calloc(1, sizeof(struct fft_wrap_settings_struct));
if(st == NULL) return NULL;
st->sz_in = buf_size;
st->dft_sz = dft_size;
st->wndw_type = window_type;
st->blocks = 1;
if(st->dft_sz < st->sz_in)
{
st->blocks = st->sz_in / st->dft_sz;
}
else
{
st->dft_sz = st->sz_in;
}
if(st->dft_sz & 1) st->dft_sz--;
st->smpls_left = st->sz_in % st->dft_sz;
if(st->smpls_left & 1) st->smpls_left--;
st->sz_out = st->dft_sz / 2;
st->buf_in = buf;
if(st->wndw_type)
{
st->buf_wndw = (double *)malloc(sizeof(double) * (st->dft_sz + 2));
if(st->buf_wndw == NULL)
{
free(st);
return NULL;
}
st->buf_wndw_coef = (double *)malloc(sizeof(double) * (st->dft_sz / 2 + 2));
if(st->buf_wndw_coef == NULL)
{
free(st->buf_wndw);
free(st);
return NULL;
}
}
st->buf_out = (double *)malloc(sizeof(double) * (st->sz_out + 2));
if(st->buf_out == NULL)
{
free(st->buf_wndw_coef);
free(st->buf_wndw);
free(st);
return NULL;
}
st->kiss_fftbuf = (kiss_fft_cpx *)malloc((st->sz_out + 1) * sizeof(kiss_fft_cpx));
if(st->kiss_fftbuf == NULL)
{
free(st->buf_out);
free(st->buf_wndw_coef);
free(st->buf_wndw);
free(st);
return NULL;
}
st->cfg = kiss_fftr_alloc(st->dft_sz, 0, NULL, NULL);
return st;
}
void fft_wrap_run(struct fft_wrap_settings_struct *st)
{
int i, j;
if(st == NULL) return;
if(st->sz_in < 4) return;
if(st->dft_sz < 4) return;
if(st->sz_out < 1) return;
if(st->buf_in == NULL) return;
if(st->buf_out == NULL) return;
if(st->kiss_fftbuf == NULL) return;
if(st->wndw_type)
{
if(st->buf_wndw == NULL) return;
if(st->wndw_type)
{
window_func(st->buf_in, st->buf_wndw, st->buf_wndw_coef, st->dft_sz, st->wndw_type, 0);
}
kiss_fftr(st->cfg, st->buf_wndw, st->kiss_fftbuf);
}
else
{
kiss_fftr(st->cfg, st->buf_in, st->kiss_fftbuf);
}
for(i=0; i<st->sz_out; i++)
{
st->buf_out[i] = ((st->kiss_fftbuf[i].r * st->kiss_fftbuf[i].r) + (st->kiss_fftbuf[i].i * st->kiss_fftbuf[i].i)) / st->sz_out;
}
for(j=1; j<st->blocks; j++)
{
if(st->wndw_type)
{
window_func(st->buf_in + (j * st->dft_sz), st->buf_wndw, st->buf_wndw_coef, st->dft_sz, st->wndw_type, j);
kiss_fftr(st->cfg, st->buf_wndw, st->kiss_fftbuf);
}
else
{
kiss_fftr(st->cfg, st->buf_in + (j * st->dft_sz), st->kiss_fftbuf);
}
for(i=0; i<st->sz_out; i++)
{
st->buf_out[i] += ((st->kiss_fftbuf[i].r * st->kiss_fftbuf[i].r) + (st->kiss_fftbuf[i].i * st->kiss_fftbuf[i].i)) / st->sz_out;
}
}
if(st->smpls_left)
{
if(st->wndw_type)
{
window_func(st->buf_in + ((j-1) * st->dft_sz) + st->smpls_left, st->buf_wndw, st->buf_wndw_coef, st->dft_sz, st->wndw_type, j);
kiss_fftr(st->cfg, st->buf_wndw, st->kiss_fftbuf);
}
else
{
kiss_fftr(st->cfg, st->buf_in + ((j-1) * st->dft_sz) + st->smpls_left, st->kiss_fftbuf);
}
for(i=0; i<st->sz_out; i++)
{
st->buf_out[i] += ((st->kiss_fftbuf[i].r * st->kiss_fftbuf[i].r) + (st->kiss_fftbuf[i].i * st->kiss_fftbuf[i].i)) / st->sz_out;
st->buf_out[i] /= (st->blocks + 1);
}
}
else
{
if(st->blocks > 1)
{
for(i=0; i<st->sz_out; i++)
{
st->buf_out[i] /= st->blocks;
}
}
}
}
void free_fft_wrap(struct fft_wrap_settings_struct *st)
{
if(st == NULL) return;
free(st->cfg);
free(st->kiss_fftbuf);
free(st->buf_out);
free(st->buf_wndw);
free(st->buf_wndw_coef);
memset(st, 0, sizeof(struct fft_wrap_settings_struct));
free(st);
}
static void window_func(const double *src, double *dest, double *coef, int sz, int type, int block)
{
int i, sz2;
sz2 = sz / 2;
if(!block)
{
if(type == FFT_WNDW_TYPE_HAMMING)
{
for(i=0; i<sz2; i++)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
coef[i] = 0.54 - (0.46 * cos((2.0 * M_PI * i) / (sz - 1))); /* Hamming (original) */
// coef[i] = 0.53836 - (0.46164 * cos((2.0 * M_PI * i) / (sz - 1))); /* Hamming marginally optimized */
}
}
else if(type == FFT_WNDW_TYPE_NUTTALL3B)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
for(i=0; i<sz2; i++)
{
// coef[i] = 0.42 - (0.5 * cos((2.0 * M_PI * i) / (sz - 1))) + (0.08 * cos((4.0 * M_PI * i) / (sz - 1))); /* Blackman */
coef[i] = 0.4243801 - (0.4973406 * cos((2.0 * M_PI * i) / (sz - 1))) + (0.0782793 * cos((4.0 * M_PI * i) / (sz - 1))); /* Nuttall3b */
}
}
else if(type == FFT_WNDW_TYPE_4TERM_BLACKMANHARRIS)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
for(i=0; i<sz2; i++)
{
coef[i] = 0.35875 - (0.48829 * cos((2.0 * M_PI * i) / (sz - 1))) + (0.14128 * cos((4.0 * M_PI * i) / (sz - 1))) - (0.01168 * cos((6.0 * M_PI * i) / (sz - 1))); /* 4-term Blackman-Harris */
}
}
else if(type == FFT_WNDW_TYPE_7TERM_BLACKMANHARRIS)
{ /* The use of DFT windows in signal-to-noise ratio and harmonic distortion computations IEEE */
for(i=0; i<sz2; i++)
{
coef[i] = 0.271051400693424 - (0.433297939234485 * cos((2.0 * M_PI * i) / (sz - 1))) + (0.218122999543110 * cos((4.0 * M_PI * i) / (sz - 1))) - (0.65925446388031e-1 * cos((6.0 * M_PI * i) / (sz - 1)))
+ (0.10811742098371e-1 * cos((8.0 * M_PI * i) / (sz - 1))) - (0.776584825226e-3 * cos((10.0 * M_PI * i) / (sz - 1))) + (0.13887217352e-4 * cos((12.0 * M_PI * i) / (sz - 1))); /* 7-term Blackman-Harris */
}
}
else if(type == FFT_WNDW_TYPE_NUTTALL4C)
{
for(i=0; i<sz2; i++)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
coef[i] = 0.3635819 - (0.4891775 * cos((2.0 * M_PI * i) / (sz - 1))) + ( 0.1365995 * cos((4.0 * M_PI * i) / (sz - 1))) - (0.0106411 * cos((6.0 * M_PI * i) / (sz - 1))); /* Nuttall4c */
}
}
else if(type == FFT_WNDW_TYPE_HANN)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
for(i=0; i<sz2; i++)
{ /* both are the same */
coef[i] = (1.0 - cos((2.0 * M_PI * i) / (sz - 1))) / 2.0; /* Hann */
// coef[i] = 0.5 - (0.5 * cos((2.0 * M_PI * i) / (sz - 1))); /* Hann */
}
}
else if(type == FFT_WNDW_TYPE_HFT223D)
{ /* Spectrum and spectral density estimation by the Discrete Fourier transform (DFT), including a comprehensive list of window functions and some new at-top windows Max Planck Institute */
for(i=0; i<sz2; i++)
{
coef[i] = 1.0 - (1.98298997309 * cos((2.0 * M_PI * i) / (sz - 1))) + (1.75556083063 * cos((4.0 * M_PI * i) / (sz - 1))) - (1.19037717712 * cos((6.0 * M_PI * i) / (sz - 1)))
+ (0.56155440797 * cos((8.0 * M_PI * i) / (sz - 1))) - (0.17296769663 * cos((10.0 * M_PI * i) / (sz - 1))) + (0.3233247087e-1 * cos((12.0 * M_PI * i) / (sz - 1)))
- (0.324954578e-2 * cos((14.0 * M_PI * i) / (sz - 1))) + (0.13801040e-3 * cos((16.0 * M_PI * i) / (sz - 1))) - (0.132725e-5 * cos((18.0 * M_PI * i) / (sz - 1))); /* 9-term HFT223D */
}
}
else
{
for(i=0; i<sz2; i++)
{
coef[i] = 0;
}
}
set_gain_unity(coef, sz2);
}
for(i=0; i<sz2; i++)
{
dest[i] = coef[i] * src[i];
dest[(sz - 1) - i] = coef[i] * src[(sz - 1) - i];
}
}
static void set_gain_unity(double *arr, int sz)
{
int i;
double total = 0.0;
if(sz < 4) return;
for(i=0; i<sz; i++)
{
total += arr[i];
}
total /= sz;
if(total != 0.0)
{
for(i=0; i<sz; i++)
{
arr[i] /= total;
}
}
}
|