Rounding Error between Python and C++ Blackmanharris Window

Dear GNU Radio,

This is most likely a shortcoming of how python handles significant
digits,
but I wanted to point out that creating a Blackmanharris window in
Python
and C++ result in different results.

In Python:
window = fft.blackmanharris(1024)

In c++:
const std::vector< float > window =
gr::filter::firdes::window(gr::filter::firdes::WIN_BLACKMAN_HARRIS,
1024,
NULL);

I have attached an image of two plots. The first plot is of the two
windows
on top of each other (green and blue), and the second is of their
difference:
differences = python_values - cpp_values;

I was wondering why my c++ and python application were getting different
results, and I have found the cause.

Turns out that this is more than a rounding error. it is an off-by-one
error at index 1 given N=1024.

In python it is defined as follows in (gnuradio/gr-fft/python/fft/
window.py)

blackmanharris = coswindow((0.35875,0.48829,0.14128,0.01168))
where
def coswindow(coeffs):
def closure(fft_size):
window = [0] * fft_size
#print list(enumerate(coeffs))
for w_index in range(fft_size):
for (c_index, coeff) in enumerate(coeffs):
window[w_index] += (-1)*c_index * coeff *
math.cos(2.0c_indexmath.pi
(w_index+0.5)/(fft_size-1))
return window
return closure

In c++ it is defined as follows in (gnuradio/gr-filter/lib/firdes.cc)

  case WIN_BLACKMAN_hARRIS:
    for(int n = -ntaps/2; n < ntaps/2; n++)
      taps[n+ntaps/2] = 0.35875 + 0.48829*cos((2*M_PI * n) / 

(float)M) +
0.14128cos((4M_PI * n) / (float)M) + 0.01168cos((6M_PI *
n)
/ (float)M);
break;

It is not immediately obvious why I’m getting different results. Yes,
the
two have different levels of precision, but I see an off-by-one error.

$ head -n 20 indexed_cpp.out
0: 6.01334e-05 - ok
1: 6.01334e-05 - ? no good.
2: 6.12008e-05
3: 6.33369e-05
4: 6.65439e-05
5: 7.08254e-05
6: 7.61861e-05
7: 8.26318e-05
8: 9.01697e-05
9: 9.88079e-05
10: 0.000108556
11: 0.000119424
12: 0.000131425
13: 0.00014457
14: 0.000158875
15: 0.000174354
16: 0.000191023
17: 0.000208902
18: 0.000228007
19: 0.00024836

$ head -n 20 indexed_python.out
0 : 6.013340171e-05
1 : 6.12008358122e-05
2 : 6.33368796032e-05
3 : 6.65438842538e-05
4 : 7.08253765149e-05
5 : 7.61860587111e-05
6 : 8.26318087304e-05
7 : 9.01696800132e-05
8 : 9.88079015359e-05
9 : 0.000108555877792
10 : 0.000119424188766
11 : 0.000131424589909
12 : 0.000144570012099
13 : 0.000158874561602
14 : 0.000174353520023
15 : 0.000191023344246
16 : 0.000208901666371
17 : 0.000228007293632
18 : 0.000248360208308
19 : 0.000269981567623


Discuss-gnuradio mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/discuss-gnuradio